diff --git a/CMake/cmake_config.h.in b/CMake/cmake_config.h.in index faa5d2591a..5257fb28b0 100644 --- a/CMake/cmake_config.h.in +++ b/CMake/cmake_config.h.in @@ -23,6 +23,13 @@ /* Just set to some reasonable threshold */ #define FLINT_FFT_SMALL_THRESHOLD 600 +/* NOTE: Here we assume this is how it works. */ +#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) +# define HAVE__ALIGNED_MALLOC 1 +#else +# define HAVE_ALIGNED_ALLOC 1 +#endif + #ifdef _MSC_VER # if defined(FLINT_BUILD_DLL) # define FLINT_DLL __declspec(dllexport) diff --git a/configure.ac b/configure.ac index 069bce7cfa..e5caf7b7de 100644 --- a/configure.ac +++ b/configure.ac @@ -888,6 +888,8 @@ AC_MSG_ERROR([Couldn't find alloca, which is required for FLINT. Please submit a report to and specify your operating system.])]) +AC_CHECK_FUNCS([aligned_alloc _aligned_malloc]) + ################################################################################ # CFLAGS ################################################################################ diff --git a/src/generic_files/memory_manager.c b/src/generic_files/memory_manager.c index 9e292c44f1..6ee4b70432 100644 --- a/src/generic_files/memory_manager.c +++ b/src/generic_files/memory_manager.c @@ -44,8 +44,13 @@ static void * (* __flint_allocate_func)(size_t) = _flint_malloc; static void * (* __flint_callocate_func)(size_t, size_t) = _flint_calloc; static void * (* __flint_reallocate_func)(void *, size_t) = _flint_realloc; static void (* __flint_free_func)(void *) = _flint_free; +#if HAVE_ALIGNED_ALLOC || HAVE__ALIGNED_MALLOC static void * (* __flint_aligned_allocate_func)(size_t, size_t) = _flint_aligned_alloc; static void (* __flint_aligned_free_func)(void *) = _flint_aligned_free; +#else +static void * (* __flint_aligned_allocate_func)(size_t, size_t) = _flint_aligned_alloc2; +static void (* __flint_aligned_free_func)(void *) = _flint_aligned_free2; +#endif FLINT_STATIC_NOINLINE void flint_memory_error(size_t size) { @@ -147,10 +152,12 @@ void flint_free(void * ptr) void * _flint_aligned_alloc(size_t alignment, size_t size) { -#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) +#if HAVE__ALIGNED_MALLOC return _aligned_malloc(size, alignment); -#else +#elif HAVE_ALIGNED_ALLOC return aligned_alloc(alignment, size); +#else + return NULL; #endif } @@ -163,7 +170,7 @@ void * _flint_aligned_alloc2(size_t alignment, size_t size) alloc_size = size + alignment; - alloc_ptr = malloc(alloc_size); + alloc_ptr = flint_malloc(alloc_size); /* Case 1: alloc_ptr aligned with (alignment, alignment - sizeof(ulong)). We only need `size + sizeof(ulong)' bytes. @@ -195,10 +202,12 @@ FLINT_WARN_UNUSED void * flint_aligned_alloc(size_t alignment, size_t size) void _flint_aligned_free(void * p) { -#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) +#if HAVE__ALIGNED_MALLOC _aligned_free(p); -#else +#elif HAVE_ALIGNED_ALLOC free(p); +#else + return; #endif } @@ -206,7 +215,7 @@ void _flint_aligned_free2(void * p) { size_t * ptr = p; if (ptr != NULL) - free((char *) ptr - ptr[-1]); + flint_free((char *) ptr - ptr[-1]); } void flint_aligned_free(void * ptr)