diff options
author | Jörg Frings-Fürst <debian@jff.email> | 2024-03-03 19:11:32 +0100 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff.email> | 2024-03-03 19:11:32 +0100 |
commit | 00893e79fc62966067af1a106567db96bd170338 (patch) | |
tree | 52b35cd0b42ca28e62a2ffbecade2e13dd8332cf /lib/flexmember.h | |
parent | 26112352a774737e1ce5580c93654a26c1e82b39 (diff) |
New upstream version 1.2upstream/1.2
Diffstat (limited to 'lib/flexmember.h')
-rw-r--r-- | lib/flexmember.h | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/flexmember.h b/lib/flexmember.h index 1545639f..6ef66a32 100644 --- a/lib/flexmember.h +++ b/lib/flexmember.h @@ -1,6 +1,6 @@ /* Sizes of structs with flexible array members. - Copyright 2016-2022 Free Software Foundation, Inc. + Copyright 2016-2024 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -20,6 +20,11 @@ Written by Paul Eggert. */ +/* This file uses _Alignof. */ +#if !_GL_CONFIG_H_INCLUDED + #error "Please include config.h first." +#endif + #include <stddef.h> /* Nonzero multiple of alignment of TYPE, suitable for FLEXSIZEOF below. @@ -38,7 +43,7 @@ followed by N bytes of other data. The result is suitable as an argument to malloc. For example: - struct s { int n; char d[FLEXIBLE_ARRAY_MEMBER]; }; + struct s { int a; char d[FLEXIBLE_ARRAY_MEMBER]; }; struct s *p = malloc (FLEXSIZEOF (struct s, d, n * sizeof (char))); FLEXSIZEOF (TYPE, MEMBER, N) is not simply (sizeof (TYPE) + N), @@ -58,3 +63,14 @@ #define FLEXSIZEOF(type, member, n) \ ((offsetof (type, member) + FLEXALIGNOF (type) - 1 + (n)) \ & ~ (FLEXALIGNOF (type) - 1)) + +/* Yield a properly aligned upper bound on the size of a struct of + type TYPE with a flexible array member named MEMBER that has N + elements. The result is suitable as an argument to malloc. + For example: + + struct s { int a; double d[FLEXIBLE_ARRAY_MEMBER]; }; + struct s *p = malloc (FLEXNSIZEOF (struct s, d, n)); + */ +#define FLEXNSIZEOF(type, member, n) \ + FLEXSIZEOF (type, member, (n) * sizeof (((type *) 0)->member[0])) |