From: Yann Collet Date: Thu, 2 Dec 2021 19:20:01 +0000 (-0800) Subject: move the alignment macro to compiler.h X-Git-Tag: v1.5.1~1^2~35^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80a13fd64555e2be571a0436a3fae0dd867ce626;p=thirdparty%2Fzstd.git move the alignment macro to compiler.h because mem.h is dropped in the Linux kernel. Changed macro definition order (gcc/clang/msvc before c11) due to a limitation in the kernel source builder. Changed the backup to sizeof(), reverting to previous behavior when no support of alignof() is detected. --- diff --git a/lib/common/compiler.h b/lib/common/compiler.h index ea5fe2f47..907b6bfac 100644 --- a/lib/common/compiler.h +++ b/lib/common/compiler.h @@ -282,6 +282,39 @@ # endif #endif +/*-************************************************************** +* Alignment check +*****************************************************************/ + +/* this test was initially positioned in mem.h, + * but this file is removed (or replaced) for linux kernel + * so it's now hosted in compiler.h, + * which remains valid for both user & kernel spaces. + */ + +#ifndef MEM_ALIGN_COND +# if defined(__GNUC__) || defined(_MSC_VER) +/* covers gcc, clang & MSVC */ +/* note : this section must come first, before C11, + * due to a limitation in the kernel source generator */ +# define MEM_ALIGN_COND(T) __alignof(T) + +# elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +/* C11 support */ +# include +# define MEM_ALIGN_COND(T) alignof(T) + +# else +/* No known support for alignof() - imperfect backup */ +# define MEM_ALIGN_COND(T) sizeof(T) + +# endif +#endif /* MEM_ALIGN_COND */ + +/*-************************************************************** +* Sanitizer +*****************************************************************/ + #if ZSTD_MEMORY_SANITIZER /* Not all platforms that support msan provide sanitizers/msan_interface.h. * We therefore declare the functions we need ourselves, rather than trying to diff --git a/lib/common/mem.h b/lib/common/mem.h index 4803897cf..1c61b7e52 100644 --- a/lib/common/mem.h +++ b/lib/common/mem.h @@ -80,26 +80,6 @@ extern "C" { #endif -/*-************************************************************** -* Alignment check -*****************************************************************/ - -/* C11 support */ -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) - -#include -# define MEM_ALIGN_COND(T) alignof(T) - -#elif defined(__GNUC__) || defined(_MSC_VER) - -# define MEM_ALIGN_COND(T) __alignof(T) - -#else - -# define MEM_ALIGN_COND(T) 1 /* most likely incorrect, just to pass tests */ - -#endif - /*-************************************************************** * Memory I/O API *****************************************************************/