Skip to content

Commit

Permalink
Add check for aligned_alloc and _aligned_malloc
Browse files Browse the repository at this point in the history
If they exist, use accordingly in memory manager.
  • Loading branch information
albinahlback committed Mar 7, 2024
1 parent 1acac0e commit f1ec31e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CMake/cmake_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@ AC_MSG_ERROR([Couldn't find alloca, which is required for FLINT. Please submit a
report to <https://github.com/flintlib/flint/issues/> and specify your
operating system.])])

AC_CHECK_FUNCS([aligned_alloc _aligned_malloc])

################################################################################
# CFLAGS
################################################################################
Expand Down
21 changes: 15 additions & 6 deletions src/generic_files/memory_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
}

Expand All @@ -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.
Expand Down Expand Up @@ -195,18 +202,20 @@ 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
}

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)
Expand Down

0 comments on commit f1ec31e

Please sign in to comment.