2 * Sherlock 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/pools.h"
17 struct memchunk *next;
24 struct mempool *p = xmalloc(sizeof(struct mempool));
26 size -= sizeof(struct memchunk);
27 p->free = p->last = NULL;
28 p->first = p->current = p->first_large = NULL;
31 p->threshold = size / 3;
36 mp_delete(struct mempool *p)
38 struct memchunk *c, *d;
40 for(d=p->first; d; d = c)
45 for(d=p->first_large; d; d = c)
54 mp_flush(struct mempool *p)
58 p->free = p->last = NULL;
59 p->current = p->first;
60 while (c = p->first_large)
62 p->first_large = c->next;
68 mp_alloc(struct mempool *p, uns s)
70 if (s <= p->threshold)
72 byte *x = (byte *)(((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
79 /* Still have free chunks from previous incarnation */
85 c = xmalloc(sizeof(struct memchunk) + p->chunk_size);
91 p->last = x + p->chunk_size;
98 struct memchunk *c = xmalloc(sizeof(struct memchunk) + s);
99 c->next = p->first_large;
106 mp_alloc_zero(struct mempool *p, uns s)
108 void *x = mp_alloc(p, s);