]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
pool: alloc a single area for all PoolBuckets
authorEric Leblond <eric@regit.org>
Thu, 26 Jul 2012 13:02:20 +0000 (15:02 +0200)
committerVictor Julien <victor@inliniac.net>
Mon, 3 Sep 2012 14:20:32 +0000 (16:20 +0200)
As we know the number and the size of PoolBucket, we can simply
allocate a single memory zone.

src/util-pool.c
src/util-pool.h

index f325cdcde70c0cd6f1f5ff33f4b878e5ea7b944f..68529b7ba44b5ba5c7bca246c8efcfe59ebcea55 100644 (file)
@@ -54,16 +54,20 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, void *(*Alloc)(void *), vo
 
     /* alloc the buckets and place them in the empty list */
     uint32_t u32 = 0;
-    for (u32 = 0; u32 < size; u32++) {
-        /* populate pool */
-        PoolBucket *pb = SCMalloc(sizeof(PoolBucket));
+    if (size > 0) {
+        PoolBucket *pb = SCCalloc(size, sizeof(PoolBucket));
+        p->pb_buffer = pb;
         if (pb == NULL)
             goto error;
-
-        memset(pb, 0, sizeof(PoolBucket));
-        pb->next = p->empty_list;
-        p->empty_list = pb;
-        p->empty_list_size++;
+        memset(pb, 0, size * sizeof(PoolBucket));
+        for (u32 = 0; u32 < size; u32++) {
+            /* populate pool */
+            pb->next = p->empty_list;
+            pb->flags |= POOL_BUCKET_PREALLOCATED;
+            p->empty_list = pb;
+            p->empty_list_size++;
+            pb++;
+        }
     }
 
     /* prealloc the buckets and requeue them to the alloc list */
@@ -116,19 +120,25 @@ void PoolFree(Pool *p) {
         p->alloc_list = pb->next;
         p->Free(pb->data);
         pb->data = NULL;
-        SCFree(pb);
+        if (! pb->flags & POOL_BUCKET_PREALLOCATED) {
+            SCFree(pb);
+        }
     }
 
     while (p->empty_list != NULL) {
         PoolBucket *pb = p->empty_list;
         p->empty_list = pb->next;
-       if (pb->data!= NULL) {
+        if (pb->data!= NULL) {
             p->Free(pb->data);
             pb->data = NULL;
         }
-        SCFree(pb);
+        if (! pb->flags & POOL_BUCKET_PREALLOCATED) {
+            SCFree(pb);
+        }
     }
 
+    if (p->pb_buffer)
+        SCFree(p->pb_buffer);
     SCFree(p);
 }
 
index b1abb996371dbe10f32b08db157b16d1f21fa74e..67c6b0663c1d7e1b2696d9835935f31ce5242fb8 100644 (file)
 #ifndef __UTIL_POOL_H__
 #define __UTIL_POOL_H__
 
+#define POOL_BUCKET_PREALLOCATED    (1 << 0)
+
 /* pool bucket structure */
 typedef struct PoolBucket_ {
     void *data;
+    uint8_t flags;
     struct PoolBucket_ *next;
 } PoolBucket;
 
@@ -41,6 +44,9 @@ typedef struct Pool_ {
     PoolBucket *empty_list;
     uint32_t empty_list_size;
 
+    PoolBucket *pb_buffer;
+    void *data_buffer;
+
     void *(*Alloc)(void *);
     void *AllocData;
     void (*Free)(void *);