I am looking to write a very simple memory pool library to store only
one data type at a time - i.e. to provide a contiguous block of memory
to be alloc'd free'd by the calling program. I am
I have come up with this API so far and will welcome any feedback on it.
In particular, I will need help with implementing the linked lists used
for record keeping as to which blocks were free or not (and for
"defragging" the pool when "holes" appear in the contiguous block)
This is what I've come up with so far. Comments/feedback welcome.
struct mempool_t
{
unsigned char * pool_ ;
unsigned int block_size ;
/* variable that keeps track of whether an element is free or not */
unsigned char * available ;
};
/* Public API */
/* Creates the pool, N elements of size item_size. Return 0 on succes, 1
otherwise */
int mempool_create(struct mempool_t *pool, const unsigned int
num_elements, const unsigned int item_size);
/* requests mem chunk from pool, 0 ptr if no more mem or rqst could not
be fulfilled */
unsigned char * mempool_malloc(struct mempool_t *pool, const unsigned
int num_elements) ;
/* requests zero-inited mem chunk from pool, 0 ptr if no more mem or
rqst could not be fulfilled */
unsigned char * mempool_calloc(struct mempool_t *pool, const unsigned
int num_elements) ;
/* requests mem chunk realloc from pool, 0 ptr if no more mem or rqst
could not be fulfilled */
unsigned char * mempool_realloc(struct mempool_t *pool, const unsigned
int num_elements) ;
/* requests mempool resize (expand only) 0 if succesful, 1 otherwise */
int mempool_resize(struct mempool_t **pool, const unsigned int
num_elements) ;
/* Local (static) functions */
int mempool_defrag(struct mempool_t * pool);