blob: c7d62560201984db8efcb675da9b4ede933c5cbd [file] [log] [blame]
Jameson Miller065feab2018-04-11 18:37:55 +00001/*
2 * Memory Pool implementation logic.
3 */
4
Elijah Newren15db4e72023-02-24 00:09:23 +00005#include "git-compat-util.h"
Jameson Miller065feab2018-04-11 18:37:55 +00006#include "mem-pool.h"
7
René Scharfe116affa2021-03-13 17:17:37 +01008#define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
Jameson Miller158dfef2018-07-02 19:49:34 +00009
10/*
Jessica Clarkee38bcc62022-01-23 20:33:47 +000011 * The inner union is an approximation for C11's max_align_t, and the
12 * struct + offsetof computes _Alignof. This can all just be replaced
13 * with _Alignof(max_align_t) if/when C11 is part of the baseline.
14 * Note that _Alignof(X) need not be the same as sizeof(X); it's only
15 * required to be a (possibly trivial) factor. They are the same for
16 * most architectures, but m68k for example has only 2-byte alignment
17 * for its 4-byte and 8-byte types, so using sizeof would waste space.
18 *
19 * Add more types to the union if the current set is insufficient.
20 */
21struct git_max_alignment {
22 char unalign;
23 union {
24 uintmax_t max_align_uintmax;
25 void *max_align_pointer;
26 } aligned;
27};
28#define GIT_MAX_ALIGNMENT offsetof(struct git_max_alignment, aligned)
29
30/*
Jameson Miller158dfef2018-07-02 19:49:34 +000031 * Allocate a new mp_block and insert it after the block specified in
32 * `insert_after`. If `insert_after` is NULL, then insert block at the
33 * head of the linked list.
34 */
Elijah Newrenf87bf282020-08-15 17:37:57 +000035static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
36 size_t block_alloc,
37 struct mp_block *insert_after)
Jameson Miller065feab2018-04-11 18:37:55 +000038{
39 struct mp_block *p;
40
Elijah Newrenf87bf282020-08-15 17:37:57 +000041 pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
Jameson Miller065feab2018-04-11 18:37:55 +000042 p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
Jameson Miller158dfef2018-07-02 19:49:34 +000043
Jameson Miller065feab2018-04-11 18:37:55 +000044 p->next_free = (char *)p->space;
45 p->end = p->next_free + block_alloc;
Jameson Miller158dfef2018-07-02 19:49:34 +000046
47 if (insert_after) {
48 p->next_block = insert_after->next_block;
49 insert_after->next_block = p;
50 } else {
Elijah Newrenf87bf282020-08-15 17:37:57 +000051 p->next_block = pool->mp_block;
52 pool->mp_block = p;
Jameson Miller158dfef2018-07-02 19:49:34 +000053 }
Jameson Miller065feab2018-04-11 18:37:55 +000054
55 return p;
56}
57
Elijah Newren44c7e1a2020-08-15 17:37:56 +000058void mem_pool_init(struct mem_pool *pool, size_t initial_size)
Jameson Miller158dfef2018-07-02 19:49:34 +000059{
Elijah Newren44c7e1a2020-08-15 17:37:56 +000060 memset(pool, 0, sizeof(*pool));
Jameson Miller158dfef2018-07-02 19:49:34 +000061 pool->block_alloc = BLOCK_GROWTH_SIZE;
62
63 if (initial_size > 0)
64 mem_pool_alloc_block(pool, initial_size, NULL);
Jameson Miller158dfef2018-07-02 19:49:34 +000065}
66
Elijah Newrenf87bf282020-08-15 17:37:57 +000067void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
Jameson Miller158dfef2018-07-02 19:49:34 +000068{
69 struct mp_block *block, *block_to_free;
70
Elijah Newrenf87bf282020-08-15 17:37:57 +000071 block = pool->mp_block;
Jameson Miller8e72d672018-07-02 19:49:37 +000072 while (block)
Jameson Miller158dfef2018-07-02 19:49:34 +000073 {
74 block_to_free = block;
75 block = block->next_block;
Jameson Miller8616a2d2018-07-02 19:49:39 +000076
77 if (invalidate_memory)
78 memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
79
Jameson Miller158dfef2018-07-02 19:49:34 +000080 free(block_to_free);
81 }
82
Elijah Newrenf87bf282020-08-15 17:37:57 +000083 pool->mp_block = NULL;
84 pool->pool_alloc = 0;
Jameson Miller158dfef2018-07-02 19:49:34 +000085}
86
Elijah Newrenf87bf282020-08-15 17:37:57 +000087void *mem_pool_alloc(struct mem_pool *pool, size_t len)
Jameson Miller065feab2018-04-11 18:37:55 +000088{
Jameson Miller8fb8e3f2018-07-02 19:49:33 +000089 struct mp_block *p = NULL;
Jameson Miller065feab2018-04-11 18:37:55 +000090 void *r;
91
René Scharfec61740d2023-12-24 18:02:04 +010092 len = DIV_ROUND_UP(len, GIT_MAX_ALIGNMENT) * GIT_MAX_ALIGNMENT;
Jameson Miller065feab2018-04-11 18:37:55 +000093
Elijah Newrenf87bf282020-08-15 17:37:57 +000094 if (pool->mp_block &&
95 pool->mp_block->end - pool->mp_block->next_free >= len)
96 p = pool->mp_block;
Jameson Miller065feab2018-04-11 18:37:55 +000097
98 if (!p) {
Elijah Newrenf87bf282020-08-15 17:37:57 +000099 if (len >= (pool->block_alloc / 2))
René Scharfe6cbae642023-12-28 20:19:06 +0100100 p = mem_pool_alloc_block(pool, len, pool->mp_block);
101 else
102 p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
Jameson Miller065feab2018-04-11 18:37:55 +0000103 }
104
105 r = p->next_free;
106 p->next_free += len;
107 return r;
108}
109
Elijah Newrenf87bf282020-08-15 17:37:57 +0000110void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
Jameson Miller065feab2018-04-11 18:37:55 +0000111{
112 size_t len = st_mult(count, size);
Elijah Newrenf87bf282020-08-15 17:37:57 +0000113 void *r = mem_pool_alloc(pool, len);
Jameson Miller065feab2018-04-11 18:37:55 +0000114 memset(r, 0, len);
115 return r;
116}
Jameson Miller0e583012018-07-02 19:49:35 +0000117
Elijah Newrena762c8c2020-08-15 17:37:55 +0000118char *mem_pool_strdup(struct mem_pool *pool, const char *str)
119{
120 size_t len = strlen(str) + 1;
121 char *ret = mem_pool_alloc(pool, len);
122
123 return memcpy(ret, str, len);
124}
125
126char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
127{
128 char *p = memchr(str, '\0', len);
129 size_t actual_len = (p ? p - str : len);
130 char *ret = mem_pool_alloc(pool, actual_len+1);
131
132 ret[actual_len] = '\0';
133 return memcpy(ret, str, actual_len);
134}
135
Elijah Newrenf87bf282020-08-15 17:37:57 +0000136int mem_pool_contains(struct mem_pool *pool, void *mem)
Jameson Miller0e583012018-07-02 19:49:35 +0000137{
138 struct mp_block *p;
139
140 /* Check if memory is allocated in a block */
Elijah Newrenf87bf282020-08-15 17:37:57 +0000141 for (p = pool->mp_block; p; p = p->next_block)
Jameson Miller0e583012018-07-02 19:49:35 +0000142 if ((mem >= ((void *)p->space)) &&
143 (mem < ((void *)p->end)))
144 return 1;
145
146 return 0;
147}
148
149void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
150{
151 struct mp_block *p;
152
153 /* Append the blocks from src to dst */
154 if (dst->mp_block && src->mp_block) {
155 /*
156 * src and dst have blocks, append
157 * blocks from src to dst.
158 */
159 p = dst->mp_block;
160 while (p->next_block)
161 p = p->next_block;
162
163 p->next_block = src->mp_block;
164 } else if (src->mp_block) {
165 /*
166 * src has blocks, dst is empty.
167 */
168 dst->mp_block = src->mp_block;
169 } else {
170 /* src is empty, nothing to do. */
171 }
172
173 dst->pool_alloc += src->pool_alloc;
174 src->pool_alloc = 0;
175 src->mp_block = NULL;
176}