-
Notifications
You must be signed in to change notification settings - Fork 0
/
allocator.h
129 lines (103 loc) · 2.9 KB
/
allocator.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef BBUTILS_ALLOCATOR_H
#define BBUTILS_ALLOCATOR_H
#include <cassert>
#include <cstring>
#include <atomic>
#include <type_traits>
void *allocator_mmap(size_t bytesNeeded);
struct MemoryChunk {
char* entries;
char* nextentry;
char* end;
MemoryChunk* next;
MemoryChunk(MemoryChunk* next, size_t bytesNeeded);
~MemoryChunk();
template<typename T>
__attribute__((always_inline))
T* alloc() {
return (T*)alloc<std::alignment_of<T>::value>(sizeof(T));
}
__attribute__((always_inline))
char* alloc(size_t size) {
auto mem = nextentry;
nextentry += size;
return mem;
}
template<size_t alignPowerTwo>
__attribute__((always_inline))
char* alloc(size_t size) {
static_assert(!(alignPowerTwo & (alignPowerTwo - 1)), "Requires power of 2");
auto mem = nextentry;
mem += alignPowerTwo - 1;
mem = (char*)(((uintptr_t)mem) & ~(alignPowerTwo - 1));
nextentry = mem + size;
return mem;
}
template<typename T>
__attribute__((always_inline))
void free(T* mem) {
return free((void*)mem, sizeof(T));
}
__attribute__((always_inline))
void free(void* mem, size_t length) {
(void)mem;
nextentry -= length;
assert((char*)mem == nextentry);
}
};
class MemoryChunkManager {
static thread_local MemoryChunk* MemoryChunk_;
std::atomic<MemoryChunk*> allMemoryChunks_;
public:
static constexpr size_t MemoryChunkSize = 1ULL << 22; // 4MB
MemoryChunkManager();
MemoryChunk* linkNewMemoryChunk();
void thread_init();
template<typename T>
__attribute__((always_inline))
T* alloc() {
MemoryChunk* myMemoryChunk = MemoryChunk_;
if(myMemoryChunk->nextentry + sizeof(T) > myMemoryChunk->end) {
myMemoryChunk = linkNewMemoryChunk();
}
auto r = myMemoryChunk->alloc<T>();
assert(r);
return r;
}
template<typename T>
__attribute__((always_inline))
void free(T* mem) {
return MemoryChunk_->free(mem);
}
template<int alignPowerTwo>
__attribute__((always_inline))
char* alloc(size_t size) {
MemoryChunk* myMemoryChunk = MemoryChunk_;
if(myMemoryChunk->nextentry + size > myMemoryChunk->end) {
myMemoryChunk = linkNewMemoryChunk();
}
auto r = myMemoryChunk->alloc<alignPowerTwo>(size);
assert(r);
return r;
}
__attribute__((always_inline))
char* realloc(void* mem, size_t size) {
char *ret;
MemoryChunk* const myMemoryChunk = MemoryChunk_;
if(((char*)mem + size) > myMemoryChunk->end) {
MemoryChunk* const myNewMemoryChunk = linkNewMemoryChunk();
ret = myNewMemoryChunk->alloc(size);
memcpy(ret, mem, myMemoryChunk->nextentry - (char*)mem);
return ret;
} else {
myMemoryChunk->nextentry = ((char*)mem + size);
return (char*)mem;
}
}
__attribute__((always_inline))
void free(void* mem, size_t length) {
return MemoryChunk_->free(mem, length);
}
~MemoryChunkManager();
};
#endif