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_spread_internal(struct mempool *pool, void *p, uns size)
300 void *old = mp_ptr(pool);
301 void *new = mp_grow_internal(pool, p-old+size);
306 mp_restore(struct mempool *pool, struct mempool_state *state)
308 struct mempool_chunk *chunk, *next;
309 struct mempool_state s = *state;
310 for (chunk = pool->state.last[0]; chunk != s.last[0]; chunk = next)
313 chunk->next = pool->unused;
314 pool->unused = chunk;
316 for (chunk = pool->state.last[1]; chunk != s.last[1]; chunk = next)
319 mp_free_big_chunk(chunk);
322 pool->last_big = &pool->last_big;
325 struct mempool_state *
326 mp_push(struct mempool *pool)
328 struct mempool_state state = pool->state;
329 struct mempool_state *p = mp_alloc_fast(pool, sizeof(*p));
331 pool->state.next = p;
336 mp_pop(struct mempool *pool)
338 ASSERT(pool->state.next);
339 struct mempool_state state = pool->state;
340 mp_restore(pool, &state);
345 #include "lib/getopt.h"
351 fill(byte *ptr, uns len, uns magic)
354 *ptr++ = (magic++ & 255);
358 check(byte *ptr, uns len, uns magic, uns align)
360 ASSERT(!((uintptr_t)ptr & (align - 1)));
362 if (*ptr++ != (magic++ & 255))
366 int main(int argc, char **argv)
371 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || argc != optind)
372 die("Invalid usage");
374 uns max = 1000, n = 0, m = 0, can_realloc = 0;
376 struct mempool_state *state[max];
377 uns len[max], num[max], align[max];
378 struct mempool *mp = mp_new(128), mp_static;
380 for (uns i = 0; i < 5000; i++)
382 for (uns j = 0; j < n; j++)
383 check(ptr[j], len[j], j, align[j]);
385 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);
386 for (struct mempool_chunk *ch = mp->state.last[0]; ch; ch = ch->next)
387 DBG("small %p %p %p %d", (byte *)ch - ch->size, ch, ch + 1, ch->size);
388 for (struct mempool_chunk *ch = mp->state.last[1]; ch; ch = ch->next)
389 DBG("big %p %p %p %d", (byte *)ch - ch->size, ch, ch + 1, ch->size);
391 int r = random_max(100);
398 else if ((r -= 1) < 0)
403 mp = mp_new(random_max(0x1000) + 1);
405 mp = &mp_static, mp_init(mp, random_max(512) + 1);
408 else if (n < max && (r -= 30) < 0)
410 len[n] = random_max(0x2000);
411 DBG("alloc(%u)", len[n]);
412 align[n] = random_max(2) ? CPU_STRUCT_ALIGN : 1;
413 ptr[n] = (align[n] == 1) ? mp_alloc_fast_noalign(mp, len[n]) : mp_alloc_fast(mp, len[n]);
414 DBG(" -> (%p)", ptr[n]);
415 fill(ptr[n], len[n], n);
419 else if (n < max && (r -= 20) < 0)
421 len[n] = random_max(0x2000);
422 DBG("start(%u)", len[n]);
423 align[n] = random_max(2) ? CPU_STRUCT_ALIGN : 1;
424 ptr[n] = (align[n] == 1) ? mp_start_fast_noalign(mp, len[n]) : mp_start_fast(mp, len[n]);
425 DBG(" -> (%p)", ptr[n]);
426 fill(ptr[n], len[n], n);
431 else if (can_realloc && n && (r -= 10) < 0)
433 if (mp_open(mp, ptr[n - 1]) != len[n - 1])
438 for (uns i = random_max(4); i--; )
441 len[k] = random_max(0x2000);
442 DBG("grow(%u)", len[k]);
443 ptr[k] = mp_grow(mp, len[k]);
444 DBG(" -> (%p)", ptr[k]);
445 check(ptr[k], MIN(l, len[k]), k, align[k]);
446 fill(ptr[k], len[k], k);
448 mp_end(mp, ptr[k] + len[k]);
451 else if (can_realloc && n && (r -= 20) < 0)
453 uns i = n - 1, l = len[i];
454 DBG("realloc(%p, %u)", ptr[i], len[i]);
455 ptr[i] = mp_realloc(mp, ptr[i], len[i] = random_max(0x2000));
456 DBG(" -> (%p, %u)", ptr[i], len[i]);
457 check(ptr[i], MIN(len[i], l), i, align[i]);
458 fill(ptr[i], len[i], i);
460 else if (m < max && (r -= 5) < 0)
464 state[m++] = mp_push(mp);
467 else if (m && (r -= 2) < 0)
475 else if (m && (r -= 1) < 0)
477 uns i = random_max(m);
478 DBG("restore(%u)", i);
479 mp_restore(mp, state[i]);
483 else if (can_realloc && n && (r -= 5) < 0)
484 ASSERT(mp_size(mp, ptr[n - 1]) == len[n - 1]);