-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.hpp
392 lines (362 loc) · 11.4 KB
/
mem.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#pragma once
#include <cstdint>
#include <cassert>
#include <cstdlib>
#include <cstdlib>
#include <new>
#ifndef __FUNCTION_NAME__
# ifdef WIN32 //WINDOWS
# define __FUNCTION_NAME__ __FUNCTION__
# else //*NIX
# define __FUNCTION_NAME__ __func__
#endif
#endif
#define RMEM_NEW(_alloc, _type) static_cast<_type*>(_alloc.alloc(sizeof(_type)).ptr)
#define RMEM_MAKE(_type, _name, _alloc) _type _name = *(static_cast<_type*>(_alloc.alloc(sizeof(_type)).ptr))
#define RMEM_DEL(_name, _alloc) { reyes::mem::blk _blk(static_cast<void*>(&_name),sizeof(_name)); _alloc.free(_blk); }
namespace reyes
{
namespace mem
{
/* Memory block sturcture. */
struct blk
{
void* ptr;
size_t size;
blk() : ptr(nullptr), size(0) {}
blk(void* p, size_t s = 0) : ptr(p), size(s) {}
template<class ptr_t>
ptr_t to() { return static_cast<ptr_t>(ptr); }
};
/* Allocator interface. */
class AllocatorI
{
public:
virtual bool owns(blk&) = 0;
virtual blk alloc(size_t size) = 0; // TODO: add align
// virtual blk realloc(blk&, size_t); // TODO: add realloc
virtual void free(blk&) = 0;
};
/* Standard malloc/free allocator on heap. */
class CAllocator : public AllocatorI
{
size_t total;
public:
CAllocator() : total(0) {}
bool owns(blk&) { return true; }
blk alloc(size_t size)
{
blk blk;
blk.ptr = ::malloc(size);
blk.size = size;
total += size;
return blk;
}
void free(blk& mem)
{
total -= mem.size;
::free(mem.ptr);
mem.ptr = 0;
}
~CAllocator()
{
assert(total == 0);
}
};
/* Standard malloc/free allocator on heap. */
typedef CAllocator mAllocator;
/* Null allocator, everything always fails. */
class NullAllocator : public AllocatorI
{
public:
bool owns(const blk&) { return false; }
blk alloc(size_t size) { return{ nullptr, 0 }; }
void free(blk& mem) { assert(!mem.ptr && !mem.size); }
};
template <size_t S>
/* Stack allocator, compile time known size (in bytes). */
class StackAllocator : public AllocatorI
{
char data[S];
char* top;
public:
StackAllocator() : top(data) {}
bool owns(blk& mem)
{
return (mem.ptr >= data && mem.ptr<data + S);
}
blk alloc(size_t size)
{
if (size > data + S - top) return{ nullptr, 0 };
blk mem = { top, size };
top += size;
return mem;
}
void free(blk& mem)
{
if (static_cast<char*>(mem.ptr) + mem.size == top)
top = static_cast<char*>(mem.ptr);
}
~StackAllocator()
{
assert(data == top);
}
};
template <class P, class F>
/* Fallback allocator: use Primary first, Fallback otherwise. */
class FallbackAllocator : private P, private F
{
public:
bool owns(blk& mem)
{
return P::owns(mem) || F::owns(mem);
}
blk alloc(size_t size)
{
blk mem = P::alloc(size);
if (!mem.ptr) mem = F::alloc(size);
return mem;
}
void free(blk& mem)
{
if (P::owns(mem))
P::free(mem);
else
F::free(mem);
}
};
template <size_t threshold, class SmallAllocator, class LargeAllocator>
/* Uses small allocator for allocs below threshold, otherwise it uses large allocator. */
class SegregatorAllocator : private SmallAllocator, private LargeAllocator, public AllocatorI
{
public:
blk alloc(size_t size)
{
return (size <= threshold)
? SmallAllocator::alloc(size)
: LargeAllocator::alloc(size);
}
void free(blk& mem)
{
if (!mem.ptr) return;
if (mem.size <= threshold) return SmallAllocator::free(mem);
return LargeAllocator::free(mem);
}
bool owns(blk& mem)
{
return (mem.size <= threshold)
? SmallAllocator::owns(mem)
: LargeAllocator::owns(mem);
}
};
template <class A, class Prefix, size_t prefix_size, class Sufix = void, size_t sufix_size = 0>
/* Allocator that wraps other allocator and adds prefix (and sufix) information. */
class AffixAllocator
{
A allocator;
public:
Prefix* getPrefixPtr(const blk& mem) const
{
return static_cast<Prefix*>(mem.ptr);
}
Sufix* getSufixPtr(const blk& mem) const
{
return static_cast<Sufix*>(static_cast<char*>(mem.ptr) + mem.size - sufix_size);
}
void* getDataPtr(const blk& mem) const
{
return static_cast<void*>(static_cast<char*>(mem.ptr) + sufix_size);
}
blk getInnerblk(const blk& mem) const
{
return{ static_cast<char*>(mem.ptr) + prefix_size, mem.size - prefix_size - sufix_size };
}
blk getOuterblk(const blk& mem) const
{
return{ static_cast<char*>(mem.ptr) - prefix_size, mem.size + prefix_size + sufix_size };
}
bool owns(blk& mem)
{
return allocator::owns(mem);
}
blk alloc(size_t size)
{
blk mem = allocator.alloc(prefix_size + size + sufix_size);
if (mem.ptr) {
if (prefix_size > 0) {
new (static_cast<char*>(mem.ptr) - prefix_size) Prefix{};
}
if (sufix_size > 0) {
new (static_cast<char*>(mem.ptr) + prefix_size + size) Sufix{};
}
getInnerblk(mem);
}
return mem;
}
void free(blk& mem)
{
allocator.free(mem);
}
};
// TODO: alignment
template<class ObjTy>
struct ObjectStack
{
// ....[ Object_n-1 ][ size_n-1 ][ Object_n ][ size_n ]
// ....................................................^ top
size_t capacity;
char* data;
char* top;
ObjectStack(size_t cap=1024)
: capacity(cap)
, data(new char[cap])
, top(data)
{}
void* alloc(size_t size)
{
if (size + sizeof(size_t) > data + capacity - top)
return nullptr;
void* mem = static_cast<void*>(top);
size_t* sz_ptr = (size_t*)(top += size);
*sz_ptr = size;
top += sizeof(size_t);
return mem;
}
ObjTy* pop(void)
{
if (!size()) return 0;
size_t size = *(size_t*)(top -= sizeof(size_t));
top -= size;
return (ObjTy*)(top);
}
ObjTy* get(char* ptr)
{
if(ptr==data) return 0;
size_t size = *(size_t*)(ptr -= sizeof(size_t));
ptr -= size;
return (ObjTy*)(ptr);
}
size_t size() const
{
return top - data;
}
operator bool(void) const
{
return size() > 0;
}
~ObjectStack()
{
assert(data == top);
delete[] data;
}
};
struct ObjectArray
{
size_t capacity;
char* data;
char* writePtr;
char* readPtr;
size_t allocCnt;
ObjectArray(size_t cap)
: capacity(cap)
, data(new char[cap])
, readPtr(data)
, writePtr(data)
, allocCnt(0)
{}
~ObjectArray()
{
assert(0 == allocCnt);
delete[] data;
}
mem::blk alloc(size_t size)
{
assert(size + sizeof(size_t) <= data + capacity - writePtr);
allocCnt++;
size_t* sz_ptr = (size_t*)(writePtr);
*sz_ptr = size;
writePtr += sizeof(size);
void* mem = (void*)(writePtr);
writePtr += size;
return {mem, size};
}
mem::blk getNext(void)
{
assert(allocCnt > 0);
assert(readPtr < writePtr);
allocCnt--;
size_t* sz_ptr = (size_t*)(readPtr);
readPtr += sizeof(size_t);
void* objptr = (void*)(readPtr);
readPtr += *sz_ptr;
return{objptr,*sz_ptr};
}
};
template<class ItemTy, size_t size>
struct Stack
{
ItemTy data[size];
size_t top;
Stack() : top(0) {}
void push(ItemTy item)
{
data[top] = item;
top++;
assert(top <= size);
}
ItemTy pop()
{
assert(top > 0);
top--;
return data[top];
}
~Stack()
{
assert(top == 0);
}
operator bool()
{
return top > 0;
}
};
template<class SlotTy, int slotCount>
struct Pool
{
SlotTy data[slotCount];
int freeList[slotCount]; // can be optimized to reuse
int freeHead;
int freeCnt;
Pool()
: freeHead(0)
, freeCnt(slotCount)
{
for (int i = 0; i < slotCount - 1; i++)
freeList[i] = i + 1;
freeList[slotCount-1] = -1;
}
SlotTy* alloc(void)
{
assert(freeHead>=0 && freeHead<slotCount);
freeCnt--;
int idx = freeHead;
freeHead = freeList[freeHead];
return data + idx;
}
void free(SlotTy* ptr)
{
freeCnt++;
int idx = (ptr - data)/sizeof(SlotTy);
freeList[idx] = freeHead;
freeHead = idx;
}
void free(SlotTy& slot)
{
this->free(&slot);
}
~Pool()
{
assert(slotCount == freeCnt);
}
};
}
}