openat: don’t close (-1)
[gnulib.git] / lib / alignof.h
blob581aaee9bcfb936faf6229e50c00dec08134f9c0
1 /* Determine alignment of types.
2 Copyright (C) 2003-2004, 2006, 2009-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #ifndef _ALIGNOF_H
18 #define _ALIGNOF_H
20 /* This file uses alignof. */
21 #if !_GL_CONFIG_H_INCLUDED
22 #error "Please include config.h first."
23 #endif
25 #include <stddef.h>
27 /* alignof_slot (TYPE)
28 Determine the alignment of a structure slot (field) of a given type,
29 at compile time. Note that the result depends on the ABI.
30 This is the same as alignof (TYPE).
31 Note: The result cannot be used as a value for an 'enum' constant,
32 due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
33 #if defined __cplusplus
34 template <class type> struct alignof_helper { char __slot1; type __slot2; };
35 # define alignof_slot(type) offsetof (alignof_helper<type>, __slot2)
36 #else
37 # define alignof_slot(type) alignof (type)
38 #endif
40 /* alignof_type (TYPE)
41 Determine the good alignment of an object of the given type at compile time.
42 Note that this is not necessarily the same as alignof_slot(type).
43 For example, with GNU C on x86 platforms and with clang on Linux/x86:
44 alignof_type(long long) = 8, but alignof_slot(long long) = 4.
45 And alignof_type(double) = 8, but
46 - when -malign-double is not specified: alignof_slot(double) = 4,
47 - when -malign-double is specified: alignof_slot(double) = 8.
48 Note: The result cannot be used as a value for an 'enum' constant,
49 due to bugs in HP-UX 10.20 cc and AIX 3.2.5 xlc. */
50 #if defined __GNUC__ || defined __clang__ || defined __IBM__ALIGNOF__
51 # define alignof_type __alignof__
52 #else
53 # define alignof_type alignof_slot
54 #endif
56 #endif /* _ALIGNOF_H */