Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_STDDEF_H |
| 3 | #define _LINUX_STDDEF_H |
| 4 | |
| 5 | #include <uapi/linux/stddef.h> |
| 6 | |
| 7 | #undef NULL |
| 8 | #define NULL ((void *)0) |
| 9 | |
| 10 | enum { |
| 11 | false = 0, |
| 12 | true = 1 |
| 13 | }; |
| 14 | |
| 15 | #undef offsetof |
| 16 | #ifdef __compiler_offsetof |
| 17 | #define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER) |
| 18 | #else |
| 19 | #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) |
| 20 | #endif |
| 21 | |
| 22 | /** |
| 23 | * sizeof_field(TYPE, MEMBER) |
| 24 | * |
| 25 | * @TYPE: The structure containing the field of interest |
| 26 | * @MEMBER: The field to return the size of |
| 27 | */ |
| 28 | #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) |
| 29 | |
| 30 | /** |
| 31 | * offsetofend(TYPE, MEMBER) |
| 32 | * |
| 33 | * @TYPE: The type of the structure |
| 34 | * @MEMBER: The member within the structure to get the end offset of |
| 35 | */ |
| 36 | #define offsetofend(TYPE, MEMBER) \ |
| 37 | (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) |
| 38 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame] | 39 | /** |
| 40 | * struct_group() - Wrap a set of declarations in a mirrored struct |
| 41 | * |
| 42 | * @NAME: The identifier name of the mirrored sub-struct |
| 43 | * @MEMBERS: The member declarations for the mirrored structs |
| 44 | * |
| 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 |
| 49 | * struct members. |
| 50 | */ |
| 51 | #define struct_group(NAME, MEMBERS...) \ |
| 52 | __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS) |
| 53 | |
| 54 | /** |
| 55 | * struct_group_attr() - Create a struct_group() with trailing attributes |
| 56 | * |
| 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 |
| 60 | * |
| 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. |
| 66 | */ |
| 67 | #define struct_group_attr(NAME, ATTRS, MEMBERS...) \ |
| 68 | __struct_group(/* no tag */, NAME, ATTRS, MEMBERS) |
| 69 | |
| 70 | /** |
| 71 | * struct_group_tagged() - Create a struct_group with a reusable tag |
| 72 | * |
| 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 |
| 76 | * |
| 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. |
| 83 | */ |
| 84 | #define struct_group_tagged(TAG, NAME, MEMBERS...) \ |
| 85 | __struct_group(TAG, NAME, /* no attrs */, MEMBERS) |
| 86 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 87 | #endif |