By default GMP uses
free for memory
allocation, and if they fail GMP prints a message to the standard error output
and terminates the program.
Alternate functions can be specified, to allocate memory in a different way or to have a different error action on running out of memory.
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
NULL, the corresponding default function is used.
These functions will be used for all memory allocation done by GMP, apart from
temporary space from
alloca if that function is available and GMP is
configured to use it (see Build Options).
Be sure to call
mp_set_memory_functions only when there are no
active GMP objects allocated using the previous memory functions! Usually
that means calling it before any other GMP function.
The functions supplied should fit the following declarations:
Return a pointer to newly allocated space with at least alloc_size bytes.
(void *ptr, size_t old_size, size_t new_size)¶
Resize a previously allocated block ptr of old_size bytes to be new_size bytes.
The block may be moved if necessary or if desired, and in that case the smaller of old_size and new_size bytes must be copied to the new location. The return value is a pointer to the resized block, that being the new location if moved or just ptr if not.
ptr is never
NULL, it’s always a previously allocated block.
new_size may be bigger or smaller than old_size.
(void *ptr, size_t size)¶
De-allocate the space pointed to by ptr.
ptr is never
NULL, it’s always a previously allocated block of
A byte here means the unit used by the
The reallocate_function parameter old_size and the
free_function parameter size are passed for convenience, but of
course they can be ignored if not needed by an implementation. The default
malloc and friends for instance don’t use them.
No error return is allowed from any of these functions, if they return then
they must have performed the specified operation. In particular note that
allocate_function or reallocate_function mustn’t return
Getting a different fatal error action is a good use for custom allocation
functions, for example giving a graphical dialog rather than the default print
stderr. How much is possible when genuinely out of memory is
another question though.
There’s currently no defined way for the allocation functions to recover from
an error such as out of memory, they must terminate program execution. A
longjmp or throwing a C++ exception will have undefined results. This
may change in the future.
GMP may use allocated blocks to hold pointers to other allocated blocks. This will limit the assumptions a conservative garbage collection scheme can make.
Since the default GMP allocation uses
malloc and friends, those
functions will be linked in even if the first thing a program does is an
mp_set_memory_functions. It’s necessary to change the GMP sources if
this is a problem.
void *(**alloc_func_ptr) (size_t),
void *(**realloc_func_ptr) (void *, size_t, size_t),
void (**free_func_ptr) (void *, size_t))
Get the current allocation functions, storing function pointers to the
locations given by the arguments. If an argument is
function pointer is not stored.
For example, to get just the current free function,
void (*freefunc) (void *, size_t); mp_get_memory_functions (NULL, NULL, &freefunc);