alignof.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* Determine alignment of types.
  2. Copyright (C) 2003-2004, 2006, 2009-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 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 General Public License for more details.
  11. You should have received a copy of the GNU 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) and _Alignof (TYPE), defined in
  20. <stdalign.h> if __alignof_is_defined is 1.
  21. Note: The result cannot be used as a value for an 'enum' constant,
  22. due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
  23. #if defined __cplusplus
  24. template <class type> struct alignof_helper { char __slot1; type __slot2; };
  25. # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
  26. #else
  27. # define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
  28. #endif
  29. /* alignof_type (TYPE)
  30. Determine the good alignment of an object of the given type at compile time.
  31. Note that this is not necessarily the same as alignof_slot(type).
  32. For example, with GNU C on x86 platforms: alignof_type(double) = 8, but
  33. - when -malign-double is not specified: alignof_slot(double) = 4,
  34. - when -malign-double is specified: alignof_slot(double) = 8.
  35. Note: The result cannot be used as a value for an 'enum' constant,
  36. due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
  37. #if defined __GNUC__ || defined __IBM__ALIGNOF__
  38. # define alignof_type __alignof__
  39. #else
  40. # define alignof_type alignof_slot
  41. #endif
  42. #endif /* _ALIGNOF_H */