A single change for the mempool and a patch for the objpool.

Object Pool Patch: if you deplete the object pool to 0 free blocks and then free back one block, the last given block will be rejected because it was exactly at the memory holding the entire pool.
Mempool change: switched memory aligning the size from the constructor to when allocating.
This commit is contained in:
Kevin Yonan 2019-07-12 18:44:39 -07:00 committed by GitHub
parent 43d8aab0fd
commit ad9b1d990c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,7 +28,7 @@ struct MemPool MemPool_Create(const size_t size)
return mempool;
else {
// align the mempool size to at least the size of an alloc node.
mempool.stack.size = __AlignSize(size, sizeof(struct MemNode));
mempool.stack.size = size;
mempool.stack.mem = malloc(1 + mempool.stack.size*sizeof *mempool.stack.mem);
if (mempool.stack.mem==NULL) {
mempool.stack.size = 0UL;
@ -69,7 +69,7 @@ void *MemPool_Alloc(struct MemPool *const mempool, const size_t size)
return NULL;
else {
struct MemNode *new_mem = NULL;
const size_t ALLOC_SIZE = size + sizeof *new_mem;
const size_t ALLOC_SIZE = __AlignSize(size + sizeof *new_mem, sizeof(intptr_t));
if (mempool->freeList.head != NULL) {
const size_t MEM_SPLIT_THRESHOLD = sizeof(intptr_t);
// if the freelist is valid, let's allocate FROM the freelist then!
@ -407,7 +407,7 @@ void *ObjPool_Alloc(struct ObjPool *const objpool)
void ObjPool_Free(struct ObjPool *const restrict objpool, void *ptr)
{
union ObjInfo p = { .byte = ptr };
if (objpool==NULL || ptr==NULL || p.byte <= objpool->stack.mem || p.byte > objpool->stack.mem + objpool->stack.size*objpool->objSize)
if (objpool==NULL || ptr==NULL || p.byte < objpool->stack.mem || p.byte > objpool->stack.mem + objpool->stack.size*objpool->objSize)
return;
else {
// when we free our Bointer, we recycle the pointer space to store the previous index