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) - offsetof(containing_type, member)) \
35 - check_types_match(*(member_ptr), ((containing_type *)0)->member))
39 * container_of_var - get pointer to enclosing structure using a variable
40 * @member_ptr: pointer to the structure member
41 * @var: a pointer to a structure of same type as this member is within
42 * @member: the name of this member within the structure.
44 * Given a pointer to a member of a structure, this macro does pointer
45 * subtraction to return the pointer to the enclosing type.
48 * static struct info *foo_to_i(struct foo *foo)
50 * struct info *i = container_of_var(foo, i, my_foo);
55 #define container_of_var(member_ptr, var, member) \
56 container_of(member_ptr, typeof(*var), member)
58 #define container_of_var(member_ptr, var, member) \
59 ((void *)((char *)(member_ptr) \
60 - ((char *)&(var)->member - (char *)(var))))
63 #endif /* CCAN_CONTAINER_OF_H */