#include <string.h>
#include <stdlib.h>
#include "test_helpers.h"
TEST_FUNCTION_START(memory_manager, state)
{
int result;
size_t alignment;
void * (* fmalloc)(size_t);
void * (* fcalloc)(size_t, size_t);
void * (* frealloc)(void *, size_t);
void (* ffree)(void *);
void * (* faligned_alloc)(size_t, size_t);
void (* faligned_free)(void *);
for (alignment = sizeof(ulong); alignment < (WORD(1) << 14); alignment <<= 1)
{
slong ix;
for (ix = 0; ix < 10 * flint_test_multiplier(); ix++)
{
size_t size;
void * ptr;
size = alignment * (1 + n_randint(state, 100));
ptr = flint_aligned_alloc(alignment, size);
memset(ptr, 0, size);
flint_aligned_free(ptr);
}
}
__flint_set_memory_functions(malloc, calloc, realloc, free);
__flint_get_memory_functions(&fmalloc, &fcalloc, &frealloc, &ffree);
result = (fmalloc == malloc
&& fcalloc == calloc
&& frealloc == realloc
&& ffree == free);
if (!result)
TEST_FUNCTION_FAIL("Non-standard non-aligned memory functions are different.");
for (alignment = sizeof(ulong); alignment < (WORD(1) << 14); alignment <<= 1)
{
slong ix;
for (ix = 0; ix < 10 * flint_test_multiplier(); ix++)
{
size_t size;
void * ptr;
size = alignment * (1 + n_randint(state, 100));
ptr = flint_aligned_alloc(alignment, size);
memset(ptr, 0, size);
flint_aligned_free(ptr);
}
}
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
# define aligned_alloc _aligned_malloc
# define aligned_free _aligned_free
#else
# define aligned_free free
#endif
__flint_set_all_memory_functions(malloc, calloc, realloc, free, aligned_alloc, aligned_free);
__flint_get_all_memory_functions(&fmalloc, &fcalloc, &frealloc, &ffree, &faligned_alloc, &faligned_free);
result = (fmalloc == malloc
&& fcalloc == calloc
&& frealloc == realloc
&& ffree == free
&& faligned_alloc == aligned_alloc
&& faligned_free == aligned_free);
if (!result)
TEST_FUNCTION_FAIL("Non-standard aligned memory functions are different.");
TEST_FUNCTION_END(state);
}