From patchwork Mon Aug 26 02:43:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 11113851 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 252091399 for ; Mon, 26 Aug 2019 02:44:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0BD212173E for ; Mon, 26 Aug 2019 02:44:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729209AbfHZCoD (ORCPT ); Sun, 25 Aug 2019 22:44:03 -0400 Received: from dcvr.yhbt.net ([64.71.152.64]:36474 "EHLO dcvr.yhbt.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729030AbfHZCoD (ORCPT ); Sun, 25 Aug 2019 22:44:03 -0400 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 5C4101F4C1; Mon, 26 Aug 2019 02:43:34 +0000 (UTC) From: Eric Wong To: Junio C Hamano Cc: git@vger.kernel.org Subject: [PATCH 10/11] introduce container_of macro Date: Mon, 26 Aug 2019 02:43:31 +0000 Message-Id: <20190826024332.3403-11-e@80x24.org> In-Reply-To: <20190826024332.3403-1-e@80x24.org> References: <20190826024332.3403-1-e@80x24.org> MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This macro is popular within the Linux kernel for supporting intrusive data structures such as linked lists, red-black trees, and chained hash tables while allowing the compiler to do type checking. I intend to use this to remove the limitation of "hashmap_entry" being location-dependent and to allow more compile-time type checking. This macro already exists in our source as "list_entry" in list.h and making "list_entry" an alias to "container_of" as the Linux kernel has done is a possibility. Signed-off-by: Eric Wong --- git-compat-util.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/git-compat-util.h b/git-compat-util.h index 83be89de0a..4cc2c8283a 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1312,4 +1312,14 @@ void unleak_memory(const void *ptr, size_t len); */ #include "banned.h" +/* + * container_of - Get the address of an object containing a field. + * + * @ptr: pointer to the field. + * @type: type of the object. + * @member: name of the field within the object. + */ +#define container_of(ptr, type, member) \ + ((type *) ((char *)(ptr) - offsetof(type, member))) + #endif