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


Custom Allocation

By default, GMP uses malloc, realloc and free for memory allocation. If malloc or realloc fails, GMP prints a message to the standard error output and terminates execution.

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

This can be done in the Berkeley compatibility library as well as the main GMP library.

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.

Be sure to call this function only when there are no active GMP objects allocated using the previous memory functions! Usually, that means that you have to call this function before any other GMP 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.