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