From: Victor Julien Date: Fri, 21 Sep 2012 13:10:28 +0000 (+0200) Subject: reintroduce pool free func for cases where block alloc is not used. X-Git-Tag: suricata-1.4beta2~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=20d2db085e098964a8b2493a4cd19a353fe9bfc3;p=thirdparty%2Fsuricata.git reintroduce pool free func for cases where block alloc is not used. --- diff --git a/src/app-layer-parser.c b/src/app-layer-parser.c index 9476179848..e470a7d5dd 100644 --- a/src/app-layer-parser.c +++ b/src/app-layer-parser.c @@ -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(); diff --git a/src/defrag.c b/src/defrag.c index 44886c6048..ee7e43729b 100644 --- a/src/defrag.c +++ b/src/defrag.c @@ -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."); diff --git a/src/detect-luajit.c b/src/detect-luajit.c index 57f48a40cc..3ea3ad9e66 100644 --- a/src/detect-luajit.c +++ b/src/detect-luajit.c @@ -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 */ diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index e4f7f85652..cc4df3afe8 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -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]); } diff --git a/src/stream-tcp.c b/src/stream-tcp.c index b1e5b6c489..e1c97498bf 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -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); diff --git a/src/stream.c b/src/stream.c index 2781111d4c..96e896ba03 100644 --- a/src/stream.c +++ b/src/stream.c @@ -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); diff --git a/src/util-pool.c b/src/util-pool.c index 7e25ea58b6..108d66f6f8 100644 --- a/src/util-pool.c +++ b/src/util-pool.c @@ -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; diff --git a/src/util-pool.h b/src/util-pool.h index edc79deffc..c92d23cfa4 100644 --- a/src/util-pool.h +++ b/src/util-pool.h @@ -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);