From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-it0-f54.google.com (mail-it0-f54.google.com [209.85.214.54]) by dpdk.org (Postfix) with ESMTP id 9804FFABC for ; Mon, 19 Dec 2016 22:59:56 +0100 (CET) Received: by mail-it0-f54.google.com with SMTP id x2so6486462itf.1 for ; Mon, 19 Dec 2016 13:59:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nD6+CNxo/SUnLbnft3WBywN7L+QRjpvBanaHqubc8AM=; b=thvXEhQQyQ4Bdku8doK6hAgKDMWzp1HwLf93jObfwQW5690snTjwV2BBG5WaKsd1da 1lq8N6e/SaC97UWkR2tAoL+a7rBOha40IbIJY6qHvEpTOvysny1DnWGCaZFy3zrJJNb8 OZI93e+XQVV+j7o/JxTpo03SH0ojvLNayKaaWlnFEx7wzML/BCDbgh3o+kkz3RVA/wHm +7qPptVkf/gOptM10P5MygWkMxolYEW8q/Lr9yM1ymZPU+zKWd8r2mEJ0oTHK3Fd34JQ 3VS9bGCFT9SgdTZ28wvHL2oiD1PR4/cSurihzMLKwvYbsCl/7+uiCQxN/BIoLoIhn0Xu pUzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nD6+CNxo/SUnLbnft3WBywN7L+QRjpvBanaHqubc8AM=; b=UcZAjj4YXePpao2XvcPUxrEtUT1q/S6EzAmv/88sQUImkCXDcBfIRJLxcBBbZIJNEf 85t4mPVPreP1w1tPP7U4C/dfWi38sW2junjpEqy7Qg2AG/M+o5dZF224ZmdVkOeRaRNr zPerjW93SG61OzuFU3FkonyJWE9A9hoTHBV8+1NoGe3U/yaQZZB5VS2tS4Tkk79Z8XY1 dkRdO+k4YO7brBn2L8iUi2rIRRXbRCnKUJao/pObybGQmvA9QiN9teV3A2UCwCOh+jqO BIcuiQIRxwzqM2J0T7CRMwFE0VICseaJWY9JqJSC1QXk2ZUW0GQPPxuQMDYwxECsAL8+ 2pdg== X-Gm-Message-State: AKaTC01QBEru04khOuyKbItrma71d/+POxOgp+rR9PUcCXt9I/zuOW+Hk8bmkSrVUWLatA== X-Received: by 10.36.64.75 with SMTP id n72mr17850119ita.105.1482184796000; Mon, 19 Dec 2016 13:59:56 -0800 (PST) Received: from xeon-e3.wavecable.com (204-195-18-65.wavecable.com. [204.195.18.65]) by smtp.gmail.com with ESMTPSA id p20sm8270581itc.2.2016.12.19.13.59.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Dec 2016 13:59:55 -0800 (PST) From: Stephen Hemminger X-Google-Original-From: Stephen Hemminger To: dev@dpdk.org Cc: Jan Blunck , Jan Viktorin , Shreyansh Jain Date: Mon, 19 Dec 2016 13:59:34 -0800 Message-Id: <20161219215944.17226-4-sthemmin@microsoft.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20161219215944.17226-1-sthemmin@microsoft.com> References: <20161219215944.17226-1-sthemmin@microsoft.com> Subject: [dpdk-dev] [PATCH 03/13] 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: Mon, 19 Dec 2016 21:59:56 -0000 From: Jan Blunck 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 db5ac91c..8dda3e29 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.11.0