gmp_lib.mp_set_memory_functions Method

GMP Native Interface for .NET

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
)
Public Shared Sub mp_set_memory_functions ( 
	alloc_func_ptr As allocate_function,
	realloc_func_ptr As reallocate_function,
	free_func_ptr As free_function
)
public:
static void mp_set_memory_functions(
	allocate_function^ alloc_func_ptr, 
	reallocate_function^ realloc_func_ptr, 
	free_function^ free_func_ptr
)
static member mp_set_memory_functions : 
        alloc_func_ptr : allocate_function * 
        realloc_func_ptr : reallocate_function * 
        free_func_ptr : free_function -> unit 

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);
' Retrieve GMP default memory allocation functions.
Dim default_allocate As allocate_function = Nothing
Dim default_reallocate As reallocate_function = Nothing
Dim default_free As free_function = Nothing
gmp_lib.gmp_get_memory_functions(default_allocate, default_reallocate, default_free)

' Create and set new memory allocation functions that count the number of times they are called.
Dim counter As Integer = 0
Dim new_allocate As allocate_function =
    Function(alloc_size As size_t) 
        counter += 1
        Return default_allocate(alloc_size)
    End Function
Dim new_reallocate As reallocate_function =
    Function(ptr As void_ptr, old_size As size_t, new_size As size_t) 
        counter += 1
        Return default_reallocate(ptr, old_size, new_size)
    End Function
Dim new_free As free_function =
    Function(ptr As void_ptr, size As size_t) 
        counter += 1
        default_free(ptr, size)
    End Function
gmp_lib.gmp_set_memory_functions(new_allocate, new_reallocate, new_free)

' Retrieve GMP memory allocation functions.
Dim allocate As allocate_function = Nothing
Dim reallocate As reallocate_function = Nothing
Dim free As free_function = Nothing
gmp_lib.gmp_get_memory_functions(allocate, reallocate, free)

' Call memory function and assert calls count.
Dim p As void_ptr = 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.gmp_set_memory_functions(Nothing, Nothing, Nothing)

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

See Also