static int PoolTestInit02 (void) { int retval = 0; Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree); if (p == NULL) goto end; if (p->alloc_list == NULL || p->empty_list == NULL) { printf("list(s) not properly initialized (a:%p e:%p): ", p->alloc_list, p->empty_list); retval = 0; goto end; } if (p->Alloc != PoolTestAlloc) { printf("Alloc func ptr %p != %p: ", p->Alloc, PoolTestAlloc); retval = 0; goto end; } if (p->Cleanup != PoolTestFree) { printf("Free func ptr %p != %p: ", p->Cleanup, PoolTestFree); retval = 0; goto end; } retval = 1; end: if (p != NULL) PoolFree(p); return retval; }
/** \brief Populate lua states pool * * \param num keyword instances * \param reloads bool indicating we have rule reloads enabled */ int DetectLuajitSetupStatesPool(int num, int reloads) { int retval = 0; pthread_mutex_lock(&luajit_states_lock); if (luajit_states == NULL) { int cnt = 0; char *conf_val = NULL; if ((ConfGet("detect-engine.luajit-states", &conf_val)) == 1) { cnt = (int)atoi(conf_val); } else { int cpus = UtilCpuGetNumProcessorsOnline(); if (cpus == 0) { 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, NULL, LuaStatePoolFree); if (luajit_states == NULL) { SCLogError(SC_ERR_LUAJIT_ERROR, "luastate pool init failed, luajit keywords won't work"); retval = -1; } } pthread_mutex_unlock(&luajit_states_lock); return retval; }
static int PoolTestInit03 (void) { int retval = 0; void *data = NULL; Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree); if (p == NULL) goto end; data = PoolGet(p); if (data == NULL) { printf("PoolGet returned NULL: "); retval = 0; goto end; } if (p->alloc_list_size != 4) { printf("p->alloc_list_size 4 != %" PRIu32 ": ", p->alloc_list_size); retval = 0; goto end; } if (p->empty_list_size != 6) { printf("p->empty_list_size 6 != %" PRIu32 ": ", p->empty_list_size); retval = 0; goto end; } retval = 1; end: if (p != NULL) PoolFree(p); return retval; }
Res PoolCreate(Pool *poolReturn, Arena arena, PoolClass klass, ArgList args) { Res res; Pool pool; void *base; AVER(poolReturn != NULL); AVERT(Arena, arena); AVERT(PoolClass, klass); /* .space.alloc: Allocate the pool instance structure with the size */ /* requested in the pool class. See .space.free */ res = ControlAlloc(&base, arena, klass->size); if (res != ResOK) goto failControlAlloc; pool = (Pool)base; /* Initialize the pool. */ res = PoolInit(pool, arena, klass, args); if (res != ResOK) goto failPoolInit; *poolReturn = pool; return ResOK; failPoolInit: ControlFree(arena, base, klass->size); failControlAlloc: return res; }
static int PoolTestInit06 (void) { int retval = 0; void *data = NULL; void *data2 = NULL; Pool *p = PoolInit(1,0,PoolTestAlloc,NULL,PoolTestFree); if (p == NULL) goto end; if (p->allocated != 0) { printf("p->allocated 0 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } data = PoolGet(p); if (data == NULL) { printf("PoolGet returned NULL: "); retval = 0; goto end; } if (p->allocated != 1) { printf("p->allocated 1 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } data2 = PoolGet(p); if (data2 != NULL) { printf("PoolGet returned %p, expected NULL: ", data2); retval = 0; goto end; } PoolReturn(p,data); data = NULL; if (p->allocated != 1) { printf("p->allocated 1 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } if (p->alloc_list_size != 1) { printf("p->alloc_list_size 1 != %" PRIu32 ": ", p->alloc_list_size); retval = 0; goto end; } retval = 1; end: if (data != NULL) SCFree(data); if (data2 != NULL) SCFree(data2); if (p != NULL) PoolFree(p); return retval; }
static int PoolTestInit01 (void) { Pool *p = PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree); if (p == NULL) return 0; PoolFree(p); return 1; }
void StreamMsgQueuesInit(void) { #ifdef DEBUG SCMutexInit(&stream_pool_memuse_mutex, NULL); #endif SCMutexLock(&stream_msg_pool_mutex); stream_msg_pool = PoolInit(0,250,sizeof(StreamMsg),NULL,StreamMsgInit,NULL,NULL); if (stream_msg_pool == NULL) exit(EXIT_FAILURE); /* XXX */ SCMutexUnlock(&stream_msg_pool_mutex); }
void StreamMsgQueuesInit(uint32_t prealloc) { #ifdef DEBUG SCMutexInit(&stream_pool_memuse_mutex, NULL); #endif SCMutexLock(&stream_msg_pool_mutex); stream_msg_pool = PoolInit(0, prealloc, 0, StreamMsgPoolAlloc,StreamMsgInit, NULL,NULL,StreamMsgPoolFree); if (stream_msg_pool == NULL) exit(EXIT_FAILURE); /* XXX */ SCMutexUnlock(&stream_msg_pool_mutex); }
PoolThread *PoolThreadInit(int threads, 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 *)) { PoolThread *pt = NULL; int i; if (threads <= 0) { SCLogDebug("error"); goto error; } pt = SCMalloc(sizeof(*pt)); if (unlikely(pt == NULL)) { SCLogDebug("memory alloc error"); goto error; } SCLogDebug("size %d", threads); pt->array = SCMalloc(threads * sizeof(PoolThreadElement)); if (pt->array == NULL) { SCLogDebug("memory alloc error"); goto error; } pt->size = threads; for (i = 0; i < threads; i++) { PoolThreadElement *e = &pt->array[i]; SCMutexInit(&e->lock, NULL); SCMutexLock(&e->lock); // SCLogDebug("size %u prealloc_size %u elt_size %u Alloc %p Init %p InitData %p Cleanup %p Free %p", // size, prealloc_size, elt_size, // Alloc, Init, InitData, Cleanup, Free); e->pool = PoolInit(size, prealloc_size, elt_size, Alloc, Init, InitData, Cleanup, Free); SCMutexUnlock(&e->lock); if (e->pool == NULL) { SCLogDebug("error"); goto error; } } return pt; error: if (pt != NULL) PoolThreadFree(pt); return NULL; }
int uTaskCtor( void ) { DBG_MSG(DBG_TRACE, "%s\n", __FUNCTION__); memset(&gCore, 0, sizeof(gCore)); QUEUE_INIT(gCore.IsrQ); TcbInit(); PoolInit(); gCore.Flags = CORE_FLAGS_INIT; return UTASK_S_OK; }
static int PoolTestInit04 (void) { int retval = 0; char *str = NULL; Pool *p = PoolInit(10,5,PoolTestAllocArg,(void *)"test",PoolTestFree); if (p == NULL) goto end; str = PoolGet(p); if (str == NULL) { printf("PoolGet returned NULL: "); retval = 0; goto end; } if (strcmp(str, "test") != 0) { printf("Memory not properly initialized: "); retval = 0; goto end; } if (p->alloc_list_size != 4) { printf("p->alloc_list_size 4 != %" PRIu32 ": ", p->alloc_list_size); retval = 0; goto end; } if (p->empty_list_size != 6) { printf("p->empty_list_size 6 != %" PRIu32 ": ", p->empty_list_size); retval = 0; goto end; } retval = 1; end: if (str != NULL) SCFree(str); if (p != NULL) PoolFree(p); return retval; }
int PoolThreadGrow(PoolThread *pt, 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 *)) { void *ptmp; size_t newsize; PoolThreadElement *e = NULL; if (pt == NULL || pt->array == NULL) { SCLogError(SC_ERR_POOL_INIT, "pool grow failed"); return -1; } newsize = pt->size + 1; SCLogDebug("newsize %lu", newsize); ptmp = SCRealloc(pt->array, (newsize * sizeof(PoolThreadElement))); if (ptmp == NULL) { SCFree(pt->array); pt->array = NULL; SCLogError(SC_ERR_POOL_INIT, "pool grow failed"); return -1; } pt->array = ptmp; pt->size = newsize; e = &pt->array[newsize - 1]; memset(e, 0x00, sizeof(*e)); SCMutexInit(&e->lock, NULL); SCMutexLock(&e->lock); e->pool = PoolInit(size, prealloc_size, elt_size, Alloc, Init, InitData, Cleanup, Free); SCMutexUnlock(&e->lock); if (e->pool == NULL) { SCLogError(SC_ERR_POOL_INIT, "pool grow failed"); return -1; } return (int)(newsize - 1); }
/** \test pool with unlimited size */ static int PoolTestInit07 (void) { int retval = 0; void *data = NULL; void *data2 = NULL; Pool *p = PoolInit(0,1,10,PoolTestAlloc,NULL,NULL,PoolTestFree); if (p == NULL) goto end; if (p->max_buckets != 0) { printf("p->max_buckets 0 != %" PRIu32 ": ", p->max_buckets); retval = 0; goto end; } if (p->allocated != 1) { printf("p->allocated 1 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } data = PoolGet(p); if (data == NULL) { printf("PoolGet returned NULL: "); retval = 0; goto end; } if (p->allocated != 1) { printf("(2) p->allocated 1 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } data2 = PoolGet(p); if (data2 == NULL) { printf("PoolGet returned NULL: "); retval = 0; goto end; } if (p->allocated != 2) { printf("(3) p->allocated 2 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } PoolReturn(p,data); data = NULL; if (p->allocated != 2) { printf("(4) p->allocated 2 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } if (p->alloc_list_size != 1) { printf("p->alloc_list_size 1 != %" PRIu32 ": ", p->alloc_list_size); retval = 0; goto end; } PoolReturn(p,data2); data2 = NULL; if (p->allocated != 1) { printf("(5) p->allocated 1 != %" PRIu32 ": ", p->allocated); retval = 0; goto end; } retval = 1; end: if (p != NULL) PoolFree(p); return retval; }