]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
reintroduce pool free func for cases where block alloc is not used.
authorVictor Julien <victor@inliniac.net>
Fri, 21 Sep 2012 13:10:28 +0000 (15:10 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 21 Sep 2012 13:10:28 +0000 (15:10 +0200)
src/app-layer-parser.c
src/defrag.c
src/detect-luajit.c
src/stream-tcp-reassemble.c
src/stream-tcp.c
src/stream.c
src/util-pool.c
src/util-pool.h

index 94761798484f821c5e56fefe1b16355fbd9d63df..e470a7d5ddf7be853cad850742138f0d669b642c 100644 (file)
@@ -1336,7 +1336,8 @@ void RegisterAppLayerParsers(void)
      * \todo Per thread pool */
     al_result_pool = PoolInit(1000, 250,
             sizeof(AppLayerParserResultElmt),
-            AlpResultElmtPoolAlloc, NULL, NULL, AlpResultElmtPoolCleanup);
+            AlpResultElmtPoolAlloc, NULL, NULL,
+            AlpResultElmtPoolCleanup, NULL);
 
     RegisterHTPParsers();
     RegisterSSLParsers();
index 44886c604864d734c02c8e3cc57eaf464fe97db1..ee7e43729bd59d88298f8329b6888a40ff399dce 100644 (file)
@@ -190,7 +190,7 @@ DefragContextNew(void)
     intmax_t frag_pool_prealloc = frag_pool_size / 2;
     dc->frag_pool = PoolInit(frag_pool_size, frag_pool_prealloc,
         sizeof(Frag),
-        NULL, DefragFragInit, dc, NULL);
+        NULL, DefragFragInit, dc, NULL, NULL);
     if (dc->frag_pool == NULL) {
         SCLogError(SC_ERR_MEM_ALLOC,
             "Defrag: Failed to initialize fragment pool.");
index 57f48a40cc3c3e2f5cebacaf1b18e4dec9065cd4..3ea3ad9e6644a11ddf598a908f818a547974b313 100644 (file)
@@ -137,7 +137,7 @@ static void *LuaStatePoolAlloc(void) {
     return luaL_newstate();
 }
 
-static void LuaStatePoolClean(void *d) {
+static void LuaStatePoolFree(void *d) {
     lua_State *s = (lua_State *)d;
     if (s != NULL)
         lua_close(s);
@@ -164,13 +164,14 @@ int DetectLuajitSetupStatesPool(int num, int reloads) {
                 cpus = 10;
             }
             cnt = num * cpus;
+            cnt *= 3; /* assume 3 threads per core */
 
             /* alloc a bunch extra so reload can add new rules/instances */
             if (reloads)
                 cnt *= 5;
         }
 
-        luajit_states = PoolInit(0, cnt, 0, LuaStatePoolAlloc, NULL, NULL, LuaStatePoolClean);
+        luajit_states = PoolInit(0, cnt, 0, LuaStatePoolAlloc, NULL, NULL, NULL, LuaStatePoolFree);
         if (luajit_states == NULL) {
             SCLogError(SC_ERR_LUAJIT_ERROR, "luastate pool init failed, luajit keywords won't work");
             retval = -1;
@@ -192,9 +193,11 @@ static lua_State *DetectLuajitGetState(void) {
 }
 
 static void DetectLuajitReturnState(lua_State *s) {
-    pthread_mutex_lock(&luajit_states_lock);
-    PoolReturn(luajit_states, (void *)s);
-    pthread_mutex_unlock(&luajit_states_lock);
+    if (s != NULL) {
+        pthread_mutex_lock(&luajit_states_lock);
+        PoolReturn(luajit_states, (void *)s);
+        pthread_mutex_unlock(&luajit_states_lock);
+    }
 }
 
 /** \brief dump stack from lua state to screen */
index e4f7f85652667567bace32a7d7d52d1594db1841..cc4df3afe805daf56353c9559417247e3ed6a691 100644 (file)
@@ -286,7 +286,7 @@ int StreamTcpReassembleInit(char quiet)
                                      sizeof (TcpSegment),
                                      TcpSegmentPoolAlloc, TcpSegmentPoolInit,
                                      (void *) &segment_pool_pktsizes[u16],
-                                     TcpSegmentPoolCleanup);
+                                     TcpSegmentPoolCleanup, NULL);
         SCMutexUnlock(&segment_pool_mutex[u16]);
     }
 
index b1e5b6c4894de53b14319befeb8084656440807f..e1c97498bfc1b7eb5df9611dcb348a9fa22660fa 100644 (file)
@@ -493,7 +493,7 @@ void StreamTcpInitConfig(char quiet)
                         sizeof(TcpSession),
                         StreamTcpSessionPoolAlloc,
                         StreamTcpSessionPoolInit, NULL,
-                        StreamTcpSessionPoolCleanup);
+                        StreamTcpSessionPoolCleanup, NULL);
     if (ssn_pool == NULL) {
         SCLogError(SC_ERR_POOL_INIT, "ssn_pool is not initialized");
         SCMutexUnlock(&ssn_pool_mutex);
index 2781111d4cf72c4cb26fb2e141555565c1ee7668..96e896ba03202e1ffac61e1b7e6f44ed77956a55 100644 (file)
@@ -147,7 +147,7 @@ void StreamMsgQueuesInit(void) {
     SCMutexInit(&stream_pool_memuse_mutex, NULL);
 #endif
     SCMutexLock(&stream_msg_pool_mutex);
-    stream_msg_pool = PoolInit(0,250,sizeof(StreamMsg),NULL,StreamMsgInit,NULL,NULL);
+    stream_msg_pool = PoolInit(0,250,sizeof(StreamMsg),NULL,StreamMsgInit,NULL,NULL,NULL);
     if (stream_msg_pool == NULL)
         exit(EXIT_FAILURE); /* XXX */
     SCMutexUnlock(&stream_msg_pool_mutex);
index 7e25ea58b6d91d6c8ac16e1337e6194f43a1beef..108d66f6f827b1b260d931ce1b38f31812971b43 100644 (file)
@@ -76,10 +76,11 @@ static int PoolDataPreAllocated(Pool *p, void *data)
  * \param Alloc An allocation function or NULL to use a standard SCMalloc
  * \param Init An init function or NULL to use a standard memset to 0
  * \param InitData Init data
- * \Param Cleanup a free function or NULL if no special treatment is needed
+ * \param Cleanup a free function or NULL if no special treatment is needed
+ * \param Free free func
  * \retval the allocated Pool
  */
-Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size,  void *(*Alloc)(), int (*Init)(void *, void *), void *InitData,  void (*Cleanup)(void *))
+Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size,  void *(*Alloc)(), int (*Init)(void *, void *), void *InitData,  void (*Cleanup)(void *), void (*Free)(void *))
 {
     Pool *p = NULL;
 
@@ -87,6 +88,8 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size,  void *
         goto error;
     if (size != 0 && elt_size == 0)
         goto error;
+    if (elt_size && Free)
+        goto error;
 
     /* setup the filter */
     p = SCMalloc(sizeof(Pool));
@@ -103,6 +106,7 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size,  void *
     p->Init = Init;
     p->InitData = InitData;
     p->Cleanup = Cleanup;
+    p->Free = Free;
     if (p->Init == NULL) {
         p->Init = PoolMemset;
         p->InitData = p;
@@ -153,7 +157,10 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size,  void *
             if (p->Init(pb->data, p->InitData) != 1) {
                 if (p->Cleanup)
                     p->Cleanup(pb->data);
-                SCFree(pb->data);
+                if (p->Free)
+                    p->Free(pb->data);
+                else
+                    SCFree(pb->data);
                 SCFree(pb);
                 goto error;
             }
@@ -205,7 +212,10 @@ void PoolFree(Pool *p) {
         if (p->Cleanup)
             p->Cleanup(pb->data);
         if (PoolDataPreAllocated(p, pb->data) == 0) {
-            SCFree(pb->data);
+            if (p->Free)
+                p->Free(pb->data);
+            else
+                SCFree(pb->data);
         }
         pb->data = NULL;
         if (! pb->flags & POOL_BUCKET_PREALLOCATED) {
@@ -220,7 +230,10 @@ void PoolFree(Pool *p) {
             if (p->Cleanup)
                 p->Cleanup(pb->data);
             if (PoolDataPreAllocated(p, pb->data) == 0) {
-                SCFree(pb->data);
+                if (p->Free)
+                    p->Free(pb->data);
+                else
+                    SCFree(pb->data);
             }
             pb->data = NULL;
         }
@@ -301,8 +314,12 @@ void PoolReturn(Pool *p, void *data) {
         if (p->Cleanup != NULL) {
             p->Cleanup(data);
         }
-        if (PoolDataPreAllocated(p, data) == 0)
-            SCFree(data);
+        if (PoolDataPreAllocated(p, data) == 0) {
+            if (p->Free)
+                p->Free(data);
+            else
+                SCFree(data);
+        }
 
         SCLogDebug("tried to return data %p to the pool %p, but no more "
                    "buckets available. Just freeing the data.", data, p);
@@ -349,7 +366,7 @@ void PoolTestFree(void *ptr) {
 
 #ifdef UNITTESTS
 static int PoolTestInit01 (void) {
-    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree);
+    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
     if (p == NULL)
         return 0;
 
@@ -360,7 +377,7 @@ static int PoolTestInit01 (void) {
 static int PoolTestInit02 (void) {
     int retval = 0;
 
-    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree);
+    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
     if (p == NULL)
         goto end;
 
@@ -396,7 +413,7 @@ static int PoolTestInit03 (void) {
     int retval = 0;
     void *data = NULL;
 
-    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree);
+    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
     if (p == NULL)
         goto end;
 
@@ -430,7 +447,7 @@ static int PoolTestInit04 (void) {
     int retval = 0;
     char *str = NULL;
 
-    Pool *p = PoolInit(10,5,strlen("test") + 1,NULL, PoolTestInitArg,(void *)"test",PoolTestFree);
+    Pool *p = PoolInit(10,5,strlen("test") + 1,NULL, PoolTestInitArg,(void *)"test",PoolTestFree, NULL);
     if (p == NULL)
         goto end;
 
@@ -470,7 +487,7 @@ static int PoolTestInit05 (void) {
     int retval = 0;
     void *data = NULL;
 
-    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL, NULL,PoolTestFree);
+    Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL, NULL,PoolTestFree, NULL);
     if (p == NULL)
         goto end;
 
@@ -520,7 +537,7 @@ static int PoolTestInit06 (void) {
     void *data = NULL;
     void *data2 = NULL;
 
-    Pool *p = PoolInit(1,0,10,PoolTestAlloc,NULL,NULL,PoolTestFree);
+    Pool *p = PoolInit(1,0,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
     if (p == NULL)
         goto end;
 
@@ -578,7 +595,7 @@ static int PoolTestInit07 (void) {
     void *data = NULL;
     void *data2 = NULL;
 
-    Pool *p = PoolInit(0,1,10,PoolTestAlloc,NULL,NULL,PoolTestFree);
+    Pool *p = PoolInit(0,1,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
     if (p == NULL)
         goto end;
 
index edc79deffc5170a9bb99a39bc224dcd504cfef6c..c92d23cfa46362228325f7ca2d1e39f56d9ede2e 100644 (file)
@@ -59,6 +59,7 @@ typedef struct Pool_ {
     int (*Init)(void *, void *);
     void *InitData;
     void (*Cleanup)(void *);
+    void (*Free)(void *);
 
     uint32_t elt_size;
     uint32_t outstanding;
@@ -66,7 +67,7 @@ typedef struct Pool_ {
 } Pool;
 
 /* prototypes */
-Pool* PoolInit(uint32_t, uint32_t, uint32_t, void *(*Alloc)(), int (*Init)(void *, void *), void *, void (*Free)(void *));
+Pool* PoolInit(uint32_t, uint32_t, uint32_t, void *(*Alloc)(), int (*Init)(void *, void *), void *, void (*Cleanup)(void *), void (*Free)(void *));
 void PoolFree(Pool *);
 void PoolPrint(Pool *);
 void PoolPrintSaturation(Pool *p);