Click or drag to resize
gmp_libmp_set_memory_functions Method
Replace the current allocation functions from the arguments.

Namespace:  Math.Gmp.Native
Assembly:  Math.Gmp.Native (in Math.Gmp.Native.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public static void mp_set_memory_functions(
	allocate_function alloc_func_ptr,
	reallocate_function realloc_func_ptr,
	free_function free_func_ptr
)

Parameters

alloc_func_ptr
Type: Math.Gmp.Nativeallocate_function
The new memory allocation function.
realloc_func_ptr
Type: Math.Gmp.Nativereallocate_function
The new memory reallocation function.
free_func_ptr
Type: Math.Gmp.Nativefree_function
The new memory de-allocation function.
Remarks

If an argument is null (Nothing in VB.NET), the corresponding default function is used.

Examples
// Retrieve GMP default memory allocation functions.
allocate_function default_allocate = null;
reallocate_function default_reallocate = null;
free_function default_free = null;
gmp_lib.mp_get_memory_functions(ref default_allocate, ref default_reallocate, ref default_free);

// Create and set new memory allocation functions that count the number of times they are called.
int counter = 0;
allocate_function new_allocate = (size_t alloc_size) => { counter++; return default_allocate(alloc_size); };
reallocate_function new_reallocate = (void_ptr ptr, size_t old_size, size_t new_size) => { counter++; return default_reallocate(ptr, old_size, new_size); };
free_function new_free = (void_ptr ptr, size_t size) => { counter++; default_free(ptr, size); };
gmp_lib.mp_set_memory_functions(new_allocate, new_reallocate, new_free);

// Retrieve GMP memory allocation functions.
allocate_function allocate = null;
reallocate_function reallocate = null;
free_function free = null;
gmp_lib.mp_get_memory_functions(ref allocate, ref reallocate, ref free);

// Call memory function and assert calls count.
void_ptr p = allocate(10);
Assert.IsTrue(counter == 1);

reallocate(p, 10, 20);
Assert.IsTrue(counter == 2);

free(p, 20);
Assert.IsTrue(counter == 3);

// Restore default memory allocation functions.
gmp_lib.mp_set_memory_functions(null, null, null);
See Also