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)) {
273 printf(
"\n----------- Hash Table Stats ------------\n");
275 printf(
"-----------------------------------------\n");
297 if (p->
Alloc != NULL) {
326 void *ptr = pb->
data;
350 if (!PoolDataPreAllocated(p, data)) {
357 SCLogDebug(
"tried to return data %p to the pool %p, but no more "
358 "buckets available. Just freeing the data.", data, p);
379 SCLogDebug(
"pool %p is using %" PRIu32
" out of %" PRIu32
" items (%02.1f%%), max %" PRIu32
380 " (%02.1f%%): pool struct memory %" PRIu64
".",
383 (
float)(p->max_outstanding) / (
float)(p->
max_buckets) * 100,
393 static void *PoolTestAlloc(
void)
400 static int PoolTestInitArg(
void *data,
void *allocdata)
402 size_t len = strlen((
char *)allocdata) + 1;
409 static void PoolTestFree(
void *ptr)
414 static int PoolTestInit01 (
void)
416 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
424 static int PoolTestInit02 (
void)
428 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
433 printf(
"list(s) not properly initialized (a:%p e:%p): ",
439 if (p->
Alloc != PoolTestAlloc) {
440 printf(
"Alloc func ptr %p != %p: ",
441 p->
Alloc, PoolTestAlloc);
446 if (p->
Cleanup != PoolTestFree) {
447 printf(
"Free func ptr %p != %p: ",
460 static int PoolTestInit03 (
void)
465 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
471 printf(
"PoolGet returned NULL: ");
495 static int PoolTestInit04 (
void)
500 Pool *p =
PoolInit(10,5,strlen(
"test") + 1,NULL, PoolTestInitArg,(
void *)
"test",PoolTestFree, NULL);
506 printf(
"PoolGet returned NULL: ");
511 if (strcmp(
str,
"test") != 0) {
512 printf(
"Memory not properly initialized: ");
536 static int PoolTestInit05 (
void)
541 Pool *p =
PoolInit(10,5,10,PoolTestAlloc,NULL, NULL,PoolTestFree, NULL);
547 printf(
"PoolGet returned NULL: ");
586 static int PoolTestInit06 (
void)
592 Pool *p =
PoolInit(1,0,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
597 printf(
"p->allocated 0 != %" PRIu32
": ", p->
allocated);
604 printf(
"PoolGet returned NULL: ");
610 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
617 printf(
"PoolGet returned %p, expected NULL: ", data2);
626 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
645 static int PoolTestInit07 (
void)
651 Pool *p =
PoolInit(0,1,10,PoolTestAlloc,NULL,NULL,PoolTestFree, NULL);
656 printf(
"p->max_buckets 0 != %" PRIu32
": ", p->
max_buckets);
662 printf(
"p->allocated 1 != %" PRIu32
": ", p->
allocated);
669 printf(
"PoolGet returned NULL: ");
675 printf(
"(2) p->allocated 1 != %" PRIu32
": ", p->
allocated);
682 printf(
"PoolGet returned NULL: ");
688 printf(
"(3) p->allocated 2 != %" PRIu32
": ", p->
allocated);
697 printf(
"(4) p->allocated 2 != %" PRIu32
": ", p->
allocated);
712 printf(
"(5) p->allocated 1 != %" PRIu32
": ", p->
allocated);