Fixed size memory pool implemented in C
The type of the handle for the memory pool is MP_handle
.
Returns the version number. The version number is formatted as AABBCC (or ABBCC), in which AA is the major version number for major feature, BB is the minor version number for interfaces and CC is the micro version number for bug fixes.
Initialization routine that returns the handle for the memory pool. The first parameter is the size of the fixed size memory chunk that the memory pool will allocates and the second parameter is the number of memory chunk initially allocated internally. When allocation failed, this routine returns NULL.
Finalization routine that free all memory.
Return a chunk of memory from memory pool.
Recycle the memory that allocated by MP_alloc
back to memory pool.
When MP_DEBUG
is defined, the memory address is checked for its validity. See
Debug mode for more detail.
- User does not have to call
MP_free
for every chunk of memory allocated byMP_alloc
. But user must callMP_finalize
to actually free the memory otherwise memory leak is followed. However, memory pool will increase its pool size when memory pool is fully occupied, so callingMP_free
to recycle unused memory can save actual memory allocation counts.
The debug mode can be activated by compiling with MP_DEBUG
macro defined. In
debug mode the MP_alloc
routine will encode unique bookkeeping information
after the end of the memory chunk for each memory of each memory pool instance.
So when MP_free
is called, it will check the bookkeeping information and
refuse to recycle the memory if information is incorrect and returns 1. This
error might usually happen in the following scenarios:
- Calling
MP_free
with memory allocated fromMP_alloc
of other memory pool. - Memory is corrupted because of out-of-range access.
- Calling
MP_free
more than once on the same memory.