chiark / gitweb /
Use right test vector file name.
[mLib] / pool.c
diff --git a/pool.c b/pool.c
index 0c4e3ed15356b3479f18fe905d8197ac2a886677..161186273b20d5d5b67b46c62677e5d41cdd72a7 100644 (file)
--- a/pool.c
+++ b/pool.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: pool.c,v 1.1 2000/07/16 12:28:48 mdw Exp $
+ * $Id: pool.c,v 1.2 2003/10/12 14:44:46 mdw Exp $
  *
  * Resource pool handling
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: pool.c,v $
+ * Revision 1.2  2003/10/12 14:44:46  mdw
+ * Various fixes.
+ *
  * Revision 1.1  2000/07/16 12:28:48  mdw
  * Support for resource pools, based on the Apache model.
  *
@@ -37,6 +40,7 @@
 
 /*----- Header files ------------------------------------------------------*/
 
+#include "align.h"
 #include "alloc.h"
 #include "arena.h"
 #include "pool.h"
@@ -61,24 +65,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 +86,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 +105,6 @@ static void *doalloc(arena *a, pool_chunk **cc, size_t sz)
   /* --- Done --- */
 
   return (p);
-
-#undef ROUNDUP
 }
 
 /* --- @pool_alloc@ --- *
@@ -161,6 +153,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 +186,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 +198,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)