Imported Upstream version 17.05
[deb_dpdk.git] / lib / librte_eal / common / include / rte_common.h
index db5ac91..e057f6e 100644 (file)
@@ -331,6 +331,29 @@ 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__ ({                \
+                       const typeof(((type *)0)->member) *_ptr = (ptr); \
+                       __attribute__((unused)) type *_target_ptr =     \
+                               (type *)(ptr);                          \
+                       (type *)(((uintptr_t)_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)