damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

pstdalign.h (4295B)


      1 #ifndef PSTDALIGN_H
      2 #define PSTDALIGN_H
      3 
      4 /*
      5  * NOTE: aligned_alloc is defined via paligned_alloc.h
      6  * and requires aligned_free to be fully portable although
      7  * free also works on C11 and platforms with posix_memalign.
      8  *
      9  * NOTE: C++11 defines alignas as a keyword but then also defines
     10  * __alignas_is_defined.
     11  *
     12  * C++14 does not define __alignas_is_defined, at least sometimes.
     13  *
     14  * GCC 8.3 reverts on this and makes C++11 behave the same as C++14
     15  * preventing a simple __cplusplus version check from working.
     16  *
     17  * Clang C++ without std=c++11 or std=c++14 does define alignas
     18  * but does so incorrectly wrt. C11 and C++11 semantics because
     19  * `alignas(4) float x;` is not recognized.
     20  * To fix such issues, either move to a std version, or
     21  * include a working stdalign.h for the given compiler before
     22  * this file.
     23  *
     24  * newlib defines _Alignas and _Alignof in sys/cdefs but rely on
     25  * gcc version for <stdaligh.h> which can lead to conflicts if
     26  * stdalign is not included.
     27  *
     28  * newlibs need for <stdalign.h> conflicts with broken C++ stdalign
     29  * but this can be fixed be using std=C++11 or newer.
     30  *
     31  * MSVC does not support <stdalign.h> at least up to MSVC 2015,
     32  * but does appear to support alignas and alignof keywords in
     33  * recent standard C++.
     34  *
     35  * TCC only supports alignas with a numeric argument like
     36  * `alignas(4)`, but not `alignas(float)`.
     37  *
     38  * If stdalign.h is supported but heuristics in this file are
     39  * insufficient to detect this, try including <stdaligh.h> manually
     40  * or define HAVE_STDALIGN_H.
     41  */
     42 
     43 /* https://github.com/dvidelabs/flatcc/issues/130 */
     44 #ifndef __alignas_is_defined
     45 #if defined(__cplusplus)
     46 #if __cplusplus == 201103 && !defined(__clang__) && ((__GNUC__ > 8) || (__GNUC__ == 8 && __GNUC_MINOR__ >= 3))
     47 #define __alignas_is_defined 1
     48 #define __alignof_is_defined 1
     49 #include <stdalign.h>
     50 #endif
     51 #endif
     52 #endif
     53 
     54 /* Allow for alternative solution to be included first. */
     55 #ifndef __alignas_is_defined
     56 
     57 #ifdef __cplusplus
     58 #if defined(PORTABLE_PATCH_CPLUSPLUS_STDALIGN)
     59 #include <stdalign.h>
     60 #undef alignas
     61 #define alignas(t) __attribute__((__aligned__(t)))
     62 #endif
     63 #endif
     64 
     65 #if !defined(PORTABLE_HAS_INCLUDE_STDALIGN)
     66 #if defined(__has_include)
     67 #if __has_include(<stdalign.h>)
     68 #define PORTABLE_HAS_INCLUDE_STDALIGN 1
     69 #else
     70 #define PORTABLE_HAS_INCLUDE_STDALIGN 0
     71 #endif
     72 #endif
     73 #endif
     74 
     75  /* https://lists.gnu.org/archive/html/bug-gnulib/2015-08/msg00003.html */
     76 #if defined(__cplusplus)
     77 #if !defined(_MSC_VER)
     78 #include <stdalign.h>
     79 #endif
     80 #if __cplusplus > 201103
     81 #define __alignas_is_defined 1
     82 #define __alignof_is_defined 1
     83 #endif
     84 #elif PORTABLE_HAS_INCLUDE_STDALIGN
     85 #include <stdalign.h>
     86 #elif !defined(__clang__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
     87 #include <stdalign.h>
     88 #elif defined(HAVE_STDALIGN_H)
     89 #include <stdaligh.h>
     90 #endif
     91 
     92 #endif /* __alignas_is_defined */
     93 
     94 #ifndef __alignas_is_defined
     95 
     96 #ifdef __cplusplus
     97 extern "C" {
     98 #endif
     99 
    100 #if (!defined(__clang__) && defined(__GNUC__) && \
    101         ((__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 7)))
    102 #undef PORTABLE_C11_STDALIGN_MISSING
    103 #define PORTABLE_C11_STDALIGN_MISSING
    104 #endif
    105 
    106 #if defined(__IBMC__)
    107 #undef PORTABLE_C11_STDALIGN_MISSING
    108 #define PORTABLE_C11_STDALIGN_MISSING
    109 #endif
    110 
    111 #if ((defined(__STDC__) && __STDC__ && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) && \
    112     !defined(PORTABLE_C11_STDALIGN_MISSING))
    113 /* C11 or newer */
    114 #include <stdalign.h>
    115 #else
    116 #if defined(__GNUC__) || defined(__IBM_ALIGNOF__) || defined(__clang__)
    117 
    118 #ifndef _Alignas
    119 #define _Alignas(t) __attribute__((__aligned__(t)))
    120 #endif
    121 
    122 #ifndef _Alignof
    123 #define _Alignof(t) __alignof__(t)
    124 #endif
    125 
    126 #elif defined(_MSC_VER)
    127 
    128 #define _Alignas(t) __declspec (align(t))
    129 #define _Alignof(t) __alignof(t)
    130 
    131 #elif defined(__TINYC__)
    132 
    133 /* Supports `_Alignas(integer-expression)`, but not `_Alignas(type)`. */
    134 #define _Alignas(t) __attribute__(aligned(t))
    135 #define _Alignof(t) __alignof__(t)
    136 
    137 #else
    138 #error please update pstdalign.h with support for current compiler and library
    139 #endif
    140 
    141 #endif /* __STDC__ */
    142 
    143 #ifndef alignas
    144 #define alignas _Alignas
    145 #endif
    146 
    147 #ifndef alignof
    148 #define alignof _Alignof
    149 #endif
    150 
    151 #define __alignas_is_defined 1
    152 #define __alignof_is_defined 1
    153 
    154 #ifdef __cplusplus
    155 }
    156 #endif
    157 
    158 #endif /* __alignas__is_defined */
    159 
    160 #include "paligned_alloc.h"
    161 
    162 #endif /* PSTDALIGN_H */