xref: /linux-6.15/tools/include/uapi/linux/stddef.h (revision 724c6ce3)
1a778f5d4SAndrii Nakryiko /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2a778f5d4SAndrii Nakryiko #ifndef _LINUX_STDDEF_H
3a778f5d4SAndrii Nakryiko #define _LINUX_STDDEF_H
4a778f5d4SAndrii Nakryiko 
5a778f5d4SAndrii Nakryiko 
6a778f5d4SAndrii Nakryiko 
7a778f5d4SAndrii Nakryiko #ifndef __always_inline
8a778f5d4SAndrii Nakryiko #define __always_inline __inline__
9a778f5d4SAndrii Nakryiko #endif
10a778f5d4SAndrii Nakryiko 
11*724c6ce3SAlexander Lobakin /* Not all C++ standards support type declarations inside an anonymous union */
12*724c6ce3SAlexander Lobakin #ifndef __cplusplus
13*724c6ce3SAlexander Lobakin #define __struct_group_tag(TAG)		TAG
14*724c6ce3SAlexander Lobakin #else
15*724c6ce3SAlexander Lobakin #define __struct_group_tag(TAG)
16*724c6ce3SAlexander Lobakin #endif
17*724c6ce3SAlexander Lobakin 
18a778f5d4SAndrii Nakryiko /**
19a778f5d4SAndrii Nakryiko  * __struct_group() - Create a mirrored named and anonyomous struct
20a778f5d4SAndrii Nakryiko  *
21a778f5d4SAndrii Nakryiko  * @TAG: The tag name for the named sub-struct (usually empty)
22a778f5d4SAndrii Nakryiko  * @NAME: The identifier name of the mirrored sub-struct
23a778f5d4SAndrii Nakryiko  * @ATTRS: Any struct attributes (usually empty)
24a778f5d4SAndrii Nakryiko  * @MEMBERS: The member declarations for the mirrored structs
25a778f5d4SAndrii Nakryiko  *
26a778f5d4SAndrii Nakryiko  * Used to create an anonymous union of two structs with identical layout
27a778f5d4SAndrii Nakryiko  * and size: one anonymous and one named. The former's members can be used
28a778f5d4SAndrii Nakryiko  * normally without sub-struct naming, and the latter can be used to
29a778f5d4SAndrii Nakryiko  * reason about the start, end, and size of the group of struct members.
30*724c6ce3SAlexander Lobakin  * The named struct can also be explicitly tagged for layer reuse (C only),
31*724c6ce3SAlexander Lobakin  * as well as both having struct attributes appended.
32a778f5d4SAndrii Nakryiko  */
33a778f5d4SAndrii Nakryiko #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
34a778f5d4SAndrii Nakryiko 	union { \
35a778f5d4SAndrii Nakryiko 		struct { MEMBERS } ATTRS; \
36*724c6ce3SAlexander Lobakin 		struct __struct_group_tag(TAG) { MEMBERS } ATTRS NAME; \
37*724c6ce3SAlexander Lobakin 	} ATTRS
38a778f5d4SAndrii Nakryiko 
39a778f5d4SAndrii Nakryiko /**
40a778f5d4SAndrii Nakryiko  * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
41a778f5d4SAndrii Nakryiko  *
42a778f5d4SAndrii Nakryiko  * @TYPE: The type of each flexible array element
43a778f5d4SAndrii Nakryiko  * @NAME: The name of the flexible array member
44a778f5d4SAndrii Nakryiko  *
45a778f5d4SAndrii Nakryiko  * In order to have a flexible array member in a union or alone in a
46a778f5d4SAndrii Nakryiko  * struct, it needs to be wrapped in an anonymous struct with at least 1
47a778f5d4SAndrii Nakryiko  * named member, but that member can be empty.
48a778f5d4SAndrii Nakryiko  */
49a778f5d4SAndrii Nakryiko #define __DECLARE_FLEX_ARRAY(TYPE, NAME)	\
50a778f5d4SAndrii Nakryiko 	struct { \
51a778f5d4SAndrii Nakryiko 		struct { } __empty_ ## NAME; \
52a778f5d4SAndrii Nakryiko 		TYPE NAME[]; \
53a778f5d4SAndrii Nakryiko 	}
54a778f5d4SAndrii Nakryiko #endif
55