1 /* Sizes of structs with flexible array members.
2 
3    Copyright 2016-2018 Free Software Foundation, Inc.
4 
5    This file is part of the GNU C Library.
6 
7    The GNU C Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public
9    License as published by the Free Software Foundation; either
10    version 3 of the License, or (at your option) any later version.
11 
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    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public
18    License along with the GNU C Library; if not, see
19    <https://www.gnu.org/licenses/>.
20 
21    Written by Paul Eggert.  */
22 
23 #include <stddef.h>
24 
25 /* Nonzero multiple of alignment of TYPE, suitable for FLEXSIZEOF below.
26    On older platforms without _Alignof, use a pessimistic bound that is
27    safe in practice even if FLEXIBLE_ARRAY_MEMBER is 1.
28    On newer platforms, use _Alignof to get a tighter bound.  */
29 
30 #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
31 # define FLEXALIGNOF(type) (sizeof (type) & ~ (sizeof (type) - 1))
32 #else
33 # define FLEXALIGNOF(type) _Alignof (type)
34 #endif
35 
36 /* Upper bound on the size of a struct of type TYPE with a flexible
37    array member named MEMBER that is followed by N bytes of other data.
38    This is not simply sizeof (TYPE) + N, since it may require
39    alignment on unusually picky C11 platforms, and
40    FLEXIBLE_ARRAY_MEMBER may be 1 on pre-C11 platforms.
41    Yield a value less than N if and only if arithmetic overflow occurs.  */
42 
43 #define FLEXSIZEOF(type, member, n) \
44    ((offsetof (type, member) + FLEXALIGNOF (type) - 1 + (n)) \
45     & ~ (FLEXALIGNOF (type) - 1))
46