pool: small code cleanups

pull/3534/head
Victor Julien 7 years ago
parent 478caac975
commit 7ce1ebe0d3

@ -81,7 +81,9 @@ static int PoolDataPreAllocated(Pool *p, void *data)
* \param Free free func * \param Free free func
* \retval the allocated Pool * \retval the allocated Pool
*/ */
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 *)) 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 *))
{ {
Pool *p = NULL; Pool *p = NULL;
@ -129,8 +131,8 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
SCLogError(SC_ERR_POOL_INIT, "alloc error"); SCLogError(SC_ERR_POOL_INIT, "alloc error");
goto error; goto error;
} }
p->pb_buffer = pb;
memset(pb, 0, size * sizeof(PoolBucket)); memset(pb, 0, size * sizeof(PoolBucket));
p->pb_buffer = pb;
for (u32 = 0; u32 < size; u32++) { for (u32 = 0; u32 < size; u32++) {
/* populate pool */ /* populate pool */
pb->next = p->empty_stack; pb->next = p->empty_stack;
@ -139,9 +141,7 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
p->empty_stack_size++; p->empty_stack_size++;
pb++; pb++;
} }
}
if (size > 0) {
p->data_buffer = SCCalloc(prealloc_size, elt_size); p->data_buffer = SCCalloc(prealloc_size, elt_size);
/* FIXME better goto */ /* FIXME better goto */
if (p->data_buffer == NULL) { if (p->data_buffer == NULL) {
@ -157,7 +157,6 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
SCLogError(SC_ERR_POOL_INIT, "alloc error"); SCLogError(SC_ERR_POOL_INIT, "alloc error");
goto error; goto error;
} }
memset(pb, 0, sizeof(PoolBucket)); memset(pb, 0, sizeof(PoolBucket));
if (p->Alloc) { if (p->Alloc) {
@ -221,7 +220,6 @@ error:
return NULL; return NULL;
} }
void PoolFree(Pool *p) void PoolFree(Pool *p)
{ {
if (p == NULL) if (p == NULL)
@ -314,10 +312,11 @@ void *PoolGet(Pool *p)
} }
p->allocated++; p->allocated++;
p->outstanding++; p->outstanding++;
#ifdef DEBUG
if (p->outstanding > p->max_outstanding) if (p->outstanding > p->max_outstanding)
p->max_outstanding = p->outstanding; p->max_outstanding = p->outstanding;
#endif
} }
SCReturnPtr(pitem, "void"); SCReturnPtr(pitem, "void");
@ -329,8 +328,10 @@ void *PoolGet(Pool *p)
void *ptr = pb->data; void *ptr = pb->data;
pb->data = NULL; pb->data = NULL;
p->outstanding++; p->outstanding++;
#ifdef DEBUG
if (p->outstanding > p->max_outstanding) if (p->outstanding > p->max_outstanding)
p->max_outstanding = p->outstanding; p->max_outstanding = p->outstanding;
#endif
SCReturnPtr(ptr,"void"); SCReturnPtr(ptr,"void");
} }

@ -66,7 +66,9 @@ typedef struct Pool_ {
uint32_t elt_size; uint32_t elt_size;
uint32_t outstanding; /**< counter of data items 'in use'. Pretty much uint32_t outstanding; /**< counter of data items 'in use'. Pretty much
* the diff between PoolGet and PoolReturn */ * the diff between PoolGet and PoolReturn */
#ifdef DEBUG
uint32_t max_outstanding; /**< max value of outstanding we saw */ uint32_t max_outstanding; /**< max value of outstanding we saw */
#endif
} Pool; } Pool;
/* prototypes */ /* prototypes */

Loading…
Cancel
Save