-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreedom_pool.h
executable file
·458 lines (373 loc) · 14.8 KB
/
freedom_pool.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
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
// freedom_pool.h v1.36 (C)2023-2024 DEMOS
//
// This is the most efficient block-pool memory management system you can find.
// I tried many before writing my own: rpmalloc, tlsf, etc.
// This code is partially based off this block allocator concept:
// https://www.codeproject.com/Articles/1180070/Simple-Variable-Size-Memory-Block-Allocator
//
// NEW (v1.35): And even more bug fixes (Jun 23, 2024)
#pragma once
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <dlfcn.h>
#ifdef __cplusplus
#include <map>
#include <iostream>
#endif
#include <dispatch/dispatch.h>
#include <dispatch/queue.h>
#include <assert.h>
#include <malloc/malloc.h>
#include "atomic.h"
// fprintf
#define DEBUG_PRINTF fprintf
//#define DISABLE_NEWDELETE_OVERRIDE
//#define DISABLE_MALLOC_FREE_OVERRIDE
// allocate on the stack (otherwise comment out)
#define FREEDOM_STACK_ALLOC
#define FREEDOM_DEBUG
//#define BREAK_ON_THRESH
static const size_t MBYTE = 1048576;
static const size_t KBYTE = 1024;
static const size_t THRESH_DEBUG_BREAK = 18 * MBYTE;
static const size_t THRESH_DEBUG_PRINT = 100 * KBYTE;
static const size_t DEFAULT_GROW = 200 * MBYTE; // 50 MB
static const size_t GROW_INCREMENT = 50 * MBYTE; // 50 MB increment growth
#define MALLOC_V4SF_ALIGNMENT 64
#define TOKEN_ID (uint64_t)'FREE'
#define DEBUGGER do { \
raise(SIGINT); \
} while (0);
typedef void *_Nullable (*real_malloc_ptr)(size_t);
typedef void *_Nullable (*real_calloc_ptr)(size_t count, size_t size);
typedef void (*real_free_ptr)(void *_Nullable p);
typedef void *_Nullable (*real_realloc_ptr)(void *_Nullable p, size_t new_size);
typedef size_t (*real_malloc_size_ptr)(const void *_Nullable ptr);
typedef size_t (*real_malloc_usable_size_ptr)(const void *_Nullable ptr);
extern real_malloc_ptr _Nullable real_malloc;
extern real_free_ptr _Nullable real_free;
extern real_calloc_ptr _Nullable real_calloc;
extern real_realloc_ptr _Nullable real_realloc;
extern real_malloc_size_ptr _Nullable real_malloc_size;
extern real_malloc_usable_size_ptr _Nullable real_malloc_usable_size;
extern "C" {
size_t malloc_size(const void *_Nullable ptr);
size_t malloc_usable_size(void *_Nullable ptr);
}
template <size_t poolsize = DEFAULT_GROW>
class FreedomPool
{
public:
static const size_t MBYTE = 1048576;
FreedomPool(): m_Internal(true), m_MaxSize(0), m_FreeSize(0)
{
initialize_overrides();
m_Lock.init();
m_FreeBlocksByOffset.empty();
m_FreeBlocksBySize.empty();
#ifndef FREEDOM_STACK_ALLOC
m_Data = NULL;
#endif
ExtendPool(poolsize);
// printBlocks();
}
~FreedomPool()
{
#ifndef FREEDOM_STACK_ALLOC
if (m_Data)
real_free(m_Data);
m_Data = NULL;
#endif
}
__inline bool IsFull() const { return m_FreeSize == 0; };
__inline bool IsEmpty() const { return m_FreeSize == m_MaxSize; };
__inline size_t GetMaxSize() const { return m_MaxSize; }
__inline size_t GetFreeSize() const { return m_FreeSize; }
__inline size_t GetUsedSize() const{ return m_MaxSize - m_FreeSize; }
__inline size_t GetNumFreeBlocks() const {
return m_FreeBlocksByOffset.size();
}
__inline size_t GetMaxFreeBlockSize() const {
return !m_FreeBlocksBySize.empty() ? m_FreeBlocksBySize.rbegin()->first : 0;
}
static void initialize_overrides()
{
if (!real_malloc) {
real_malloc = (real_malloc_ptr)dlsym(RTLD_NEXT, "malloc");
}
if (!real_free) {
real_free = (real_free_ptr)dlsym(RTLD_NEXT, "free");
}
if (!real_calloc) {
real_calloc = (real_calloc_ptr)dlsym(RTLD_NEXT, "calloc");
}
if (!real_realloc) {
real_realloc = (real_realloc_ptr)dlsym(RTLD_NEXT, "realloc");
}
if (!real_malloc_size) {
real_malloc_size = (real_malloc_size_ptr)dlsym(RTLD_NEXT, "malloc_size");
}
if (!real_malloc_usable_size) {
real_malloc_usable_size = (real_malloc_usable_size_ptr)dlsym(RTLD_NEXT, "malloc_usable_size");
}
}
void *_Nullable malloc(size_t nb_bytes)
{
if (!real_malloc) initialize_overrides();
if ( m_Internal || !m_MaxSize )
return real_malloc(nb_bytes);
if (GetFreeSize() < nb_bytes + 3 * sizeof(void*)) {
#ifdef FREEDOM_STACK_ALLOC
DEBUG_PRINTF(stderr, "FreedomPool::malloc() Ran out of space allocating %lld MB used %lld of %lld MB. Static model, returning NULL\n", nb_bytes/MBYTE, GetUsedSize()/MBYTE, GetMaxSize()/MBYTE, (GetUsedSize() + nb_bytes + 3 * sizeof(void*) + GROW_INCREMENT) / MBYTE);
return NULL;
#else
DEBUG_PRINTF(stderr, "FreedomPool::malloc() Ran out of space allocating %lld MB used %lld of %lld MB, expanding to %lld MB\n", nb_bytes/MBYTE, GetUsedSize()/MBYTE, GetMaxSize()/MBYTE, (GetUsedSize() + nb_bytes + 3 * sizeof(void*) + GROW_INCREMENT) / MBYTE);
dispatch_async( dispatch_get_main_queue(), ^{
// grow pool by GROW_INCREMENT MB + requested size
ExtendPool( GetUsedSize() + nb_bytes + 3 * sizeof(void*) + GROW_INCREMENT * MBYTE);
});
#endif
}
return Malloc(nb_bytes);
}
void *_Nullable calloc(size_t count, size_t size)
{
if (!real_calloc) initialize_overrides();
if ( m_Internal || !m_MaxSize )
return real_calloc(count, size);
return Malloc(count * size);
}
void free(void *_Nullable p)
{
if (!real_free) initialize_overrides();
if ( m_Internal || !m_MaxSize ) {
real_free(p);
return;
}
if (p && *(uint64_t*)((int8_t*)p - sizeof(void*)) == TOKEN_ID)
Free(p);
else if (p) real_free(p);
}
void *_Nullable realloc(void *_Nullable p, size_t new_size)
{
void *new_p;
size_t old_size = 0;
if (!real_realloc) initialize_overrides();
if ( m_Internal || !m_MaxSize )
return real_realloc(p, new_size);
if (p && (*(uint64_t*)((int8_t*)p - sizeof(void*)) == TOKEN_ID))
{
old_size = *(size_t*)((void**)p - 2);
if (old_size <= 0)
return real_malloc(new_size);
if (!(new_p = Malloc(new_size)))
return nullptr;
memcpy(new_p, p, old_size);
Free(p);
return new_p;
} else
return real_realloc(p, new_size);
}
size_t malloc_size(const void *_Nullable p)
{
if (!real_malloc_size) initialize_overrides();
if ( m_Internal || !m_MaxSize )
return real_malloc_size(p);
if (p && *(uint64_t*)((int8_t*)p - sizeof(void*)) == TOKEN_ID)
return *((size_t *) p - 2) + 4;
else
return real_malloc_size(p);
}
size_t malloc_usable_size(const void *_Nullable p)
{
if (!real_malloc_usable_size) initialize_overrides();
if ( m_Internal || !m_MaxSize )
return real_malloc_usable_size(p);
if (p && *(uint64_t*)((int8_t*)p - sizeof(void*)) == TOKEN_ID)
return *((size_t *) p - 2) + 4;
return real_malloc_usable_size(p);
}
size_t ExtendPool(size_t ExtraSize)
{
#ifndef FREEDOM_STACK_ALLOC
if (m_MaxSize) {
DEBUG_PRINTF(stderr, "FreedomPool isn't allowed to extend passed initial in static allocation. Initially set to %ulld\n", ExtraSize/MBYTE);
return m_MaxSize;
}
#endif
m_Lock.lock();
size_t NewBlockOffset = m_MaxSize;
size_t NewBlockSize = ExtraSize;
DEBUG_PRINTF(stderr, "Expanding FreedomPool internal size to: %lld MB\n", ExtraSize/MBYTE);
if (!m_FreeBlocksByOffset.empty())
{
auto LastBlockIt = m_FreeBlocksByOffset.end();
--LastBlockIt;
const auto LastBlockOffset = LastBlockIt->first;
const auto LastBlockSize = LastBlockIt->second.Size;
if (LastBlockOffset + LastBlockSize == m_MaxSize)
{
// Extend the last block
NewBlockOffset = LastBlockOffset;
NewBlockSize += LastBlockSize;
// VERIFY_EXPR(LastBlockIt->second.OrderBySizeIt->first == LastBlockSize &&
// LastBlockIt->second.OrderBySizeIt->second == LastBlockIt);
m_FreeBlocksBySize.erase(LastBlockIt->second.OrderBySizeIt);
m_FreeBlocksByOffset.erase(LastBlockIt);
}
}
AddNewBlock(NewBlockOffset, NewBlockSize);
m_MaxSize += ExtraSize;
m_FreeSize += ExtraSize;
#ifndef FREEDOM_STACK_ALLOC
if (!m_Data)
m_Data = (int8_t*)real_malloc(m_MaxSize);
else
m_Data = (int8_t*)real_realloc(m_Data, m_MaxSize);
#endif
#ifdef DILIGENT_DEBUG
VERIFY_EXPR(m_FreeBlocksByOffset.size() == m_FreeBlocksBySize.size());
if (!m_DbgDisableDebugValidation)
DbgVerifyList();
#endif
m_Lock.unlock();
return m_MaxSize;
}
void printBlocks()
{
static int x = 0;
if (!(x++%100))
for (auto it = m_BlockCount.rbegin(); it != m_BlockCount.rend(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
}
protected:
struct FreeBlockInfo;
// Type of the map that keeps memory blocks sorted by their offsets
using TFreeBlocksByOffsetMap = std::map<size_t, FreeBlockInfo>;
// Type of the map that keeps memory blocks sorted by their sizes
using TFreeBlocksBySizeMap = std::multimap<size_t, typename TFreeBlocksByOffsetMap::iterator>;
typedef struct FreeBlockInfo
{
FreeBlockInfo(size_t _Size) : Size(_Size) { }
// Block size (no reserved space for the size of allocation)
size_t Size;
// Iterator referencing this block in the multimap sorted by the block size
TFreeBlocksBySizeMap::iterator OrderBySizeIt;
} FreeBlockInfo;
void AddNewBlock(size_t Offset, size_t Size)
{
auto NewBlockIt = m_FreeBlocksByOffset.emplace(Offset, Size);
auto OrderIt = m_FreeBlocksBySize.emplace(Size, NewBlockIt.first);
NewBlockIt.first->second.OrderBySizeIt = OrderIt;
}
void *_Nullable Malloc(size_t Size)
{
m_Lock.trylock();
m_Internal = true;
Size += sizeof(void*) * 3;
if (m_FreeSize < Size) {
m_Internal = false;
m_Lock.unlock();
return NULL;
}
// Get the first block that is large enough to encompass Size bytes
auto SmallestBlockItIt = m_FreeBlocksBySize.lower_bound(Size);
if(SmallestBlockItIt == m_FreeBlocksBySize.end()) {
m_Internal = false;
m_Lock.unlock();
return NULL;
}
auto SmallestBlockIt = SmallestBlockItIt->second;
int64_t Offset = SmallestBlockIt->first;
int64_t NewOffset = Offset + Size;
int64_t NewSize = SmallestBlockIt->second.Size - Size;
m_FreeBlocksBySize.erase(SmallestBlockItIt);
m_FreeBlocksByOffset.erase(SmallestBlockIt);
if (NewSize > 0) {
AddNewBlock(NewOffset, NewSize);
}
m_FreeSize -= Size;
*(size_t*)(&m_Data[Offset]) = Offset;
*(size_t*)(&m_Data[Offset + sizeof(void*)]) = Size - 3 * sizeof(void*);
*(uint64_t*)(&m_Data[Offset + 2 * sizeof(void*)]) = TOKEN_ID;
m_Internal = false;
m_Lock.unlock();
return (void*)&m_Data[Offset + 3 * sizeof(void*)];
}
void Free(void *_Nullable ptr)
{
m_Lock.lock();
m_Internal = true;
uint64_t Token = *((uint64_t*)ptr - 1);
if (Token != TOKEN_ID) {
DEBUG_PRINTF(stderr, "WARNING: Trying to internal_free non-native pointer, incorrect tokenID\n");
m_Internal = false;
m_Lock.unlock();
return;
}
size_t Offset = (size_t)((uint64_t*)ptr - 3 * sizeof(void*));
size_t Size = *(uint64_t*)((int8_t*)ptr - 2 * sizeof(void*)) + 3 * sizeof(void*);
// Find the first element whose offset is greater than the specified offset
auto NextBlockIt = m_FreeBlocksByOffset.upper_bound(Offset);
auto PrevBlockIt = NextBlockIt;
if(PrevBlockIt != m_FreeBlocksByOffset.begin())
--PrevBlockIt;
else
PrevBlockIt = m_FreeBlocksByOffset.end();
size_t NewSize, NewOffset;
if(PrevBlockIt != m_FreeBlocksByOffset.end() && Offset == PrevBlockIt->first + PrevBlockIt->second.Size)
{
NewSize = PrevBlockIt->second.Size + Size;
NewOffset = PrevBlockIt->first;
if (NextBlockIt != m_FreeBlocksByOffset.end() && Offset + Size == NextBlockIt->first)
{
NewSize += NextBlockIt->second.Size;
m_FreeBlocksBySize.erase(PrevBlockIt->second.OrderBySizeIt);
m_FreeBlocksBySize.erase(NextBlockIt->second.OrderBySizeIt);
// Delete the range of two blocks
++NextBlockIt;
m_FreeBlocksByOffset.erase(PrevBlockIt, NextBlockIt);
} else
{
m_FreeBlocksBySize.erase(PrevBlockIt->second.OrderBySizeIt);
m_FreeBlocksByOffset.erase(PrevBlockIt);
}
} else if (NextBlockIt != m_FreeBlocksByOffset.end() && Offset + Size == NextBlockIt->first)
{
NewSize = Size + NextBlockIt->second.Size;
NewOffset = Offset;
m_FreeBlocksBySize.erase(NextBlockIt->second.OrderBySizeIt);
m_FreeBlocksByOffset.erase(NextBlockIt);
} else
{
NewSize = Size;
NewOffset = Offset;
}
AddNewBlock(NewOffset, NewSize);
m_FreeSize += Size;
m_Internal = false;
m_Lock.unlock();
}
#ifdef FREEDOM_STACK_ALLOC
int8_t m_Data[poolsize];
#else
int8_t *m_Data;
#endif
size_t m_MaxSize, m_FreeSize;
TFreeBlocksByOffsetMap m_FreeBlocksByOffset;
TFreeBlocksBySizeMap m_FreeBlocksBySize;
std::map<size_t, int> m_BlockCount;
AtomicLock m_Lock;
volatile bool m_Internal;
};
#if !defined(DISABLE_NEWDELETE_OVERRIDE)
#ifdef __cplusplus
void *_Nullable operator new(std::size_t n);
void operator delete(void *_Nullable p) throw();
void *_Nullable operator new[](std::size_t n);
void operator delete[](void *_Nullable p) throw();
#endif // __cplusplus
#endif // DISABLE_NEWDELETE_OVERRIDE
extern FreedomPool<DEFAULT_GROW> bigpool;