From: Martin Mares Date: Sun, 24 Oct 1999 19:58:55 +0000 (+0000) Subject: Clearable pools. X-Git-Tag: holmes-import~1665 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=5ce5a465727aef69c6faf7ab17d20bf17c66f336;p=libucw.git Clearable pools. --- diff --git a/lib/pool.c b/lib/pool.c index 8b6c73e1..8c352f0f 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -21,8 +21,9 @@ new_pool(uns size) struct mempool *p = xmalloc(sizeof(struct mempool)); size -= sizeof(struct memchunk); - p->chunks = NULL; p->free = p->last = NULL; + p->first = p->current = p->first_large = NULL; + p->plast = &p->first; p->chunk_size = size; p->threshold = size / 3; return p; @@ -31,17 +32,35 @@ new_pool(uns size) void free_pool(struct mempool *p) { - struct memchunk *c = p->chunks; + struct memchunk *c, *d; - while (c) + for(d=p->first; d; d = c) { - struct memchunk *n = c->next; - free(c); - c = n; + c = d->next; + free(d); + } + for(d=p->first_large; d; d = c) + { + c = d->next; + free(d); } free(p); } +void +flush_pool(struct mempool *p) +{ + struct memchunk *c; + + p->free = p->last = NULL; + p->current = p->first; + while (c = p->first_large) + { + p->first_large = c->next; + free(c); + } +} + void * pool_alloc(struct mempool *p, uns s) { @@ -50,9 +69,19 @@ pool_alloc(struct mempool *p, uns s) byte *x = (byte *)(((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1)); if (x + s > p->last) { - struct memchunk *c = xmalloc(sizeof(struct memchunk) + p->chunk_size); - c->next = p->chunks; - p->chunks = c; + struct memchunk *c; + + if (p->current && p->current->next) + /* Still have free chunks from previous incarnation */ + c = p->current->next; + else + { + c = xmalloc(sizeof(struct memchunk) + p->chunk_size); + *p->plast = c; + p->plast = &c->next; + c->next = NULL; + } + p->current = c; x = c->data; p->last = x + p->chunk_size; } @@ -62,8 +91,8 @@ pool_alloc(struct mempool *p, uns s) else { struct memchunk *c = xmalloc(sizeof(struct memchunk) + s); - c->next = p->chunks; - p->chunks = c; + c->next = p->first_large; + p->first_large = c; return c->data; } } diff --git a/lib/pools.h b/lib/pools.h index 3129c6a7..54658cf7 100644 --- a/lib/pools.h +++ b/lib/pools.h @@ -1,7 +1,7 @@ /* * Sherlock Library -- Memory Pools * - * (c) 1997 Martin Mares, + * (c) 1997--1999 Martin Mares */ #ifndef POOL_ALIGN @@ -9,18 +9,20 @@ #endif struct mempool { - struct memchunk *chunks; byte *free, *last; + struct memchunk *first, *current, **plast; + struct memchunk *first_large; uns chunk_size, threshold; }; struct mempool *new_pool(uns); void free_pool(struct mempool *); +void flush_pool(struct mempool *); void *pool_alloc(struct mempool *, uns); extern inline void *fast_alloc(struct mempool *p, uns l) { - void *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1)); + byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1)); byte *ee = f + l; if (ee > p->last) return pool_alloc(p, l); @@ -30,7 +32,7 @@ extern inline void *fast_alloc(struct mempool *p, uns l) extern inline void *fast_alloc_noalign(struct mempool *p, uns l) { - void *f = p->free; + byte *f = p->free; byte *ee = f + l; if (ee > p->last) return pool_alloc(p, l);