| 1 |
/* A substitute for ISO C11 <stdalign.h>. |
| 2 |
|
| 3 |
Copyright 2011-2022 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 |
/* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other |
| 48 |
standard headers, defines conflicting implementations of _Alignas |
| 49 |
and _Alignof that are no better than ours; override them. */ |
| 50 |
#undef _Alignas |
| 51 |
#undef _Alignof |
| 52 |
|
| 53 |
/* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023 |
| 54 |
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>. |
| 55 |
clang versions < 8.0.0 have the same bug. */ |
| 56 |
#if (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 \ |
| 57 |
|| (defined __GNUC__ && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \ |
| 58 |
&& !defined __clang__) \ |
| 59 |
|| (defined __clang__ && __clang_major__ < 8)) |
| 60 |
# ifdef __cplusplus |
| 61 |
# if (201103 <= __cplusplus || defined _MSC_VER) |
| 62 |
# define _Alignof(type) alignof (type) |
| 63 |
# else |
| 64 |
template <class __t> struct __alignof_helper { char __a; __t __b; }; |
| 65 |
# define _Alignof(type) offsetof (__alignof_helper<type>, __b) |
| 66 |
# define _GL_STDALIGN_NEEDS_STDDEF 1 |
| 67 |
# endif |
| 68 |
# else |
| 69 |
# define _Alignof(type) offsetof (struct { char __a; type __b; }, __b) |
| 70 |
# define _GL_STDALIGN_NEEDS_STDDEF 1 |
| 71 |
# endif |
| 72 |
#endif |
| 73 |
#if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)) |
| 74 |
# define alignof _Alignof |
| 75 |
#endif |
| 76 |
#define __alignof_is_defined 1 |
| 77 |
|
| 78 |
/* alignas (A), also known as _Alignas (A), aligns a variable or type |
| 79 |
to the alignment A, where A is an integer constant expression. For |
| 80 |
example: |
| 81 |
|
| 82 |
int alignas (8) foo; |
| 83 |
struct s { int a; int alignas (8) bar; }; |
| 84 |
|
| 85 |
aligns the address of FOO and the offset of BAR to be multiples of 8. |
| 86 |
|
| 87 |
A should be a power of two that is at least the type's alignment |
| 88 |
and at most the implementation's alignment limit. This limit is |
| 89 |
2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable |
| 90 |
to MSVC through at least version 10.0, A should be an integer |
| 91 |
constant, as MSVC does not support expressions such as 1 << 3. |
| 92 |
To be portable to Sun C 5.11, do not align auto variables to |
| 93 |
anything stricter than their default alignment. |
| 94 |
|
| 95 |
The following C11 requirements are not supported here: |
| 96 |
|
| 97 |
- If A is zero, alignas has no effect. |
| 98 |
- alignas can be used multiple times; the strictest one wins. |
| 99 |
- alignas (TYPE) is equivalent to alignas (alignof (TYPE)). |
| 100 |
|
| 101 |
*/ |
| 102 |
|
| 103 |
#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 |
| 104 |
# if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER) |
| 105 |
# define _Alignas(a) alignas (a) |
| 106 |
# elif (!defined __attribute__ \ |
| 107 |
&& ((defined __APPLE__ && defined __MACH__ \ |
| 108 |
? 4 < __GNUC__ + (1 <= __GNUC_MINOR__) \ |
| 109 |
: __GNUC__ && !defined __ibmxl__) \ |
| 110 |
|| (4 <= __clang_major__) \ |
| 111 |
|| (__ia64 && (61200 <= __HP_cc || 61200 <= __HP_aCC)) \ |
| 112 |
|| __ICC || 0x590 <= __SUNPRO_C || 0x0600 <= __xlC__)) |
| 113 |
# define _Alignas(a) __attribute__ ((__aligned__ (a))) |
| 114 |
# elif 1300 <= _MSC_VER |
| 115 |
# define _Alignas(a) __declspec (align (a)) |
| 116 |
# endif |
| 117 |
#endif |
| 118 |
#if ((defined _Alignas \ |
| 119 |
&& !(defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) \ |
| 120 |
|| (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__)) |
| 121 |
# define alignas _Alignas |
| 122 |
#endif |
| 123 |
#if (defined alignas \ |
| 124 |
|| (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) |
| 125 |
# define __alignas_is_defined 1 |
| 126 |
#endif |
| 127 |
|
| 128 |
/* Include <stddef.h> if needed for offsetof. */ |
| 129 |
#if _GL_STDALIGN_NEEDS_STDDEF |
| 130 |
# include <stddef.h> |
| 131 |
#endif |
| 132 |
|
| 133 |
#endif /* _GL_STDALIGN_H */ |