Merge branch 'ab/branch-sort' into maint
[git/debian.git] / mem-pool.c
blob8401761dda0a2a585f6593e2212ff41db7135170
1 /*
2 * Memory Pool implementation logic.
3 */
5 #include "cache.h"
6 #include "mem-pool.h"
8 #define BLOCK_GROWTH_SIZE 1024*1024 - sizeof(struct mp_block);
11 * Allocate a new mp_block and insert it after the block specified in
12 * `insert_after`. If `insert_after` is NULL, then insert block at the
13 * head of the linked list.
15 static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
16 size_t block_alloc,
17 struct mp_block *insert_after)
19 struct mp_block *p;
21 pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
22 p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
24 p->next_free = (char *)p->space;
25 p->end = p->next_free + block_alloc;
27 if (insert_after) {
28 p->next_block = insert_after->next_block;
29 insert_after->next_block = p;
30 } else {
31 p->next_block = pool->mp_block;
32 pool->mp_block = p;
35 return p;
38 void mem_pool_init(struct mem_pool *pool, size_t initial_size)
40 memset(pool, 0, sizeof(*pool));
41 pool->block_alloc = BLOCK_GROWTH_SIZE;
43 if (initial_size > 0)
44 mem_pool_alloc_block(pool, initial_size, NULL);
47 void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
49 struct mp_block *block, *block_to_free;
51 block = pool->mp_block;
52 while (block)
54 block_to_free = block;
55 block = block->next_block;
57 if (invalidate_memory)
58 memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
60 free(block_to_free);
63 pool->mp_block = NULL;
64 pool->pool_alloc = 0;
67 void *mem_pool_alloc(struct mem_pool *pool, size_t len)
69 struct mp_block *p = NULL;
70 void *r;
72 /* round up to a 'uintmax_t' alignment */
73 if (len & (sizeof(uintmax_t) - 1))
74 len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
76 if (pool->mp_block &&
77 pool->mp_block->end - pool->mp_block->next_free >= len)
78 p = pool->mp_block;
80 if (!p) {
81 if (len >= (pool->block_alloc / 2))
82 return mem_pool_alloc_block(pool, len, pool->mp_block);
84 p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
87 r = p->next_free;
88 p->next_free += len;
89 return r;
92 void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
94 size_t len = st_mult(count, size);
95 void *r = mem_pool_alloc(pool, len);
96 memset(r, 0, len);
97 return r;
100 char *mem_pool_strdup(struct mem_pool *pool, const char *str)
102 size_t len = strlen(str) + 1;
103 char *ret = mem_pool_alloc(pool, len);
105 return memcpy(ret, str, len);
108 char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
110 char *p = memchr(str, '\0', len);
111 size_t actual_len = (p ? p - str : len);
112 char *ret = mem_pool_alloc(pool, actual_len+1);
114 ret[actual_len] = '\0';
115 return memcpy(ret, str, actual_len);
118 int mem_pool_contains(struct mem_pool *pool, void *mem)
120 struct mp_block *p;
122 /* Check if memory is allocated in a block */
123 for (p = pool->mp_block; p; p = p->next_block)
124 if ((mem >= ((void *)p->space)) &&
125 (mem < ((void *)p->end)))
126 return 1;
128 return 0;
131 void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
133 struct mp_block *p;
135 /* Append the blocks from src to dst */
136 if (dst->mp_block && src->mp_block) {
138 * src and dst have blocks, append
139 * blocks from src to dst.
141 p = dst->mp_block;
142 while (p->next_block)
143 p = p->next_block;
145 p->next_block = src->mp_block;
146 } else if (src->mp_block) {
148 * src has blocks, dst is empty.
150 dst->mp_block = src->mp_block;
151 } else {
152 /* src is empty, nothing to do. */
155 dst->pool_alloc += src->pool_alloc;
156 src->pool_alloc = 0;
157 src->mp_block = NULL;