X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/d94be36654214c09fa6510611658325a84d5cfa7..bfab5602f254a61a5c7bf354ca184fa582b9e609:/pool.c diff --git a/pool.c b/pool.c index 0c4e3ed..d89bab2 100644 --- a/pool.c +++ b/pool.c @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: pool.c,v 1.1 2000/07/16 12:28:48 mdw Exp $ + * $Id$ * * Resource pool handling * * (c) 2000 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of the mLib utilities library. * @@ -15,28 +15,23 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with mLib; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 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)