]> mj.ucw.cz Git - libucw.git/commitdiff
Clearable pools.
authorMartin Mares <mj@ucw.cz>
Sun, 24 Oct 1999 19:58:55 +0000 (19:58 +0000)
committerMartin Mares <mj@ucw.cz>
Sun, 24 Oct 1999 19:58:55 +0000 (19:58 +0000)
lib/pool.c
lib/pools.h

index 8b6c73e1427fdbe97ec8547d58d15b93e70b66fe..8c352f0fa9f83081cbf92551fa09179a52375958 100644 (file)
@@ -21,8 +21,9 @@ new_pool(uns size)
   struct mempool *p = xmalloc(sizeof(struct mempool));
 
   size -= sizeof(struct memchunk);
-  p->chunks = NULL;
   p->free = p->last = NULL;
+  p->first = p->current = p->first_large = NULL;
+  p->plast = &p->first;
   p->chunk_size = size;
   p->threshold = size / 3;
   return p;
@@ -31,17 +32,35 @@ new_pool(uns size)
 void
 free_pool(struct mempool *p)
 {
-  struct memchunk *c = p->chunks;
+  struct memchunk *c, *d;
 
-  while (c)
+  for(d=p->first; d; d = c)
     {
-      struct memchunk *n = c->next;
-      free(c);
-      c = n;
+      c = d->next;
+      free(d);
+    }
+  for(d=p->first_large; d; d = c)
+    {
+      c = d->next;
+      free(d);
     }
   free(p);
 }
 
+void
+flush_pool(struct mempool *p)
+{
+  struct memchunk *c;
+
+  p->free = p->last = NULL;
+  p->current = p->first;
+  while (c = p->first_large)
+    {
+      p->first_large = c->next;
+      free(c);
+    }
+}
+
 void *
 pool_alloc(struct mempool *p, uns s)
 {
@@ -50,9 +69,19 @@ pool_alloc(struct mempool *p, uns s)
       byte *x = (byte *)(((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
       if (x + s > p->last)
        {
-         struct memchunk *c = xmalloc(sizeof(struct memchunk) + p->chunk_size);
-         c->next = p->chunks;
-         p->chunks = c;
+         struct memchunk *c;
+
+         if (p->current && p->current->next)
+           /* Still have free chunks from previous incarnation */
+           c = p->current->next;
+         else
+           {
+             c = xmalloc(sizeof(struct memchunk) + p->chunk_size);
+             *p->plast = c;
+             p->plast = &c->next;
+             c->next = NULL;
+           }
+         p->current = c;
          x = c->data;
          p->last = x + p->chunk_size;
        }
@@ -62,8 +91,8 @@ pool_alloc(struct mempool *p, uns s)
   else
     {
       struct memchunk *c = xmalloc(sizeof(struct memchunk) + s);
-      c->next = p->chunks;
-      p->chunks = c;
+      c->next = p->first_large;
+      p->first_large = c;
       return c->data;
     }
 }
index 3129c6a7a7bda97bf4ebbbbe4ba5e6382d15f3e5..54658cf7043d532f1c6604123e0a400db994fd16 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     Sherlock Library -- Memory Pools
  *
- *     (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
+ *     (c) 1997--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  */
 
 #ifndef POOL_ALIGN
@@ -9,18 +9,20 @@
 #endif
 
 struct mempool {
-  struct memchunk *chunks;
   byte *free, *last;
+  struct memchunk *first, *current, **plast;
+  struct memchunk *first_large;
   uns chunk_size, threshold;
 };
 
 struct mempool *new_pool(uns);
 void free_pool(struct mempool *);
+void flush_pool(struct mempool *);
 void *pool_alloc(struct mempool *, uns);
 
 extern inline void *fast_alloc(struct mempool *p, uns l)
 {
-  void *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
+  byte *f = (void *) (((uns) p->free + POOL_ALIGN - 1) & ~(POOL_ALIGN - 1));
   byte *ee = f + l;
   if (ee > p->last)
     return pool_alloc(p, l);
@@ -30,7 +32,7 @@ extern inline void *fast_alloc(struct mempool *p, uns l)
 
 extern inline void *fast_alloc_noalign(struct mempool *p, uns l)
 {
-  void *f = p->free;
+  byte *f = p->free;
   byte *ee = f + l;
   if (ee > p->last)
     return pool_alloc(p, l);