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