Skip to content

Commit f1ec31e

Browse files
committed
Add check for aligned_alloc and _aligned_malloc
If they exist, use accordingly in memory manager.
1 parent 1acac0e commit f1ec31e

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

CMake/cmake_config.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@
2323
/* Just set to some reasonable threshold */
2424
#define FLINT_FFT_SMALL_THRESHOLD 600
2525

26+
/* NOTE: Here we assume this is how it works. */
27+
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
28+
# define HAVE__ALIGNED_MALLOC 1
29+
#else
30+
# define HAVE_ALIGNED_ALLOC 1
31+
#endif
32+
2633
#ifdef _MSC_VER
2734
# if defined(FLINT_BUILD_DLL)
2835
# define FLINT_DLL __declspec(dllexport)

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ AC_MSG_ERROR([Couldn't find alloca, which is required for FLINT. Please submit a
888888
report to <https://github.com/flintlib/flint/issues/> and specify your
889889
operating system.])])
890890

891+
AC_CHECK_FUNCS([aligned_alloc _aligned_malloc])
892+
891893
################################################################################
892894
# CFLAGS
893895
################################################################################

src/generic_files/memory_manager.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,13 @@ static void * (* __flint_allocate_func)(size_t) = _flint_malloc;
4444
static void * (* __flint_callocate_func)(size_t, size_t) = _flint_calloc;
4545
static void * (* __flint_reallocate_func)(void *, size_t) = _flint_realloc;
4646
static void (* __flint_free_func)(void *) = _flint_free;
47+
#if HAVE_ALIGNED_ALLOC || HAVE__ALIGNED_MALLOC
4748
static void * (* __flint_aligned_allocate_func)(size_t, size_t) = _flint_aligned_alloc;
4849
static void (* __flint_aligned_free_func)(void *) = _flint_aligned_free;
50+
#else
51+
static void * (* __flint_aligned_allocate_func)(size_t, size_t) = _flint_aligned_alloc2;
52+
static void (* __flint_aligned_free_func)(void *) = _flint_aligned_free2;
53+
#endif
4954

5055
FLINT_STATIC_NOINLINE void flint_memory_error(size_t size)
5156
{
@@ -147,10 +152,12 @@ void flint_free(void * ptr)
147152

148153
void * _flint_aligned_alloc(size_t alignment, size_t size)
149154
{
150-
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
155+
#if HAVE__ALIGNED_MALLOC
151156
return _aligned_malloc(size, alignment);
152-
#else
157+
#elif HAVE_ALIGNED_ALLOC
153158
return aligned_alloc(alignment, size);
159+
#else
160+
return NULL;
154161
#endif
155162
}
156163

@@ -163,7 +170,7 @@ void * _flint_aligned_alloc2(size_t alignment, size_t size)
163170

164171
alloc_size = size + alignment;
165172

166-
alloc_ptr = malloc(alloc_size);
173+
alloc_ptr = flint_malloc(alloc_size);
167174

168175
/* Case 1: alloc_ptr aligned with (alignment, alignment - sizeof(ulong)).
169176
We only need `size + sizeof(ulong)' bytes.
@@ -195,18 +202,20 @@ FLINT_WARN_UNUSED void * flint_aligned_alloc(size_t alignment, size_t size)
195202

196203
void _flint_aligned_free(void * p)
197204
{
198-
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
205+
#if HAVE__ALIGNED_MALLOC
199206
_aligned_free(p);
200-
#else
207+
#elif HAVE_ALIGNED_ALLOC
201208
free(p);
209+
#else
210+
return;
202211
#endif
203212
}
204213

205214
void _flint_aligned_free2(void * p)
206215
{
207216
size_t * ptr = p;
208217
if (ptr != NULL)
209-
free((char *) ptr - ptr[-1]);
218+
flint_free((char *) ptr - ptr[-1]);
210219
}
211220

212221
void flint_aligned_free(void * ptr)

0 commit comments

Comments
 (0)