-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.c
563 lines (471 loc) · 15.8 KB
/
pool.c
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
The algorithms of this code have been adapted from the Apache runtime library.
The standard memory allocation strategy of C is to keep track of each and
every piece of RAM. This is an incredibly error prone activity, as proved
by the countless memory allocation bugs that appear in code built using
that strategy.
Another strategy, and the one that I have been proposing since some years,
is to use the garbage collector and get rid of most problems.
There is an intermediate strategy however, based on pools of memory.
Normally you have a set of related allocations, that can be done from a
memory pool, that is released in a single call when the module that uses
the pool is finished or when the activity associated with the pool is
finished.
A pool, for instance, is handy for a hash table with variable length keys
and variable lengths objects. When the hash table is destroyed, all the
memory that it required can be freed with a single call to pool destroy.
Many other applications are possible. Normally, all big applications are
built with modules that are more or less independent and could benefit
from pooled allocation. In a database application all memory used by one
query can be pooled and released when the query is done.
An example of pool management is in the Apache Runtime (APR) library.
There, the module apr_pools.c proposes a sophisticated interface for
pooled memory management. That library can be downloaded at no cost,
and its license is quite liberal. I adapted some of the code for the
container library, reducing the size of the module to just 1K (1076
bytes when compiled with lcc-win) from around 40K of the original.
The reduced interface proposes
newPool (create a pool)
PoolAlloc (allocate)
PoolFinalize (release memory)
I think all other stuff is not essential. No need for parent pools,
pools sub-allocation, sibling management, process shared pools,
printf versions that use a pool, and many other features like
compatibility with older versions... No need for that.
Obviously you may have different requirements, in that case use the
APR library, a fine piece of software
*/
#ifndef _MSC_VER
#include <inttypes.h>
#endif
#ifndef TEST
#include "containers.h"
#else
#include <string.h>
#include <stdlib.h>
#endif
#define MAX_INDEX 20
#define ALIGN(size, boundary) (((size) + ((boundary) - 1)) & ~((boundary) - 1))
#define ALIGN_DEFAULT(size) ALIGN(size, 8)
#ifdef TEST
/** The fundamental pool type */
typedef struct Pool Pool;
#endif
/*
* Note The max_free_index and current_free_index fields are not really
* indices, but quantities of BOUNDARY_SIZE big memory blocks.
*/
struct MemoryNode_t {
struct MemoryNode_t *next; /**< next memnode */
struct MemoryNode_t **ref; /**< reference to self */
uint32_t index; /**< size */
uint32_t free_index; /**< how much free */
char *first_avail; /**< pointer to first free memory */
char *endp; /**< pointer to end of free memory */
};
typedef struct MemoryNode_t MemoryNode_t;
typedef struct {
/** largest used index into free[], always < MAX_INDEX */
uint32_t max_index;
/** Total size (in BOUNDARY_SIZE multiples) of unused memory before
* blocks are given back. @see SetMaxFree().
* @note Initialized to 0,
* which means to never give back blocks.
*/
uint32_t max_free_index;
/**
* Memory size (in BOUNDARY_SIZE multiples) that currently must be freed
* before blocks are given back. Range: 0..max_free_index
*/
uint32_t current_free_index;
Pool *owner;
/**
* Lists of free nodes. Slot 0 is used for oversized nodes,
* and the slots 1..MAX_INDEX-1 contain nodes of sizes
* (i+1) * BOUNDARY_SIZE. Example for BOUNDARY_INDEX == 12:
* slot 0: nodes larger than 81920
* slot 1: size 8192
* slot 2: size 12288
* ...
* slot 19: size 81920
*/
MemoryNode_t *free[MAX_INDEX];
} Allocator;
/** Create a new pool. */
static Pool *newPool(ContainerAllocator *m);
/**
* Clear all memory in the pool and run all the cleanups. This also destroys all
* subpools.
* @param p The pool to clear
* @remark This does not actually free the memory, it just allows the pool
* to re-use this memory for the next allocation.
* @see PoolFinalize()
*/
static void PoolClear(Pool *p);
/**
* Destroy the pool. This takes similar action as PoolClear() and then
* frees all the memory.
* @param p The pool to destroy
* @remark This will actually free the memory
*/
static void PoolFinalize(Pool *p);
/**
* Allocate a block of memor from a pool
* @param p The pool to allocate from
* @param size The amount of memory to allocate
* @return The allocated memory
*/
static void *PoolAlloc(Pool *p, size_t size);
/** The base size of a memory node - aligned. */
#define MEMORYNODE_SIZE ALIGN_DEFAULT(sizeof(MemoryNode_t))
/**
* Destroy an allocator
* @param allocator The allocator to be destroyed
* @remark Any memnodes not given back to the allocator prior to destroying
* will _not_ be free()d.
*/
static void destroyAllocator(Allocator *allocator,ContainerAllocator *m);
/**
* Free a list of blocks of mem, giving them back to the allocator.
* The list is typically terminated by a memnode with its next field
* set to NULL.
* @param allocator The allocator to give the mem back to
* @param memnode The memory node to return
*/
static void allocator_free(Allocator *allocator, MemoryNode_t *memnode,ContainerAllocator *m);
/*
* Magic numbers
*/
#define MIN_ALLOC 8192
#define BOUNDARY_INDEX 12
#define BOUNDARY_SIZE (1 << BOUNDARY_INDEX)
static MemoryNode_t *newAllocator(Allocator *allocator, size_t in_size,ContainerAllocator *m)
{
MemoryNode_t *node, **ref;
uint32_t max_index;
size_t size, i, idx;
/* Round up the block size to the next boundary, but always
* allocate at least a certain size (MIN_ALLOC).
*/
size = ALIGN(in_size + MEMORYNODE_SIZE, BOUNDARY_SIZE);
if (size < in_size) {
return NULL;
}
if (size < MIN_ALLOC)
size = MIN_ALLOC;
/* Find the index for this node size by
* dividing its size by the boundary size
*/
idx = (size >> BOUNDARY_INDEX) - 1;
if (idx > UINT32_MAX) {
return NULL;
}
/* First see if there are any nodes in the area we know
* our node will fit into.
*/
if (idx <= allocator->max_index) {
/* Walk the free list to see if there are
* any nodes on it of the requested size
*
* NOTE: an optimization would be to check
* allocator->free[index] first and if no
* node is present, directly use
* allocator->free[max_index]. This seems
* like overkill though and could cause
* memory waste.
*/
max_index = allocator->max_index;
ref = &allocator->free[idx];
i = idx;
while (*ref == NULL && i < max_index) {
ref++;
i++;
}
if ((node = *ref) != NULL) {
/* If we have found a node and it doesn't have any
* nodes waiting in line behind it _and_ we are on
* the highest available index, find the new highest
* available index
*/
if ((*ref = node->next) == NULL && i >= max_index) {
do {
ref--;
max_index--;
}
while (*ref == NULL && max_index > 0);
allocator->max_index = max_index;
}
allocator->current_free_index += node->index;
if (allocator->current_free_index > allocator->max_free_index)
allocator->current_free_index = allocator->max_free_index;
node->next = NULL;
node->first_avail = (char *)node + MEMORYNODE_SIZE;
return node;
}
}
/* If we found nothing, seek the sink (at index 0), if
* it is not empty.
*/
else if (allocator->free[0]) {
/* Walk the free list to see if there are
* any nodes on it of the requested size
*/
ref = &allocator->free[0];
while ((node = *ref) != NULL && idx > node->index)
ref = &node->next;
if (node) {
*ref = node->next;
allocator->current_free_index += node->index;
if (allocator->current_free_index > allocator->max_free_index)
allocator->current_free_index = allocator->max_free_index;
node->next = NULL;
node->first_avail = (char *)node + MEMORYNODE_SIZE;
return node;
}
}
/* If we haven't got a suitable node, malloc a new one
* and initialize it.
*/
if ((node = m->malloc(size)) == NULL)
return NULL;
node->next = NULL;
node->index = (uint32_t)idx;
node->first_avail = (char *)node + MEMORYNODE_SIZE;
node->endp = (char *)node + size;
return node;
}
static void allocator_free(Allocator *allocator, MemoryNode_t *node,ContainerAllocator *m)
{
MemoryNode_t *next, *freelist = NULL;
uint32_t idx, max_index;
uint32_t max_free_index, current_free_index;
max_index = allocator->max_index;
max_free_index = allocator->max_free_index;
current_free_index = allocator->current_free_index;
/* Walk the list of submitted nodes and free them one by one,
* shoving them in the right 'size' buckets as we go.
*/
do {
next = node->next;
idx = node->index;
if (max_free_index != 0
&& idx > current_free_index) {
node->next = freelist;
freelist = node;
}
else if (idx < MAX_INDEX) {
/* Add the node to the appropiate 'size' bucket. Adjust
* the max_index when appropiate.
*/
if ((node->next = allocator->free[idx]) == NULL
&& idx > max_index) {
max_index = idx;
}
allocator->free[idx] = node;
if (current_free_index >= idx)
current_free_index -= idx;
else
current_free_index = 0;
}
else {
/* This node is too large to keep in a specific size bucket,
* just add it to the sink (at index 0).
*/
node->next = allocator->free[0];
allocator->free[0] = node;
if (current_free_index >= idx)
current_free_index -= idx;
else
current_free_index = 0;
}
} while ((node = next) != NULL);
allocator->max_index = max_index;
allocator->current_free_index = current_free_index;
while (freelist != NULL) {
node = freelist;
freelist = node->next;
m->free(node);
}
}
/* The ref field in the Pool struct holds a
* pointer to the pointer referencing this pool.
*/
struct Pool {
Allocator *allocator;
const char *tag;
MemoryNode_t *active;
MemoryNode_t *self; /* The node containing the pool itself */
char *self_first_avail;
ContainerAllocator *MemManager;
};
#define SIZEOF_POOL_T ALIGN_DEFAULT(sizeof(Pool))
static void destroyAllocator(Allocator *allocator,ContainerAllocator *m)
{
uint32_t idx;
MemoryNode_t *node, **ref;
for (idx = 0; idx < MAX_INDEX; idx++) {
ref = &allocator->free[idx];
while ((node = *ref) != NULL) {
*ref = node->next;
m->free(node);
}
}
}
/* Node list management helper macros; list_insert() inserts 'node'
* before 'point'. */
#define list_insert(node, point) do { \
node->ref = point->ref; \
*node->ref = node; \
node->next = point; \
point->ref = &node->next; \
} while (0)
/* list_remove() removes 'node' from its list. */
#define list_remove(node) do { \
*node->ref = node->next; \
node->next->ref = node->ref; \
} while (0)
/* Returns the amount of free space in the given node. */
#define node_free_space(node_) ((size_t)(node_->endp - node_->first_avail))
/*
* Memory allocation
*/
static void * PoolAlloc(Pool *pool, size_t in_size)
{
MemoryNode_t *active, *node;
void *mem;
size_t size, free_index;
size = ALIGN_DEFAULT(in_size);
if (size < in_size) {
return NULL;
}
active = pool->active;
/* If the active node has enough bytes left, use it. */
if (size <= node_free_space(active)) {
mem = active->first_avail;
active->first_avail += size;
return mem;
}
node = active->next;
if (size <= node_free_space(node)) {
list_remove(node);
}
else {
if ((node = newAllocator(pool->allocator, size,pool->MemManager)) == NULL) {
return NULL;
}
}
node->free_index = 0;
mem = node->first_avail;
node->first_avail += size;
list_insert(node, active);
pool->active = node;
free_index = (ALIGN(active->endp - active->first_avail + 1,
BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
active->free_index = (uint32_t)free_index;
node = active->next;
if (free_index >= node->free_index)
return mem;
do {
node = node->next;
}
while (free_index < node->free_index);
list_remove(active);
list_insert(active, node);
return mem;
}
static void * PoolCalloc(Pool *pool,size_t n, size_t size)
{
void *mem;
size *= n;
if ((mem = PoolAlloc(pool, size)) != NULL) {
memset(mem, 0, size);
}
return mem;
}
#if 0
static void *PoolRealloc(Pool *pool,void *ptr, size_t size)
{
void *mem = PoolAlloc(pool,size);
if (mem == NULL)
return NULL;
memcpy(mem,ptr,size);
return mem;
}
#endif
/*
* Pool creation/destruction
*/
static void PoolClear(Pool *pool)
{
MemoryNode_t *active;
/* Find the node attached to the pool structure, reset it, make
* it the active node and free the rest of the nodes.
*/
active = pool->active = pool->self;
active->first_avail = pool->self_first_avail;
if (active->next == active)
return;
*active->ref = NULL;
allocator_free(pool->allocator, active->next,pool->MemManager);
active->next = active;
active->ref = &active->next;
}
static void PoolFinalize(Pool *pool)
{
MemoryNode_t *active;
Allocator *allocator;
/* Find the block attached to the pool structure. Save a copy of the
* allocator pointer, because the pool struct soon will be no more.
*/
allocator = pool->allocator;
active = pool->self;
*active->ref = NULL;
/* Free all the nodes in the pool (including the node holding the
* pool struct), by giving them back to the allocator.
*/
allocator_free(allocator, active,pool->MemManager);
destroyAllocator(allocator,pool->MemManager);
}
static Pool *newPool(ContainerAllocator *m)
{
Pool *pool;
MemoryNode_t *node;
Allocator *pool_allocator;
if (m == NULL)
m = CurrentAllocator;
if ((pool_allocator = m->calloc(1,sizeof(Allocator))) == NULL) {
return NULL;
}
if ((node = newAllocator(pool_allocator, MIN_ALLOC - MEMORYNODE_SIZE,m)) == NULL) {
return NULL;
}
node->next = node;
node->ref = &node->next;
pool = (Pool *)node->first_avail;
node->first_avail = pool->self_first_avail = (char *)pool + SIZEOF_POOL_T;
pool->allocator = pool_allocator;
pool->active = pool->self = node;
pool->tag = NULL;
pool_allocator->owner = pool;
pool->MemManager = m;
return pool;
}
PoolAllocatorInterface iPool = {
newPool,
PoolAlloc,
PoolCalloc,
PoolClear,
PoolFinalize,
};
#ifdef TEST
int main(void)
{
Pool *pool;
void *mem;
pool = newPool();
mem = PoolAlloc(pool,1024);
memset(mem,0,1024);
PoolDestroy(pool);
return 0;
}
#endif