From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wj0-f196.google.com (mail-wj0-f196.google.com [209.85.210.196]) by dpdk.org (Postfix) with ESMTP id 38F3E10C4D for ; Wed, 21 Dec 2016 16:10:02 +0100 (CET) Received: by mail-wj0-f196.google.com with SMTP id j10so32511868wjb.3 for ; Wed, 21 Dec 2016 07:10:02 -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=dQjM1FjF4zzVWFdfXEG9Na2FO0aGW4AJLVgz4ffZR5wJt7wyHe48Cj/SosMk+3PpXY nGB36bq27vexEZ203walN5ARyfXTQe+N3siYQkay6Vks6vkSioOhEiqAcn+KnIQJRglb /o3r6WXcebwYVIZdYCNoLiOT+7JrQWoX7J0ZquPp2+e9AE1CSAiogH9HrCNCNiSykvjJ UqeztlOY4vDxF7npBpRg0fUAQrG0qW8oDJF3a3kWH2LVR83+db0NNeQvxukqvH9h+ATt hwUA4wFNAj+a+me1ydkRePig4QkxF559Q2uVIxs3s0g4Av0e+VCLlTTfBCse8GK6XwGg oLjA== 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=FDlzzd8ksVS6WSEWNEWv9kfX12PMIsZepEevXMT/pUNeksC7vBLfeTUoEmAj6GAHhs WYgyK4GGIh0I6g34dnjpUzBi0q2IjtiH8/YU9+7VhXSE0S/FbDxvemASM76Vo04w4Axb 1Z2YxUBPV3xoQ7RFBuLyX59SpA0J9SJmS8pSt9VIFPf0UgKxa//kXMtF/Jm+qEIrwM2y CrsnMh8aGQGwjThvonuT4nQ635xHetsnjWNaJKr5UrCMwfMD5u3cpm6miwn772kbdudL w/UckqVEwe+vKhDK35qVJvIx9jCFNE8aeq0LCeUt2vwQ/quU08ujgO7cp8ejlCSySUPc b71w== X-Gm-Message-State: AIkVDXL5H2/9FyHhjGg83czVpGF6U2RuHrZNOA0Vw3TMexaxSP8hOxwqfe5aNlbmhksDNg== X-Received: by 10.194.127.68 with SMTP id ne4mr2344763wjb.82.1482333001767; Wed, 21 Dec 2016 07:10:01 -0800 (PST) Received: from weierstrass.local.net ([91.200.109.169]) by smtp.gmail.com with ESMTPSA id 135sm27627375wmh.14.2016.12.21.07.10.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Dec 2016 07:10:00 -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: Wed, 21 Dec 2016 16:09:24 +0100 Message-Id: <1482332986-7599-2-git-send-email-jblunck@infradead.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1482332986-7599-1-git-send-email-jblunck@infradead.org> References: <1482332986-7599-1-git-send-email-jblunck@infradead.org> Subject: [dpdk-dev] [PATCH v4 01/23] 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: Wed, 21 Dec 2016 15:10:02 -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