2 * Memory Pool implementation logic.
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 *mem_pool, size_t block_alloc, struct mp_block *insert_after)
19 mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
20 p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
22 p->next_free = (char *)p->space;
23 p->end = p->next_free + block_alloc;
26 p->next_block = insert_after->next_block;
27 insert_after->next_block = p;
29 p->next_block = mem_pool->mp_block;
30 mem_pool->mp_block = p;
36 void mem_pool_init(struct mem_pool *pool, size_t initial_size)
38 memset(pool, 0, sizeof(*pool));
39 pool->block_alloc = BLOCK_GROWTH_SIZE;
42 mem_pool_alloc_block(pool, initial_size, NULL);
45 void mem_pool_discard(struct mem_pool *mem_pool, int invalidate_memory)
47 struct mp_block *block, *block_to_free;
49 block = mem_pool->mp_block;
52 block_to_free = block;
53 block = block->next_block;
55 if (invalidate_memory)
56 memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
61 mem_pool->mp_block = NULL;
62 mem_pool->pool_alloc = 0;
65 void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
67 struct mp_block *p = NULL;
70 /* round up to a 'uintmax_t' alignment */
71 if (len & (sizeof(uintmax_t) - 1))
72 len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));
74 if (mem_pool->mp_block &&
75 mem_pool->mp_block->end - mem_pool->mp_block->next_free >= len)
76 p = mem_pool->mp_block;
79 if (len >= (mem_pool->block_alloc / 2))
80 return mem_pool_alloc_block(mem_pool, len, mem_pool->mp_block);
82 p = mem_pool_alloc_block(mem_pool, mem_pool->block_alloc, NULL);
90 void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
92 size_t len = st_mult(count, size);
93 void *r = mem_pool_alloc(mem_pool, len);
98 char *mem_pool_strdup(struct mem_pool *pool, const char *str)
100 size_t len = strlen(str) + 1;
101 char *ret = mem_pool_alloc(pool, len);
103 return memcpy(ret, str, len);
106 char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
108 char *p = memchr(str, '\0', len);
109 size_t actual_len = (p ? p - str : len);
110 char *ret = mem_pool_alloc(pool, actual_len+1);
112 ret[actual_len] = '\0';
113 return memcpy(ret, str, actual_len);
116 int mem_pool_contains(struct mem_pool *mem_pool, void *mem)
120 /* Check if memory is allocated in a block */
121 for (p = mem_pool->mp_block; p; p = p->next_block)
122 if ((mem >= ((void *)p->space)) &&
123 (mem < ((void *)p->end)))
129 void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
133 /* Append the blocks from src to dst */
134 if (dst->mp_block && src->mp_block) {
136 * src and dst have blocks, append
137 * blocks from src to dst.
140 while (p->next_block)
143 p->next_block = src->mp_block;
144 } else if (src->mp_block) {
146 * src has blocks, dst is empty.
148 dst->mp_block = src->mp_block;
150 /* src is empty, nothing to do. */
153 dst->pool_alloc += src->pool_alloc;
155 src->mp_block = NULL;