stdalign.in.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* A substitute for ISO C11 <stdalign.h>.
  2. Copyright 2011-2012 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, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* Written by Paul Eggert and Bruno Haible. */
  15. #ifndef _GL_STDALIGN_H
  16. #define _GL_STDALIGN_H
  17. /* ISO C11 <stdalign.h> for platforms that lack it.
  18. References:
  19. ISO C11 (latest free draft
  20. <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf>)
  21. sections 6.5.3.4, 6.7.5, 7.15.
  22. C++11 (latest free draft
  23. <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf>)
  24. section 18.10. */
  25. /* alignof (TYPE), also known as _Alignof (TYPE), yields the alignment
  26. requirement of a structure member (i.e., slot or field) that is of
  27. type TYPE, as an integer constant expression.
  28. This differs from GCC's __alignof__ operator, which can yield a
  29. better-performing alignment for an object of that type. For
  30. example, on x86 with GCC, __alignof__ (double) and __alignof__
  31. (long long) are 8, whereas alignof (double) and alignof (long long)
  32. are 4 unless the option '-malign-double' is used.
  33. The result cannot be used as a value for an 'enum' constant, if you
  34. want to be portable to HP-UX 10.20 cc and AIX 3.2.5 xlc. */
  35. #include <stddef.h>
  36. #if defined __cplusplus
  37. template <class __t> struct __alignof_helper { char __a; __t __b; };
  38. # define _Alignof(type) offsetof (__alignof_helper<type>, __b)
  39. #else
  40. # define _Alignof(type) offsetof (struct { char __a; type __b; }, __b)
  41. #endif
  42. #define alignof _Alignof
  43. #define __alignof_is_defined 1
  44. /* alignas (A), also known as _Alignas (A), aligns a variable or type
  45. to the alignment A, where A is an integer constant expression. For
  46. example:
  47. int alignas (8) foo;
  48. struct s { int a; int alignas (8) bar; };
  49. aligns the address of FOO and the offset of BAR to be multiples of 8.
  50. A should be a power of two that is at least the type's alignment
  51. and at most the implementation's alignment limit. This limit is
  52. 2**28 on typical GNUish hosts, and 2**13 on MSVC. To be portable
  53. to MSVC through at least version 10.0, A should be an integer
  54. constant, as MSVC does not support expressions such as 1 << 3.
  55. To be portable to Sun C 5.11, do not align auto variables to
  56. anything stricter than their default alignment.
  57. The following C11 requirements are not supported here:
  58. - If A is zero, alignas has no effect.
  59. - alignas can be used multiple times; the strictest one wins.
  60. - alignas (TYPE) is equivalent to alignas (alignof (TYPE)).
  61. */
  62. #if __GNUC__ || __IBMC__ || __IBMCPP__ || 0x5110 <= __SUNPRO_C
  63. # define _Alignas(a) __attribute__ ((__aligned__ (a)))
  64. #elif 1300 <= _MSC_VER
  65. # define _Alignas(a) __declspec (align (a))
  66. #endif
  67. #ifdef _Alignas
  68. # define alignas _Alignas
  69. # define __alignas_is_defined 1
  70. #endif
  71. #endif /* _GL_STDALIGN_H */