From 00893e79fc62966067af1a106567db96bd170338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 3 Mar 2024 19:11:32 +0100 Subject: New upstream version 1.2 --- lib/flexmember.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/flexmember.h') 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 /* 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])) -- cgit v1.2.3