Home | History | Annotate | Line # | Download | only in import
stdalign.in.h revision 1.1.1.2
      1      1.1  christos /* A substitute for ISO C11 <stdalign.h>.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright 2011-2022 Free Software Foundation, Inc.
      4      1.1  christos 
      5  1.1.1.2  christos    This file is free software: you can redistribute it and/or modify
      6  1.1.1.2  christos    it under the terms of the GNU Lesser General Public License as
      7  1.1.1.2  christos    published by the Free Software Foundation; either version 2.1 of the
      8  1.1.1.2  christos    License, or (at your option) any later version.
      9      1.1  christos 
     10  1.1.1.2  christos    This file is distributed in the hope that it will be useful,
     11      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  1.1.1.2  christos    GNU Lesser General Public License for more details.
     14      1.1  christos 
     15  1.1.1.2  christos    You should have received a copy of the GNU Lesser General Public License
     16  1.1.1.2  christos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
     17      1.1  christos 
     18      1.1  christos /* Written by Paul Eggert and Bruno Haible.  */
     19      1.1  christos 
     20      1.1  christos #ifndef _GL_STDALIGN_H
     21      1.1  christos #define _GL_STDALIGN_H
     22      1.1  christos 
     23      1.1  christos /* ISO C11 <stdalign.h> for platforms that lack it.
     24      1.1  christos 
     25      1.1  christos    References:
     26      1.1  christos    ISO C11 (latest free draft
     27      1.1  christos    <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf>)
     28      1.1  christos    sections 6.5.3.4, 6.7.5, 7.15.
     29      1.1  christos    C++11 (latest free draft
     30      1.1  christos    <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>)
     31      1.1  christos    section 18.10. */
     32      1.1  christos 
     33      1.1  christos /* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment
     34      1.1  christos    requirement of a structure member (i.e., slot or field) that is of
     35      1.1  christos    type TYPE, as an integer constant expression.
     36      1.1  christos 
     37  1.1.1.2  christos    This differs from GCC's and clang's __alignof__ operator, which can
     38  1.1.1.2  christos    yield a better-performing alignment for an object of that type.  For
     39  1.1.1.2  christos    example, on x86 with GCC and on Linux/x86 with clang,
     40  1.1.1.2  christos    __alignof__ (double) and __alignof__ (long long) are 8, whereas
     41  1.1.1.2  christos    alignof (double) and alignof (long long) are 4 unless the option
     42  1.1.1.2  christos    '-malign-double' is used.
     43      1.1  christos 
     44      1.1  christos    The result cannot be used as a value for an 'enum' constant, if you
     45      1.1  christos    want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc.
     46      1.1  christos 
     47      1.1  christos    Include <stddef.h> for offsetof.  */
     48      1.1  christos #include <stddef.h>
     49      1.1  christos 
     50      1.1  christos /* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other
     51      1.1  christos    standard headers, defines conflicting implementations of _Alignas
     52      1.1  christos    and _Alignof that are no better than ours; override them.  */
     53      1.1  christos #undef _Alignas
     54      1.1  christos #undef _Alignof
     55      1.1  christos 
     56      1.1  christos /* GCC releases before GCC 4.9 had a bug in _Alignof.  See GCC bug 52023
     57  1.1.1.2  christos    <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
     58  1.1.1.2  christos    clang versions < 8.0.0 have the same bug.  */
     59      1.1  christos #if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \
     60  1.1.1.2  christos      || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
     61  1.1.1.2  christos          && !defined __clang__) \
     62  1.1.1.2  christos      || (defined __clang__ && __clang_major__ < 8))
     63      1.1  christos # ifdef __cplusplus
     64      1.1  christos #  if 201103 <= __cplusplus
     65      1.1  christos #   define _Alignof(type) alignof (type)
     66      1.1  christos #  else
     67      1.1  christos    template <class __t> struct __alignof_helper { char __a; __t __b; };
     68      1.1  christos #   define _Alignof(type) offsetof (__alignof_helper<type>, __b)
     69      1.1  christos #  endif
     70      1.1  christos # else
     71      1.1  christos #  define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
     72      1.1  christos # endif
     73      1.1  christos #endif
     74      1.1  christos #if ! (defined __cplusplus && 201103 <= __cplusplus)
     75      1.1  christos # define alignof _Alignof
     76      1.1  christos #endif
     77      1.1  christos #define __alignof_is_defined 1
     78      1.1  christos 
     79      1.1  christos /* alignas (A), also known as _Alignas (A), aligns a variable or type
     80      1.1  christos    to the alignment A, where A is an integer constant expression.  For
     81      1.1  christos    example:
     82      1.1  christos 
     83      1.1  christos       int alignas (8) foo;
     84      1.1  christos       struct s { int a; int alignas (8) bar; };
     85      1.1  christos 
     86      1.1  christos    aligns the address of FOO and the offset of BAR to be multiples of 8.
     87      1.1  christos 
     88      1.1  christos    A should be a power of two that is at least the type's alignment
     89      1.1  christos    and at most the implementation's alignment limit.  This limit is
     90      1.1  christos    2**28 on typical GNUish hosts, and 2**13 on MSVC.  To be portable
     91      1.1  christos    to MSVC through at least version 10.0, A should be an integer
     92      1.1  christos    constant, as MSVC does not support expressions such as 1 << 3.
     93      1.1  christos    To be portable to Sun C 5.11, do not align auto variables to
     94      1.1  christos    anything stricter than their default alignment.
     95      1.1  christos 
     96      1.1  christos    The following C11 requirements are not supported here:
     97      1.1  christos 
     98      1.1  christos      - If A is zero, alignas has no effect.
     99      1.1  christos      - alignas can be used multiple times; the strictest one wins.
    100      1.1  christos      - alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
    101      1.1  christos 
    102      1.1  christos    */
    103      1.1  christos 
    104      1.1  christos #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
    105      1.1  christos # if defined __cplusplus && 201103 <= __cplusplus
    106      1.1  christos #  define _Alignas(a) alignas (a)
    107  1.1.1.2  christos # elif (!defined __attribute__ \
    108  1.1.1.2  christos         && ((defined __APPLE__ && defined __MACH__ \
    109  1.1.1.2  christos              ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \
    110  1.1.1.2  christos              : __GNUC__ && !defined __ibmxl__) \
    111  1.1.1.2  christos             || (4 <= __clang_major__) \
    112  1.1.1.2  christos             || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \
    113  1.1.1.2  christos             || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__))
    114      1.1  christos #  define _Alignas(a) __attribute__ ((__aligned__ (a)))
    115      1.1  christos # elif 1300 <= _MSC_VER
    116      1.1  christos #  define _Alignas(a) __declspec (align (a))
    117      1.1  christos # endif
    118      1.1  christos #endif
    119      1.1  christos #if ((defined _Alignas && ! (defined __cplusplus && 201103 <= __cplusplus)) \
    120      1.1  christos      || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__))
    121      1.1  christos # define alignas _Alignas
    122      1.1  christos #endif
    123      1.1  christos #if defined alignas || (defined __cplusplus && 201103 <= __cplusplus)
    124      1.1  christos # define __alignas_is_defined 1
    125      1.1  christos #endif
    126      1.1  christos 
    127      1.1  christos #endif /* _GL_STDALIGN_H */
    128