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