|
Packit Service |
4684c1 |
/* Sizes of structs with flexible array members.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
Copyright 2016-2020 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
This file is part of the GNU C Library.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
4684c1 |
modify it under the terms of the GNU General Public
|
|
Packit Service |
4684c1 |
License as published by the Free Software Foundation; either
|
|
Packit Service |
4684c1 |
version 3 of the License, or (at your option) any later version.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
4684c1 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
4684c1 |
General Public License for more details.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
You should have received a copy of the GNU General Public
|
|
Packit Service |
4684c1 |
License along with the GNU C Library; if not, see
|
|
Packit Service |
4684c1 |
<https://www.gnu.org/licenses/>.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
Written by Paul Eggert. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include <stddef.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Nonzero multiple of alignment of TYPE, suitable for FLEXSIZEOF below.
|
|
Packit Service |
4684c1 |
On older platforms without _Alignof, use a pessimistic bound that is
|
|
Packit Service |
4684c1 |
safe in practice even if FLEXIBLE_ARRAY_MEMBER is 1.
|
|
Packit Service |
4684c1 |
On newer platforms, use _Alignof to get a tighter bound. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112
|
|
Packit Service |
4684c1 |
# define FLEXALIGNOF(type) (sizeof (type) & ~ (sizeof (type) - 1))
|
|
Packit Service |
4684c1 |
#else
|
|
Packit Service |
4684c1 |
# define FLEXALIGNOF(type) _Alignof (type)
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Yield a properly aligned upper bound on the size of a struct of
|
|
Packit Service |
4684c1 |
type TYPE with a flexible array member named MEMBER that is
|
|
Packit Service |
4684c1 |
followed by N bytes of other data. The result is suitable as an
|
|
Packit Service |
4684c1 |
argument to malloc. For example:
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct s { int n; char d[FLEXIBLE_ARRAY_MEMBER]; };
|
|
Packit Service |
4684c1 |
struct s *p = malloc (FLEXSIZEOF (struct s, d, n * sizeof (char)));
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
FLEXSIZEOF (TYPE, MEMBER, N) is not simply (sizeof (TYPE) + N),
|
|
Packit Service |
4684c1 |
since FLEXIBLE_ARRAY_MEMBER may be 1 on pre-C11 platforms. Nor is
|
|
Packit Service |
4684c1 |
it simply (offsetof (TYPE, MEMBER) + N), as that might yield a size
|
|
Packit Service |
4684c1 |
that causes malloc to yield a pointer that is not properly aligned
|
|
Packit Service |
4684c1 |
for TYPE; for example, if sizeof (int) == alignof (int) == 4,
|
|
Packit Service |
4684c1 |
malloc (offsetof (struct s, d) + 3 * sizeof (char)) is equivalent
|
|
Packit Service |
4684c1 |
to malloc (7) and might yield a pointer that is not a multiple of 4
|
|
Packit Service |
4684c1 |
(which means the pointer is not properly aligned for struct s),
|
|
Packit Service |
4684c1 |
whereas malloc (FLEXSIZEOF (struct s, d, 3 * sizeof (char))) is
|
|
Packit Service |
4684c1 |
equivalent to malloc (8) and must yield a pointer that is a
|
|
Packit Service |
4684c1 |
multiple of 4.
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
Yield a value less than N if and only if arithmetic overflow occurs. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#define FLEXSIZEOF(type, member, n) \
|
|
Packit Service |
4684c1 |
((offsetof (type, member) + FLEXALIGNOF (type) - 1 + (n)) \
|
|
Packit Service |
4684c1 |
& ~ (FLEXALIGNOF (type) - 1))
|