New module 'alignof'.
[gnulib.git] / lib / alignof.h
1 /* Determine alignment of types.
2    Copyright (C) 2003-2004, 2006, 2009 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #ifndef _ALIGNOF_H
19 #define _ALIGNOF_H
20
21 #include <stddef.h>
22
23 /* Determine the alignment of a type at compile time.  */
24 #if defined __GNUC__
25 # define alignof __alignof__
26 #elif defined __cplusplus
27   template <class type> struct alignof_helper { char __slot1; type __slot2; };
28 # define alignof(type) offsetof (alignof_helper<type>, __slot2)
29 #elif defined __hpux
30   /* Work around a HP-UX 10.20 cc bug with enums constants defined as offsetof
31      values.  */
32 # define alignof(type) (sizeof (type) <= 4 ? 4 : 8)
33 #elif defined _AIX
34   /* Work around an AIX 3.2.5 xlc bug with enums constants defined as offsetof
35      values.  */
36 # define alignof(type) (sizeof (type) <= 4 ? 4 : 8)
37 #else
38 # define alignof(type) offsetof (struct { char __slot1; type __slot2; }, __slot2)
39 #endif
40
41 #endif /* _ALIGNOF_H */