]> git.ozlabs.org Git - ccan/commitdiff
New variant of container_of: container_of_var, for list.h
authorRusty Russell <rusty@vivaldi>
Fri, 28 Dec 2007 12:59:11 +0000 (23:59 +1100)
committerRusty Russell <rusty@vivaldi>
Fri, 28 Dec 2007 12:59:11 +0000 (23:59 +1100)
container_of/container_of.h
container_of/test/compile_fail-bad-type.c
container_of/test/compile_fail-types.c
container_of/test/compile_fail-var-types.c [new file with mode: 0644]
container_of/test/run.c

index bdb23d07a163e2e0933568db35f0892c1dce7b5c..1f4b18e4426a1c3c0bfc5c47e2a4e807ae7e2a52 100644 (file)
          - check_types_match(*(member_ptr), ((containing_type *)0)->member))
 
 
+/**
+ * container_of_var - get pointer to enclosing structure using a variable
+ * @member_ptr: pointer to the structure member
+ * @var: a pointer to a structure of same type as this member is within
+ * @member: the name of this member within the structure.
+ *
+ * Given a pointer to a member of a structure, this macro does pointer
+ * 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)
+ *     {
+ *             struct info *i = container_of_var(foo, i, my_foo);
+ *             return i;
+ *     }
+ */
+#ifdef 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)))
+#endif
+
 #endif /* CCAN_CONTAINER_OF_H */
index d372fa54672bc7ae9f24fb5a0c0626c17a08a4b9..01dfd454597d0eb7d43244a3af81396e1648fc70 100644 (file)
@@ -13,6 +13,7 @@ int main(int argc, char *argv[])
        char *p;
 
 #ifdef FAIL
+       /* p is a char *, but this gives a struct foo * */
        p = container_of(intp, struct foo, a);
 #else
        p = (char *)intp;
index 33d98784413337296b43db0c4e936757cdb0b027..69f02daf289d3ff8cfe82db09029f49f777babac 100644 (file)
@@ -12,6 +12,7 @@ int main(int argc, char *argv[])
        int *intp = &foo.a;
 
 #ifdef FAIL
+       /* b is a char, but intp is an int * */
        foop = container_of(intp, struct foo, b);
 #else
        foop = NULL;
diff --git a/container_of/test/compile_fail-var-types.c b/container_of/test/compile_fail-var-types.c
new file mode 100644 (file)
index 0000000..5c77679
--- /dev/null
@@ -0,0 +1,21 @@
+#include "container_of/container_of.h"
+#include <stdlib.h>
+
+struct foo {
+       int a;
+       char b;
+};
+
+int main(int argc, char *argv[])
+{
+       struct foo foo = { .a = 1, .b = 2 }, *foop;
+       int *intp = &foo.a;
+
+#ifdef FAIL
+       /* b is a char, but intp is an int * */
+       foop = container_of_var(intp, foop, b);
+#else
+       foop = NULL;
+#endif
+       return intp == NULL;
+}
index ff567325fd912b743894c25199b79c59f34a1b71..d08b06a667d17dbee81725e579c238a501396956 100644 (file)
@@ -12,8 +12,10 @@ int main(int argc, char *argv[])
        int *intp = &foo.a;
        char *charp = &foo.b;
 
-       plan_tests(2);
+       plan_tests(4);
        ok1(container_of(intp, struct foo, a) == &foo);
        ok1(container_of(charp, struct foo, b) == &foo);
+       ok1(container_of_var(intp, &foo, a) == &foo);
+       ok1(container_of_var(charp, &foo, b) == &foo);
        return exit_status();
 }