alignof.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Determine alignment of types.
  2. Copyright (C) 2003-2004, 2006, 2009-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. #ifndef _ALIGNOF_H
  15. #define _ALIGNOF_H
  16. #include <stddef.h>
  17. /* alignof_slot (TYPE)
  18. Determine the alignment of a structure slot (field) of a given type,
  19. at compile time. Note that the result depends on the ABI.
  20. This is the same as alignof (TYPE) and _Alignof (TYPE), defined in
  21. <stdalign.h> if __alignof_is_defined is 1.
  22. Note: The result cannot be used as a value for an 'enum' constant,
  23. due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
  24. #if defined __cplusplus
  25. template <class type> struct alignof_helper { char __slot1; type __slot2; };
  26. # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
  27. #else
  28. # define alignof_slot(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
  29. #endif
  30. /* alignof_type (TYPE)
  31. Determine the good alignment of an object of the given type at compile time.
  32. Note that this is not necessarily the same as alignof_slot(type).
  33. For example, with GNU C on x86 platforms: 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__
  39. # define alignof_type __alignof__
  40. #else
  41. # define alignof_type alignof_slot
  42. #endif
  43. #endif /* _ALIGNOF_H */