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) {
116 memset(p,0,
sizeof(
Pool));
127 if (p->
Init == NULL) {
128 p->
Init = PoolMemset;
142 for (u32 = 0; u32 < size; u32++) {
159 for (u32 = 0; u32 < prealloc_size; u32++) {
173 if (pb->
data == NULL) {
236 if (!PoolDataPreAllocated(p, pb->
data)) {
251 if (pb->
data!= NULL) {
254 if (!PoolDataPreAllocated(p, pb->
data)) {
276 printf(
"\n----------- Hash Table Stats ------------\n");
278 printf(
"-----------------------------------------\n");
300 if (p->
Alloc != NULL) {
329 void *ptr = pb->
data;
353 if (!PoolDataPreAllocated(p, data)) {
360 SCLogDebug(
"tried to return data %p to the pool %p, but no more "
361 "buckets available. Just freeing the data.", data, p);
382 SCLogDebug(
"pool %p is using %" PRIu32
" out of %" PRIu32
" items (%02.1f%%), max %" PRIu32
383 " (%02.1f%%): pool struct memory %" PRIu64
".",
386 (
float)(p->max_outstanding) / (
float)(p->
max_buckets) * 100,
396 static void *PoolTestAlloc(
void)
403 static int PoolTestInitArg(
void *data,
void *allocdata)
405 size_t len = strlen((
char *)allocdata) + 1;
412 static void PoolTestFree(
void *ptr)
417 static int PoolTestInit01 (
void)
419 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
427 static int PoolTestInit02 (
void)
431 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
436 printf(
"list(s) not properly initialized (a:%p e:%p): ",
442 if (p->
Alloc != PoolTestAlloc) {
443 printf(
"Alloc func ptr %p != %p: ",
444 p->
Alloc, PoolTestAlloc);
449 if (p->
Cleanup != PoolTestFree) {
450 printf(
"Free func ptr %p != %p: ",
463 static int PoolTestInit03 (
void)
468 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
474 printf(
"PoolGet returned NULL: ");
498 static int PoolTestInit04 (
void)
503 Pool *p =
PoolInit(10,5,strlen(
"test") + 1,NULL, PoolTestInitArg,(
void *)
"test",PoolTestFree, NULL);
509 printf(
"PoolGet returned NULL: ");
514 if (strcmp(
str,
"test") != 0) {
515 printf(
"Memory not properly initialized: ");
539 static int PoolTestInit05 (
void)
544 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL, NULL,PoolTestFree, NULL);
550 printf(
"PoolGet returned NULL: ");
589 static int PoolTestInit06 (
void)
595 Pool *p =
PoolInit(1,0,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
600 printf(
"p->allocated 0 != %" PRIu32
": ", p->
allocated);
607 printf(
"PoolGet returned NULL: ");
613 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
620 printf(
"PoolGet returned %p, expected NULL: ", data2);
629 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
648 static int PoolTestInit07 (
void)
654 Pool *p =
PoolInit(0,1,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
659 printf(
"p->max_buckets 0 != %" PRIu32
": ", p->
max_buckets);
665 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
672 printf(
"PoolGet returned NULL: ");
678 printf(
"(2) p->allocated 1 != %" PRIu32
": ", p->
allocated);
685 printf(
"PoolGet returned NULL: ");
691 printf(
"(3) p->allocated 2 != %" PRIu32
": ", p->
allocated);
700 printf(
"(4) p->allocated 2 != %" PRIu32
": ", p->
allocated);
715 printf(
"(5) p->allocated 1 != %" PRIu32
": ", p->
allocated);