eal: define container_of macro
authorJan Blunck <jblunck@infradead.org>
Fri, 23 Dec 2016 15:57:52 +0000 (16:57 +0100)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Sat, 24 Dec 2016 17:45:01 +0000 (18:45 +0100)
This macro is based on Jan Viktorin's original patch but also checks the
type of the passed pointer against the type of the member.

Signed-off-by: Jan Viktorin <viktorin@rehivetech.com>
Signed-off-by: Shreyansh Jain <shreyansh.jain@nxp.com>
[jblunck@infradead.org: add type checking and __extension__]
Signed-off-by: Jan Blunck <jblunck@infradead.org>
lib/librte_eal/common/include/rte_common.h

index db5ac91..8dda3e2 100644 (file)
@@ -331,6 +331,26 @@ rte_bsf32(uint32_t v)
 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
 #endif
 
+/**
+ * Return pointer to the wrapping struct instance.
+ *
+ * Example:
+ *
+ *  struct wrapper {
+ *      ...
+ *      struct child c;
+ *      ...
+ *  };
+ *
+ *  struct child *x = obtain(...);
+ *  struct wrapper *w = container_of(x, struct wrapper, c);
+ */
+#ifndef container_of
+#define container_of(ptr, type, member)        __extension__ ({                \
+                       typeof(((type *)0)->member) *_ptr = (ptr);      \
+                       (type *)(((char *)_ptr) - offsetof(type, member)); })
+#endif
+
 #define _RTE_STR(x) #x
 /** Take a macro value and get a string version of it */
 #define RTE_STR(x) _RTE_STR(x)