18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _LINUX_STDDEF_H
38c2ecf20Sopenharmony_ci#define _LINUX_STDDEF_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <uapi/linux/stddef.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#undef NULL
88c2ecf20Sopenharmony_ci#define NULL ((void *)0)
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cienum {
118c2ecf20Sopenharmony_ci	false	= 0,
128c2ecf20Sopenharmony_ci	true	= 1
138c2ecf20Sopenharmony_ci};
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#undef offsetof
168c2ecf20Sopenharmony_ci#ifdef __compiler_offsetof
178c2ecf20Sopenharmony_ci#define offsetof(TYPE, MEMBER)	__compiler_offsetof(TYPE, MEMBER)
188c2ecf20Sopenharmony_ci#else
198c2ecf20Sopenharmony_ci#define offsetof(TYPE, MEMBER)	((size_t)&((TYPE *)0)->MEMBER)
208c2ecf20Sopenharmony_ci#endif
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/**
238c2ecf20Sopenharmony_ci * sizeof_field(TYPE, MEMBER)
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * @TYPE: The structure containing the field of interest
268c2ecf20Sopenharmony_ci * @MEMBER: The field to return the size of
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/**
318c2ecf20Sopenharmony_ci * offsetofend(TYPE, MEMBER)
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * @TYPE: The type of the structure
348c2ecf20Sopenharmony_ci * @MEMBER: The member within the structure to get the end offset of
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#define offsetofend(TYPE, MEMBER) \
378c2ecf20Sopenharmony_ci	(offsetof(TYPE, MEMBER)	+ sizeof_field(TYPE, MEMBER))
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/**
408c2ecf20Sopenharmony_ci * struct_group() - Wrap a set of declarations in a mirrored struct
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * @NAME: The identifier name of the mirrored sub-struct
438c2ecf20Sopenharmony_ci * @MEMBERS: The member declarations for the mirrored structs
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * Used to create an anonymous union of two structs with identical
468c2ecf20Sopenharmony_ci * layout and size: one anonymous and one named. The former can be
478c2ecf20Sopenharmony_ci * used normally without sub-struct naming, and the latter can be
488c2ecf20Sopenharmony_ci * used to reason about the start, end, and size of the group of
498c2ecf20Sopenharmony_ci * struct members.
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci#define struct_group(NAME, MEMBERS...)	\
528c2ecf20Sopenharmony_ci	__struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/**
558c2ecf20Sopenharmony_ci * struct_group_attr() - Create a struct_group() with trailing attributes
568c2ecf20Sopenharmony_ci *
578c2ecf20Sopenharmony_ci * @NAME: The identifier name of the mirrored sub-struct
588c2ecf20Sopenharmony_ci * @ATTRS: Any struct attributes to apply
598c2ecf20Sopenharmony_ci * @MEMBERS: The member declarations for the mirrored structs
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * Used to create an anonymous union of two structs with identical
628c2ecf20Sopenharmony_ci * layout and size: one anonymous and one named. The former can be
638c2ecf20Sopenharmony_ci * used normally without sub-struct naming, and the latter can be
648c2ecf20Sopenharmony_ci * used to reason about the start, end, and size of the group of
658c2ecf20Sopenharmony_ci * struct members. Includes structure attributes argument.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci#define struct_group_attr(NAME, ATTRS, MEMBERS...) \
688c2ecf20Sopenharmony_ci	__struct_group(/* no tag */, NAME, ATTRS, MEMBERS)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/**
718c2ecf20Sopenharmony_ci * struct_group_tagged() - Create a struct_group with a reusable tag
728c2ecf20Sopenharmony_ci *
738c2ecf20Sopenharmony_ci * @TAG: The tag name for the named sub-struct
748c2ecf20Sopenharmony_ci * @NAME: The identifier name of the mirrored sub-struct
758c2ecf20Sopenharmony_ci * @MEMBERS: The member declarations for the mirrored structs
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * Used to create an anonymous union of two structs with identical
788c2ecf20Sopenharmony_ci * layout and size: one anonymous and one named. The former can be
798c2ecf20Sopenharmony_ci * used normally without sub-struct naming, and the latter can be
808c2ecf20Sopenharmony_ci * used to reason about the start, end, and size of the group of
818c2ecf20Sopenharmony_ci * struct members. Includes struct tag argument for the named copy,
828c2ecf20Sopenharmony_ci * so the specified layout can be reused later.
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_ci#define struct_group_tagged(TAG, NAME, MEMBERS...) \
858c2ecf20Sopenharmony_ci	__struct_group(TAG, NAME, /* no attrs */, MEMBERS)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/**
888c2ecf20Sopenharmony_ci * DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci * @TYPE: The type of each flexible array element
918c2ecf20Sopenharmony_ci * @NAME: The name of the flexible array member
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * In order to have a flexible array member in a union or alone in a
948c2ecf20Sopenharmony_ci * struct, it needs to be wrapped in an anonymous struct with at least 1
958c2ecf20Sopenharmony_ci * named member, but that member can be empty.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ci#define DECLARE_FLEX_ARRAY(TYPE, NAME) \
988c2ecf20Sopenharmony_ci	__DECLARE_FLEX_ARRAY(TYPE, NAME)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#endif
101