alignof.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Determine alignment of types.
  2. Copyright (C) 2003-2004, 2006, 2009-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 <https://www.gnu.org/licenses/>. */
  13. #ifndef _ALIGNOF_H
  14. #define _ALIGNOF_H
  15. #include <stddef.h>
  16. /* alignof_slot (TYPE)
  17. Determine the alignment of a structure slot (field) of a given type,
  18. at compile time. Note that the result depends on the ABI.
  19. This is the same as alignof (TYPE).
  20. Note: The result cannot be used as a value for an 'enum' constant,
  21. due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
  22. #if defined __cplusplus
  23. template <class type> struct alignof_helper { char __slot1; type __slot2; };
  24. # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
  25. #else
  26. # define alignof_slot(type) alignof (type)
  27. #endif
  28. /* alignof_type (TYPE)
  29. Determine the good alignment of an object of the given type at compile time.
  30. Note that this is not necessarily the same as alignof_slot(type).
  31. For example, with GNU C on x86 platforms and with clang on Linux/x86:
  32. alignof_type(long long) = 8, but alignof_slot(long long) = 4.
  33. And alignof_type(double) = 8, but
  34. - when -malign-double is not specified: alignof_slot(double) = 4,
  35. - when -malign-double is specified: alignof_slot(double) = 8.
  36. Note: The result cannot be used as a value for an 'enum' constant,
  37. due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
  38. #if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__
  39. # define alignof_type __alignof__
  40. #else
  41. # define alignof_type alignof_slot
  42. #endif
  43. #endif /* _ALIGNOF_H */