From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by dpdk.org (Postfix) with ESMTP id 29032FB49 for ; Tue, 20 Dec 2016 12:12:16 +0100 (CET) Received: by mail-wm0-f68.google.com with SMTP id g23so23713278wme.1 for ; Tue, 20 Dec 2016 03:12:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=T+rKJB7dzdFzk0/b3hpq3AO6pmHz3NP5abxSAH4N4w0=; b=NOOIkQokXXmKQU9sTVmTxT6Y1Npu6PA+M+yQgBqx5y/dn9vF+ESs2zMe+6mDPvxCRb AWCoUAZIGbBDTsAsVBJHT/DPw2JRsgyGhzvQ20jUL+2Qf56apFS6hqITEVlaB/cJA3ux RQ34l6gmKVfJwSgurCXxI3A7rAVZE85XHe1s5mQ0gjbi4bgcLxXgcEx1gyxFS9gqEHDI XeEeXXE+N2/GMPzHoftaJczv8aUpPMNg0okHfGMOUSVseir4GdIkFRO0tzzsDWROaLyS +CxSWPAQJ8vKetca7h1Kw5oFCloiLZo6AiED3z3ArOgAmew/dAcqaEPvfqzmqX9x28dU 0tpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=T+rKJB7dzdFzk0/b3hpq3AO6pmHz3NP5abxSAH4N4w0=; b=ctX0Jv/+fztEDX1uJy67XpHpCgnc+spJJEAg7Wwc6oGGFUWcTIxMZjo/7d+1PoysnC UH9hBDopTdnlQxm6/YVYMsueqT7ZFPxBEPWaC7Jui9C9GglWxMdsvnmQuhqsKI91lnQF oB5hGFZDYGMMwk15qVK4wwrRDsEMXYy1eB/jtWQdO7xuORX4rKCSK2bdmxdSV2PG6CIP PJVbMQ68YXamxAtKShbfsj9EBo5w0XLPvOeVbnKuCTS8eEkmd1DTzIkn4NoiR0DDVKoc wkEtggHr33g1p1EQkW1jL5y8cZTGxwSi46Yhvk8YRGUkK+TqLnb3jKrGZewdWkDf7VfQ sKEQ== X-Gm-Message-State: AIkVDXJ7jZDA4PPPmjzrK7MKFNE7DEMPMxnYqFki7Tuu/t665YqqYZKZpllpmAZDxjnVhw== X-Received: by 10.28.217.195 with SMTP id q186mr1459957wmg.17.1482232336501; Tue, 20 Dec 2016 03:12:16 -0800 (PST) Received: from weierstrass.local.net ([91.200.109.169]) by smtp.gmail.com with ESMTPSA id q65sm21527756wmd.6.2016.12.20.03.12.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Dec 2016 03:12:15 -0800 (PST) Sender: Jan Blunck From: Jan Blunck To: dev@dpdk.org Cc: shreyansh.jain@nxp.com, david.marchand@6wind.com, stephen@networkplumber.org, Jan Viktorin Date: Tue, 20 Dec 2016 12:11:47 +0100 Message-Id: <1482232315-21626-2-git-send-email-jblunck@infradead.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1482232315-21626-1-git-send-email-jblunck@infradead.org> References: <1482232315-21626-1-git-send-email-jblunck@infradead.org> Subject: [dpdk-dev] [PATCH v3 1/9] eal: define container_of macro X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Dec 2016 11:12:18 -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 Signed-off-by: Shreyansh Jain [jblunck@infradead.org: add type checking and __extension__] Signed-off-by: Jan Blunck --- 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