1 /* A substitute for ISO C11 <stdalign.h>. 2 3 Copyright 2011-2021 Free Software Foundation, Inc. 4 5 This file is free software: you can redistribute it and/or modify 6 it under the terms of the GNU Lesser General Public License as 7 published by the Free Software Foundation; either version 2.1 of the 8 License, or (at your option) any later version. 9 10 This file 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 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser 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 and clang's __alignof__ operator, which can 38 yield a better-performing alignment for an object of that type. For 39 example, on x86 with GCC and on Linux/x86 with clang, 40 __alignof__ (double) and __alignof__ (long long) are 8, whereas 41 alignof (double) and alignof (long long) are 4 unless the option 42 '-malign-double' is used. 43 44 The result cannot be used as a value for an 'enum' constant, if you 45 want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. 46 47 Include <stddef.h> for offsetof. */ 48 #include <stddef.h> 49 50 /* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other 51 standard headers, defines conflicting implementations of _Alignas 52 and _Alignof that are no better than ours; override them. */ 53 #undef _Alignas 54 #undef _Alignof 55 56 /* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023 57 <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. 58 clang versions < 8.0.0 have the same bug. */ 59 #if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \ 60 || (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \ 61 && !defined __clang__) \ 62 || (defined __clang__ && __clang_major__ < 8)) 63 # ifdef __cplusplus 64 # if 201103 <= __cplusplus 65 # define _Alignof(type) alignof (type) 66 # else 67 template <class __t> struct __alignof_helper { char __a; __t __b; }; 68 # define _Alignof(type) offsetof (__alignof_helper<type>, __b) 69 # endif 70 # else 71 # define _Alignof(type) offsetof (struct { char __a; type __b; }, __b) 72 # endif 73 #endif 74 #if ! (defined __cplusplus && 201103 <= __cplusplus) 75 # define alignof _Alignof 76 #endif 77 #define __alignof_is_defined 1 78 79 /* alignas (A), also known as _Alignas (A), aligns a variable or type 80 to the alignment A, where A is an integer constant expression. For 81 example: 82 83 int alignas (8) foo; 84 struct s { int a; int alignas (8) bar; }; 85 86 aligns the address of FOO and the offset of BAR to be multiples of 8. 87 88 A should be a power of two that is at least the type's alignment 89 and at most the implementation's alignment limit. This limit is 90 2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable 91 to MSVC through at least version 10.0, A should be an integer 92 constant, as MSVC does not support expressions such as 1 << 3. 93 To be portable to Sun C 5.11, do not align auto variables to 94 anything stricter than their default alignment. 95 96 The following C11 requirements are not supported here: 97 98 - If A is zero, alignas has no effect. 99 - alignas can be used multiple times; the strictest one wins. 100 - alignas (TYPE) is equivalent to alignas (alignof (TYPE)). 101 102 */ 103 104 #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 105 # if defined __cplusplus && 201103 <= __cplusplus 106 # define _Alignas(a) alignas (a) 107 # elif (!defined __attribute__ \ 108 && ((defined __APPLE__ && defined __MACH__ \ 109 ? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \ 110 : __GNUC__ && !defined __ibmxl__) \ 111 || (4 <= __clang_major__) \ 112 || (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \ 113 || __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__)) 114 # define _Alignas(a) __attribute__ ((__aligned__ (a))) 115 # elif 1300 <= _MSC_VER 116 # define _Alignas(a) __declspec (align (a)) 117 # endif 118 #endif 119 #if ((defined _Alignas && ! (defined __cplusplus && 201103 <= __cplusplus)) \ 120 || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__)) 121 # define alignas _Alignas 122 #endif 123 #if defined alignas || (defined __cplusplus && 201103 <= __cplusplus) 124 # define __alignas_is_defined 1 125 #endif 126 127 #endif /* _GL_STDALIGN_H */