stddef.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. #ifndef _LINUX_STDDEF_H
  3. #define _LINUX_STDDEF_H
  4. #ifndef __always_inline
  5. #define __always_inline __inline__
  6. #endif
  7. /* Not all C++ standards support type declarations inside an anonymous union */
  8. #ifndef __cplusplus
  9. #define __struct_group_tag(TAG) TAG
  10. #else
  11. #define __struct_group_tag(TAG)
  12. #endif
  13. /**
  14. * __struct_group() - Create a mirrored named and anonyomous struct
  15. *
  16. * @TAG: The tag name for the named sub-struct (usually empty)
  17. * @NAME: The identifier name of the mirrored sub-struct
  18. * @ATTRS: Any struct attributes (usually empty)
  19. * @MEMBERS: The member declarations for the mirrored structs
  20. *
  21. * Used to create an anonymous union of two structs with identical layout
  22. * and size: one anonymous and one named. The former's members can be used
  23. * normally without sub-struct naming, and the latter can be used to
  24. * reason about the start, end, and size of the group of struct members.
  25. * The named struct can also be explicitly tagged for layer reuse (C only),
  26. * as well as both having struct attributes appended.
  27. */
  28. #define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
  29. union { \
  30. struct { MEMBERS } ATTRS; \
  31. struct __struct_group_tag(TAG) { MEMBERS } ATTRS NAME; \
  32. } ATTRS
  33. /**
  34. * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
  35. *
  36. * @TYPE: The type of each flexible array element
  37. * @NAME: The name of the flexible array member
  38. *
  39. * In order to have a flexible array member in a union or alone in a
  40. * struct, it needs to be wrapped in an anonymous struct with at least 1
  41. * named member, but that member can be empty.
  42. */
  43. #define __DECLARE_FLEX_ARRAY(TYPE, NAME) \
  44. struct { \
  45. struct { } __empty_ ## NAME; \
  46. TYPE NAME[]; \
  47. }
  48. #endif