2 * Memory Pool implementation logic.
8 static struct mp_block
*mem_pool_alloc_block(struct mem_pool
*mem_pool
, size_t block_alloc
)
12 mem_pool
->pool_alloc
+= sizeof(struct mp_block
) + block_alloc
;
13 p
= xmalloc(st_add(sizeof(struct mp_block
), block_alloc
));
14 p
->next_block
= mem_pool
->mp_block
;
15 p
->next_free
= (char *)p
->space
;
16 p
->end
= p
->next_free
+ block_alloc
;
17 mem_pool
->mp_block
= p
;
22 void *mem_pool_alloc(struct mem_pool
*mem_pool
, size_t len
)
27 /* round up to a 'uintmax_t' alignment */
28 if (len
& (sizeof(uintmax_t) - 1))
29 len
+= sizeof(uintmax_t) - (len
& (sizeof(uintmax_t) - 1));
31 for (p
= mem_pool
->mp_block
; p
; p
= p
->next_block
)
32 if (p
->end
- p
->next_free
>= len
)
36 if (len
>= (mem_pool
->block_alloc
/ 2)) {
37 mem_pool
->pool_alloc
+= len
;
41 p
= mem_pool_alloc_block(mem_pool
, mem_pool
->block_alloc
);
49 void *mem_pool_calloc(struct mem_pool
*mem_pool
, size_t count
, size_t size
)
51 size_t len
= st_mult(count
, size
);
52 void *r
= mem_pool_alloc(mem_pool
, len
);