[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 12/10] libxl: New convenience macro CONTAINING_STRUCT



Provide a convenient and type-safe wrapper which does the correct
dance to subtract offsetof.

Signed-off-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
---
 tools/libxl/libxl_internal.h |   35 +++++++++++++++++++++++++++++++++++
 1 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index e0ff15c..e0c2ad6 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -1224,6 +1224,41 @@ _hidden void libxl__ao__destroy(libxl_ctx*, libxl__ao 
*ao);
  * Convenience macros.
  */
 
+/*
+ * [GET_]CONTAINING_STRUCT work like this.  Given:
+ *    typedef struct {
+ *      ...
+ *      member_type member_name;
+ *      ...
+ *    } outer_type;
+ * Then:
+ *    void GET_CONTAINING_STRUCT(outer_type *outer_var [NB:UPDATED],
+ *                               some_type *inner_ptr,
+ *                               member_name);
+ *    outer_type *CONTAINING_STRUCT(outer_type,
+ *                                  some_type *inner_ptr,
+ *                                  member_name);
+ * The semantics are that after:
+ *    outer_type outer, *outer_var;
+ *    member_type *inner_ptr = &outer->member_name;
+ *    GET_CONTAINING_STRUCT(outer_var, &outer_ptr->member_name, member_name)
+ * The following hold:
+ *    CONTAINING_STRUCT(inner_ptr, outer_type, member_name) == outer_ptr
+ *    outer_var == &outer
+ */
+#define GET_CONTAINING_STRUCT(outer_var, inner_ptr, member_name)        \
+    ((outer_var) = (void*)((char*)(inner_ptr) -                         \
+                           offsetof(typeof(*(outer_var)), member_name)), \
+     (void)(&(outer_var)->member_name ==                                \
+           (typeof(inner_ptr))0) /* type check */,                      \
+     (void)0)
+#define CONTAINING_STRUCT(outer_type, inner_ptr, member_name)           \
+    ({                                                                  \
+        typeof(outer_type) *containing_struct;                          \
+        GET_CONTAINING_STRUCT(containing_struct, inner_ptr, member_name); \
+        containing_struct;                                              \
+    })
+
 
 /*
  * All of these assume (or define)
-- 
1.7.2.5


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.