X-Git-Url: https://git.distorted.org.uk/~mdw/mLib/blobdiff_plain/d94be36654214c09fa6510611658325a84d5cfa7..eae919b59938b827743e7542b6e529a5183c21ea:/pool.c?ds=sidebyside diff --git a/pool.c b/pool.c index 0c4e3ed..c9b05ec 100644 --- a/pool.c +++ b/pool.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: pool.c,v 1.1 2000/07/16 12:28:48 mdw Exp $ + * $Id$ * * Resource pool handling * @@ -27,16 +27,11 @@ * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: pool.c,v $ - * Revision 1.1 2000/07/16 12:28:48 mdw - * Support for resource pools, based on the Apache model. - * - */ - /*----- Header files ------------------------------------------------------*/ +#include + +#include "align.h" #include "alloc.h" #include "arena.h" #include "pool.h" @@ -61,24 +56,13 @@ static void *doalloc(arena *a, pool_chunk **cc, size_t sz) void *p; size_t csz, ssz; - /* --- Round up the requested size --- * - * - * This assumes too much about how various objects are aligned. It could - * do with improvement some time. This is, I believe, the only - * nonportability in the code, and it should work on `sane' architectures - * anyway. - */ - -#define ROUNDUP(sz) ((sz + 15) % 16) - - sz = ROUNDUP(sz); - /* --- See if there's enough space --- * * * The chunks are sorted by available space, so if there's not enough space * in the first chunk there isn't enough space anywhere. */ + ALIGN(sz); c = *cc; if (c && c->left >= sz) { p = c->p; @@ -93,9 +77,10 @@ static void *doalloc(arena *a, pool_chunk **cc, size_t sz) */ else { - ssz = ROUNDUP(sizeof(pool_chunk)); - csz = (ssz + sz + POOL_CHUNKSZ - 1) % POOL_CHUNKSZ; - p = x_alloc(a, csz); + ssz = sizeof(pool_chunk); + ALIGN(ssz); + csz = (ssz + sz + POOL_CHUNKSZ - 1); csz -= csz % POOL_CHUNKSZ; + c = x_alloc(a, csz); p = (char *)c + ssz; c->p = (char *)p + sz; c->left = csz - ssz - sz; @@ -111,8 +96,6 @@ static void *doalloc(arena *a, pool_chunk **cc, size_t sz) /* --- Done --- */ return (p); - -#undef ROUNDUP } /* --- @pool_alloc@ --- * @@ -161,6 +144,25 @@ static void pfree(arena *a, void *p) { return; } /* Trivial */ static arena_ops pool_ops = { palloc, arena_fakerealloc, pfree, 0 }; +/* --- @pool_init@ --- * + * + * Arguments: @pool *p@ = pointer to the pool structure to initialize + * @arena *a@ = pointer to an arena to allocate memory from + * + * Returns: --- + * + * Use: Initializes a chunk of memory as a resource pool which is not + * a child of any other resource pool. + */ + +void pool_init(pool *p, arena *a) +{ + p->a.ops = &pool_ops; + p->c = 0; + p->r = 0; + p->pa = a; +} + /* --- @pool_create@ --- * * * Arguments: @arena *a@ = pointer to an arena to allocate memory from @@ -175,10 +177,8 @@ pool *pool_create(arena *a) { pool_chunk *c = 0; pool *p = doalloc(a, &c, sizeof(pool)); - p->a.ops = &pool_ops; + pool_init(p, a); p->c = c; - p->r = 0; - p->pa = a; return (p); } @@ -189,8 +189,9 @@ pool *pool_create(arena *a) * Returns: --- * * Use: Destroys a pool, freeing all of the resources within it. If - * this is a root pool, its memory will be deallocated; if it's - * a subpool, it is emptied and can be used again. + * this is a pool created by @pool_create@, its memory will be + * deallocated; if it's a subpool or it was initialized by + * @pool_init@, it is emptied and can be used again. */ void pool_destroy(pool *p)