1 #ifndef CCAN_CONTAINER_OF_H
2 #define CCAN_CONTAINER_OF_H
6 #include <ccan/check_type/check_type.h>
9 * container_of - get pointer to enclosing structure
10 * @member_ptr: pointer to the structure member
11 * @containing_type: the type this member is within
12 * @member: the name of this member within the structure.
14 * Given a pointer to a member of a structure, this macro does pointer
15 * subtraction to return the pointer to the enclosing type.
23 * int some_other_field;
27 * static struct info *foo_to_info(struct foo *foo)
29 * return container_of(foo, struct info, my_foo);
32 #define container_of(member_ptr, containing_type, member) \
33 ((containing_type *) \
34 ((char *)(member_ptr) \
35 - container_off(containing_type, member)) \
36 + check_types_match(*(member_ptr), ((containing_type *)0)->member))
39 * container_off - get offset to enclosing structure
40 * @containing_type: the type this member is within
41 * @member: the name of this member within the structure.
43 * Given a pointer to a member of a structure, this macro does
44 * typechecking and figures out the offset to the enclosing type.
52 * int some_other_field;
56 * static struct info *foo_to_info(struct foo *foo)
58 * size_t off = container_off(struct info, my_foo);
59 * return (void *)((char *)foo - off);
62 #define container_off(containing_type, member) \
63 offsetof(containing_type, member)
66 * container_of_var - get pointer to enclosing structure using a variable
67 * @member_ptr: pointer to the structure member
68 * @container_var: a pointer of same type as this member's container
69 * @member: the name of this member within the structure.
71 * Given a pointer to a member of a structure, this macro does pointer
72 * subtraction to return the pointer to the enclosing type.
75 * static struct info *foo_to_i(struct foo *foo)
77 * struct info *i = container_of_var(foo, i, my_foo);
82 #define container_of_var(member_ptr, container_var, member) \
83 container_of(member_ptr, typeof(*container_var), member)
85 #define container_of_var(member_ptr, container_var, member) \
86 ((void *)((char *)(member_ptr) - \
87 container_off_var(container_var, member)))
91 * container_off_var - get offset of a field in enclosing structure
92 * @container_var: a pointer to a container structure
93 * @member: the name of a member within the structure.
95 * Given (any) pointer to a structure and a its member name, this
96 * macro does pointer subtraction to return offset of member in a
97 * structure memory layout.
101 #define container_off_var(var, member) \
102 container_off(typeof(*var), member)
104 #define container_off_var(var, member) \
105 ((char *)&(var)->member - (char *)(var))
108 #endif /* CCAN_CONTAINER_OF_H */