2 * UCW Library -- Memory Pools (One-Time Allocation)
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/mempool.h"
16 struct memchunk *next;
23 struct mempool *p = xmalloc(sizeof(struct mempool));
25 size -= sizeof(struct memchunk);
26 p->free = p->last = NULL;
27 p->first = p->current = p->first_large = NULL;
30 p->threshold = size / 3;
35 mp_delete(struct mempool *p)
37 struct memchunk *c, *d;
39 for(d=p->first; d; d = c)
44 for(d=p->first_large; d; d = c)
53 mp_flush(struct mempool *p)
57 p->free = p->last = NULL;
58 p->current = p->first;
59 while (c = p->first_large)
61 p->first_large = c->next;
67 mp_alloc(struct mempool *p, uns s)
69 if (s <= p->threshold)
71 byte *x = (byte *)(((addr_int_t) p->free + POOL_ALIGN - 1) & ~(addr_int_t)(POOL_ALIGN - 1));
78 /* Still have free chunks from previous incarnation */
84 c = xmalloc(sizeof(struct memchunk) + p->chunk_size);
90 p->last = x + p->chunk_size;
97 struct memchunk *c = xmalloc(sizeof(struct memchunk) + s);
98 c->next = p->first_large;
105 mp_alloc_zero(struct mempool *p, uns s)
107 void *x = mp_alloc(p, s);