pool: add error msgs and improve memory layout

pull/414/head
Victor Julien 12 years ago
parent 46af6b7e0f
commit 016d03bdaf

@ -84,17 +84,25 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
{
Pool *p = NULL;
if (size != 0 && prealloc_size > size)
if (size != 0 && prealloc_size > size) {
SCLogError(SC_ERR_POOL_INIT, "size error");
goto error;
if (size != 0 && elt_size == 0)
}
if (size != 0 && elt_size == 0) {
SCLogError(SC_ERR_POOL_INIT, "size != 0 && elt_size == 0");
goto error;
if (elt_size && Free)
}
if (elt_size && Free) {
SCLogError(SC_ERR_POOL_INIT, "elt_size && Free");
goto error;
}
/* setup the filter */
p = SCMalloc(sizeof(Pool));
if (unlikely(p == NULL))
if (unlikely(p == NULL)) {
SCLogError(SC_ERR_POOL_INIT, "alloc error");
goto error;
}
memset(p,0,sizeof(Pool));
@ -116,8 +124,10 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
uint32_t u32 = 0;
if (size > 0) {
PoolBucket *pb = SCCalloc(size, sizeof(PoolBucket));
if (unlikely(pb == NULL))
if (unlikely(pb == NULL)) {
SCLogError(SC_ERR_POOL_INIT, "alloc error");
goto error;
}
p->pb_buffer = pb;
memset(pb, 0, size * sizeof(PoolBucket));
for (u32 = 0; u32 < size; u32++) {
@ -133,15 +143,19 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
if (size > 0) {
p->data_buffer = SCCalloc(prealloc_size, elt_size);
/* FIXME better goto */
if (p->data_buffer == NULL)
if (p->data_buffer == NULL) {
SCLogError(SC_ERR_POOL_INIT, "alloc error");
goto error;
}
}
/* prealloc the buckets and requeue them to the alloc list */
for (u32 = 0; u32 < prealloc_size; u32++) {
if (size == 0) { /* unlimited */
PoolBucket *pb = SCMalloc(sizeof(PoolBucket));
if (unlikely(pb == NULL))
if (unlikely(pb == NULL)) {
SCLogError(SC_ERR_POOL_INIT, "alloc error");
goto error;
}
memset(pb, 0, sizeof(PoolBucket));
@ -151,10 +165,12 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
pb->data = SCMalloc(p->elt_size);
}
if (pb->data == NULL) {
SCLogError(SC_ERR_POOL_INIT, "alloc error");
SCFree(pb);
goto error;
}
if (p->Init(pb->data, p->InitData) != 1) {
SCLogError(SC_ERR_POOL_INIT, "init error");
if (p->Cleanup)
p->Cleanup(pb->data);
if (p->Free)
@ -174,11 +190,14 @@ Pool *PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *
p->alloc_list_size++;
} else {
PoolBucket *pb = p->empty_list;
if (pb == NULL)
if (pb == NULL) {
SCLogError(SC_ERR_POOL_INIT, "alloc error");
goto error;
}
pb->data = (char *)p->data_buffer + u32 * elt_size;
if (p->Init(pb->data, p->InitData) != 1) {
SCLogError(SC_ERR_POOL_INIT, "init error");
if (p->Cleanup)
p->Cleanup(pb->data);
goto error;

@ -45,16 +45,16 @@ typedef struct Pool_ {
uint32_t preallocated;
uint32_t allocated;
uint32_t alloc_list_size;
PoolBucket *alloc_list;
PoolBucket *alloc_list_tail;
uint32_t alloc_list_size;
PoolBucket *empty_list;
uint32_t empty_list_size;
PoolBucket *pb_buffer;
void *data_buffer;
int data_buffer_size;
void *data_buffer;
PoolBucket *pb_buffer;
void *(*Alloc)();
int (*Init)(void *, void *);

Loading…
Cancel
Save