generated from 8dcc/c-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibpool.c
More file actions
440 lines (377 loc) · 13.7 KB
/
libpool.c
File metadata and controls
440 lines (377 loc) · 13.7 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
/*
* Copyright 2024 8dcc
*
* This program is part of libpool, a tiny (ANSI) C library for pool allocation.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "libpool.h"
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
/*
* External allocation functions.
*/
#if defined(LIBPOOL_NO_STDLIB)
PoolAllocFuncPtr pool_ext_alloc = NULL;
PoolFreeFuncPtr pool_ext_free = NULL;
#else /* !defined(LIBPOOL_NO_STDLIB) */
#include <stdlib.h>
PoolAllocFuncPtr pool_ext_alloc = malloc;
PoolFreeFuncPtr pool_ext_free = free;
#endif /* !defined(LIBPOOL_NO_STDLIB) */
/*
* External multithreading functions.
*/
#if defined(LIBPOOL_THREAD_SAFE)
#if defined(LIBPOOL_NO_STDLIB)
PoolMutexNewFuncPtr pool_ext_mutex_new = NULL;
PoolMutexLockFuncPtr pool_ext_mutex_lock = NULL;
PoolMutexUnlockFuncPtr pool_ext_mutex_unlock = NULL;
PoolMutexDestroyFuncPtr pool_ext_mutex_destroy = NULL;
#else /* !defined(LIBPOOL_NO_STDLIB) */
#include <pthread.h>
static void* pool_ext_mutex_new_impl(void) {
pthread_mutex_t* mutex = pool_ext_alloc(sizeof(pthread_mutex_t));
if (pthread_mutex_init(mutex, NULL) != 0) {
pool_ext_free(mutex);
return NULL;
}
return mutex;
}
static bool pool_ext_mutex_lock_impl(void* mutex) {
return pthread_mutex_lock((pthread_mutex_t*)mutex) == 0;
}
static bool pool_ext_mutex_unlock_impl(void* mutex) {
return pthread_mutex_unlock((pthread_mutex_t*)mutex) == 0;
}
static bool pool_ext_mutex_destroy_impl(void* mutex) {
return pthread_mutex_destroy((pthread_mutex_t*)mutex) == 0;
}
PoolMutexNewFuncPtr pool_ext_mutex_new = pool_ext_mutex_new_impl;
PoolMutexLockFuncPtr pool_ext_mutex_lock = pool_ext_mutex_lock_impl;
PoolMutexUnlockFuncPtr pool_ext_mutex_unlock = pool_ext_mutex_unlock_impl;
PoolMutexDestroyFuncPtr pool_ext_mutex_destroy = pool_ext_mutex_destroy_impl;
#endif /* !defined(LIBPOOL_NO_STDLIB) */
#endif /* defined(LIBPOOL_THEAD_SAFE) */
/*
* External valgrind macros.
*/
#if defined(LIBPOOL_NO_VALGRIND)
#define VALGRIND_CREATE_MEMPOOL(A, B, C) ((void)0)
#define VALGRIND_DESTROY_MEMPOOL(A) ((void)0)
#define VALGRIND_MEMPOOL_ALLOC(A, B, C) ((void)0)
#define VALGRIND_MEMPOOL_FREE(A, B) ((void)0)
#define VALGRIND_MAKE_MEM_DEFINED(A, B) ((void)0)
#define VALGRIND_MAKE_MEM_NOACCESS(A, B) ((void)0)
#else /* !defined(LIBPOOL_NO_VALGRIND) */
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>
#endif /* !defined(LIBPOOL_NO_VALGRIND) */
/*
* Alignment macros.
*
* If alignment is enabled, the 'ALIGN2BOUNDARY' macro will align the specified
* size to the specified boundary. For more information, see:
* https://8dcc.github.io/reversing/challenge10.html#c-translation
*/
#if defined(LIBPOOL_NO_ALIGNMENT)
#define ALIGN2BOUNDARY(ADDR, BOUND) (ADDR)
#else /* !defined(LIBPOOL_NO_ALIGNMENT) */
#define ALIGN2BOUNDARY(SIZE, BOUNDARY) \
(((SIZE) + (BOUNDARY)-1) & ~((BOUNDARY)-1))
#endif /* !defined(LIBPOOL_NO_ALIGNMENT) */
#if !defined(LIBPOOL_LOG)
#define LIBPOOL_LOG(STR) ((void)0)
#endif /* !defined(LIBPOOL_LOG) */
/*----------------------------------------------------------------------------*/
/*
* Linked list of pointers, used to store the start of the chunk arrays inside a
* pool.
*
* We need to store them as a linked list, since there can be an arbitrary
* number of them, one for each call to `pool_expand' plus the initial one from
* `pool_new'. New pointers will be prepended to the linked list.
*/
typedef struct ArrayStart ArrayStart;
struct ArrayStart {
ArrayStart* next;
void* arr;
};
/*
* The actual pool structure, which contains a pointer to the first chunk, and
* a pointer to the start of the linked list of free chunks.
*
* We need to store the first chunk for freeing the actual `Chunk' array once
* the user is done with the pool.
*
* The user is able to allocate with O(1) time, because the `Pool.free_chunk'
* pointer always points to a free chunk without needing to iterate anything.
*/
struct Pool {
void* free_chunk;
ArrayStart* array_starts;
size_t chunk_sz;
#if defined(LIBPOOL_THREAD_SAFE)
void* lock;
#endif /* defined(LIBPOOL_THREAD_SAFE) */
};
/*----------------------------------------------------------------------------*/
/*
* Ensure that all external function pointers are defined with valid addresses.
*/
static bool pool_assert_ext_funcs(void) {
if (pool_ext_alloc == NULL || pool_ext_free == NULL)
return false;
#if defined(LIBPOOL_THREAD_SAFE)
if (pool_ext_mutex_new == NULL || pool_ext_mutex_lock == NULL ||
pool_ext_mutex_unlock == NULL || pool_ext_mutex_destroy == NULL)
return false;
#endif /* defined(LIBPOOL_THREAD_SAFE) */
return true;
}
/*
* We use an exteran allocation function (by default `malloc', but can be
* overwritten by user) to allocate a `Pool' structure, and the array of
* chunks. You can think of a chunk as the following union:
*
* union Chunk {
* union Chunk* next_free;
* char user_data[CHUNK_SZ];
* };
*
* In this hypothetical union, the data in a non-free chunk will be overwritten
* by the user, in the `user_data' array, where `CHUNK_SZ' was specified by the
* caller of `pool_new'. However, if the chunk is free, the union uses the
* `Chunk.next_free' pointer to build a linked list of available chunks, shown
* below.
*
* This is explained in more detail (and with diagrams) in my blog article:
* https://8dcc.github.io/programming/pool-allocator.html
*/
Pool* pool_new(size_t pool_sz, size_t chunk_sz) {
Pool* pool;
char* arr;
size_t i;
if (pool_sz == 0) {
LIBPOOL_LOG("Invalid pool size.");
return NULL;
}
#if defined(LIBPOOL_NO_ALIGNMENT)
if (chunk_sz < sizeof(void*)) {
LIBPOOL_LOG("No alignment, and small pool size.");
return NULL;
}
#else /* !defined(LIBPOOL_NO_ALIGNMENT) */
chunk_sz = ALIGN2BOUNDARY(chunk_sz, sizeof(void*));
#endif /* !defined(LIBPOOL_NO_ALIGNMENT) */
if (!pool_assert_ext_funcs()) {
LIBPOOL_LOG("A pointer to an external function is not initialized.");
return NULL;
}
pool = pool_ext_alloc(sizeof(Pool));
if (pool == NULL) {
LIBPOOL_LOG("Failed to allocate 'Pool' structure.");
return NULL;
}
pool->array_starts = pool_ext_alloc(sizeof(ArrayStart));
if (pool->array_starts == NULL) {
LIBPOOL_LOG("Failed to allocate 'ArrayStart' structure.");
pool_ext_free(pool);
return NULL;
}
arr = pool_ext_alloc(pool_sz * chunk_sz);
if (arr == NULL) {
LIBPOOL_LOG("Failed to allocate actual data array.");
pool_ext_free(pool->array_starts);
pool_ext_free(pool);
return NULL;
}
#if defined(LIBPOOL_THREAD_SAFE)
pool->lock = pool_ext_mutex_new();
if (pool->lock == NULL) {
LIBPOOL_LOG("Failed to create and initialize mutex.");
pool_ext_free(arr);
pool_ext_free(pool->array_starts);
pool_ext_free(pool);
return NULL;
}
#endif /* defined(LIBPOOL_THREAD_SAFE) */
/*
* Build the linked list. Use the first N bytes of the free chunks for
* storing the (hypothetical) `.next' pointer. This is why `chunk_sz' must
* be greater or equal than `sizeof(void*)'.
*/
for (i = 0; i < pool_sz - 1; i++)
*(void**)(arr + i * chunk_sz) = arr + (i + 1) * chunk_sz;
*(void**)(arr + (pool_sz - 1) * chunk_sz) = NULL;
pool->free_chunk = arr;
pool->array_starts->next = NULL;
pool->array_starts->arr = arr;
pool->chunk_sz = chunk_sz;
VALGRIND_MAKE_MEM_NOACCESS(arr, pool_sz * chunk_sz);
VALGRIND_MAKE_MEM_NOACCESS(pool->array_starts, sizeof(ArrayStart));
VALGRIND_MAKE_MEM_NOACCESS(pool, sizeof(Pool));
VALGRIND_CREATE_MEMPOOL(pool, 0, 0);
return pool;
}
/*
* Expanding the pool simply means allocating a new chunk array, and prepending
* it to the `pool->free_chunk' linked list.
*
* 1. Allocate a new `ArrayStart' structure.
* 2. Allocate a new chunk array with the specified size.
* 3. Link the new array together, just like in `pool_new'.
* 4. Prepend the new chunk array to the existing linked list of free chunks.
* 5. Prepend the new `ArrayStart' to the existing linked list of array starts.
*/
bool pool_expand(Pool* pool, size_t extra_sz) {
bool result = true;
ArrayStart* array_start;
char* extra_arr;
size_t i;
if (pool == NULL || extra_sz <= 0) {
LIBPOOL_LOG("Invalid pool pointer or extra size.");
return false;
}
#if defined(LIBPOOL_THREAD_SAFE)
if (!pool_ext_mutex_lock(pool->lock)) {
LIBPOOL_LOG("Failed to lock mutex.");
return false;
}
#endif /* defined(LIBPOOL_THREAD_SAFE) */
VALGRIND_MAKE_MEM_DEFINED(pool, sizeof(Pool));
array_start = pool_ext_alloc(sizeof(ArrayStart));
if (array_start == NULL) {
LIBPOOL_LOG("Failed to allocate additional 'ArrayStart' structure.");
result = false;
goto alloc_err;
}
extra_arr = pool_ext_alloc(extra_sz * pool->chunk_sz);
if (extra_arr == NULL) {
LIBPOOL_LOG("Failed to allocate additional data array.");
pool_ext_free(array_start);
result = false;
goto alloc_err;
}
for (i = 0; i < extra_sz - 1; i++)
*(void**)(extra_arr + i * pool->chunk_sz) =
extra_arr + (i + 1) * pool->chunk_sz;
*(void**)(extra_arr + (extra_sz - 1) * pool->chunk_sz) = pool->free_chunk;
pool->free_chunk = extra_arr;
array_start->arr = extra_arr;
array_start->next = pool->array_starts;
pool->array_starts = array_start;
VALGRIND_MAKE_MEM_NOACCESS(extra_arr, extra_sz * pool->chunk_sz);
VALGRIND_MAKE_MEM_NOACCESS(array_start, sizeof(ArrayStart));
alloc_err:
VALGRIND_MAKE_MEM_NOACCESS(pool, sizeof(Pool));
#if defined(LIBPOOL_THREAD_SAFE)
pool_ext_mutex_unlock(pool->lock);
#endif /* defined(LIBPOOL_THREAD_SAFE) */
return result;
}
/*
* When closing the pool, we traverse the list of `ArrayStart' structures, which
* contain the base address of each chunk array. We free the array, and then the
* `ArrayStart' structure itself. Lastly, we free the `Pool' structure.
*/
void pool_destroy(Pool* pool) {
ArrayStart* array_start;
ArrayStart* next;
if (pool == NULL) {
LIBPOOL_LOG("Invalid pool pointer.");
return;
}
#if defined(LIBPOOL_THREAD_SAFE)
if (!pool_ext_mutex_lock(pool->lock)) {
LIBPOOL_LOG("Failed to lock mutex.");
return;
}
#endif /* defined(LIBPOOL_THREAD_SAFE) */
VALGRIND_MAKE_MEM_DEFINED(pool, sizeof(Pool));
array_start = pool->array_starts;
while (array_start != NULL) {
VALGRIND_MAKE_MEM_DEFINED(array_start, sizeof(ArrayStart));
next = array_start->next;
pool_ext_free(array_start->arr);
pool_ext_free(array_start);
array_start = next;
}
#if defined(LIBPOOL_THREAD_SAFE)
pool_ext_mutex_unlock(pool->lock);
pool_ext_mutex_destroy(pool->lock);
#endif /* defined(LIBPOOL_THREAD_SAFE) */
VALGRIND_DESTROY_MEMPOOL(pool);
pool_ext_free(pool);
}
/*----------------------------------------------------------------------------*/
/*
* The allocation process is very simple and fast. Since the `pool' has a
* pointer to the start of a linked list of free (hypothetical) `Chunk'
* structures, we can just return that pointer, and set the new start of the
* linked list to the second item of the old list.
*/
void* pool_alloc(Pool* pool) {
void* result = NULL;
if (pool == NULL) {
LIBPOOL_LOG("Invalid pool pointer.");
return NULL;
}
#if defined(LIBPOOL_THREAD_SAFE)
if (!pool_ext_mutex_lock(pool->lock)) {
LIBPOOL_LOG("Failed to lock mutex.");
return NULL;
}
#endif /* defined(LIBPOOL_THREAD_SAFE) */
VALGRIND_MAKE_MEM_DEFINED(pool, sizeof(Pool));
if (pool->free_chunk == NULL) {
LIBPOOL_LOG("No free chunks in pool.");
goto done;
}
VALGRIND_MAKE_MEM_DEFINED(pool->free_chunk, sizeof(void**));
result = pool->free_chunk;
pool->free_chunk = *(void**)pool->free_chunk;
VALGRIND_MEMPOOL_ALLOC(pool, result, pool->chunk_sz);
VALGRIND_MAKE_MEM_NOACCESS(pool->free_chunk, sizeof(void**));
VALGRIND_MAKE_MEM_NOACCESS(pool, sizeof(Pool));
done:
#if defined(LIBPOOL_THREAD_SAFE)
pool_ext_mutex_unlock(pool->lock);
#endif /* defined(LIBPOOL_THREAD_SAFE) */
return result;
}
/*
* Note that, since we are using a linked list, the caller doesn't need to free
* in the same order that used when allocating.
*/
void pool_free(Pool* pool, void* ptr) {
if (pool == NULL || ptr == NULL) {
LIBPOOL_LOG("Invalid pool or data pointer.");
return;
}
#if defined(LIBPOOL_THREAD_SAFE)
if (!pool_ext_mutex_lock(pool->lock)) {
LIBPOOL_LOG("Failed to lock mutex.");
return;
}
#endif /* defined(LIBPOOL_THREAD_SAFE) */
VALGRIND_MAKE_MEM_DEFINED(pool, sizeof(Pool));
*(void**)ptr = pool->free_chunk;
pool->free_chunk = ptr;
VALGRIND_MAKE_MEM_NOACCESS(pool, sizeof(Pool));
VALGRIND_MEMPOOL_FREE(pool, ptr);
#if defined(LIBPOOL_THREAD_SAFE)
pool_ext_mutex_unlock(pool->lock);
#endif /* defined(LIBPOOL_THREAD_SAFE) */
}