1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_STDDEF_H
3 #define _LINUX_STDDEF_H
5 #include <uapi/linux/stddef.h>
8 #define NULL ((void *)0)
16 #ifdef __compiler_offsetof
17 #define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
19 #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
23 * sizeof_field(TYPE, MEMBER)
25 * @TYPE: The structure containing the field of interest
26 * @MEMBER: The field to return the size of
28 #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
31 * offsetofend(TYPE, MEMBER)
33 * @TYPE: The type of the structure
34 * @MEMBER: The member within the structure to get the end offset of
36 #define offsetofend(TYPE, MEMBER) \
37 (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
40 * struct_group() - Wrap a set of declarations in a mirrored struct
42 * @NAME: The identifier name of the mirrored sub-struct
43 * @MEMBERS: The member declarations for the mirrored structs
45 * Used to create an anonymous union of two structs with identical
46 * layout and size: one anonymous and one named. The former can be
47 * used normally without sub-struct naming, and the latter can be
48 * used to reason about the start, end, and size of the group of
51 #define struct_group(NAME, MEMBERS...) \
52 __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
55 * struct_group_attr() - Create a struct_group() with trailing attributes
57 * @NAME: The identifier name of the mirrored sub-struct
58 * @ATTRS: Any struct attributes to apply
59 * @MEMBERS: The member declarations for the mirrored structs
61 * Used to create an anonymous union of two structs with identical
62 * layout and size: one anonymous and one named. The former can be
63 * used normally without sub-struct naming, and the latter can be
64 * used to reason about the start, end, and size of the group of
65 * struct members. Includes structure attributes argument.
67 #define struct_group_attr(NAME, ATTRS, MEMBERS...) \
68 __struct_group(/* no tag */, NAME, ATTRS, MEMBERS)
71 * struct_group_tagged() - Create a struct_group with a reusable tag
73 * @TAG: The tag name for the named sub-struct
74 * @NAME: The identifier name of the mirrored sub-struct
75 * @MEMBERS: The member declarations for the mirrored structs
77 * Used to create an anonymous union of two structs with identical
78 * layout and size: one anonymous and one named. The former can be
79 * used normally without sub-struct naming, and the latter can be
80 * used to reason about the start, end, and size of the group of
81 * struct members. Includes struct tag argument for the named copy,
82 * so the specified layout can be reused later.
84 #define struct_group_tagged(TAG, NAME, MEMBERS...) \
85 __struct_group(TAG, NAME, /* no attrs */, MEMBERS)
88 * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
90 * @TYPE: The type of each flexible array element
91 * @NAME: The name of the flexible array member
93 * In order to have a flexible array member in a union or alone in a
94 * struct, it needs to be wrapped in an anonymous struct with at least 1
95 * named member, but that member can be empty.
97 #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
98 __DECLARE_FLEX_ARRAY(TYPE, NAME)