2 * UCW Library -- Growing arrays over mempools
4 * (c) 2014 Martin Mares <mj@ucw.cz>
9 #include <ucw/mempool.h>
13 static void *gary_mp_alloc(struct gary_allocator *a, size_t size)
15 return mp_alloc(a->data, size);
18 static void *gary_mp_realloc(struct gary_allocator *a, void *ptr, size_t old_size, size_t new_size)
20 if (new_size <= old_size)
24 * In the future, we might want to do something like mp_realloc(),
25 * but we have to check that it is indeed the last block in the pool.
27 void *new = mp_alloc(a->data, new_size);
28 memcpy(new, ptr, old_size);
32 static void gary_mp_free(struct gary_allocator *a UNUSED, void *ptr UNUSED)
36 struct gary_allocator *gary_new_allocator_mp(struct mempool *mp)
38 struct gary_allocator *a = mp_alloc(mp, sizeof(*a));
39 *a = (struct gary_allocator) {
40 .alloc = gary_mp_alloc,
41 .realloc = gary_mp_realloc,
54 struct mempool *mp = mp_new(4096);
55 struct gary_allocator *alloc = gary_new_allocator_mp(mp);
57 GARY_INIT_ALLOC(a, 5, alloc);
59 for (int i=0; i<5; i++)
69 for (int i=0; i<(int)GARY_SIZE(a); i++)