stdalign.in.h 4.6 KB

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