From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <jblunck@gmail.com>
Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68])
 by dpdk.org (Postfix) with ESMTP id A17EA20F
 for <dev@dpdk.org>; Mon, 21 Nov 2016 17:57:05 +0100 (CET)
Received: by mail-wm0-f68.google.com with SMTP id u144so543999wmu.0
 for <dev@dpdk.org>; Mon, 21 Nov 2016 08:57:05 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
 h=sender:from:to:cc:subject:date:message-id;
 bh=T+rKJB7dzdFzk0/b3hpq3AO6pmHz3NP5abxSAH4N4w0=;
 b=PzsGFMHVD9yTnQoknJtQ1WmdvNfdASiULEVPLjTgb5ziPeampxrqzcn0XXelGuAiGI
 wk68/TOJOCNxL20HlrmecVsL3H4WYsCHmRSW7s9pXgIo/rYUrEk3HCd/KEB1MeWQ/7Zz
 3aFFFSk5fP1YGMajDsTCBWhcDT7SOgxhnX4jrLOSKWGD7wrozovSXizSvuvXGI78WIHE
 VJF+80rV9ojE0ORNpedXxVkyarOnshhd5y6fR4Ndrp7VBf/pAfRe/GTXGgibwHBuq6PK
 2MvDHqmneSWWyxyha/zJzH/ttsUJIKu0Qnj489Gyb0HbjlZB+PnOHnuhCJg8NLS6loVK
 B3+w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20130820;
 h=x-gm-message-state:sender:from:to:cc:subject:date:message-id;
 bh=T+rKJB7dzdFzk0/b3hpq3AO6pmHz3NP5abxSAH4N4w0=;
 b=T9iOMT6O+YtHkMLiYCA+b3/cizrCVAlP1lRH5jo7NVPksIhDgmvtiN+ajLhU338lx5
 qvIXPSU6xKQCh4E8nxZLqhszn59Ml3g4cbcGu2zX6zn0nnz/CkpJzwrsB7DcCd2rcAGm
 WERzb6vL5TMTkMUzSefIUnBlGwkDqSjaZM17o7LWJV31YeRDPD7dTiyXISUu4NqyLZLz
 Kh5Iln5y+fVtmxeUwGjbEJ9z0qm0yh588ptz0pqs9pbs1ar+KOS64PytfoajgGhUMVDd
 oHZKQQ2o6/Zf/VY3ymDswx4O5wyOpk+BXKlUeMZxzJUynyJXq6iP+MrK/Gz9LGFS7sZd
 z0/Q==
X-Gm-Message-State: AKaTC00GVtIS3vJy8GDXh+/HC3ubpXxiobVLIUxiNl/i0nTmZzbhIw12pNHh50FwjH4BxA==
X-Received: by 10.28.126.146 with SMTP id z140mr18035634wmc.84.1479747425249; 
 Mon, 21 Nov 2016 08:57:05 -0800 (PST)
Received: from weierstrass.local.net ([91.200.110.70])
 by smtp.gmail.com with ESMTPSA id v202sm20536051wmv.8.2016.11.21.08.57.03
 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
 Mon, 21 Nov 2016 08:57:04 -0800 (PST)
Sender: Jan Blunck <jblunck@gmail.com>
From: Jan Blunck <jblunck@infradead.org>
To: dev@dpdk.org
Cc: shreyansh.jain@nxp.com, david.marchand@6wind.com,
 Jan Viktorin <viktorin@rehivetech.com>
Date: Mon, 21 Nov 2016 17:55:15 +0100
Message-Id: <1479747322-5774-1-git-send-email-jblunck@infradead.org>
X-Mailer: git-send-email 2.7.4
Subject: [dpdk-dev] [PATCH v2 1/8] eal: define container_of macro
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches and discussions about DPDK <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Mon, 21 Nov 2016 16:57:05 -0000

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 | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/lib/librte_eal/common/include/rte_common.h b/lib/librte_eal/common/include/rte_common.h
index db5ac91..8dda3e2 100644
--- a/lib/librte_eal/common/include/rte_common.h
+++ b/lib/librte_eal/common/include/rte_common.h
@@ -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)
-- 
2.7.4