From 8bdaa529df668d0f868d82be39ddbffe9626f5e7 Mon Sep 17 00:00:00 2001 From: Andrei Alexeyev Date: Tue, 24 Sep 2019 22:58:05 +0300 Subject: [PATCH] fix for platforms where alignof(max_align_t) < 16 i686 windows in particular --- meson.build | 2 +- src/list.h | 6 +++++- src/util/compat.h | 23 ++++++++++------------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/meson.build b/meson.build index e2ceb96f..7b057457 100644 --- a/meson.build +++ b/meson.build @@ -188,7 +188,7 @@ config.set('TAISEI_BUILDCONF_HAVE_TIMESPEC', have_timespec) config.set('TAISEI_BUILDCONF_HAVE_INT128', cc.sizeof('__int128') == 16) config.set('TAISEI_BUILDCONF_HAVE_LONG_DOUBLE', cc.sizeof('long double') > 8) config.set('TAISEI_BUILDCONF_HAVE_POSIX', have_posix) -config.set('TAISEI_BUILDCONF_HAVE_MAX_ALIGN_T', cc.compiles('#include \nmax_align_t i;', name : 'max_align_t test')) +config.set('TAISEI_BUILDCONF_MALLOC_ALIGNMENT', cc.alignment('max_align_t', prefix : '#include \n')) prefer_relpath_systems = [ 'windows', diff --git a/src/list.h b/src/list.h index c825566e..e68cf4b4 100644 --- a/src/list.h +++ b/src/list.h @@ -11,7 +11,11 @@ #include "taisei.h" -#define LIST_ALIGN alignas(16) +#if TAISEI_BUILDCONF_MALLOC_ALIGNMENT < 16 + #define LIST_ALIGN alignas(TAISEI_BUILDCONF_MALLOC_ALIGNMENT) +#else + #define LIST_ALIGN alignas(16) +#endif typedef struct ListInterface ListInterface; typedef struct List List; diff --git a/src/util/compat.h b/src/util/compat.h index 4cf1673c..e55c2d83 100644 --- a/src/util/compat.h +++ b/src/util/compat.h @@ -155,8 +155,12 @@ typedef _Complex double complex; // `CMPLX` like ((double complex){ x, y }) #undef CMPLX -#ifndef TAISEI_BUILDCONF_HAVE_MAX_ALIGN_T -typedef complex max_align_t; +// FIXME this is likely incorrect! +#if TAISEI_BUILDCONF_MALLOC_ALIGNMENT < 0 + #warning max_align_t not supported + #undef TAISEI_BUILDCONF_MALLOC_ALIGNMENT + #define TAISEI_BUILDCONF_MALLOC_ALIGNMENT 8 + typedef struct { alignas(TAISEI_BUILDCONF_MALLOC_ALIGNMENT) long double a; } max_align_t; #endif // In case the C11 CMPLX macro is not present, try our best to provide a substitute @@ -264,19 +268,12 @@ typedef complex max_align_t; #define INLINE static inline attr_must_inline __attribute__((gnu_inline, artificial)) -#ifdef NDEBUG - #define _ensure_aligned(ptr, alignment) (ptr) -#else - INLINE void *_ensure_aligned(void *ptr, size_t alignment) { - assert(((uintptr_t)ptr & (alignment - 1)) == 0); - return ptr; - } -#endif - #ifdef USE_GNU_EXTENSIONS #define ASSUME_ALIGNED(expr, alignment) (__extension__ ({ \ - assert(__builtin_constant_p(alignment)); \ - __builtin_assume_aligned(_ensure_aligned((expr), (alignment)), (alignment)); \ + static_assert(__builtin_constant_p(alignment), ""); \ + __auto_type _assume_aligned_ptr = (expr); \ + assert(((uintptr_t)_assume_aligned_ptr & ((alignment) - 1)) == 0); \ + __builtin_assume_aligned(_assume_aligned_ptr, (alignment)); \ })) #else #define ASSUME_ALIGNED(expr, alignment) (expr)