/* -*-c-*-
*
- * $Id: pool.c,v 1.1 2000/07/16 12:28:48 mdw Exp $
+ * $Id$
*
* Resource pool handling
*
* 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 <string.h>
+
+#include "align.h"
#include "alloc.h"
#include "arena.h"
#include "pool.h"
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;
*/
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;
/* --- Done --- */
return (p);
-
-#undef ROUNDUP
}
/* --- @pool_alloc@ --- *
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
{
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);
}
* 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)