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.
dev
Yann Collet 2021-12-02 11:20:01 -08:00
parent ffbdc8ac57
commit 80a13fd645
2 changed files with 33 additions and 20 deletions

View File

@ -282,6 +282,39 @@
# endif # endif
#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 <stdalign.h>
# 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 #if ZSTD_MEMORY_SANITIZER
/* Not all platforms that support msan provide sanitizers/msan_interface.h. /* Not all platforms that support msan provide sanitizers/msan_interface.h.
* We therefore declare the functions we need ourselves, rather than trying to * We therefore declare the functions we need ourselves, rather than trying to

View File

@ -80,26 +80,6 @@ extern "C" {
#endif #endif
/*-**************************************************************
* Alignment check
*****************************************************************/
/* C11 support */
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
#include <stdalign.h>
# 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 * Memory I/O API
*****************************************************************/ *****************************************************************/