X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fcontainer_of%2Fcontainer_of.h;h=2a6b1cd54e9dcf97f57b0b282e23bb7de82ae0db;hp=ec66f106f2aeb0144b17455973607b873ae88731;hb=da72623aec30213a593bd23dca80c89416598f75;hpb=a8b248ea9de55316cac4423a99a727ca7b54e0fc diff --git a/ccan/container_of/container_of.h b/ccan/container_of/container_of.h index ec66f106..2a6b1cd5 100644 --- a/ccan/container_of/container_of.h +++ b/ccan/container_of/container_of.h @@ -15,13 +15,16 @@ * subtraction to return the pointer to the enclosing type. * * Example: - * struct info - * { + * struct foo { + * int fielda, fieldb; + * // ... + * }; + * struct info { * int some_other_field; * struct foo my_foo; * }; * - * struct info *foo_to_info(struct foo *foop) + * static struct info *foo_to_info(struct foo *foo) * { * return container_of(foo, struct info, my_foo); * } @@ -42,24 +45,19 @@ * subtraction to return the pointer to the enclosing type. * * Example: - * struct info - * { - * int some_other_field; - * struct foo my_foo; - * }; - * - * struct info *foo_to_info(struct foo *foop) + * static struct info *foo_to_i(struct foo *foo) * { * struct info *i = container_of_var(foo, i, my_foo); * return i; * } */ -#ifdef HAVE_TYPEOF +#if HAVE_TYPEOF #define container_of_var(member_ptr, var, member) \ container_of(member_ptr, typeof(*var), member) #else -#define container_of_var(member_ptr, var, member) \ - ((void *)((char *)(member_ptr) - offsetof(containing_type, member))) +#define container_of_var(member_ptr, var, member) \ + ((void *)((char *)(member_ptr) \ + - ((char *)&(var)->member - (char *)(var)))) #endif #endif /* CCAN_CONTAINER_OF_H */