47static int PoolMemset(
void *pitem,
void *initdata)
58static bool PoolDataPreAllocated(
Pool *p,
void *data)
84Pool *
PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size,
85 void *(*Alloc)(
void),
int (*Init)(
void *,
void *),
void *InitData,
86 void (*Cleanup)(
void *),
void (*Free)(
void *))
92 if (size != 0 && prealloc_size > size) {
96 if (size != 0 && elt_size == 0) {
100 if (elt_size && Free) {
104 if (elt_size == 0 && Alloc == NULL) {
125 if (p->
Init == NULL) {
126 p->
Init = PoolMemset;
140 for (u32 = 0; u32 < size; u32++) {
157 for (u32 = 0; u32 < prealloc_size; u32++) {
170 if (pb->
data == NULL) {
233 if (!PoolDataPreAllocated(p, pb->
data)) {
248 if (pb->
data!= NULL) {
251 if (!PoolDataPreAllocated(p, pb->
data)) {
290 if (p->
Alloc != NULL) {
319 void *ptr = pb->
data;
343 if (!PoolDataPreAllocated(p, data)) {
350 SCLogDebug(
"tried to return data %p to the pool %p, but no more "
351 "buckets available. Just freeing the data.", data, p);
374static void *PoolTestAlloc(
void)
381static int PoolTestInitArg(
void *data,
void *allocdata)
383 size_t len = strlen((
char *)allocdata) + 1;
390static void PoolTestFree(
void *ptr)
394static int PoolTestInit01 (
void)
396 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
404static int PoolTestInit02 (
void)
408 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
413 printf(
"list(s) not properly initialized (a:%p e:%p): ",
419 if (p->
Alloc != PoolTestAlloc) {
420 printf(
"Alloc func ptr %p != %p: ",
421 p->
Alloc, PoolTestAlloc);
426 if (p->
Cleanup != PoolTestFree) {
427 printf(
"Free func ptr %p != %p: ",
440static int PoolTestInit03 (
void)
445 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
451 printf(
"PoolGet returned NULL: ");
475static int PoolTestInit04 (
void)
480 Pool *p =
PoolInit(10,5,strlen(
"test") + 1,NULL, PoolTestInitArg,(
void *)
"test",PoolTestFree, NULL);
486 printf(
"PoolGet returned NULL: ");
491 if (strcmp(
str,
"test") != 0) {
492 printf(
"Memory not properly initialized: ");
516static int PoolTestInit05 (
void)
521 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL, NULL,PoolTestFree, NULL);
527 printf(
"PoolGet returned NULL: ");
566static int PoolTestInit06 (
void)
572 Pool *p =
PoolInit(1,0,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
577 printf(
"p->allocated 0 != %" PRIu32
": ", p->
allocated);
584 printf(
"PoolGet returned NULL: ");
590 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
597 printf(
"PoolGet returned %p, expected NULL: ", data2);
606 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
625static int PoolTestInit07 (
void)
631 Pool *p =
PoolInit(0,1,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
636 printf(
"p->max_buckets 0 != %" PRIu32
": ", p->
max_buckets);
642 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
649 printf(
"PoolGet returned NULL: ");
655 printf(
"(2) p->allocated 1 != %" PRIu32
": ", p->
allocated);
662 printf(
"PoolGet returned NULL: ");
668 printf(
"(3) p->allocated 2 != %" PRIu32
": ", p->
allocated);
677 printf(
"(4) p->allocated 2 != %" PRIu32
": ", p->
allocated);
692 printf(
"(5) p->allocated 1 != %" PRIu32
": ", p->
allocated);
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Pool * PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int(*Init)(void *, void *), void *InitData, void(*Cleanup)(void *), void(*Free)(void *))
Init a Pool.
void PoolReturn(Pool *p, void *data)
void PoolThreadRegisterTests(void)
void PoolRegisterTests(void)
struct PoolBucket_ * next
uint32_t alloc_stack_size
int(* Init)(void *, void *)
uint32_t empty_stack_size
size_t strlcpy(char *dst, const char *src, size_t siz)
#define SCReturnPtr(x, type)
thread_local SCError sc_errno
#define POOL_BUCKET_PREALLOCATED