Merge branch 'js/ident-date-fix'
[git/debian.git] / mem-pool.c
blob389d7af447036ef58e64d2041a8c6743a862817a
1 /*
2 * Memory Pool implementation logic.
3 */
5 #include "cache.h"
6 #include "mem-pool.h"
8 static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool, size_t block_alloc)
10 struct mp_block *p;
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;
19 return p;
22 void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
24 struct mp_block *p;
25 void *r;
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)
33 break;
35 if (!p) {
36 if (len >= (mem_pool->block_alloc / 2)) {
37 mem_pool->pool_alloc += len;
38 return xmalloc(len);
41 p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc);
44 r = p->next_free;
45 p->next_free += len;
46 return r;
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);
53 memset(r, 0, len);
54 return r;