Repository navigation
Expand file tree
/
Copy pathBumpPool.h
More file actions
291 lines (238 loc) · 9.48 KB
/
Copy pathBumpPool.h
File metadata and controls
291 lines (238 loc) · 9.48 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
/*
* Author: 2025- TheTrustedComputer
*
* Memory pools are an optimization technique for efficient allocation and deallocation without the overhead of malloc() and free().
* By using large preallocated blocks and growing them as needed, memory pools can reduce fragmentation and improve cache performance.
* We implemented a rudimentary bump allocator that prioritizes user data over metadata, moving the free list pointers as necessary.
*/
#ifndef BUMPPOOL_H
#define BUMPPOOL_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#if __STDC_VERSION__ >= 201112l
#if __has_include("threads.h")
#include <threads.h>
#else
#include "Compat/threads.h"
#endif
#endif
#if __STDC_VERSION__ < 202311l
#define constexpr const
#define nullptr NULL
#endif
#ifndef RECSIO_H
#include "RECSIO.h"
#endif
#define MemoryPool_probable(_x) __builtin_expect(_x, 1)
#define MemoryPool_improbable(_x) __builtin_expect(_x, 0)
#pragma pack(push, 1)
typedef struct MemoryBlock
{
struct MemoryBlock *restrict next; // Pointer to next free block
size_t size; // Free memory size, excluding header
}
MemoryBlock;
typedef struct
{
void *restrict *restrict baseAddr; // Array of base addresses
MemoryBlock *restrict freeList; // Pointer to first free block
MemoryBlock *restrict lastFree; // Pointer to last free block
size_t size, chunks; // Managed size; number of allocated chunks
mtx_t lock; // Mutex for thread safety
}
MemoryPool;
#pragma pack(pop)
static constexpr size_t MEMPOOL_INIT_SIZE = 1073741824; // 1 GB
static constexpr size_t MEMPOOL_EXPAND_RATE = MEMPOOL_INIT_SIZE;
static constexpr size_t MEMPOOL_HEADER_SIZE = sizeof(MemoryBlock);
////////////////////////////////////////////////////////////////
/// @brief Finds a free block with a given size.
/// @param _pool Unaliased pointer to the memory pool.
/// @param _SIZE Requested block size in bytes.
/// @param _pFree Output pointer for the previous free block.
/// @param _cFree Output pointer for the current free block.
////////////////////////////////////////////////////////////////
static inline void MemoryPool_findFree(MemoryPool *const restrict _pool, const size_t _SIZE, MemoryBlock *restrict *const restrict _pFree, MemoryBlock *restrict *const restrict _cFree)
{
*_pFree = nullptr;
*_cFree = _pool->freeList;
while (*_cFree)
{
if (MemoryPool_probable((*_cFree)->size >= _SIZE))
{
return;
}
*_pFree = *_cFree;
*_cFree = (*_cFree)->next;
}
*_pFree = nullptr;
}
////////////////////////////////////////////////////////////////
/// @brief Initializes a memory pool with zero blocks.
/// @param _pool Unaliased pointer to the memory pool.
/// @return Zero if successful; otherwise non-zero.
// 1 => Base address allocation failed.
// 2 => Memory pool allocation failed.
// 3 => Block size is too small.
// 4 => Mutex initialization failed.
////////////////////////////////////////////////////////////////
int MemoryPool_init(MemoryPool *const restrict _pool)
{
_pool->size = MEMPOOL_INIT_SIZE;
_pool->chunks = 0;
if (MemoryPool_improbable(!(_pool->baseAddr = REC_malloc(sizeof(*_pool->baseAddr) * (_pool->chunks + 1), "Could not initialize the base address array.", false))))
{
return 1;
}
if (MemoryPool_improbable(!(_pool->baseAddr[_pool->chunks] = REC_malloc(_pool->size, "Could not initialize the memory pool.", false))))
{
return 2;
}
if (MemoryPool_improbable(_pool->size < MEMPOOL_HEADER_SIZE))
{
fprintf(stderr, "\e[1mMEMORY POOL ERROR: The block size is too small.\e[0m\n");
REC_free(_pool->baseAddr[_pool->chunks]);
REC_free(_pool->baseAddr);
return 3;
}
if (MemoryPool_improbable(REC_mtx_init(&_pool->lock, mtx_plain, "Could not initialize the memory pool mutex.", false) != thrd_success))
{
REC_free(_pool->baseAddr[_pool->chunks]);
REC_free(_pool->baseAddr);
return 4;
}
_pool->freeList = _pool->baseAddr[_pool->chunks];
_pool->freeList->size = _pool->size - MEMPOOL_HEADER_SIZE;
_pool->freeList->next = nullptr;
_pool->lastFree = _pool->freeList;
return 0;
}
///////////////////////////////////////////////////////////////
/// @brief Releases all memory used by a memory pool.
/// @param _pool Unaliased pointer to the memory pool.
///////////////////////////////////////////////////////////////
void MemoryPool_destroy(MemoryPool *const restrict _pool)
{
if (_pool->baseAddr)
{
for (size_t i = 0; i <= _pool->chunks; i++)
{
REC_free(_pool->baseAddr[i]);
}
REC_free(_pool->baseAddr);
_pool->freeList = _pool->lastFree = nullptr;
}
_pool->size = _pool->chunks = 0;
mtx_destroy(&_pool->lock);
}
//////////////////////////////////////////////////////////////////////////////////
/// @brief Expands the memory pool by a factor of `MEMPOOL_EXPAND_RATE`.
/// @param _pool Unaliased pointer to the memory pool.
/// @return Zero if successful, otherwise non-zero.
/// 1 => Expansion failed.
/// 2 => Reallocation failed.
//////////////////////////////////////////////////////////////////////////////////
int MemoryPool_expand(MemoryPool *const restrict _pool)
{
void *const restrict expandChunk = REC_malloc(MEMPOOL_EXPAND_RATE, "Could not expand the memory pool, not increasing size.", false);
if (MemoryPool_improbable(!expandChunk))
{
return 1;
}
void *restrict *const restrict expandBase = REC_realloc(_pool->baseAddr, sizeof(*_pool->baseAddr) * (++_pool->chunks + 1));
if (MemoryPool_improbable(!expandBase))
{
REC_free(expandChunk);
return 2;
}
_pool->baseAddr = expandBase;
_pool->baseAddr[_pool->chunks] = expandChunk;
MemoryBlock *const restrict chunkBlock = expandChunk;
chunkBlock->size = MEMPOOL_EXPAND_RATE - MEMPOOL_HEADER_SIZE;
chunkBlock->next = nullptr;
_pool->lastFree ? (_pool->lastFree->next = chunkBlock) : (_pool->freeList = chunkBlock);
_pool->lastFree = chunkBlock;
_pool->size += MEMPOOL_EXPAND_RATE;
return 0;
}
////////////////////////////////////////////////////////////////////////////
/// @brief Allocates a block within a memory pool of a given size.
/// @param _pool Unaliased pointer to the memory pool.
/// @param _SIZE Requested block size in bytes.
/// @return Base address to the found free block; otherwise NULL.
////////////////////////////////////////////////////////////////////////////
void *MemoryPool_alloc(MemoryPool *const restrict _pool, const size_t _SIZE)
{
if (_SIZE)
{
MemoryBlock *restrict prevBlk, *restrict currBlk;
for (;;)
{
MemoryPool_findFree(_pool, _SIZE, &prevBlk, &currBlk);
if (MemoryPool_improbable(!currBlk))
{
if (MemoryPool_improbable(MemoryPool_expand(_pool)))
{
return nullptr;
}
continue;
}
break;
}
const size_t REM = currBlk->size - _SIZE;
if (MemoryPool_probable(REM >= MEMPOOL_HEADER_SIZE))
{
MemoryBlock *const restrict splitBlk = (MemoryBlock*)((uint8_t*)(currBlk) + _SIZE);
splitBlk->size = REM;
splitBlk->next = currBlk->next;
prevBlk ? (prevBlk->next = splitBlk) : (_pool->freeList = splitBlk);
_pool->lastFree == currBlk ? (_pool->lastFree = splitBlk) : 0;
}
else
{
prevBlk ? (prevBlk->next = currBlk->next) : (_pool->freeList = currBlk->next);
_pool->lastFree == currBlk ? (_pool->lastFree = prevBlk) : 0;
}
return currBlk;
}
return nullptr;
}
////////////////////////////////////////////////////////////
/// @brief Zeros all values used by a memory pool.
/// @param _pool Unaliased pointer to the memory pool.
////////////////////////////////////////////////////////////
void MemoryPool_clear(MemoryPool *const restrict _pool)
{
for (size_t i = 0; i <= _pool->chunks; i++)
{
memset(_pool->baseAddr[i] + MEMPOOL_HEADER_SIZE, 0, _pool->size - MEMPOOL_HEADER_SIZE);
}
}
/////////////////////////////////////////////////////////////////
/// @brief Thread-safe version of `MemoryPool_alloc()`.
/// @param _pool Unaliased pointer to the memory pool.
/// @param _SIZE Requested block size in bytes.
/// @return The value of `MemoryPool_alloc()`.
/////////////////////////////////////////////////////////////////
void *MemoryPool_alloc_safe(MemoryPool *const restrict _pool, const size_t _SIZE)
{
mtx_lock(&_pool->lock);
void *const restrict allocRes = MemoryPool_alloc(_pool, _SIZE);
mtx_unlock(&_pool->lock);
return allocRes;
}
//////////////////////////////////////////////////////////////////
/// @brief Thread-safe version of `MemoryPool_expand()`.
/// @param _pool Unaliased pointer to the memory pool.
/// @return The value of `MemoryPool_expand()`.
//////////////////////////////////////////////////////////////////
int MemoryPool_expand_safe(MemoryPool *const restrict _pool)
{
mtx_lock(&_pool->lock);
int expandRes = MemoryPool_expand(_pool);
mtx_unlock(&_pool->lock);
return expandRes;
}
#endif // BUMPPOOL_H //