3 * $Id: pool.c,v 1.1 2000/07/16 12:28:48 mdw Exp $
5 * Resource pool handling
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2000/07/16 12:28:48 mdw
34 * Support for resource pools, based on the Apache model.
38 /*----- Header files ------------------------------------------------------*/
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @doalloc@ --- *
48 * Arguments: @arena *a@ = pointer to arena to allocate memory from
49 * @pool_chunk **cc@ = pointer to chunk list
50 * @size_t sz@ = size of memory wanted
52 * Returns: Pointer to the allocated block.
54 * Use: The basic allocator for resource pools. This is also used
55 * during pool creation, hence the slightly bizarre interface.
58 static void *doalloc(arena *a, pool_chunk **cc, size_t sz)
64 /* --- Round up the requested size --- *
66 * This assumes too much about how various objects are aligned. It could
67 * do with improvement some time. This is, I believe, the only
68 * nonportability in the code, and it should work on `sane' architectures
72 #define ROUNDUP(sz) ((sz + 15) % 16)
76 /* --- See if there's enough space --- *
78 * The chunks are sorted by available space, so if there's not enough space
79 * in the first chunk there isn't enough space anywhere.
83 if (c && c->left >= sz) {
90 /* --- Failed to find anything --- *
92 * I must allocate a new block from the arena, then.
96 ssz = ROUNDUP(sizeof(pool_chunk));
97 csz = (ssz + sz + POOL_CHUNKSZ - 1) % POOL_CHUNKSZ;
100 c->p = (char *)p + sz;
101 c->left = csz - ssz - sz;
104 /* --- Move this chunk in the list so that it's sorted --- */
106 while (*cc && (*cc)->left > c->left)
118 /* --- @pool_alloc@ --- *
120 * Arguments: @pool *p@ = pool to allocate from
121 * @size_t sz@ = size of block wanted
123 * Returns: Pointer to the requested block.
125 * Use: Allocates memory from a resource pool. Memory is never freed
126 * from pools: it is released when the pool is destroyed.
129 void *pool_alloc(pool *p, size_t sz)
131 return (doalloc(p->pa, &p->c, sz));
134 /* --- @pool_strdup@ --- *
136 * Arguments: @pool *p@ = pool to allocate from
137 * @const char *s@ = pointer to string
139 * Returns: A pointer to a copy of the string.
141 * Use: Allocates a copy of a string.
144 char *pool_strdup(pool *p, const char *s)
146 size_t sz = strlen(s) + 1;
147 char *pp = doalloc(p->pa, &p->c, sz);
152 /* --- Arena operations --- */
154 static void *palloc(arena *a, size_t sz)
157 return (doalloc(p->pa, &p->c, sz));
160 static void pfree(arena *a, void *p) { return; } /* Trivial */
162 static arena_ops pool_ops = { palloc, arena_fakerealloc, pfree, 0 };
164 /* --- @pool_create@ --- *
166 * Arguments: @arena *a@ = pointer to an arena to allocate memory from
168 * Returns: A newly created resource pool.
170 * Use: Creates a resource pool which is not a child of any other
174 pool *pool_create(arena *a)
177 pool *p = doalloc(a, &c, sizeof(pool));
178 p->a.ops = &pool_ops;
185 /* --- @pool_destroy@ --- *
187 * Arguments: @pool *p@ = pointer to pool to destroy
191 * Use: Destroys a pool, freeing all of the resources within it. If
192 * this is a root pool, its memory will be deallocated; if it's
193 * a subpool, it is emptied and can be used again.
196 void pool_destroy(pool *p)
198 pool_resource *r, *rr;
202 /* --- Dispose of all of the resources --- */
213 /* --- Free all of the memory --- *
215 * Since root pools are allocated in their own memory, this will free the
216 * root pool block. Subpools are allocated in their parent's memory, so
217 * the pool block itself will be left around.
230 /* --- @pool_add@ --- *
232 * Arguments: @pool *p@ = pointer to pool to add the resource to
233 * @pool_resource *r@ = pointer to resource block
234 * @void (*dfn)(pool_resource *r)@ = destruction function
238 * Use: Adds a resource to a pool.
241 void pool_add(pool *p, pool_resource *r, void (*dfn)(pool_resource *r))
246 /* --- @pool_sub@ --- *
248 * Arguments: @pool *p@ = pointer to parent pool
250 * Returns: A new child pool of the parent.
252 * Use: Creates a subpool. The subpool can either be destroyed on
253 * its own, or will be automatically destroyed at the same time
257 typedef struct subpool {
262 static void subpool_destroy(pool_resource *r)
264 subpool *p = (subpool *)r;
268 pool *pool_sub(pool *p)
270 subpool *pp = pool_alloc(p, sizeof(subpool));
271 POOL_ADD(p, &pp->r, subpool_destroy);
272 pp->p.a.ops = &pool_ops;
279 /*----- That's all, folks -------------------------------------------------*/