stddef.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_STDDEF_H
  3. #define _LINUX_STDDEF_H
  4. #include <uapi/linux/stddef.h>
  5. #undef NULL
  6. #define NULL ((void *)0)
  7. enum {
  8. false = 0,
  9. true = 1
  10. };
  11. #undef offsetof
  12. #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
  13. /**
  14. * sizeof_field() - Report the size of a struct field in bytes
  15. *
  16. * @TYPE: The structure containing the field of interest
  17. * @MEMBER: The field to return the size of
  18. */
  19. #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
  20. /**
  21. * offsetofend() - Report the offset of a struct field within the struct
  22. *
  23. * @TYPE: The type of the structure
  24. * @MEMBER: The member within the structure to get the end offset of
  25. */
  26. #define offsetofend(TYPE, MEMBER) \
  27. (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
  28. /**
  29. * struct_group() - Wrap a set of declarations in a mirrored struct
  30. *
  31. * @NAME: The identifier name of the mirrored sub-struct
  32. * @MEMBERS: The member declarations for the mirrored structs
  33. *
  34. * Used to create an anonymous union of two structs with identical
  35. * layout and size: one anonymous and one named. The former can be
  36. * used normally without sub-struct naming, and the latter can be
  37. * used to reason about the start, end, and size of the group of
  38. * struct members.
  39. */
  40. #define struct_group(NAME, MEMBERS...) \
  41. __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
  42. /**
  43. * struct_group_attr() - Create a struct_group() with trailing attributes
  44. *
  45. * @NAME: The identifier name of the mirrored sub-struct
  46. * @ATTRS: Any struct attributes to apply
  47. * @MEMBERS: The member declarations for the mirrored structs
  48. *
  49. * Used to create an anonymous union of two structs with identical
  50. * layout and size: one anonymous and one named. The former can be
  51. * used normally without sub-struct naming, and the latter can be
  52. * used to reason about the start, end, and size of the group of
  53. * struct members. Includes structure attributes argument.
  54. */
  55. #define struct_group_attr(NAME, ATTRS, MEMBERS...) \
  56. __struct_group(/* no tag */, NAME, ATTRS, MEMBERS)
  57. /**
  58. * struct_group_tagged() - Create a struct_group with a reusable tag
  59. *
  60. * @TAG: The tag name for the named sub-struct
  61. * @NAME: The identifier name of the mirrored sub-struct
  62. * @MEMBERS: The member declarations for the mirrored structs
  63. *
  64. * Used to create an anonymous union of two structs with identical
  65. * layout and size: one anonymous and one named. The former can be
  66. * used normally without sub-struct naming, and the latter can be
  67. * used to reason about the start, end, and size of the group of
  68. * struct members. Includes struct tag argument for the named copy,
  69. * so the specified layout can be reused later.
  70. */
  71. #define struct_group_tagged(TAG, NAME, MEMBERS...) \
  72. __struct_group(TAG, NAME, /* no attrs */, MEMBERS)
  73. /**
  74. * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
  75. *
  76. * @TYPE: The type of each flexible array element
  77. * @NAME: The name of the flexible array member
  78. *
  79. * In order to have a flexible array member in a union or alone in a
  80. * struct, it needs to be wrapped in an anonymous struct with at least 1
  81. * named member, but that member can be empty.
  82. */
  83. #define DECLARE_FLEX_ARRAY(TYPE, NAME) \
  84. __DECLARE_FLEX_ARRAY(TYPE, NAME)
  85. #endif