Go to the first, previous, next, last section, table of contents.


Custom Allocation

By default, the MP functions use malloc, realloc, and free for memory allocation. If malloc or realloc fails, the MP library terminates execution after printing a fatal error message to standard error.

For some applications, you may wish to allocate memory in other ways, or you may not want to have a fatal error when there is no more memory available. To accomplish this, you can specify alternative memory allocation functions.

Function: void mp_set_memory_functions (
void *(*alloc_func_ptr) (size_t),
void *(*realloc_func_ptr) (void *, size_t, size_t),
void (*free_func_ptr) (void *, size_t))
Replace the current allocation functions from the arguments. If an argument is NULL, the corresponding default function is retained.

Make sure to call this function in such a way that there are no active MP objects that were allocated using the previously active allocation function! Usually, that means that you have to call this function before any other MP function.

The functions you supply should fit the following declarations:

Function: void * allocate_function (size_t alloc_size)
This function should return a pointer to newly allocated space with at least alloc_size storage units.

Function: void * reallocate_function (void *ptr, size_t old_size, size_t new_size)
This function should return a pointer to newly allocated space of at least new_size storage units, after copying at least the first old_size storage units from ptr. It should also de-allocate the space at ptr.

You can assume that the space at ptr was formerly returned from allocate_function or reallocate_function, for a request for old_size storage units.

Function: void deallocate_function (void *ptr, size_t size)
De-allocate the space pointed to by ptr.

You can assume that the space at ptr was formerly returned from allocate_function or reallocate_function, for a request for size storage units.

(A storage unit is the unit in which the sizeof operator returns the size of an object, normally an 8 bit byte.)


Go to the first, previous, next, last section, table of contents.