|
| 1 | +/* CC0 (Public domain) - see ccan/licenses/CC0 file for details */ |
| 2 | +#ifndef CCAN_CONTAINER_OF_H |
| 3 | +#define CCAN_CONTAINER_OF_H |
| 4 | +#include "ccan/check_type/check_type.h" |
| 5 | + |
| 6 | +/** |
| 7 | + * container_of - get pointer to enclosing structure |
| 8 | + * @member_ptr: pointer to the structure member |
| 9 | + * @containing_type: the type this member is within |
| 10 | + * @member: the name of this member within the structure. |
| 11 | + * |
| 12 | + * Given a pointer to a member of a structure, this macro does pointer |
| 13 | + * subtraction to return the pointer to the enclosing type. |
| 14 | + * |
| 15 | + * Example: |
| 16 | + * struct foo { |
| 17 | + * int fielda, fieldb; |
| 18 | + * // ... |
| 19 | + * }; |
| 20 | + * struct info { |
| 21 | + * int some_other_field; |
| 22 | + * struct foo my_foo; |
| 23 | + * }; |
| 24 | + * |
| 25 | + * static struct info *foo_to_info(struct foo *foo) |
| 26 | + * { |
| 27 | + * return container_of(foo, struct info, my_foo); |
| 28 | + * } |
| 29 | + */ |
| 30 | +#define container_of(member_ptr, containing_type, member) \ |
| 31 | + ((containing_type *) \ |
| 32 | + ((char *)(member_ptr) \ |
| 33 | + - container_off(containing_type, member)) \ |
| 34 | + + check_types_match(*(member_ptr), ((containing_type *)0)->member)) |
| 35 | + |
| 36 | + |
| 37 | +/** |
| 38 | + * container_of_or_null - get pointer to enclosing structure, or NULL |
| 39 | + * @member_ptr: pointer to the structure member |
| 40 | + * @containing_type: the type this member is within |
| 41 | + * @member: the name of this member within the structure. |
| 42 | + * |
| 43 | + * Given a pointer to a member of a structure, this macro does pointer |
| 44 | + * subtraction to return the pointer to the enclosing type, unless it |
| 45 | + * is given NULL, in which case it also returns NULL. |
| 46 | + * |
| 47 | + * Example: |
| 48 | + * struct foo { |
| 49 | + * int fielda, fieldb; |
| 50 | + * // ... |
| 51 | + * }; |
| 52 | + * struct info { |
| 53 | + * int some_other_field; |
| 54 | + * struct foo my_foo; |
| 55 | + * }; |
| 56 | + * |
| 57 | + * static struct info *foo_to_info_allowing_null(struct foo *foo) |
| 58 | + * { |
| 59 | + * return container_of_or_null(foo, struct info, my_foo); |
| 60 | + * } |
| 61 | + */ |
| 62 | +static inline char *container_of_or_null_(void *member_ptr, size_t offset) |
| 63 | +{ |
| 64 | + return member_ptr ? (char *)member_ptr - offset : NULL; |
| 65 | +} |
| 66 | +#define container_of_or_null(member_ptr, containing_type, member) \ |
| 67 | + ((containing_type *) \ |
| 68 | + container_of_or_null_(member_ptr, \ |
| 69 | + container_off(containing_type, member)) \ |
| 70 | + + check_types_match(*(member_ptr), ((containing_type *)0)->member)) |
| 71 | + |
| 72 | +/** |
| 73 | + * container_off - get offset to enclosing structure |
| 74 | + * @containing_type: the type this member is within |
| 75 | + * @member: the name of this member within the structure. |
| 76 | + * |
| 77 | + * Given a pointer to a member of a structure, this macro does |
| 78 | + * typechecking and figures out the offset to the enclosing type. |
| 79 | + * |
| 80 | + * Example: |
| 81 | + * struct foo { |
| 82 | + * int fielda, fieldb; |
| 83 | + * // ... |
| 84 | + * }; |
| 85 | + * struct info { |
| 86 | + * int some_other_field; |
| 87 | + * struct foo my_foo; |
| 88 | + * }; |
| 89 | + * |
| 90 | + * static struct info *foo_to_info(struct foo *foo) |
| 91 | + * { |
| 92 | + * size_t off = container_off(struct info, my_foo); |
| 93 | + * return (void *)((char *)foo - off); |
| 94 | + * } |
| 95 | + */ |
| 96 | +#define container_off(containing_type, member) \ |
| 97 | + offsetof(containing_type, member) |
| 98 | + |
| 99 | +/** |
| 100 | + * container_of_var - get pointer to enclosing structure using a variable |
| 101 | + * @member_ptr: pointer to the structure member |
| 102 | + * @container_var: a pointer of same type as this member's container |
| 103 | + * @member: the name of this member within the structure. |
| 104 | + * |
| 105 | + * Given a pointer to a member of a structure, this macro does pointer |
| 106 | + * subtraction to return the pointer to the enclosing type. |
| 107 | + * |
| 108 | + * Example: |
| 109 | + * static struct info *foo_to_i(struct foo *foo) |
| 110 | + * { |
| 111 | + * struct info *i = container_of_var(foo, i, my_foo); |
| 112 | + * return i; |
| 113 | + * } |
| 114 | + */ |
| 115 | +#if HAVE_TYPEOF |
| 116 | +#define container_of_var(member_ptr, container_var, member) \ |
| 117 | + container_of(member_ptr, typeof(*container_var), member) |
| 118 | +#else |
| 119 | +#define container_of_var(member_ptr, container_var, member) \ |
| 120 | + ((void *)((char *)(member_ptr) - \ |
| 121 | + container_off_var(container_var, member))) |
| 122 | +#endif |
| 123 | + |
| 124 | +/** |
| 125 | + * container_off_var - get offset of a field in enclosing structure |
| 126 | + * @container_var: a pointer to a container structure |
| 127 | + * @member: the name of a member within the structure. |
| 128 | + * |
| 129 | + * Given (any) pointer to a structure and a its member name, this |
| 130 | + * macro does pointer subtraction to return offset of member in a |
| 131 | + * structure memory layout. |
| 132 | + * |
| 133 | + */ |
| 134 | +#if HAVE_TYPEOF |
| 135 | +#define container_off_var(var, member) \ |
| 136 | + container_off(typeof(*var), member) |
| 137 | +#else |
| 138 | +#define container_off_var(var, member) \ |
| 139 | + ((const char *)&(var)->member - (const char *)(var)) |
| 140 | +#endif |
| 141 | + |
| 142 | +#endif /* CCAN_CONTAINER_OF_H */ |
0 commit comments