47 static int PoolMemset(
void *pitem,
void *initdata)
58 static bool PoolDataPreAllocated(
Pool *p,
void *data)
84 Pool *
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);
374 static void *PoolTestAlloc(
void)
381 static int PoolTestInitArg(
void *data,
void *allocdata)
383 size_t len = strlen((
char *)allocdata) + 1;
390 static void PoolTestFree(
void *ptr)
394 static int PoolTestInit01 (
void)
396 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
404 static 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: ",
440 static int PoolTestInit03 (
void)
445 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
451 printf(
"PoolGet returned NULL: ");
475 static 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: ");
516 static int PoolTestInit05 (
void)
521 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL, NULL,PoolTestFree, NULL);
527 printf(
"PoolGet returned NULL: ");
566 static 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);
625 static 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);