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