stddef.h 4.9 KB

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