unistr, unigbrk: Use const-improved function macros for 30 functions.
[gnulib.git] / lib / flexmember.h
blobb4d86c29fb5496a9ed3bd87c2d56cf42265e4194
1 /* Sizes of structs with flexible array members.
3 Copyright 2016-2025 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <https://www.gnu.org/licenses/>.
21 Written by Paul Eggert. */
23 /* This file uses _Alignof. */
24 #if !_GL_CONFIG_H_INCLUDED
25 #error "Please include config.h first."
26 #endif
28 #include <stddef.h>
30 /* Nonzero multiple of alignment of TYPE, suitable for FLEXSIZEOF below.
31 If _Alignof might not exist or might not work correctly on
32 structs with flexible array members, use a pessimistic bound that is
33 safe in practice even if FLEXIBLE_ARRAY_MEMBER is 1.
34 Otherwise, use _Alignof to get a tighter bound. */
36 #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 || defined _Alignof
37 # define FLEXALIGNOF(type) (sizeof (type) & ~ (sizeof (type) - 1))
38 #else
39 # define FLEXALIGNOF(type) _Alignof (type)
40 #endif
42 /* Yield a properly aligned upper bound on the size of a struct of
43 type TYPE with a flexible array member named MEMBER that is
44 followed by N bytes of other data. The result is suitable as an
45 argument to malloc. For example:
47 struct s { int a; char d[FLEXIBLE_ARRAY_MEMBER]; };
48 struct s *p = malloc (FLEXSIZEOF (struct s, d, n * sizeof (char)));
50 FLEXSIZEOF (TYPE, MEMBER, N) is not simply (sizeof (TYPE) + N),
51 since FLEXIBLE_ARRAY_MEMBER may be 1 on pre-C11 platforms. Nor is
52 it simply (offsetof (TYPE, MEMBER) + N), as that might yield a size
53 that causes malloc to yield a pointer that is not properly aligned
54 for TYPE; for example, if sizeof (int) == alignof (int) == 4,
55 malloc (offsetof (struct s, d) + 3 * sizeof (char)) is equivalent
56 to malloc (7) and might yield a pointer that is not a multiple of 4
57 (which means the pointer is not properly aligned for struct s),
58 whereas malloc (FLEXSIZEOF (struct s, d, 3 * sizeof (char))) is
59 equivalent to malloc (8) and must yield a pointer that is a
60 multiple of 4.
62 Yield a value less than N if and only if arithmetic overflow occurs. */
64 #define FLEXSIZEOF(type, member, n) \
65 ((offsetof (type, member) + FLEXALIGNOF (type) - 1 + (n)) \
66 & ~ (FLEXALIGNOF (type) - 1))
68 /* Yield a properly aligned upper bound on the size of a struct of
69 type TYPE with a flexible array member named MEMBER that has N
70 elements. The result is suitable as an argument to malloc.
71 For example:
73 struct s { int a; double d[FLEXIBLE_ARRAY_MEMBER]; };
74 struct s *p = malloc (FLEXNSIZEOF (struct s, d, n));
76 #define FLEXNSIZEOF(type, member, n) \
77 FLEXSIZEOF (type, member, (n) * sizeof (((type *) 0)->member[0]))