2 * UCW Library -- Memory Pools (One-Time Allocation)
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
5 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include "lib/mempool.h"
18 #define MP_CHUNK_TAIL ALIGN_TO(sizeof(struct mempool_chunk), CPU_STRUCT_ALIGN)
19 #define MP_SIZE_MAX (~0U - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
21 struct mempool_chunk {
22 struct mempool_chunk *next;
27 mp_align_size(uns size)
30 return ALIGN_TO(size + MP_CHUNK_TAIL, CPU_PAGE_SIZE) - MP_CHUNK_TAIL;
32 return ALIGN_TO(size, CPU_STRUCT_ALIGN);
37 mp_init(struct mempool *pool, uns chunk_size)
39 chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
40 *pool = (struct mempool) {
41 .chunk_size = chunk_size,
42 .threshold = chunk_size >> 1,
43 .last_big = &pool->last_big };
47 mp_new_big_chunk(uns size)
49 struct mempool_chunk *chunk;
50 chunk = xmalloc(size + MP_CHUNK_TAIL) + size;
56 mp_free_big_chunk(struct mempool_chunk *chunk)
58 xfree((void *)chunk - chunk->size);
62 mp_new_chunk(uns size)
65 struct mempool_chunk *chunk;
66 chunk = page_alloc(size + MP_CHUNK_TAIL) + size;
70 return mp_new_big_chunk(size);
75 mp_free_chunk(struct mempool_chunk *chunk)
78 page_free((void *)chunk - chunk->size, chunk->size + MP_CHUNK_TAIL);
80 mp_free_big_chunk(chunk);
85 mp_new(uns chunk_size)
87 chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
88 struct mempool_chunk *chunk = mp_new_chunk(chunk_size);
89 struct mempool *pool = (void *)chunk - chunk_size;
90 DBG("Creating mempool %p with %u bytes long chunks", pool, chunk_size);
92 *pool = (struct mempool) {
93 .state = { .free = { chunk_size - sizeof(*pool) }, .last = { chunk } },
94 .chunk_size = chunk_size,
95 .threshold = chunk_size >> 1,
96 .last_big = &pool->last_big };
101 mp_free_chain(struct mempool_chunk *chunk)
105 struct mempool_chunk *next = chunk->next;
106 mp_free_chunk(chunk);
112 mp_free_big_chain(struct mempool_chunk *chunk)
116 struct mempool_chunk *next = chunk->next;
117 mp_free_big_chunk(chunk);
123 mp_delete(struct mempool *pool)
125 DBG("Deleting mempool %p", pool);
126 mp_free_big_chain(pool->state.last[1]);
127 mp_free_chain(pool->unused);
128 mp_free_chain(pool->state.last[0]); // can contain the mempool structure
132 mp_flush(struct mempool *pool)
134 mp_free_big_chain(pool->state.last[1]);
135 struct mempool_chunk *chunk, *next;
136 for (chunk = pool->state.last[0]; chunk && (void *)chunk - chunk->size != pool; chunk = next)
139 chunk->next = pool->unused;
140 pool->unused = chunk;
142 pool->state.last[0] = chunk;
143 pool->state.free[0] = chunk ? chunk->size - sizeof(*pool) : 0;
144 pool->state.last[1] = NULL;
145 pool->state.free[1] = 0;
146 pool->state.next = NULL;
147 pool->last_big = &pool->last_big;
151 mp_stats_chain(struct mempool_chunk *chunk, struct mempool_stats *stats, uns idx)
155 stats->chain_size[idx] += chunk->size + sizeof(*chunk);
156 stats->chain_count[idx]++;
159 stats->total_size += stats->chain_size[idx];
163 mp_stats(struct mempool *pool, struct mempool_stats *stats)
165 bzero(stats, sizeof(*stats));
166 mp_stats_chain(pool->state.last[0], stats, 0);
167 mp_stats_chain(pool->state.last[1], stats, 1);
168 mp_stats_chain(pool->unused, stats, 2);
172 mp_alloc_internal(struct mempool *pool, uns size)
174 struct mempool_chunk *chunk;
175 if (size <= pool->threshold)
180 chunk = pool->unused;
181 pool->unused = chunk->next;
184 chunk = mp_new_chunk(pool->chunk_size);
185 chunk->next = pool->state.last[0];
186 pool->state.last[0] = chunk;
187 pool->state.free[0] = pool->chunk_size - size;
188 return (void *)chunk - pool->chunk_size;
190 else if (likely(size <= MP_SIZE_MAX))
193 uns aligned = ALIGN_TO(size, CPU_STRUCT_ALIGN);
194 chunk = mp_new_big_chunk(aligned);
195 chunk->next = pool->state.last[1];
196 pool->state.last[1] = chunk;
197 pool->state.free[1] = aligned - size;
198 return pool->last_big = (void *)chunk - aligned;
201 die("Cannot allocate %u bytes from a mempool", size);
205 mp_alloc(struct mempool *pool, uns size)
207 return mp_alloc_fast(pool, size);
211 mp_alloc_noalign(struct mempool *pool, uns size)
213 return mp_alloc_fast_noalign(pool, size);
217 mp_alloc_zero(struct mempool *pool, uns size)
219 void *ptr = mp_alloc_fast(pool, size);
225 mp_start_internal(struct mempool *pool, uns size)
227 void *ptr = mp_alloc_internal(pool, size);
228 pool->state.free[pool->idx] += size;
233 mp_start(struct mempool *pool, uns size)
235 return mp_start_fast(pool, size);
239 mp_start_noalign(struct mempool *pool, uns size)
241 return mp_start_fast_noalign(pool, size);
245 mp_grow_internal(struct mempool *pool, uns size)
247 if (unlikely(size > MP_SIZE_MAX))
248 die("Cannot allocate %u bytes of memory", size);
249 uns avail = mp_avail(pool);
250 void *ptr = mp_ptr(pool);
253 uns amortized = likely(avail <= MP_SIZE_MAX / 2) ? avail * 2 : MP_SIZE_MAX;
254 amortized = MAX(amortized, size);
255 amortized = ALIGN_TO(amortized, CPU_STRUCT_ALIGN);
256 struct mempool_chunk *chunk = pool->state.last[1], *next = chunk->next;
257 ptr = xrealloc(ptr, amortized + MP_CHUNK_TAIL);
258 chunk = ptr + amortized;
260 chunk->size = amortized;
261 pool->state.last[1] = chunk;
262 pool->state.free[1] = amortized;
263 pool->last_big = ptr;
268 void *p = mp_start_internal(pool, size);
269 memcpy(p, ptr, avail);
275 mp_open(struct mempool *pool, void *ptr)
277 return mp_open_fast(pool, ptr);
281 mp_realloc(struct mempool *pool, void *ptr, uns size)
283 return mp_realloc_fast(pool, ptr, size);
287 mp_realloc_zero(struct mempool *pool, void *ptr, uns size)
289 uns old_size = mp_open_fast(pool, ptr);
290 ptr = mp_grow(pool, size);
292 bzero(ptr + old_size, size - old_size);
293 mp_end(pool, ptr + size);
298 mp_restore(struct mempool *pool, struct mempool_state *state)
300 struct mempool_chunk *chunk, *next;
301 struct mempool_state s = *state;
302 for (chunk = pool->state.last[0]; chunk != s.last[0]; chunk = next)
305 chunk->next = pool->unused;
306 pool->unused = chunk;
308 for (chunk = pool->state.last[1]; chunk != s.last[1]; chunk = next)
311 mp_free_big_chunk(chunk);
314 pool->last_big = &pool->last_big;
317 struct mempool_state *
318 mp_push(struct mempool *pool)
320 struct mempool_state state = pool->state;
321 struct mempool_state *p = mp_alloc_fast(pool, sizeof(*p));
323 pool->state.next = p;
328 mp_pop(struct mempool *pool)
330 ASSERT(pool->state.next);
331 struct mempool_state state = pool->state;
332 mp_restore(pool, &state);
337 #include "lib/getopt.h"
343 fill(byte *ptr, uns len, uns magic)
346 *ptr++ = (magic++ & 255);
350 check(byte *ptr, uns len, uns magic, uns align)
352 ASSERT(!((uintptr_t)ptr & (align - 1)));
354 if (*ptr++ != (magic++ & 255))
358 int main(int argc, char **argv)
363 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || argc != optind)
364 die("Invalid usage");
366 uns max = 1000, n = 0, m = 0, can_realloc = 0;
368 struct mempool_state *state[max];
369 uns len[max], num[max], align[max];
370 struct mempool *mp = mp_new(128), mp_static;
372 for (uns i = 0; i < 5000; i++)
374 for (uns j = 0; j < n; j++)
375 check(ptr[j], len[j], j, align[j]);
377 DBG("free_small=%u free_big=%u idx=%u chunk_size=%u last_big=%p", mp->state.free[0], mp->state.free[1], mp->idx, mp->chunk_size, mp->last_big);
378 for (struct mempool_chunk *ch = mp->state.last[0]; ch; ch = ch->next)
379 DBG("small %p %p %p %d", (byte *)ch - ch->size, ch, ch + 1, ch->size);
380 for (struct mempool_chunk *ch = mp->state.last[1]; ch; ch = ch->next)
381 DBG("big %p %p %p %d", (byte *)ch - ch->size, ch, ch + 1, ch->size);
383 int r = random_max(100);
390 else if ((r -= 1) < 0)
395 mp = mp_new(random_max(0x1000) + 1);
397 mp = &mp_static, mp_init(mp, random_max(512) + 1);
400 else if (n < max && (r -= 30) < 0)
402 len[n] = random_max(0x2000);
403 DBG("alloc(%u)", len[n]);
404 align[n] = random_max(2) ? CPU_STRUCT_ALIGN : 1;
405 ptr[n] = (align[n] == 1) ? mp_alloc_fast_noalign(mp, len[n]) : mp_alloc_fast(mp, len[n]);
406 DBG(" -> (%p)", ptr[n]);
407 fill(ptr[n], len[n], n);
411 else if (n < max && (r -= 20) < 0)
413 len[n] = random_max(0x2000);
414 DBG("start(%u)", len[n]);
415 align[n] = random_max(2) ? CPU_STRUCT_ALIGN : 1;
416 ptr[n] = (align[n] == 1) ? mp_start_fast_noalign(mp, len[n]) : mp_start_fast(mp, len[n]);
417 DBG(" -> (%p)", ptr[n]);
418 fill(ptr[n], len[n], n);
423 else if (can_realloc && n && (r -= 10) < 0)
425 if (mp_open(mp, ptr[n - 1]) != len[n - 1])
430 for (uns i = random_max(4); i--; )
433 len[k] = random_max(0x2000);
434 DBG("grow(%u)", len[k]);
435 ptr[k] = mp_grow(mp, len[k]);
436 DBG(" -> (%p)", ptr[k]);
437 check(ptr[k], MIN(l, len[k]), k, align[k]);
438 fill(ptr[k], len[k], k);
440 mp_end(mp, ptr[k] + len[k]);
443 else if (can_realloc && n && (r -= 20) < 0)
445 uns i = n - 1, l = len[i];
446 DBG("realloc(%p, %u)", ptr[i], len[i]);
447 ptr[i] = mp_realloc(mp, ptr[i], len[i] = random_max(0x2000));
448 DBG(" -> (%p, %u)", ptr[i], len[i]);
449 check(ptr[i], MIN(len[i], l), i, align[i]);
450 fill(ptr[i], len[i], i);
452 else if (m < max && (r -= 5) < 0)
456 state[m++] = mp_push(mp);
459 else if (m && (r -= 2) < 0)
467 else if (m && (r -= 1) < 0)
469 uns i = random_max(m);
470 DBG("restore(%u)", i);
471 mp_restore(mp, state[i]);
475 else if (can_realloc && n && (r -= 5) < 0)
476 ASSERT(mp_size(mp, ptr[n - 1]) == len[n - 1]);