pool: remove InitData argument

Only used in unittests.

Update Init callback and update callers.
pull/15429/head
Victor Julien 4 months ago
parent dc05422769
commit 474788fd49

@ -161,8 +161,8 @@ DefragContextNew(void)
frag_pool_size = DEFAULT_DEFRAG_POOL_SIZE;
}
uint32_t frag_pool_prealloc = (uint32_t)frag_pool_size / 2;
dc->frag_pool = PoolInit(
(uint32_t)frag_pool_size, frag_pool_prealloc, sizeof(Frag), NULL, NULL, NULL, NULL);
dc->frag_pool =
PoolInit((uint32_t)frag_pool_size, frag_pool_prealloc, sizeof(Frag), NULL, NULL, NULL);
if (dc->frag_pool == NULL) {
FatalError("Defrag: Failed to initialize fragment pool.");
}

@ -309,7 +309,7 @@ static void *TcpSegmentPoolAlloc(void)
return seg;
}
static int TcpSegmentPoolInit(void *data, void *initdata)
static int TcpSegmentPoolInit(void *data)
{
TcpSegment *seg = (TcpSegment *) data;
TcpSegmentPcapHdrStorage *pcap_hdr;

@ -418,7 +418,7 @@ static void *StreamTcpSessionPoolAlloc(void)
return ptr;
}
static int StreamTcpSessionPoolInit(void *data, void* initdata)
static int StreamTcpSessionPoolInit(void *data)
{
memset(data, 0, sizeof(TcpSession));
StreamTcpIncrMemuse((uint64_t)sizeof(TcpSession));

@ -42,7 +42,7 @@
* Other params are as for PoolInit()
*/
PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, uint32_t elt_size,
void *(*Alloc)(void), int (*Init)(void *, void *), void (*Cleanup)(void *))
void *(*Alloc)(void), int (*Init)(void *), void (*Cleanup)(void *))
{
sc_errno = SC_OK;
@ -76,7 +76,7 @@ PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, u
// 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, NULL, Cleanup);
e->pool = PoolInit(size, prealloc_size, elt_size, Alloc, Init, Cleanup);
SCMutexUnlock(&e->lock);
if (e->pool == NULL) {
SCLogDebug("error");
@ -124,7 +124,6 @@ int PoolThreadExpand(PoolThread *pt)
settings.elt_size = e->pool->elt_size;
settings.Alloc = e->pool->Alloc;
settings.Init = e->pool->Init;
settings.InitData = e->pool->InitData;
settings.Cleanup = e->pool->Cleanup;
SCMutexUnlock(&e->lock);
@ -133,7 +132,7 @@ int PoolThreadExpand(PoolThread *pt)
SCMutexInit(&e->lock, NULL);
SCMutexLock(&e->lock);
e->pool = PoolInit(settings.max_buckets, settings.preallocated, settings.elt_size,
settings.Alloc, settings.Init, settings.InitData, settings.Cleanup);
settings.Alloc, settings.Init, settings.Cleanup);
SCMutexUnlock(&e->lock);
if (e->pool == NULL) {
SCLogError("pool grow failed");
@ -235,16 +234,6 @@ static void *PoolThreadTestAlloc(void)
return data;
}
static
int PoolThreadTestInit(void *data, void *allocdata)
{
if (!data)
return 0;
memset(data, 0x00, sizeof(allocdata));
return 1;
}
static
void PoolThreadTestFree(void *data)
{
@ -262,7 +251,7 @@ static int PoolThreadTestInit01(void)
static int PoolThreadTestInit02(void)
{
PoolThread *pt = PoolThreadInit(4, /* threads */
10, 5, 10, PoolThreadTestAlloc, PoolThreadTestInit, PoolThreadTestFree);
10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
FAIL_IF(pt == NULL);
PoolThreadFree(pt);
PASS;
@ -287,7 +276,7 @@ static int PoolThreadTestGet01(void)
static int PoolThreadTestGet02(void)
{
PoolThread *pt = PoolThreadInit(4, /* threads */
10, 5, 10, PoolThreadTestAlloc, PoolThreadTestInit, PoolThreadTestFree);
10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
FAIL_IF_NULL(pt);
void *data = PoolThreadGetById(pt, 3);
@ -303,7 +292,7 @@ static int PoolThreadTestGet02(void)
static int PoolThreadTestReturn01(void)
{
PoolThread *pt = PoolThreadInit(4, /* threads */
10, 5, 10, PoolThreadTestAlloc, PoolThreadTestInit, PoolThreadTestFree);
10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
FAIL_IF_NULL(pt);
void *data = PoolThreadGetById(pt, 3);
@ -336,7 +325,7 @@ static int PoolThreadTestGrow01(void)
static int PoolThreadTestGrow02(void)
{
PoolThread *pt = PoolThreadInit(4, /* threads */
10, 5, 10, PoolThreadTestAlloc, PoolThreadTestInit, PoolThreadTestFree);
10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
FAIL_IF_NULL(pt);
FAIL_IF(PoolThreadExpand(pt) < 0);
@ -347,7 +336,7 @@ static int PoolThreadTestGrow02(void)
static int PoolThreadTestGrow03(void)
{
PoolThread *pt = PoolThreadInit(4, /* threads */
10, 5, 10, PoolThreadTestAlloc, PoolThreadTestInit, PoolThreadTestFree);
10, 5, 10, PoolThreadTestAlloc, NULL, PoolThreadTestFree);
FAIL_IF_NULL(pt);
FAIL_IF(PoolThreadExpand(pt) < 0);

@ -66,7 +66,7 @@ void PoolThreadRegisterTests(void);
* \param threads number of threads to use this
* \retval pt thread pool or NULL on error */
PoolThread *PoolThreadInit(int threads, uint32_t size, uint32_t prealloc_size, uint32_t elt_size,
void *(*Alloc)(void), int (*Init)(void *, void *), void (*Cleanup)(void *));
void *(*Alloc)(void), int (*Init)(void *), void (*Cleanup)(void *));
/** \brief grow a thread pool by one
* \note copies settings from initial PoolThreadInit() call

@ -45,14 +45,6 @@
#include "util-debug.h"
#include "util-validate.h"
static int PoolMemset(void *pitem, void *initdata)
{
Pool *p = (Pool *) initdata;
memset(pitem, 0, p->elt_size);
return 1;
}
/**
* \brief Check if data is preallocated
* \note make sure to call with nonnull pointers
@ -63,6 +55,15 @@ static bool PoolDataPreAllocated(Pool *p, void *data)
return delta >= 0 && delta <= p->data_buffer_size;
}
static bool PoolInitData(const Pool *p, void *data)
{
if (p->Init != NULL) {
return p->Init(data) == 1;
}
memset(data, 0, p->elt_size);
return true;
}
/** \brief Init a Pool
*
* PoolInit() creates a ::Pool. The Alloc function must only do
@ -75,12 +76,11 @@ static bool PoolDataPreAllocated(Pool *p, void *data)
* \param elt_size Memory size of an element
* \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
* \retval the allocated Pool
*/
Pool *PoolInit(const uint32_t size, const uint32_t prealloc_size, const uint32_t elt_size,
void *(*Alloc)(void), int (*Init)(void *, void *), void *InitData, void (*Cleanup)(void *))
void *(*Alloc)(void), int (*Init)(void *), void (*Cleanup)(void *))
{
sc_errno = SC_OK;
@ -112,12 +112,7 @@ Pool *PoolInit(const uint32_t size, const uint32_t prealloc_size, const uint32_t
p->data_buffer_size = prealloc_size * elt_size;
p->Alloc = Alloc;
p->Init = Init;
p->InitData = InitData;
p->Cleanup = Cleanup;
if (p->Init == NULL) {
p->Init = PoolMemset;
p->InitData = p;
}
/* alloc the buckets and place them in the empty list */
if (size > 0) {
@ -162,7 +157,7 @@ Pool *PoolInit(const uint32_t size, const uint32_t prealloc_size, const uint32_t
sc_errno = SC_ENOMEM;
goto error;
}
if (p->Init(pb->data, p->InitData) != 1) {
if (PoolInitData(p, pb->data) == false) {
SCFree(pb->data);
SCFree(pb);
sc_errno = SC_EINVAL;
@ -181,7 +176,7 @@ Pool *PoolInit(const uint32_t size, const uint32_t prealloc_size, const uint32_t
}
pb->data = (char *)p->data_buffer + i * elt_size;
if (p->Init(pb->data, p->InitData) != 1) {
if (PoolInitData(p, pb->data) == false) {
pb->data = NULL;
sc_errno = SC_EINVAL;
goto error;
@ -279,7 +274,7 @@ void *PoolGet(Pool *p)
}
if (pitem != NULL) {
if (p->Init(pitem, p->InitData) != 1) {
if (PoolInitData(p, pitem) == false) {
SCFree(pitem);
SCReturnPtr(NULL, "void");
}
@ -360,14 +355,6 @@ static void *PoolTestAlloc(void)
return NULL;
return ptr;
}
static int PoolTestInitArg(void *data, void *allocdata)
{
size_t len = strlen((char *)allocdata) + 1;
char *str = data;
if (str != NULL)
strlcpy(str,(char *)allocdata,len);
return 1;
}
static void PoolTestFree(void *ptr)
{
@ -375,7 +362,7 @@ static void PoolTestFree(void *ptr)
static int PoolTestInit01 (void)
{
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, NULL, PoolTestFree);
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, PoolTestFree);
FAIL_IF_NOT(p != NULL);
PoolFree(p);
@ -384,7 +371,7 @@ static int PoolTestInit01 (void)
static int PoolTestInit02 (void)
{
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, NULL, PoolTestFree);
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, PoolTestFree);
FAIL_IF_NOT(p != NULL);
FAIL_IF_NOT(p->alloc_stack != NULL);
@ -401,7 +388,7 @@ static int PoolTestInit02 (void)
static int PoolTestInit03 (void)
{
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, NULL, PoolTestFree);
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, PoolTestFree);
FAIL_IF_NOT(p != NULL);
void *data = PoolGet(p);
@ -417,15 +404,12 @@ static int PoolTestInit03 (void)
static int PoolTestInit04 (void)
{
Pool *p = PoolInit(
10, 5, strlen("test") + 1, NULL, PoolTestInitArg, (void *)"test", PoolTestFree);
Pool *p = PoolInit(10, 5, strlen("test") + 1, NULL, NULL, PoolTestFree);
FAIL_IF_NOT(p != NULL);
char *str = PoolGet(p);
FAIL_IF_NOT(str != NULL);
FAIL_IF_NOT(strcmp(str, "test") == 0);
FAIL_IF_NOT(p->alloc_stack_size == 4);
FAIL_IF_NOT(p->empty_stack_size == 6);
@ -436,7 +420,7 @@ static int PoolTestInit04 (void)
static int PoolTestInit05 (void)
{
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, NULL, PoolTestFree);
Pool *p = PoolInit(10, 5, 10, PoolTestAlloc, NULL, PoolTestFree);
FAIL_IF_NOT(p != NULL);
void *data = PoolGet(p);
@ -459,7 +443,7 @@ static int PoolTestInit05 (void)
static int PoolTestInit06 (void)
{
Pool *p = PoolInit(1, 0, 10, PoolTestAlloc, NULL, NULL, PoolTestFree);
Pool *p = PoolInit(1, 0, 10, PoolTestAlloc, NULL, PoolTestFree);
FAIL_IF_NOT(p != NULL);
FAIL_IF_NOT(p->allocated == 0);
@ -486,7 +470,7 @@ static int PoolTestInit06 (void)
/** \test pool with unlimited size */
static int PoolTestInit07 (void)
{
Pool *p = PoolInit(0, 1, 10, PoolTestAlloc, NULL, NULL, PoolTestFree);
Pool *p = PoolInit(0, 1, 10, PoolTestAlloc, NULL, PoolTestFree);
FAIL_IF_NOT(p != NULL);
FAIL_IF_NOT(p->max_buckets == 0);

@ -58,8 +58,7 @@ typedef struct Pool_ {
PoolBucket *pb_buffer;
void *(*Alloc)(void);
int (*Init)(void *, void *);
void *InitData;
int (*Init)(void *);
void (*Cleanup)(void *);
uint32_t elt_size;
@ -71,8 +70,8 @@ typedef struct Pool_ {
} Pool;
/* prototypes */
Pool *PoolInit(uint32_t, uint32_t, uint32_t, void *(*Alloc)(void), int (*Init)(void *, void *),
void *, void (*Cleanup)(void *));
Pool *PoolInit(uint32_t, uint32_t, uint32_t, void *(*Alloc)(void), int (*Init)(void *),
void (*Cleanup)(void *));
void PoolFree(Pool *);
void *PoolGet(Pool *);

Loading…
Cancel
Save