-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryPool.h
More file actions
526 lines (428 loc) · 13.9 KB
/
MemoryPool.h
File metadata and controls
526 lines (428 loc) · 13.9 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#pragma once
#include <assert.h>
#include <vector>
#include <array>
#include <list>
#include <map>
#define DEBUG_MEMORY 0
#define ENABLE_STATISTIC 0
namespace mem
{
typedef unsigned short u16;
typedef signed int s32;
typedef unsigned int u32;
typedef signed long long s64;
typedef unsigned long long u64;
typedef double f64;
typedef void* address_ptr;
/*
* class MemoryPool
*/
class MemoryPool final
{
public:
MemoryPool(const MemoryPool&) = delete;
MemoryPool& operator=(const MemoryPool&) = delete;
/*
* class MemoryAllocator. Allocator for pool
*/
class MemoryAllocator
{
public:
explicit MemoryAllocator() noexcept = default;
virtual ~MemoryAllocator() = default;
virtual address_ptr allocate(u64 size, u32 aligment = 0, void* user = nullptr) = 0;
virtual void deallocate(address_ptr memory, u64 size = 0, void* user = nullptr) = 0;
};
static constexpr u64 k_mixSizePageSize = 65'536;
/*
* MemoryPool constuctor
* param pageSize : page size (best size 65KB, but no more)
* param allocator: allocator
* param user: user data
*/
explicit MemoryPool(u64 pageSize, MemoryAllocator* allocator = MemoryPool::getDefaultMemoryAllocator(), bool deleteUnusedPools = true, void* user = nullptr) noexcept;
/*
* ~MemoryPool destuctor
*/
~MemoryPool();
/*
* Request free memory from pool
* param size: count bytes will be requested
* param aligment: aligment
*/
address_ptr allocMemory(u64 size, u32 aligment = 0);
template<class T>
T* allocElement()
{
return reinterpret_cast<T*>(allocMemory(sizeof(T)));
}
template<class T>
T* allocArray(u64 count)
{
return reinterpret_cast<T*>(allocMemory(sizeof(T) * count));
}
/*
* Return memory to pool
* param address_ptr: address of memory
*/
void freeMemory(address_ptr memory);
/*
* Prepare small table pools
* call it if need more speed, but it creates a log of empty pools
*/
void preAllocatePools();
/*
* Reset pools, Return all requested allocation
*/
void reset();
/*
* Free pool, delete all pools
*/
void clear();
/*
* default allocator
*/
static MemoryAllocator* getDefaultMemoryAllocator();
void collectStatistic();
private:
MemoryAllocator* m_allocator;
void* m_userData;
struct Pool;
struct PoolTable;
template<class T>
struct Node
{
Node<T>() noexcept
: _prev(nullptr)
, _next(nullptr)
{
}
Node<T>(const Node<T>&) = default;
void reset()
{
_next = nullptr;
_prev = nullptr;
}
T* _prev;
T* _next;
};
template<class T>
class List final
{
public:
List() noexcept
{
List<T>::clear();
}
List(const List<T>&) = default;
~List() = default;
T* begin()
{
return _end._next;
}
T* end()
{
return &_end;
}
bool empty() const
{
return &_end == _end._next;
}
void clear()
{
link(&_end, &_end);
#if DEBUG_MEMORY
_size = 0;
#endif
}
void priorityInsert(T* node)
{
//TODO use binary search
T* current = begin();
while (current != end())
{
if (node < current)
{
link(current->_prev, node);
link(node, current);
#if DEBUG_MEMORY
++_size;
#endif
return;
}
current = current->_next;
}
link(_end._prev, node);
link(node, &_end);
#if DEBUG_MEMORY
++_size;
#endif
}
void insert(T* node)
{
link(_end._prev, node);
link(node, &_end);
#if DEBUG_MEMORY
++_size;
#endif
}
void merge()
{
if (List<T>::empty())
{
return;
}
T* node = List<T>::begin();
T* nodeNext = List<T>::begin()->_next;
while (nodeNext != List<T>::end())
{
address_ptr ptrNext = (address_ptr)(reinterpret_cast<u64>(node->ptr()) + node->_size);
if (nodeNext == ptrNext)
{
node->_size += nodeNext->_size;
nodeNext = List<T>::erase(nodeNext);
continue;
}
node = node->_next;
nodeNext = nodeNext->_next;
}
}
T* erase(T* node)
{
link(node->_prev, node->_next);
#if DEBUG_MEMORY
--_size;
assert(_size >= 0);
#endif
return node->_next;
}
private:
void link(T* l, T* r)
{
l->_next = r;
r->_prev = l;
}
T _end;
#if DEBUG_MEMORY
s64 _size;
#endif
};
struct Block : Node<Block>
{
Block()
: _pool(nullptr)
, _size(0)
{
}
Block(const Block&) = default;
Block(Pool* pool, u64 size) noexcept
: _pool(pool)
, _size(size)
{
}
Pool* _pool;
u64 _size;
#if DEBUG_MEMORY
address_ptr _ptr;
#endif //DEBUG_MEMORY
address_ptr ptr()
{
#if DEBUG_MEMORY
u64 offset = sizeof(Block);
address_ptr ptr = reinterpret_cast<address_ptr>(reinterpret_cast<u64>(this) + offset);
assert(ptr);
return ptr;
#else
return reinterpret_cast<address_ptr>(reinterpret_cast<u64>(this) + sizeof(Block));
#endif
}
bool compare(Block* block)
{
address_ptr ptrNext = reinterpret_cast<address_ptr>(reinterpret_cast<u64>(block->ptr()) + block->_size);
if (ptrNext == block)
{
return true;
}
return false;
}
};
struct Pool : Node<Pool>
{
Pool()
: _table(nullptr)
, _blockSize(0)
, _poolSize(0)
{
_used.clear();
_free.clear();
}
Pool(const Pool&) = default;
Pool(PoolTable const* table, u64 blockSize, u64 poolSize) noexcept
: _table(table)
, _blockSize(blockSize)
, _poolSize(poolSize)
{
_used.clear();
_free.clear();
}
address_ptr ptr() const
{
#if DEBUG_MEMORY
u64 offset = sizeof(Pool);
address_ptr ptr = reinterpret_cast<address_ptr>(reinterpret_cast<u64>(this) + offset);
assert(ptr);
return ptr;
#else
return reinterpret_cast<address_ptr>(reinterpret_cast<u64>(this) + sizeof(Pool));
#endif
}
void reset()
{
Block* block = _used.begin();
while (block != _used.end())
{
Block* newBlock = block->_next;
_free.insert(block);
block = newBlock;
}
_used.clear();
}
PoolTable const* _table;
u64 _blockSize;
const u64 _poolSize;
List<Block> _used;
List<Block> _free;
};
struct PoolTable
{
enum Type : u32
{
Default = 0,
SmallTable,
};
PoolTable() noexcept
: _size(0)
, _type(Type::Default)
{
}
PoolTable(const PoolTable& table)
: _size(0)
, _type(Type::Default)
{
//wrong runtime logic, but need for compile
assert(false);
}
explicit PoolTable(u16 size, PoolTable::Type type) noexcept
: _size(size)
, _type(Type::Default)
{
}
List<Pool> _activePools;
List<Pool> _fullPools;
u64 _size;
Type _type;
};
static const u64 k_countPagesPerAllocation = 16;
static const u64 k_maxSizeSmallTableAllocation = 32'768;
std::array<u16, (k_maxSizeSmallTableAllocation >> 2)> m_smallTableIndex;
std::vector<PoolTable> m_smallPoolTables;
PoolTable m_poolTable;
const u64 k_pageSize;
const u64 k_maxSizePoolAllocation;
List<Block> m_largeAllocations;
static MemoryAllocator* s_defaultMemoryAllocator;
Pool* allocateFixedBlocksPool(PoolTable* table, u32 align);
Pool* allocatePool(PoolTable* table, u32 align);
void deallocatePool(Pool* pool);
Block* initBlock(address_ptr ptr, Pool* pool, u64 size);
void freeBlock(Block* block);
Block* allocateFromSmallTables(u64 size);
Block* allocateFromTable(u64 size);
void collectEmptyPools(List<Pool>& pools, std::vector<Pool*>& markedToDelete);
std::vector<Pool*> m_markedToDelete;
const bool k_deleteUnusedPools;
#if ENABLE_STATISTIC
struct Statistic
{
Statistic()
{
memset(this, 0, sizeof(Statistic));
}
void reset()
{
_allocateTime = 0;
_dealocateTime = 0;
_generalAllocationCount = 0;
_generalAllocationSize = 0;
for (u32 i = 0; i < 3; ++i)
{
_tableAllocationCount[i] = 0;
_tableAllocationSizes[i] = 0;
_poolsAllocationCount[i] = 0;
_poolAllocationSizes[i] = 0;
}
_globalAllocationSize = 0;
}
template<u32 type>
void registerAllocation(u64 size)
{
static_assert(type < 3);
++_tableAllocationCount[type];
_tableAllocationSizes[type] += size;
assert((u64)_tableAllocationCount[type] < (u64)std::numeric_limits<s64>().max());
++_generalAllocationCount;
assert((u64)_generalAllocationCount < (u64)std::numeric_limits<s64>().max());
_generalAllocationSize += size;
}
template<u32 type>
void registerDeallocation(u64 size)
{
static_assert(type < 3);
--_tableAllocationCount[type];
_tableAllocationSizes[type] -= size;
assert(_tableAllocationCount[type] >= 0 && _tableAllocationSizes[type] >= 0);
--_generalAllocationCount;
_generalAllocationSize -= size;
assert(_generalAllocationCount >= 0 && _generalAllocationSize >= 0);
}
template<u32 type>
void registerPoolAllocation(u64 size)
{
static_assert(type < 3);
++_poolsAllocationCount[type];
_poolAllocationSizes[type] += size;
assert((u64)_poolsAllocationCount[type] < (u64)std::numeric_limits<s64>().max() && (u64)_poolAllocationSizes[type] < (u64)std::numeric_limits<s64>().max());
}
template<u32 type>
void registerPoolDeallocation(u64 size)
{
static_assert(type < 3);
--_poolsAllocationCount[type];
_poolAllocationSizes[type] -= size;
assert(_poolsAllocationCount[type] >= 0 && _poolAllocationSizes[type] >= 0);
}
u64 _allocateTime;
u64 _dealocateTime;
s64 _tableAllocationCount[3];
s64 _tableAllocationSizes[3];
s64 _generalAllocationCount;
u64 _generalAllocationSize;
s64 _poolsAllocationCount[3];
s64 _poolAllocationSizes[3];
u64 _globalAllocationSize;
};
Statistic m_statistic;
#endif //ENABLE_STATISTIC
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
class DefaultMemoryAllocator : public MemoryPool::MemoryAllocator
{
public:
explicit DefaultMemoryAllocator() noexcept = default;
~DefaultMemoryAllocator() = default;
address_ptr allocate(u64 size, u32 aligment = 0, void* user = nullptr) override;
void deallocate(address_ptr memory, u64 size = 0, void* user = nullptr) override;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
} //namespace mem