alignof.h 2.2 KB

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