3 * $Id: pool.c,v 1.3 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @doalloc@ --- *
41 * Arguments: @arena *a@ = pointer to arena to allocate memory from
42 * @pool_chunk **cc@ = pointer to chunk list
43 * @size_t sz@ = size of memory wanted
45 * Returns: Pointer to the allocated block.
47 * Use: The basic allocator for resource pools. This is also used
48 * during pool creation, hence the slightly bizarre interface.
51 static void *doalloc(arena *a, pool_chunk **cc, size_t sz)
57 /* --- See if there's enough space --- *
59 * The chunks are sorted by available space, so if there's not enough space
60 * in the first chunk there isn't enough space anywhere.
65 if (c && c->left >= sz) {
72 /* --- Failed to find anything --- *
74 * I must allocate a new block from the arena, then.
78 ssz = sizeof(pool_chunk);
80 csz = (ssz + sz + POOL_CHUNKSZ - 1); csz -= csz % POOL_CHUNKSZ;
83 c->p = (char *)p + sz;
84 c->left = csz - ssz - sz;
87 /* --- Move this chunk in the list so that it's sorted --- */
89 while (*cc && (*cc)->left > c->left)
99 /* --- @pool_alloc@ --- *
101 * Arguments: @pool *p@ = pool to allocate from
102 * @size_t sz@ = size of block wanted
104 * Returns: Pointer to the requested block.
106 * Use: Allocates memory from a resource pool. Memory is never freed
107 * from pools: it is released when the pool is destroyed.
110 void *pool_alloc(pool *p, size_t sz)
112 return (doalloc(p->pa, &p->c, sz));
115 /* --- @pool_strdup@ --- *
117 * Arguments: @pool *p@ = pool to allocate from
118 * @const char *s@ = pointer to string
120 * Returns: A pointer to a copy of the string.
122 * Use: Allocates a copy of a string.
125 char *pool_strdup(pool *p, const char *s)
127 size_t sz = strlen(s) + 1;
128 char *pp = doalloc(p->pa, &p->c, sz);
133 /* --- Arena operations --- */
135 static void *palloc(arena *a, size_t sz)
138 return (doalloc(p->pa, &p->c, sz));
141 static void pfree(arena *a, void *p) { return; } /* Trivial */
143 static arena_ops pool_ops = { palloc, arena_fakerealloc, pfree, 0 };
145 /* --- @pool_init@ --- *
147 * Arguments: @pool *p@ = pointer to the pool structure to initialize
148 * @arena *a@ = pointer to an arena to allocate memory from
152 * Use: Initializes a chunk of memory as a resource pool which is not
153 * a child of any other resource pool.
156 void pool_init(pool *p, arena *a)
158 p->a.ops = &pool_ops;
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));
183 /* --- @pool_destroy@ --- *
185 * Arguments: @pool *p@ = pointer to pool to destroy
189 * Use: Destroys a pool, freeing all of the resources within it. If
190 * this is a pool created by @pool_create@, its memory will be
191 * deallocated; if it's a subpool or it was initialized by
192 * @pool_init@, it is emptied and can be used again.
195 void pool_destroy(pool *p)
197 pool_resource *r, *rr;
201 /* --- Dispose of all of the resources --- */
212 /* --- Free all of the memory --- *
214 * Since root pools are allocated in their own memory, this will free the
215 * root pool block. Subpools are allocated in their parent's memory, so
216 * the pool block itself will be left around.
229 /* --- @pool_add@ --- *
231 * Arguments: @pool *p@ = pointer to pool to add the resource to
232 * @pool_resource *r@ = pointer to resource block
233 * @void (*dfn)(pool_resource *r)@ = destruction function
237 * Use: Adds a resource to a pool.
240 void pool_add(pool *p, pool_resource *r, void (*dfn)(pool_resource *r))
245 /* --- @pool_sub@ --- *
247 * Arguments: @pool *p@ = pointer to parent pool
249 * Returns: A new child pool of the parent.
251 * Use: Creates a subpool. The subpool can either be destroyed on
252 * its own, or will be automatically destroyed at the same time
256 typedef struct subpool {
261 static void subpool_destroy(pool_resource *r)
263 subpool *p = (subpool *)r;
267 pool *pool_sub(pool *p)
269 subpool *pp = pool_alloc(p, sizeof(subpool));
270 POOL_ADD(p, &pp->r, subpool_destroy);
271 pp->p.a.ops = &pool_ops;
278 /*----- That's all, folks -------------------------------------------------*/