[dpdk-dev] [PATCH v2 01/12] eal: define container_of macro

Shreyansh Jain shreyansh.jain at nxp.com
Tue Dec 13 14:37:01 CET 2016


From: Jan Blunck <jblunck at infradead.org>

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 at rehivetech.com>
[shreyansh.jain at nxp.com: Fix checkpatch error]
Signed-off-by: Shreyansh Jain <shreyansh.jain at nxp.com>
[jblunck at infradead.org: add type checking and __extension__]
Signed-off-by: Jan Blunck <jblunck at infradead.org>

--
v2:
 - fix checkpatch error
---
 lib/librte_eal/common/include/rte_common.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h
index db5ac91..3eb8d11 100644
--- a/lib/librte_eal/common/include/rte_common.h
+++ b/lib/librte_eal/common/include/rte_common.h
@@ -331,6 +331,27 @@ 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)
-- 
2.7.4



More information about the dev mailing list