Memory pools are designed for efficient dynamic memory management when you have a fixed number of fixed size objects you need to manage. A large chunk of memory must first be allocated by traditional means before passing it to the pool API. Once received however, the memory is managed according to the restrictions described above, without worrying about fragmentation or other traditional dynamic memory management concerns.
To access the pool API, include the following header file:
#include <parlib/pool.h>
struct pool;
typedef struct pool pool_t;
void pool_init(pool_t *pool, void* buffer, void **object_queue,
size_t num_objects, size_t object_size);
size_t pool_size(pool_t *pool);
size_t pool_available(pool_t *pool);
void* pool_alloc(pool_t *pool);
int pool_free(pool_t* pool, void *object);
Initialize a pool. All memory MUST be allocated externally. The pool implementation simply manages the objects contained in the buffer passed to this function. This allows us to use the same pool implementation for both statically and dynamically allocated memory.