X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=lib%2Fpool.c;h=d581466ba210c87b8a3cb92dea9778a9f78cf259;hb=b8a57a2068377dbc303ae97f015bd07b20051b6a;hp=623f0517490e09c8e57bddc40b49e7f9578ec8d2;hpb=5b53087fa5a07ff89d34cf3bf3bc1b28809f05c2;p=libucw.git diff --git a/lib/pool.c b/lib/pool.c index 623f0517..d581466b 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -1,22 +1,25 @@ /* * Sherlock Library -- Memory Pools (One-Time Allocation) * - * (c) 1997--1999 Martin Mares + * (c) 1997--2001 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ -#include -#include - #include "lib/lib.h" #include "lib/pools.h" +#include +#include + struct memchunk { struct memchunk *next; byte data[0]; }; struct mempool * -new_pool(uns size) +mp_new(uns size) { struct mempool *p = xmalloc(sizeof(struct mempool)); @@ -30,25 +33,25 @@ new_pool(uns size) } void -free_pool(struct mempool *p) +mp_delete(struct mempool *p) { struct memchunk *c, *d; for(d=p->first; d; d = c) { c = d->next; - free(d); + xfree(d); } for(d=p->first_large; d; d = c) { c = d->next; - free(d); + xfree(d); } - free(p); + xfree(p); } void -flush_pool(struct mempool *p) +mp_flush(struct mempool *p) { struct memchunk *c; @@ -57,12 +60,12 @@ flush_pool(struct mempool *p) while (c = p->first_large) { p->first_large = c->next; - free(c); + xfree(c); } } void * -pool_alloc(struct mempool *p, uns s) +mp_alloc(struct mempool *p, uns s) { if (s <= p->threshold) { @@ -98,3 +101,20 @@ pool_alloc(struct mempool *p, uns s) return c->data; } } + +void * +mp_alloc_zero(struct mempool *p, uns s) +{ + void *x = mp_alloc(p, s); + bzero(x, s); + return x; +} + +char * +mp_strdup(struct mempool *p, char *s) +{ + uns l = strlen(s) + 1; + char *t = mp_alloc_fast_noalign(p, l); + memcpy(t, s, l); + return t; +}