-
Notifications
You must be signed in to change notification settings - Fork 21
/
allocator.hpp
328 lines (258 loc) · 7.11 KB
/
allocator.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// LGPL 3 or higher Robert Burner Schadek rburners@gmail.com
#pragma once
#include <stdio.h>
#include <stddef.h>
#include <limits>
#include <algorithm>
#include <unordered_map>
#include <deque>
#include <memory>
#include <stdexcept>
#include <conv.hpp>
#define BASE_ALLOCATOR_TYPES(NAME,T) \
typedef size_t size_type; \
typedef ptrdiff_t difference_type; \
typedef T* pointer; \
typedef const T* const_pointer; \
typedef T& reference; \
typedef const T& const_reference; \
typedef T value_type \
namespace sweet {
template<typename T>
class BaseAllocator {
public:
BASE_ALLOCATOR_TYPES(BaseAllocator,T);
inline BaseAllocator() {}
template<typename U, typename... Args>
void construct(U* p, Args&&... args) {
::new((void*)p) U(std::forward<Args>(args)...);
}
inline void destroy(pointer p) {
p->~T();
}
};
template<typename T>
class Mallocator : public BaseAllocator<T> {
public:
BASE_ALLOCATOR_TYPES(Mallocator,T);
inline Mallocator() {}
template<typename U>
struct rebind {typedef Mallocator<U> other;};
inline pointer address(reference r) const noexcept {
return &r;
}
inline const_pointer address(const_reference r) const noexcept {
return &r;
}
inline pointer allocate(size_type n, const void* =0) {
auto ptr = malloc(n * sizeof(T));
if(ptr == nullptr) {
throw std::bad_alloc();
}
return reinterpret_cast<pointer>(ptr);
}
inline void deallocate(pointer ptr, size_type) {
free(ptr);
}
inline size_type max_size() const {
return std::numeric_limits<size_type>::max();
}
};
template<typename T>
class FailAllocator : public BaseAllocator<T> {
public:
BASE_ALLOCATOR_TYPES(FailAllocator,T);
inline FailAllocator() {}
template<typename U>
struct rebind {typedef FailAllocator<U> other;};
template<class Z>
using DefaultType = FailAllocator<Z>;
inline pointer address(reference) const noexcept {
return nullptr;
}
inline const_pointer address(const_reference) const noexcept {
return nullptr;
}
inline pointer allocate(size_type, const void* =0) {
return nullptr;
}
inline void deallocate(pointer, size_type) {
}
inline size_type max_size() const {
return 0u;
}
};
template<typename T, template<typename> class Primary, template<typename> class Secondary>
class FallbackAllocator : public Primary<T>, public Secondary<T> {
public:
BASE_ALLOCATOR_TYPES(FallbackAllocator,T);
private:
std::unordered_map<pointer,size_type> secMemory;
public:
inline FallbackAllocator() {}
template<typename U>
struct rebind {
typedef FallbackAllocator<U,Primary,Secondary> other;
};
inline pointer address(reference r) const noexcept {
pointer p = Primary<T>::address(r);
return p != nullptr ? p : Secondary<T>::address(r);
}
inline const_pointer address(const_reference r) const noexcept {
pointer p = Primary<T>::address(r);
return p != nullptr ? p : Secondary<T>::address(r);
}
inline pointer allocate(size_type n, const void* hint=0) {
pointer ptr;
ptr = Primary<T>::allocate(n,hint);
if(ptr == nullptr) {
ptr = Secondary<T>::allocate(n,hint);
secMemory.insert(std::make_pair(ptr,n));
}
if(ptr == nullptr) {
throw std::bad_alloc();
}
return reinterpret_cast<pointer>(ptr);
}
inline void deallocate(pointer ptr, size_type size) {
auto it = this->secMemory.find(ptr);
if(it != this->secMemory.end()) {
Secondary<T>::deallocate(it->first, it->second);
this->secMemory.erase(it);
} else {
Primary<T>::deallocate(ptr, size);
}
}
inline size_type max_size() const {
return std::max(Primary<T>::max_size(), Secondary<T>::max_size());
}
};
template<typename T, size_t BumpSize = 4096>
class BumpAllocator : public BaseAllocator<T> {
private:
T bump[BumpSize];
size_t idx;
public:
BASE_ALLOCATOR_TYPES(BumpAllocator,T);
template<class Z>
using DefaultType = BumpAllocator<Z>;
inline BumpAllocator() : idx(0) {}
template<typename U>
struct rebind {typedef BumpAllocator<U,BumpSize> other;};
inline pointer address(reference r) const noexcept {
return &r;
}
inline const_pointer address(const_reference r) const noexcept {
return &r;
}
inline pointer allocate(size_type s, const void* =0) {
if(this->idx + s >= BumpSize) {
return nullptr;
}
auto ret = &(this->bump[this->idx]);
this->idx += s;
return ret;
}
inline void deallocate(pointer p, size_type s) {
pointer a = p + s;
pointer b = &(this->bump[this->idx]);
if(a == b) {
this->idx -= s;
}
}
inline size_type max_size() const {
return BumpSize * sizeof(T);
}
};
template<typename T, typename A, size_t BumpSize = 4096>
class SingleBlockAllocator : public BaseAllocator<T> {
private:
size_t idx;
T* base;
A allocator;
public:
BASE_ALLOCATOR_TYPES(BumpAllocator,T);
inline SingleBlockAllocator() : idx(0), base(nullptr) {}
inline ~SingleBlockAllocator() {
this->allocator.deallocate(base, BumpSize);
}
/*template<typename U>
struct rebind {typedef SingleBlockAllocator<U,A,BumpSize> other;};
*/
inline pointer address(reference r) const noexcept {
return &r;
}
inline const_pointer address(const_reference r) const noexcept {
return &r;
}
inline pointer allocate(size_type s, const void* =0) {
if(this->base == nullptr) {
this->base = allocator.allocate(BumpSize);
}
if(this->idx + s >= BumpSize) {
return nullptr;
}
auto ret = &(this->base[this->idx]);
this->idx += s;
return ret;
}
inline void deallocate(pointer p, size_type s) {
pointer a = p + s;
pointer b = &(this->base[this->idx]);
if(a == b) {
this->idx -= s;
}
}
inline size_type max_size() const {
return BumpSize * sizeof(T);
}
};
template<typename T, typename Allocator>
class FreeVectorAllocator : public BaseAllocator<T> {
public:
BASE_ALLOCATOR_TYPES(BumpAllocator,T);
private:
template<typename U>
struct StoreNode {
U* ptr;
size_t size;
inline StoreNode(U* p, size_type s) : ptr(p), size(s) {}
};
std::deque<StoreNode<T>> store;
Allocator allocator;
public:
inline FreeVectorAllocator() {}
inline ~FreeVectorAllocator() {
std::for_each(this->store.begin(), this->store.end(), [&](const StoreNode<T>& n) {
allocator.deallocate(n.ptr, n.size);
});
}
template<typename U>
struct rebind {typedef FreeVectorAllocator<U, Allocator> other;};
inline pointer address(reference r) const noexcept {
return allocator.address(r);
}
inline const_pointer address(const_reference r) const noexcept {
return allocator.address(r);
}
inline pointer allocate(size_type size, const void* h=0) {
auto it = std::find_if(store.begin(), store.end(),
[&](const typename std::deque<StoreNode<T>>::value_type& s) {
return s.size == size;
});
if(it != this->store.end()) {
pointer ptr = it->ptr;
this->store.erase(it);
return ptr;
} else {
return this->allocator.allocate(size, h);
}
}
inline void deallocate(pointer p, size_type s) {
this->store.push_back(StoreNode<T>(p,s));
}
inline size_type max_size() const {
return this->allocator.max_size();
}
};
}