Node:Custom Allocation, Next:Language Bindings, Previous:BSD Compatible Functions, Up:Top
By default GMP uses malloc
, realloc
and 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.
This feature is available in the Berkeley compatibility library (see BSD Compatible Functions) as well as the main GMP library.
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)) |
Function |
Replace the current allocation functions from the arguments. If an argument
is NULL , the corresponding default function is used.
These functions will be used for all memory allocation done by GMP, apart from
temporary space from Be sure to call |
The functions supplied should fit the following declarations:
void * allocate_function (size_t alloc_size) | Function |
Return a pointer to newly allocated space with at least alloc_size bytes. |
void * reallocate_function (void *ptr, size_t old_size, size_t new_size) | Function |
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 |
void deallocate_function (void *ptr, size_t size) | Function |
De-allocate the space pointed to by ptr.
ptr is never |
A byte here means the unit used by the sizeof
operator.
The old_size parameters to reallocate_function and
deallocate_function are passed for convenience, but of course can be
ignored if not needed. The default functions using 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
NULL
.
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
to 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.