Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better validation and faster handling of pools. #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions ape_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,38 @@
#include <stdlib.h>
#include <stdio.h>

ape_pool_t *ape_new_pool(size_t size, size_t n)
ape_pool_t *ape_new_pool(size_t size, const size_t n)
{
unsigned int i;

size_t i;
const size_t stop = n - 1;
ape_pool_t * pool, *current;

if (size == 0) {
size = sizeof(ape_pool_t);
} else if ( n == 0 ) {
return NULL;
} else if (size < sizeof(ape_pool_t) ) {
return NULL;
}

ape_pool_t *pool = malloc(size * n), *current = NULL;
if (n == 0) {
return NULL;
}
current = pool = malloc(size * n);
pool->prev = NULL;

for (i = 0; i < n; i++) {
current = (ape_pool_t*)(((char *)&pool[0])+(i*size));
/* contiguous blocks */
current->next = (i == n-1 ? NULL : (ape_pool_t*)(((char *)&pool[0])+((i+1)*size)));
i = 0;
while(current != NULL) {
current->ptr.data = NULL;
current->flags = (i == 0 ? APE_POOL_ALLOC : 0);
if (current->next) {
current->flags = 0;
if (i == stop) {
current = current->next = NULL;
} else {
i++;
current->next = (ape_pool_t*) (( (char *)&pool[0]) + (i * size));
current->next->prev = current;
current = current->next;
}
}
pool->flags = APE_POOL_ALLOC;

return pool;
}
Expand Down