]> git.ipfire.org Git - thirdparty/git.git/blob - mem-pool.c
Merge branch 'jc/checkout-detach-wo-tracking-report'
[thirdparty/git.git] / mem-pool.c
1 /*
2 * Memory Pool implementation logic.
3 */
4
5 #include "git-compat-util.h"
6 #include "mem-pool.h"
7
8 #define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
9
10 /*
11 * 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 */
21 struct 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 /*
31 * 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 */
35 static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
36 size_t block_alloc,
37 struct mp_block *insert_after)
38 {
39 struct mp_block *p;
40
41 pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
42 p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
43
44 p->next_free = (char *)p->space;
45 p->end = p->next_free + block_alloc;
46
47 if (insert_after) {
48 p->next_block = insert_after->next_block;
49 insert_after->next_block = p;
50 } else {
51 p->next_block = pool->mp_block;
52 pool->mp_block = p;
53 }
54
55 return p;
56 }
57
58 void mem_pool_init(struct mem_pool *pool, size_t initial_size)
59 {
60 memset(pool, 0, sizeof(*pool));
61 pool->block_alloc = BLOCK_GROWTH_SIZE;
62
63 if (initial_size > 0)
64 mem_pool_alloc_block(pool, initial_size, NULL);
65 }
66
67 void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
68 {
69 struct mp_block *block, *block_to_free;
70
71 block = pool->mp_block;
72 while (block)
73 {
74 block_to_free = block;
75 block = block->next_block;
76
77 if (invalidate_memory)
78 memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
79
80 free(block_to_free);
81 }
82
83 pool->mp_block = NULL;
84 pool->pool_alloc = 0;
85 }
86
87 void *mem_pool_alloc(struct mem_pool *pool, size_t len)
88 {
89 struct mp_block *p = NULL;
90 void *r;
91
92 len = DIV_ROUND_UP(len, GIT_MAX_ALIGNMENT) * GIT_MAX_ALIGNMENT;
93
94 if (pool->mp_block &&
95 pool->mp_block->end - pool->mp_block->next_free >= len)
96 p = pool->mp_block;
97
98 if (!p) {
99 if (len >= (pool->block_alloc / 2))
100 p = mem_pool_alloc_block(pool, len, pool->mp_block);
101 else
102 p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
103 }
104
105 r = p->next_free;
106 p->next_free += len;
107 return r;
108 }
109
110 static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
111 va_list ap)
112 {
113 struct mp_block *block = pool->mp_block;
114 char *next_free = block ? block->next_free : NULL;
115 size_t available = block ? block->end - block->next_free : 0;
116 va_list cp;
117 int len, len2;
118 size_t size;
119 char *ret;
120
121 va_copy(cp, ap);
122 len = vsnprintf(next_free, available, fmt, cp);
123 va_end(cp);
124 if (len < 0)
125 BUG("your vsnprintf is broken (returned %d)", len);
126
127 size = st_add(len, 1); /* 1 for NUL */
128 ret = mem_pool_alloc(pool, size);
129
130 /* Shortcut; relies on mem_pool_alloc() not touching buffer contents. */
131 if (ret == next_free)
132 return ret;
133
134 len2 = vsnprintf(ret, size, fmt, ap);
135 if (len2 != len)
136 BUG("your vsnprintf is broken (returns inconsistent lengths)");
137 return ret;
138 }
139
140 char *mem_pool_strfmt(struct mem_pool *pool, const char *fmt, ...)
141 {
142 va_list ap;
143 char *ret;
144
145 va_start(ap, fmt);
146 ret = mem_pool_strvfmt(pool, fmt, ap);
147 va_end(ap);
148 return ret;
149 }
150
151 void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
152 {
153 size_t len = st_mult(count, size);
154 void *r = mem_pool_alloc(pool, len);
155 memset(r, 0, len);
156 return r;
157 }
158
159 char *mem_pool_strdup(struct mem_pool *pool, const char *str)
160 {
161 size_t len = strlen(str) + 1;
162 char *ret = mem_pool_alloc(pool, len);
163
164 return memcpy(ret, str, len);
165 }
166
167 char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
168 {
169 char *p = memchr(str, '\0', len);
170 size_t actual_len = (p ? p - str : len);
171 char *ret = mem_pool_alloc(pool, actual_len+1);
172
173 ret[actual_len] = '\0';
174 return memcpy(ret, str, actual_len);
175 }
176
177 int mem_pool_contains(struct mem_pool *pool, void *mem)
178 {
179 struct mp_block *p;
180
181 /* Check if memory is allocated in a block */
182 for (p = pool->mp_block; p; p = p->next_block)
183 if ((mem >= ((void *)p->space)) &&
184 (mem < ((void *)p->end)))
185 return 1;
186
187 return 0;
188 }
189
190 void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
191 {
192 struct mp_block *p;
193
194 /* Append the blocks from src to dst */
195 if (dst->mp_block && src->mp_block) {
196 /*
197 * src and dst have blocks, append
198 * blocks from src to dst.
199 */
200 p = dst->mp_block;
201 while (p->next_block)
202 p = p->next_block;
203
204 p->next_block = src->mp_block;
205 } else if (src->mp_block) {
206 /*
207 * src has blocks, dst is empty.
208 */
209 dst->mp_block = src->mp_block;
210 } else {
211 /* src is empty, nothing to do. */
212 }
213
214 dst->pool_alloc += src->pool_alloc;
215 src->pool_alloc = 0;
216 src->mp_block = NULL;
217 }