Memory Pool C Library Source

Download Version 2.1.0 (5/31/2006)

This is a memory pool library managed multiple heaps that can be allocated and destroyed without fragmenting memory. You can have multiple heaps and reset them easily completely reclaiming the memory (as opposed to standard heaps).

With it you can mpool_open() a new heap, then mpool_alloc(), mpool_calloc(), mpool_realloc(), mpool_free() to your heart's content. Once you are done with the memory-pool you can run mpool_clear() or mpool_close() and completely remove the memory associated with the pools. This is very handy if you are working with some large blocks of memory and want to reset back to a clean state.

Check out the mpool.h file for more information. Sorry for minimal docs. If you have any questions or problems feel free to send me mail.

Gray Watson

Functions

mpool_t *mpool_open(const unsigned int flags, const unsigned int page_size, void *start_addr, int *error_p)
Open/allocate a new memory pool.
int mpool_close(mpool_t *mp_p)
Close/free a memory allocation pool.
int mpool_clear(mpool_t *mp_p)
Wipe a memory pool clean so we can start again.
void *mpool_alloc(mpool_t *mp_p, const unsigned long byte_size, int *error_p)
Allocate space for bytes inside of an already open memory pool.
void *mpool_calloc(mpool_t *mp_p, const unsigned long ele_n, const unsigned long ele_size, int *error_p)
Allocate space for elements of bytes in the memory pool and zero the space afterwards.
int mpool_free(mpool_t *mp_p, void *addr, const unsigned long size)
Free an address from a memory pool. This is different from normal free because it needs the addresses size. Future versions of the library will not have that restriction.
void *mpool_resize(mpool_t *mp_p, void *old_addr, const unsigned long old_byte_size, const unsigned long new_byte_size, int *error_p)
Reallocate an address in a memory pool to a new size. This is different from realloc in that it needs the old address' size. If you don't have it then you need to allocate new space, copy the data, and free the old pointer yourself.
int mpool_stats(const mpool_t *mp_p, unsigned int *page_size_p, unsigned long *num_alloced_p, unsigned long *user_alloced_p, unsigned long *max_alloced_p, unsigned long *tot_alloced_p)
Return stats from the memory pool.
int mpool_set_log_func(mpool_t *mp_p, mpool_log_func_t log_func)
Set the Return stats from the memory pool.
int mpool_set_max_pages(mpool_t *mp_p, const unsigned int max_pages)
Set the maximum number of pages that the library will use. Once it hits the limit it will return MPOOL_ERROR_NO_PAGES.

NOTE: if the MPOOL_FLAG_HEAVY_PACKING is set then this max-pages value will include the page with the mpool header structure in it. If the flag is _not_ set then the max-pages will not include this first page.

const char *mpool_strerror(const int error)
Return the corresponding string for the error number.

More 256 Sources
Gray Watson Land