2 * UCW Library -- Memory Pools (One-Time Allocation)
4 * (c) 1997--2014 Martin Mares <mj@ucw.cz>
5 * (c) 2007--2014 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 <ucw/alloc.h>
15 #include <ucw/mempool.h>
19 #define MP_CHUNK_TAIL ALIGN_TO(sizeof(struct mempool_chunk), CPU_STRUCT_ALIGN)
20 #define MP_SIZE_MAX (SIZE_MAX - MP_CHUNK_TAIL - CPU_PAGE_SIZE)
22 struct mempool_chunk {
24 struct mempool *pool; // Can be useful when analysing coredump for memory leaks
26 struct mempool_chunk *next;
31 mp_align_size(size_t size)
33 #ifdef CONFIG_UCW_POOL_IS_MMAP
34 return ALIGN_TO(size + MP_CHUNK_TAIL, CPU_PAGE_SIZE) - MP_CHUNK_TAIL;
36 return ALIGN_TO(size, CPU_STRUCT_ALIGN);
40 static void *mp_allocator_alloc(struct ucw_allocator *a, size_t size)
42 struct mempool *mp = (struct mempool *) a;
43 return mp_alloc_fast(mp, size);
46 static void *mp_allocator_realloc(struct ucw_allocator *a, void *ptr, size_t old_size, size_t new_size)
48 if (new_size <= old_size)
52 * In the future, we might want to do something like mp_realloc(),
53 * but we have to check that it is indeed the last block in the pool.
55 struct mempool *mp = (struct mempool *) a;
56 void *new = mp_alloc_fast(mp, new_size);
57 memcpy(new, ptr, old_size);
61 static void mp_allocator_free(struct ucw_allocator *a UNUSED, void *ptr UNUSED)
67 mp_init(struct mempool *pool, size_t chunk_size)
69 chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
70 *pool = (struct mempool) {
72 .alloc = mp_allocator_alloc,
73 .realloc = mp_allocator_realloc,
74 .free = mp_allocator_free,
76 .chunk_size = chunk_size,
77 .threshold = chunk_size >> 1,
78 .last_big = &pool->last_big
83 mp_new_big_chunk(struct mempool *pool, size_t size)
85 struct mempool_chunk *chunk;
86 chunk = xmalloc(size + MP_CHUNK_TAIL) + size;
89 pool->total_size += size + MP_CHUNK_TAIL;
94 mp_free_big_chunk(struct mempool *pool, struct mempool_chunk *chunk)
96 pool->total_size -= chunk->size + MP_CHUNK_TAIL;
97 xfree((void *)chunk - chunk->size);
101 mp_new_chunk(struct mempool *pool, size_t size)
103 #ifdef CONFIG_UCW_POOL_IS_MMAP
104 struct mempool_chunk *chunk;
105 chunk = page_alloc(size + MP_CHUNK_TAIL) + size;
108 pool->total_size += size + MP_CHUNK_TAIL;
111 return mp_new_big_chunk(pool, size);
116 mp_free_chunk(struct mempool *pool, struct mempool_chunk *chunk)
118 #ifdef CONFIG_UCW_POOL_IS_MMAP
119 pool->total_size -= chunk->size + MP_CHUNK_TAIL;
120 page_free((void *)chunk - chunk->size, chunk->size + MP_CHUNK_TAIL);
122 mp_free_big_chunk(pool, chunk);
127 mp_new(size_t chunk_size)
129 chunk_size = mp_align_size(MAX(sizeof(struct mempool), chunk_size));
130 struct mempool_chunk *chunk = mp_new_chunk(NULL, chunk_size);
131 struct mempool *pool = (void *)chunk - chunk_size;
132 DBG("Creating mempool %p with %u bytes long chunks", pool, chunk_size);
137 *pool = (struct mempool) {
139 .alloc = mp_allocator_alloc,
140 .realloc = mp_allocator_realloc,
141 .free = mp_allocator_free,
143 .state = { .free = { chunk_size - sizeof(*pool) }, .last = { chunk } },
144 .chunk_size = chunk_size,
145 .threshold = chunk_size >> 1,
146 .last_big = &pool->last_big,
147 .total_size = chunk->size + MP_CHUNK_TAIL,
153 mp_free_chain(struct mempool *pool, struct mempool_chunk *chunk)
157 struct mempool_chunk *next = chunk->next;
158 mp_free_chunk(pool, chunk);
164 mp_free_big_chain(struct mempool *pool, struct mempool_chunk *chunk)
168 struct mempool_chunk *next = chunk->next;
169 mp_free_big_chunk(pool, chunk);
175 mp_delete(struct mempool *pool)
177 DBG("Deleting mempool %p", pool);
178 mp_free_big_chain(pool, pool->state.last[1]);
179 mp_free_chain(pool, pool->unused);
180 mp_free_chain(pool, pool->state.last[0]); // can contain the mempool structure
184 mp_flush(struct mempool *pool)
186 mp_free_big_chain(pool, pool->state.last[1]);
187 struct mempool_chunk *chunk, *next;
188 for (chunk = pool->state.last[0]; chunk && (void *)chunk - chunk->size != pool; chunk = next)
191 chunk->next = pool->unused;
192 pool->unused = chunk;
194 pool->state.last[0] = chunk;
195 pool->state.free[0] = chunk ? chunk->size - sizeof(*pool) : 0;
196 pool->state.last[1] = NULL;
197 pool->state.free[1] = 0;
198 pool->state.next = NULL;
199 pool->last_big = &pool->last_big;
203 mp_stats_chain(struct mempool *pool, struct mempool_chunk *chunk, struct mempool_stats *stats, uint idx)
207 stats->chain_size[idx] += chunk->size + MP_CHUNK_TAIL;
208 stats->chain_count[idx]++;
211 stats->used_size += chunk->size;
212 if ((byte *)pool == (byte *)chunk - chunk->size)
213 stats->used_size -= sizeof(*pool);
217 stats->total_size += stats->chain_size[idx];
221 mp_stats(struct mempool *pool, struct mempool_stats *stats)
223 bzero(stats, sizeof(*stats));
224 mp_stats_chain(pool, pool->state.last[0], stats, 0);
225 mp_stats_chain(pool, pool->state.last[1], stats, 1);
226 mp_stats_chain(pool, pool->unused, stats, 2);
227 stats->used_size -= pool->state.free[0] + pool->state.free[1];
228 ASSERT(stats->total_size == pool->total_size);
229 ASSERT(stats->used_size <= stats->total_size);
233 mp_total_size(struct mempool *pool)
235 return pool->total_size;
239 mp_shrink(struct mempool *pool, u64 min_total_size)
243 struct mempool_chunk *chunk = pool->unused;
244 if (!chunk || pool->total_size - (chunk->size + MP_CHUNK_TAIL) < min_total_size)
246 pool->unused = chunk->next;
247 mp_free_chunk(pool, chunk);
252 mp_alloc_internal(struct mempool *pool, size_t size)
254 struct mempool_chunk *chunk;
255 if (size <= pool->threshold)
260 chunk = pool->unused;
261 pool->unused = chunk->next;
265 chunk = mp_new_chunk(pool, pool->chunk_size);
270 chunk->next = pool->state.last[0];
271 pool->state.last[0] = chunk;
272 pool->state.free[0] = pool->chunk_size - size;
273 return (void *)chunk - pool->chunk_size;
275 else if (likely(size <= MP_SIZE_MAX))
278 size_t aligned = ALIGN_TO(size, CPU_STRUCT_ALIGN);
279 chunk = mp_new_big_chunk(pool, aligned);
280 chunk->next = pool->state.last[1];
284 pool->state.last[1] = chunk;
285 pool->state.free[1] = aligned - size;
286 return pool->last_big = (void *)chunk - aligned;
289 die("Cannot allocate %zu bytes from a mempool", size);
293 mp_alloc(struct mempool *pool, size_t size)
295 return mp_alloc_fast(pool, size);
299 mp_alloc_noalign(struct mempool *pool, size_t size)
301 return mp_alloc_fast_noalign(pool, size);
305 mp_alloc_zero(struct mempool *pool, size_t size)
307 void *ptr = mp_alloc_fast(pool, size);
313 mp_start_internal(struct mempool *pool, size_t size)
315 void *ptr = mp_alloc_internal(pool, size);
316 pool->state.free[pool->idx] += size;
321 mp_start(struct mempool *pool, size_t size)
323 return mp_start_fast(pool, size);
327 mp_start_noalign(struct mempool *pool, size_t size)
329 return mp_start_fast_noalign(pool, size);
333 mp_grow_internal(struct mempool *pool, size_t size)
335 if (unlikely(size > MP_SIZE_MAX))
336 die("Cannot allocate %zu bytes of memory", size);
337 size_t avail = mp_avail(pool);
338 void *ptr = mp_ptr(pool);
341 size_t amortized = likely(avail <= MP_SIZE_MAX / 2) ? avail * 2 : MP_SIZE_MAX;
342 amortized = MAX(amortized, size);
343 amortized = ALIGN_TO(amortized, CPU_STRUCT_ALIGN);
344 struct mempool_chunk *chunk = pool->state.last[1], *next = chunk->next;
345 pool->total_size = pool->total_size - chunk->size + amortized;
346 ptr = xrealloc(ptr, amortized + MP_CHUNK_TAIL);
347 chunk = ptr + amortized;
349 chunk->size = amortized;
350 pool->state.last[1] = chunk;
351 pool->state.free[1] = amortized;
352 pool->last_big = ptr;
357 void *p = mp_start_internal(pool, size);
358 memcpy(p, ptr, avail);
364 mp_open(struct mempool *pool, void *ptr)
366 return mp_open_fast(pool, ptr);
370 mp_realloc(struct mempool *pool, void *ptr, size_t size)
372 return mp_realloc_fast(pool, ptr, size);
376 mp_realloc_zero(struct mempool *pool, void *ptr, size_t size)
378 size_t old_size = mp_open_fast(pool, ptr);
379 ptr = mp_grow(pool, size);
381 bzero(ptr + old_size, size - old_size);
382 mp_end(pool, ptr + size);
387 mp_spread_internal(struct mempool *pool, void *p, size_t size)
389 void *old = mp_ptr(pool);
390 void *new = mp_grow_internal(pool, p-old+size);
395 mp_restore(struct mempool *pool, struct mempool_state *state)
397 struct mempool_chunk *chunk, *next;
398 struct mempool_state s = *state;
399 for (chunk = pool->state.last[0]; chunk != s.last[0]; chunk = next)
402 chunk->next = pool->unused;
403 pool->unused = chunk;
405 for (chunk = pool->state.last[1]; chunk != s.last[1]; chunk = next)
408 mp_free_big_chunk(pool, chunk);
411 pool->last_big = &pool->last_big;
414 struct mempool_state *
415 mp_push(struct mempool *pool)
417 struct mempool_state state = pool->state;
418 struct mempool_state *p = mp_alloc_fast(pool, sizeof(*p));
420 pool->state.next = p;
425 mp_pop(struct mempool *pool)
427 ASSERT(pool->state.next);
428 mp_restore(pool, pool->state.next);
433 #include <ucw/getopt.h>
439 fill(byte *ptr, uint len, uint magic)
442 *ptr++ = (magic++ & 255);
446 check(byte *ptr, uint len, uint magic, uint align)
448 ASSERT(!((uintptr_t)ptr & (align - 1)));
450 if (*ptr++ != (magic++ & 255))
454 int main(int argc, char **argv)
459 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || argc != optind)
460 die("Invalid usage");
462 uint max = 1000, n = 0, m = 0, can_realloc = 0;
464 struct mempool_state *state[max];
465 uint len[max], num[max], align[max];
466 struct mempool *mp = mp_new(128), mp_static;
468 for (uint i = 0; i < 5000; i++)
470 for (uint j = 0; j < n; j++)
471 check(ptr[j], len[j], j, align[j]);
473 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);
474 for (struct mempool_chunk *ch = mp->state.last[0]; ch; ch = ch->next)
475 DBG("small %p %p %p %d", (byte *)ch - ch->size, ch, ch + 1, ch->size);
476 for (struct mempool_chunk *ch = mp->state.last[1]; ch; ch = ch->next)
477 DBG("big %p %p %p %d", (byte *)ch - ch->size, ch, ch + 1, ch->size);
479 int r = random_max(100);
486 else if ((r -= 1) < 0)
491 mp = mp_new(random_max(0x1000) + 1);
493 mp = &mp_static, mp_init(mp, random_max(512) + 1);
496 else if (n < max && (r -= 30) < 0)
498 len[n] = random_max(0x2000);
499 DBG("alloc(%u)", len[n]);
500 align[n] = random_max(2) ? CPU_STRUCT_ALIGN : 1;
501 ptr[n] = (align[n] == 1) ? mp_alloc_fast_noalign(mp, len[n]) : mp_alloc_fast(mp, len[n]);
502 DBG(" -> (%p)", ptr[n]);
503 fill(ptr[n], len[n], n);
507 else if (n < max && (r -= 20) < 0)
509 len[n] = random_max(0x2000);
510 DBG("start(%u)", len[n]);
511 align[n] = random_max(2) ? CPU_STRUCT_ALIGN : 1;
512 ptr[n] = (align[n] == 1) ? mp_start_fast_noalign(mp, len[n]) : mp_start_fast(mp, len[n]);
513 DBG(" -> (%p)", ptr[n]);
514 fill(ptr[n], len[n], n);
519 else if (can_realloc && n && (r -= 10) < 0)
521 if (mp_open(mp, ptr[n - 1]) != len[n - 1])
526 for (uint i = random_max(4); i--; )
529 len[k] = random_max(0x2000);
530 DBG("grow(%u)", len[k]);
531 ptr[k] = mp_grow(mp, len[k]);
532 DBG(" -> (%p)", ptr[k]);
533 check(ptr[k], MIN(l, len[k]), k, align[k]);
534 fill(ptr[k], len[k], k);
536 mp_end(mp, ptr[k] + len[k]);
539 else if (can_realloc && n && (r -= 20) < 0)
541 uint i = n - 1, l = len[i];
542 DBG("realloc(%p, %u)", ptr[i], len[i]);
543 ptr[i] = mp_realloc(mp, ptr[i], len[i] = random_max(0x2000));
544 DBG(" -> (%p, %u)", ptr[i], len[i]);
545 check(ptr[i], MIN(len[i], l), i, align[i]);
546 fill(ptr[i], len[i], i);
548 else if (m < max && (r -= 5) < 0)
552 state[m++] = mp_push(mp);
555 else if (m && (r -= 2) < 0)
563 else if (m && (r -= 1) < 0)
565 uint i = random_max(m);
566 DBG("restore(%u)", i);
567 mp_restore(mp, state[i]);
571 else if (can_realloc && n && (r -= 5) < 0)
572 ASSERT(mp_size(mp, ptr[n - 1]) == len[n - 1]);
575 struct mempool_stats stats;
576 mp_stats(mp, &stats);