git
/
mem-pool.c
218 строк · 5.1 Кб
1/*
2* Memory Pool implementation logic.
3*/
4
5#include "git-compat-util.h"
6#include "mem-pool.h"
7#include "gettext.h"
8
9#define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
10
11/*
12* The inner union is an approximation for C11's max_align_t, and the
13* struct + offsetof computes _Alignof. This can all just be replaced
14* with _Alignof(max_align_t) if/when C11 is part of the baseline.
15* Note that _Alignof(X) need not be the same as sizeof(X); it's only
16* required to be a (possibly trivial) factor. They are the same for
17* most architectures, but m68k for example has only 2-byte alignment
18* for its 4-byte and 8-byte types, so using sizeof would waste space.
19*
20* Add more types to the union if the current set is insufficient.
21*/
22struct git_max_alignment {
23char unalign;
24union {
25uintmax_t max_align_uintmax;
26void *max_align_pointer;
27} aligned;
28};
29#define GIT_MAX_ALIGNMENT offsetof(struct git_max_alignment, aligned)
30
31/*
32* Allocate a new mp_block and insert it after the block specified in
33* `insert_after`. If `insert_after` is NULL, then insert block at the
34* head of the linked list.
35*/
36static struct mp_block *mem_pool_alloc_block(struct mem_pool *pool,
37size_t block_alloc,
38struct mp_block *insert_after)
39{
40struct mp_block *p;
41
42pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
43p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));
44
45p->next_free = (char *)p->space;
46p->end = p->next_free + block_alloc;
47
48if (insert_after) {
49p->next_block = insert_after->next_block;
50insert_after->next_block = p;
51} else {
52p->next_block = pool->mp_block;
53pool->mp_block = p;
54}
55
56return p;
57}
58
59void mem_pool_init(struct mem_pool *pool, size_t initial_size)
60{
61memset(pool, 0, sizeof(*pool));
62pool->block_alloc = BLOCK_GROWTH_SIZE;
63
64if (initial_size > 0)
65mem_pool_alloc_block(pool, initial_size, NULL);
66}
67
68void mem_pool_discard(struct mem_pool *pool, int invalidate_memory)
69{
70struct mp_block *block, *block_to_free;
71
72block = pool->mp_block;
73while (block)
74{
75block_to_free = block;
76block = block->next_block;
77
78if (invalidate_memory)
79memset(block_to_free->space, 0xDD, ((char *)block_to_free->end) - ((char *)block_to_free->space));
80
81free(block_to_free);
82}
83
84pool->mp_block = NULL;
85pool->pool_alloc = 0;
86}
87
88void *mem_pool_alloc(struct mem_pool *pool, size_t len)
89{
90struct mp_block *p = NULL;
91void *r;
92
93len = DIV_ROUND_UP(len, GIT_MAX_ALIGNMENT) * GIT_MAX_ALIGNMENT;
94
95if (pool->mp_block &&
96pool->mp_block->end - pool->mp_block->next_free >= len)
97p = pool->mp_block;
98
99if (!p) {
100if (len >= (pool->block_alloc / 2))
101p = mem_pool_alloc_block(pool, len, pool->mp_block);
102else
103p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
104}
105
106r = p->next_free;
107p->next_free += len;
108return r;
109}
110
111static char *mem_pool_strvfmt(struct mem_pool *pool, const char *fmt,
112va_list ap)
113{
114struct mp_block *block = pool->mp_block;
115char *next_free = block ? block->next_free : NULL;
116size_t available = block ? block->end - block->next_free : 0;
117va_list cp;
118int len, len2;
119size_t size;
120char *ret;
121
122va_copy(cp, ap);
123len = vsnprintf(next_free, available, fmt, cp);
124va_end(cp);
125if (len < 0)
126die(_("unable to format message: %s"), fmt);
127
128size = st_add(len, 1); /* 1 for NUL */
129ret = mem_pool_alloc(pool, size);
130
131/* Shortcut; relies on mem_pool_alloc() not touching buffer contents. */
132if (ret == next_free)
133return ret;
134
135len2 = vsnprintf(ret, size, fmt, ap);
136if (len2 != len)
137BUG("your vsnprintf is broken (returns inconsistent lengths)");
138return ret;
139}
140
141char *mem_pool_strfmt(struct mem_pool *pool, const char *fmt, ...)
142{
143va_list ap;
144char *ret;
145
146va_start(ap, fmt);
147ret = mem_pool_strvfmt(pool, fmt, ap);
148va_end(ap);
149return ret;
150}
151
152void *mem_pool_calloc(struct mem_pool *pool, size_t count, size_t size)
153{
154size_t len = st_mult(count, size);
155void *r = mem_pool_alloc(pool, len);
156memset(r, 0, len);
157return r;
158}
159
160char *mem_pool_strdup(struct mem_pool *pool, const char *str)
161{
162size_t len = strlen(str) + 1;
163char *ret = mem_pool_alloc(pool, len);
164
165return memcpy(ret, str, len);
166}
167
168char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
169{
170char *p = memchr(str, '\0', len);
171size_t actual_len = (p ? p - str : len);
172char *ret = mem_pool_alloc(pool, actual_len+1);
173
174ret[actual_len] = '\0';
175return memcpy(ret, str, actual_len);
176}
177
178int mem_pool_contains(struct mem_pool *pool, void *mem)
179{
180struct mp_block *p;
181
182/* Check if memory is allocated in a block */
183for (p = pool->mp_block; p; p = p->next_block)
184if ((mem >= ((void *)p->space)) &&
185(mem < ((void *)p->end)))
186return 1;
187
188return 0;
189}
190
191void mem_pool_combine(struct mem_pool *dst, struct mem_pool *src)
192{
193struct mp_block *p;
194
195/* Append the blocks from src to dst */
196if (dst->mp_block && src->mp_block) {
197/*
198* src and dst have blocks, append
199* blocks from src to dst.
200*/
201p = dst->mp_block;
202while (p->next_block)
203p = p->next_block;
204
205p->next_block = src->mp_block;
206} else if (src->mp_block) {
207/*
208* src has blocks, dst is empty.
209*/
210dst->mp_block = src->mp_block;
211} else {
212/* src is empty, nothing to do. */
213}
214
215dst->pool_alloc += src->pool_alloc;
216src->pool_alloc = 0;
217src->mp_block = NULL;
218}
219