-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbllocator.hpp
296 lines (243 loc) · 6.03 KB
/
bllocator.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
#pragma once
#include <stdio.h>
#include <stddef.h>
#include <limits>
#include <algorithm>
#include <deque>
#include <memory>
#include <stdexcept>
namespace sweet {
class BaseAllocator {
public:
virtual void* allocate(size_t) = 0;
virtual void deallocate(void*, size_t=0) = 0;
virtual size_t max() const = 0;
virtual bool allocated(void*) const = 0;
};
class Mallocator : public BaseAllocator {
public:
inline void* allocate(size_t s) override {
return malloc(s);
}
inline void deallocate(void* ptr, size_t=0) override {
free(ptr);
}
inline size_t max() const override {
return std::numeric_limits<size_t>::max();
}
inline bool allocated(void*) const override {
return true;
}
};
class FailAllocator : public BaseAllocator {
public:
inline void* allocate(size_t) override {
return nullptr;
}
inline void deallocate(void*, size_t) override {
}
inline size_t max() const override {
return 0u;
}
inline bool allocated(void*) const override {
return false;
}
};
template<typename A1, typename A2>
class FallbackAllocator : public BaseAllocator {
private:
A1 a1;
A2 a2;
public:
inline void* allocate(size_t s) override {
void* ptr = a1.allocate(s);
if(ptr == nullptr) {
ptr = a2.allocate(s);
}
if(ptr == nullptr) {
throw std::bad_alloc();
} else {
return ptr;
}
}
inline void deallocate(void* ptr, size_t s) override {
if(a1.allocated(ptr)) {
a1.deallocate(ptr, s);
} else if(a2.allocated(ptr)) {
a2.deallocate(ptr, s);
} else {
throw std::logic_error("Either of the two Allocated allocated "
"the given ptr.");
}
}
inline size_t max() const {
return std::max(a1.max(), a2.max());
}
inline bool allocated(void* ptr) const override {
if(a1.allocated(ptr)) {
return true;
} else if(a2.allocated(ptr)) {
return true;
} else {
return false;
}
}
};
template<typename A>
class FreeDequeAllocator : public BaseAllocator {
private:
struct StoreNode {
void* ptr;
size_t size;
inline StoreNode(void* p, size_t s) : ptr(p), size(s) {}
};
std::deque<StoreNode> store;
A allocator;
public:
inline ~FreeDequeAllocator() {
std::for_each(this->store.begin(), this->store.end(), [&](const StoreNode& n) {
allocator.deallocate(n.ptr, n.size);
});
}
inline void* allocate(size_t size) override {
auto it = std::find_if(store.begin(), store.end(),
[&](const typename std::deque<StoreNode>::value_type& s) {
return s.size == size;
});
if(it != this->store.end()) {
void* ptr = it->ptr;
this->store.erase(it);
return ptr;
} else {
return this->allocator.allocate(size);
}
}
inline void deallocate(void* p, size_t s) override {
this->store.push_back(StoreNode(p,s));
}
inline size_t max() const override {
return this->allocator.max();
}
inline bool allocated(void* p) const override {
return this->allocator.allocated(p);
}
};
template<size_t Size = 4096>
class StackAllocator : public BaseAllocator {
private:
char stack[Size];
size_t idx;
public:
inline StackAllocator() : idx(0u) {}
inline void* allocate(size_t size) override {
size_t nIdx = this->idx + size;
if(nIdx > Size) {
return nullptr;
} else {
void* ret = reinterpret_cast<void*>(&this->stack[this->idx]);
this->idx = nIdx;
return ret;
}
}
inline void deallocate(void* p, size_t s) override {
char* a = static_cast<char*>(p) + s;
char* b = &(this->stack[this->idx]);
if(a == b) {
this->idx -= s;
}
}
inline size_t max() const override {
return Size;
}
inline bool allocated(void* p) const override {
return p >= &this->stack && p <= &this->stack[this->idx];
}
};
template<size_t Size = 4096>
class PoolAllocator : public BaseAllocator {
private:
char* stack;
size_t idx;
public:
inline PoolAllocator() : stack(static_cast<char*>(malloc(Size))), idx(0u) {
if(this->stack == nullptr) {
throw std::bad_alloc();
}
}
inline ~PoolAllocator() {
free(this->stack);
}
inline void* allocate(size_t size) override {
size_t nIdx = this->idx + size;
if(nIdx > Size) {
return nullptr;
} else {
void* ret = reinterpret_cast<void*>(&this->stack[this->idx]);
this->idx = nIdx;
return ret;
}
}
inline void deallocate(void* p, size_t s) override {
char* a = static_cast<char*>(p) + s;
char* b = &(this->stack[this->idx]);
if(a == b) {
this->idx -= s;
}
}
inline size_t max() const override {
return Size;
}
inline bool allocated(void* p) const override {
return p >= this->stack && p <= &this->stack[this->idx];
}
};
template<typename T, typename Allo>
class STLAllo {
Allo allo;
public:
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;
inline STLAllo() {}
template<typename U>
struct rebind {typedef STLAllo<U,Allo> 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 = allo.allocate(sizeof(T) * n);
if(ptr == nullptr) {
throw std::bad_alloc();
}
return reinterpret_cast<pointer>(ptr);
}
inline void deallocate(pointer ptr, size_type s) {
this->allo.deallocate(ptr, sizeof(T) * s);
}
inline size_type max_size() const {
return this->allo.max();
}
template<typename... Args>
void construct(pointer p, Args&&... args) {
::new((void*)p) T(std::forward<Args>(args)...);
}
void destroy(pointer p) {
p->~T();
}
};
template <class T1, class T2, class A1, class A2>
bool operator==(const STLAllo<T1,A1>&, const STLAllo<T2,A2>&) {
return std::is_same<A1,A2>::value;
}
template <class T1, class T2, class A1, class A2>
bool operator!=(const STLAllo<T1,A1>&, const STLAllo<T2,A2>&) {
return !std::is_same<A1,A2>::value;
}
}