xref: /DragonStub/inc/dragonstub/linux/stddef.h (revision f412fd2a1a248b546b7085648dece8d908077fab)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STDDEF_H
3 #define _LINUX_STDDEF_H
4 
5 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
6 #ifndef _UAPI_LINUX_STDDEF_H
7 #define _UAPI_LINUX_STDDEF_H
8 
9 #include "../compiler_types.h"
10 
11 #ifndef __always_inline
12 #define __always_inline inline
13 #endif
14 
15 /**
16  * __struct_group() - Create a mirrored named and anonyomous struct
17  *
18  * @TAG: The tag name for the named sub-struct (usually empty)
19  * @NAME: The identifier name of the mirrored sub-struct
20  * @ATTRS: Any struct attributes (usually empty)
21  * @MEMBERS: The member declarations for the mirrored structs
22  *
23  * Used to create an anonymous union of two structs with identical layout
24  * and size: one anonymous and one named. The former's members can be used
25  * normally without sub-struct naming, and the latter can be used to
26  * reason about the start, end, and size of the group of struct members.
27  * The named struct can also be explicitly tagged for layer reuse, as well
28  * as both having struct attributes appended.
29  */
30 #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
31 	union { \
32 		struct { MEMBERS } ATTRS; \
33 		struct TAG { MEMBERS } ATTRS NAME; \
34 	}
35 
36 #ifdef __cplusplus
37 /* sizeof(struct{}) is 1 in C++, not 0, can't use C version of the macro. */
38 #define __DECLARE_FLEX_ARRAY(T, member)	\
39 	T member[0]
40 #else
41 /**
42  * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
43  *
44  * @TYPE: The type of each flexible array element
45  * @NAME: The name of the flexible array member
46  *
47  * In order to have a flexible array member in a union or alone in a
48  * struct, it needs to be wrapped in an anonymous struct with at least 1
49  * named member, but that member can be empty.
50  */
51 #define __DECLARE_FLEX_ARRAY(TYPE, NAME)	\
52 	struct { \
53 		struct { } __empty_ ## NAME; \
54 		TYPE NAME[]; \
55 	}
56 #endif
57 
58 #ifndef __counted_by
59 #define __counted_by(m)
60 #endif
61 
62 #endif /* _UAPI_LINUX_STDDEF_H */
63 
64 
65 #undef NULL
66 #define NULL ((void *)0)
67 
68 // enum {
69 // 	false	= 0,
70 // 	true	= 1
71 // };
72 
73 #undef offsetof
74 #define offsetof(TYPE, MEMBER)	__builtin_offsetof(TYPE, MEMBER)
75 
76 /**
77  * sizeof_field() - Report the size of a struct field in bytes
78  *
79  * @TYPE: The structure containing the field of interest
80  * @MEMBER: The field to return the size of
81  */
82 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
83 
84 /**
85  * offsetofend() - Report the offset of a struct field within the struct
86  *
87  * @TYPE: The type of the structure
88  * @MEMBER: The member within the structure to get the end offset of
89  */
90 #define offsetofend(TYPE, MEMBER) \
91 	(offsetof(TYPE, MEMBER)	+ sizeof_field(TYPE, MEMBER))
92 
93 /**
94  * struct_group() - Wrap a set of declarations in a mirrored struct
95  *
96  * @NAME: The identifier name of the mirrored sub-struct
97  * @MEMBERS: The member declarations for the mirrored structs
98  *
99  * Used to create an anonymous union of two structs with identical
100  * layout and size: one anonymous and one named. The former can be
101  * used normally without sub-struct naming, and the latter can be
102  * used to reason about the start, end, and size of the group of
103  * struct members.
104  */
105 #define struct_group(NAME, MEMBERS...)	\
106 	__struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
107 
108 /**
109  * struct_group_attr() - Create a struct_group() with trailing attributes
110  *
111  * @NAME: The identifier name of the mirrored sub-struct
112  * @ATTRS: Any struct attributes to apply
113  * @MEMBERS: The member declarations for the mirrored structs
114  *
115  * Used to create an anonymous union of two structs with identical
116  * layout and size: one anonymous and one named. The former can be
117  * used normally without sub-struct naming, and the latter can be
118  * used to reason about the start, end, and size of the group of
119  * struct members. Includes structure attributes argument.
120  */
121 #define struct_group_attr(NAME, ATTRS, MEMBERS...) \
122 	__struct_group(/* no tag */, NAME, ATTRS, MEMBERS)
123 
124 /**
125  * struct_group_tagged() - Create a struct_group with a reusable tag
126  *
127  * @TAG: The tag name for the named sub-struct
128  * @NAME: The identifier name of the mirrored sub-struct
129  * @MEMBERS: The member declarations for the mirrored structs
130  *
131  * Used to create an anonymous union of two structs with identical
132  * layout and size: one anonymous and one named. The former can be
133  * used normally without sub-struct naming, and the latter can be
134  * used to reason about the start, end, and size of the group of
135  * struct members. Includes struct tag argument for the named copy,
136  * so the specified layout can be reused later.
137  */
138 #define struct_group_tagged(TAG, NAME, MEMBERS...) \
139 	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
140 
141 /**
142  * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
143  *
144  * @TYPE: The type of each flexible array element
145  * @NAME: The name of the flexible array member
146  *
147  * In order to have a flexible array member in a union or alone in a
148  * struct, it needs to be wrapped in an anonymous struct with at least 1
149  * named member, but that member can be empty.
150  */
151 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
152 	__DECLARE_FLEX_ARRAY(TYPE, NAME)
153 
154 #endif
155