From patchwork Sun Nov 20 10:05:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Blunck X-Patchwork-Id: 17097 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 5092737B2; Sun, 20 Nov 2016 11:07:11 +0100 (CET) Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by dpdk.org (Postfix) with ESMTP id B32012BD6 for ; Sun, 20 Nov 2016 11:07:08 +0100 (CET) Received: by mail-wm0-f68.google.com with SMTP id m203so18247189wma.3 for ; Sun, 20 Nov 2016 02:07:08 -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=TzLMqF+14hI2gWMp6lTq0bPntOpsBUXHJmqsjnmNFkY2xqv5IcNbCli1ae1kW/K3lo 8UndnTnrl/T/mOa/u4URq/hC02rmHqyVe4orv8JiqOHo/fjIX8g1dkGBoEKTkh3woxjm ZEXvlMCq9TqInPXSABYFUcvLm7HT6sJQIRgJoZtaeidLK/wGiNKVPR1s+slJbUjvgwrL yfZUBEautl7SsTfJQuZoPucNQ6p38+dR1UfLF69ZtUhfBn4JmxRgX/XrI7rpdQnHPllY zdjC6yRyDmeepF/tyczNvMlGky0eKQjoxJ/lzAyM5y6tdj6brReJ4PTtk+V+FmwISxUy 6oPg== 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=OS8zlTjlfAU+VvYcKT9S2BpgKZ4kFyHHdSG6zj294Voq6GILW8xmTgOzP10QRWD/pm 4551/4uPo7zd3mdPBFZ7LxZoiO80RiXirgXrkgbqOZSax4dEvSRrfKULoyE21XGGUj/1 ajRAi9BM0BfHsBGddLHwRIezTRKLY+gZNXKtWq14+38atsoExSkJOmBO1MXD87Jw5sor El4PjHjXTo66408svgaAasqnZ9Qb2aJ3PUGz2vvHe42ZtzjCWRnRdpRWRcbi/w7EW9Az CGD0TCtpNgclqEzkcxi2c/4Y6D7VE7JASijI5r1xHfhYexeaGK1F8WdV82oyBlbLteze MjAw== X-Gm-Message-State: AKaTC01+MLVJI8ZMEXCHYqGqI9MnEk9Autsgbjiok902JhZIMGwTOEoE1vR2PyExIFUIyg== X-Received: by 10.194.115.70 with SMTP id jm6mr5262234wjb.140.1479636428446; Sun, 20 Nov 2016 02:07:08 -0800 (PST) Received: from weierstrass.local.net ([91.200.110.70]) by smtp.gmail.com with ESMTPSA id d184sm13235434wmd.8.2016.11.20.02.07.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 20 Nov 2016 02:07:07 -0800 (PST) From: Jan Blunck To: dev@dpdk.org Cc: shreyansh.jain@nxp.com, Jan Viktorin Date: Sun, 20 Nov 2016 11:05:21 +0100 Message-Id: <1479636327-4166-1-git-send-email-jblunck@infradead.org> X-Mailer: git-send-email 2.7.4 Subject: [dpdk-dev] [PATCH 1/7] 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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)