xref: /dflybsd-src/contrib/grep/lib/flexmember.h (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
1*09d4459fSDaniel Fojt /* Sizes of structs with flexible array members.
2*09d4459fSDaniel Fojt 
3*09d4459fSDaniel Fojt    Copyright 2016-2020 Free Software Foundation, Inc.
4*09d4459fSDaniel Fojt 
5*09d4459fSDaniel Fojt    This file is part of the GNU C Library.
6*09d4459fSDaniel Fojt 
7*09d4459fSDaniel Fojt    The GNU C Library is free software; you can redistribute it and/or
8*09d4459fSDaniel Fojt    modify it under the terms of the GNU General Public
9*09d4459fSDaniel Fojt    License as published by the Free Software Foundation; either
10*09d4459fSDaniel Fojt    version 3 of the License, or (at your option) any later version.
11*09d4459fSDaniel Fojt 
12*09d4459fSDaniel Fojt    The GNU C Library is distributed in the hope that it will be useful,
13*09d4459fSDaniel Fojt    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*09d4459fSDaniel Fojt    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*09d4459fSDaniel Fojt    General Public License for more details.
16*09d4459fSDaniel Fojt 
17*09d4459fSDaniel Fojt    You should have received a copy of the GNU General Public
18*09d4459fSDaniel Fojt    License along with the GNU C Library; if not, see
19*09d4459fSDaniel Fojt    <https://www.gnu.org/licenses/>.
20*09d4459fSDaniel Fojt 
21*09d4459fSDaniel Fojt    Written by Paul Eggert.  */
22*09d4459fSDaniel Fojt 
23*09d4459fSDaniel Fojt #include <stddef.h>
24*09d4459fSDaniel Fojt 
25*09d4459fSDaniel Fojt /* Nonzero multiple of alignment of TYPE, suitable for FLEXSIZEOF below.
26*09d4459fSDaniel Fojt    On older platforms without _Alignof, use a pessimistic bound that is
27*09d4459fSDaniel Fojt    safe in practice even if FLEXIBLE_ARRAY_MEMBER is 1.
28*09d4459fSDaniel Fojt    On newer platforms, use _Alignof to get a tighter bound.  */
29*09d4459fSDaniel Fojt 
30*09d4459fSDaniel Fojt #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
31*09d4459fSDaniel Fojt # define FLEXALIGNOF(type) (sizeof (type) & ~ (sizeof (type) - 1))
32*09d4459fSDaniel Fojt #else
33*09d4459fSDaniel Fojt # define FLEXALIGNOF(type) _Alignof (type)
34*09d4459fSDaniel Fojt #endif
35*09d4459fSDaniel Fojt 
36*09d4459fSDaniel Fojt /* Yield a properly aligned upper bound on the size of a struct of
37*09d4459fSDaniel Fojt    type TYPE with a flexible array member named MEMBER that is
38*09d4459fSDaniel Fojt    followed by N bytes of other data.  The result is suitable as an
39*09d4459fSDaniel Fojt    argument to malloc.  For example:
40*09d4459fSDaniel Fojt 
41*09d4459fSDaniel Fojt      struct s { int n; char d[FLEXIBLE_ARRAY_MEMBER]; };
42*09d4459fSDaniel Fojt      struct s *p = malloc (FLEXSIZEOF (struct s, d, n * sizeof (char)));
43*09d4459fSDaniel Fojt 
44*09d4459fSDaniel Fojt    FLEXSIZEOF (TYPE, MEMBER, N) is not simply (sizeof (TYPE) + N),
45*09d4459fSDaniel Fojt    since FLEXIBLE_ARRAY_MEMBER may be 1 on pre-C11 platforms.  Nor is
46*09d4459fSDaniel Fojt    it simply (offsetof (TYPE, MEMBER) + N), as that might yield a size
47*09d4459fSDaniel Fojt    that causes malloc to yield a pointer that is not properly aligned
48*09d4459fSDaniel Fojt    for TYPE; for example, if sizeof (int) == alignof (int) == 4,
49*09d4459fSDaniel Fojt    malloc (offsetof (struct s, d) + 3 * sizeof (char)) is equivalent
50*09d4459fSDaniel Fojt    to malloc (7) and might yield a pointer that is not a multiple of 4
51*09d4459fSDaniel Fojt    (which means the pointer is not properly aligned for struct s),
52*09d4459fSDaniel Fojt    whereas malloc (FLEXSIZEOF (struct s, d, 3 * sizeof (char))) is
53*09d4459fSDaniel Fojt    equivalent to malloc (8) and must yield a pointer that is a
54*09d4459fSDaniel Fojt    multiple of 4.
55*09d4459fSDaniel Fojt 
56*09d4459fSDaniel Fojt    Yield a value less than N if and only if arithmetic overflow occurs.  */
57*09d4459fSDaniel Fojt 
58*09d4459fSDaniel Fojt #define FLEXSIZEOF(type, member, n) \
59*09d4459fSDaniel Fojt    ((offsetof (type, member) + FLEXALIGNOF (type) - 1 + (n)) \
60*09d4459fSDaniel Fojt     & ~ (FLEXALIGNOF (type) - 1))
61