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);
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;
}
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 */
* \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;
goto error;
if (size != 0 && elt_size == 0)
goto error;
+ if (elt_size && Free)
+ goto error;
/* setup the filter */
p = SCMalloc(sizeof(Pool));
p->Init = Init;
p->InitData = InitData;
p->Cleanup = Cleanup;
+ p->Free = Free;
if (p->Init == NULL) {
p->Init = PoolMemset;
p->InitData = p;
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;
}
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) {
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 (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);
#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;
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;
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;
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;
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;
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;
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;