-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocator.h
More file actions
74 lines (60 loc) · 1.81 KB
/
Allocator.h
File metadata and controls
74 lines (60 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Created by Agustin Gianni (agustin.gianni@gmail.com) on 11/21/16.
//
#ifndef THREADPROFILER_ALLOCATOR_H
#define THREADPROFILER_ALLOCATOR_H
#include <cstdint>
#include <cstddef>
#include "DiskPool.h"
// Create an instance of 'T' in a per thread fashion.
template <typename T>
struct PerThreadPolicy {
template <typename ... Ts>
static T &instance(Ts ... args) {
thread_local T g_instance(args ...);
return g_instance;
}
};
// Create an instance of 'T' in a per process fashion.
template <typename T>
struct PerProcessPolicy {
template <typename ... Ts>
static T &instance(Ts ... args) {
static T g_instance(args ...);
return g_instance;
}
};
// Create a memory allocator using a given 'MemoryPool'.
template <typename MemoryPool, template <typename> class ThreadingPolicy>
class Allocator {
public:
static uint8_t *alloc(size_t size) {
return instance().alloc(size);
}
template <typename T>
static uint8_t *alloc() {
return instance().alloc(sizeof(T));
}
static uint8_t *alloc(MemoryPool &pool, size_t size) {
return pool.alloc(size);
}
template <typename T>
static uint8_t *alloc(MemoryPool &pool) {
return pool.alloc(sizeof(T));
}
// Return an instance to the memory pool according to 'ThreadingPolicy'.
static MemoryPool &instance() {
return ThreadingPolicy<MemoryPool>::instance("/tmp/memory.log", GB(8));
}
private:
// Avoid explicit construction and destruction.
Allocator() = delete;
~Allocator() = delete;
// Avoid copies.
Allocator(const Allocator &) = delete;
Allocator &operator=(const Allocator &) = delete;
// Avoid moves.
Allocator(Allocator &&) = delete;
Allocator &operator=(Allocator &&) = delete;
};
#endif //THREADPROFILER_ALLOCATOR_H