Skip to content

Commit

Permalink
Merge pull request #505 from aeiouaeiouaeiouaeiouaeiouaeiou/target-mm…
Browse files Browse the repository at this point in the history
…-free

Add LEGACY_MACOS macros, use _mm_free() for KRML_ALIGNED_FREE on macOS <10.15
  • Loading branch information
msprotz authored Dec 14, 2024
2 parents 21c28f3 + ccfd441 commit 8b1ec11
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions include/krml/internal/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@
# define inline __inline__
#endif

/* Include Apple-specific macros for use in defining KRML_ALIGNED_MALLOC. */
/* There is no support for aligned_alloc() in macOS before Catalina, so
* let's make a macro to use _mm_malloc() and _mm_free() functions
* from mm_malloc.h. */
#if defined(__APPLE__) && defined(__MACH__)
#include <AvailabilityMacros.h>
# include <AvailabilityMacros.h>
# if defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \
(MAC_OS_X_VERSION_MIN_REQUIRED < 101500)
# include <mm_malloc.h>
# define LEGACY_MACOS
# else
# undef LEGACY_MACOS
#endif
#endif

/******************************************************************************/
Expand Down Expand Up @@ -128,8 +137,7 @@
#endif

/* MinGW-W64 does not support C11 aligned_alloc, but it supports
* MSVC's _aligned_malloc. Also, fallback to use mm_malloc.h
* implementation for macOS systems prior to 10.15 Catalina.
* MSVC's _aligned_malloc.
*/
#ifndef KRML_ALIGNED_MALLOC
# ifdef __MINGW32__
Expand All @@ -139,10 +147,7 @@
defined(_MSC_VER) || \
(defined(__MINGW32__) && defined(__MINGW64_VERSION_MAJOR)))
# define KRML_ALIGNED_MALLOC(X, Y) _aligned_malloc(Y, X)
# elif defined(__APPLE__) && defined(__MACH__) && \
defined(MAC_OS_X_VERSION_MIN_REQUIRED) && \
(MAC_OS_X_VERSION_MIN_REQUIRED < 101500)
# include <mm_malloc.h>
# elif defined(LEGACY_MACOS)
# define KRML_ALIGNED_MALLOC(X, Y) _mm_malloc(Y, X)
# else
# define KRML_ALIGNED_MALLOC(X, Y) aligned_alloc(X, Y)
Expand All @@ -161,6 +166,8 @@
defined(_MSC_VER) || \
(defined(__MINGW32__) && defined(__MINGW64_VERSION_MAJOR)))
# define KRML_ALIGNED_FREE(X) _aligned_free(X)
# elif defined(LEGACY_MACOS)
# define KRML_ALIGNED_FREE(X) _mm_free(X)
# else
# define KRML_ALIGNED_FREE(X) free(X)
# endif
Expand Down

0 comments on commit 8b1ec11

Please sign in to comment.