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 ------------------------------------------------------*/
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @doalloc@ --- *
43 * Arguments: @arena *a@ = pointer to arena to allocate memory from
44 * @pool_chunk **cc@ = pointer to chunk list
45 * @size_t sz@ = size of memory wanted
47 * Returns: Pointer to the allocated block.
49 * Use: The basic allocator for resource pools. This is also used
50 * during pool creation, hence the slightly bizarre interface.
53 static void *doalloc(arena
*a
, pool_chunk
**cc
, size_t sz
)
59 /* --- See if there's enough space --- *
61 * The chunks are sorted by available space, so if there's not enough space
62 * in the first chunk there isn't enough space anywhere.
67 if (c
&& c
->left
>= sz
) {
74 /* --- Failed to find anything --- *
76 * I must allocate a new block from the arena, then.
80 ssz
= sizeof(pool_chunk
);
82 csz
= (ssz
+ sz
+ POOL_CHUNKSZ
- 1); csz
-= csz
% POOL_CHUNKSZ
;
85 c
->p
= (char *)p
+ sz
;
86 c
->left
= csz
- ssz
- sz
;
89 /* --- Move this chunk in the list so that it's sorted --- */
91 while (*cc
&& (*cc
)->left
> c
->left
)
101 /* --- @pool_alloc@ --- *
103 * Arguments: @pool *p@ = pool to allocate from
104 * @size_t sz@ = size of block wanted
106 * Returns: Pointer to the requested block.
108 * Use: Allocates memory from a resource pool. Memory is never freed
109 * from pools: it is released when the pool is destroyed.
112 void *pool_alloc(pool
*p
, size_t sz
)
114 return (doalloc(p
->pa
, &p
->c
, sz
));
117 /* --- @pool_strdup@ --- *
119 * Arguments: @pool *p@ = pool to allocate from
120 * @const char *s@ = pointer to string
122 * Returns: A pointer to a copy of the string.
124 * Use: Allocates a copy of a string.
127 char *pool_strdup(pool
*p
, const char *s
)
129 size_t sz
= strlen(s
) + 1;
130 char *pp
= doalloc(p
->pa
, &p
->c
, sz
);
135 /* --- Arena operations --- */
137 static void *palloc(arena
*a
, size_t sz
)
140 return (doalloc(p
->pa
, &p
->c
, sz
));
143 static void pfree(arena
*a
, void *p
) { return; } /* Trivial */
145 static arena_ops pool_ops
= { palloc
, arena_fakerealloc
, pfree
, 0 };
147 /* --- @pool_init@ --- *
149 * Arguments: @pool *p@ = pointer to the pool structure to initialize
150 * @arena *a@ = pointer to an arena to allocate memory from
154 * Use: Initializes a chunk of memory as a resource pool which is not
155 * a child of any other resource pool.
158 void pool_init(pool
*p
, arena
*a
)
160 p
->a
.ops
= &pool_ops
;
166 /* --- @pool_create@ --- *
168 * Arguments: @arena *a@ = pointer to an arena to allocate memory from
170 * Returns: A newly created resource pool.
172 * Use: Creates a resource pool which is not a child of any other
176 pool
*pool_create(arena
*a
)
179 pool
*p
= doalloc(a
, &c
, sizeof(pool
));
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 pool created by @pool_create@, its memory will be
193 * deallocated; if it's a subpool or it was initialized by
194 * @pool_init@, it is emptied and can be used again.
197 void pool_destroy(pool
*p
)
199 pool_resource
*r
, *rr
;
203 /* --- Dispose of all of the resources --- */
214 /* --- Free all of the memory --- *
216 * Since root pools are allocated in their own memory, this will free the
217 * root pool block. Subpools are allocated in their parent's memory, so
218 * the pool block itself will be left around.
231 /* --- @pool_add@ --- *
233 * Arguments: @pool *p@ = pointer to pool to add the resource to
234 * @pool_resource *r@ = pointer to resource block
235 * @void (*dfn)(pool_resource *r)@ = destruction function
239 * Use: Adds a resource to a pool.
242 void pool_add(pool
*p
, pool_resource
*r
, void (*dfn
)(pool_resource
*r
))
247 /* --- @pool_sub@ --- *
249 * Arguments: @pool *p@ = pointer to parent pool
251 * Returns: A new child pool of the parent.
253 * Use: Creates a subpool. The subpool can either be destroyed on
254 * its own, or will be automatically destroyed at the same time
258 typedef struct subpool
{
263 static void subpool_destroy(pool_resource
*r
)
265 subpool
*p
= (subpool
*)r
;
269 pool
*pool_sub(pool
*p
)
271 subpool
*pp
= pool_alloc(p
, sizeof(subpool
));
272 POOL_ADD(p
, &pp
->r
, subpool_destroy
);
273 pp
->p
.a
.ops
= &pool_ops
;
280 /*----- That's all, folks -------------------------------------------------*/