From patchwork Fri Jan 10 21:29:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 104536 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C867A3858039 for ; Fri, 10 Jan 2025 21:35:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C867A3858039 Authentication-Results: sourceware.org; dkim=fail reason="signature verification failed" (768-bit key, unprotected) header.d=tromey.com header.i=@tromey.com header.a=rsa-sha256 header.s=default header.b=oIrhwMu1 X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta038.useast.a.cloudfilter.net (omta038.useast.a.cloudfilter.net [44.202.169.37]) by sourceware.org (Postfix) with ESMTPS id 32F6D3857C6C for ; Fri, 10 Jan 2025 21:29:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 32F6D3857C6C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 32F6D3857C6C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.37 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736544562; cv=none; b=u2eHcGtvO8zMVKKBFSFpmCVcEasLipot4HDD2QohfzAlzaiCOzU5dYagevy3WbKVcpleuiHL4Y7HF1ptEJEJzjCUIGRbH+Nl11rAl1/E0g3CU0pHbUY3+UaNAqfdd5Ytce8+CRGklCEG0KVyTM2T0TyDc8a7b1Tqm0mMfY4XwAA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736544562; c=relaxed/simple; bh=ubASIFHJBbHgLiEcBxWQZlTcgI1rh7bCG8kP8hCBe2Y=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=hR2kiOtDBjhKhWHPnljDLL6q5gHjQ61bApkds9WYvUPr5OpFn3YDpj1YEo6tmj169oCrpFxWwquCAJ1LvfTN2jOdTIkuhq3xEIwfsrQdJE7JF6RhBPhpCmCsc6OG57OfDWI6aMcjedg0wNunIPkx5Og/mk8OAeq+TAUBGvZq29E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 32F6D3857C6C Received: from eig-obgw-6008a.ext.cloudfilter.net ([10.0.30.227]) by cmsmtp with ESMTPS id VtEKttoKWjMK7WMZEtpaxJ; Fri, 10 Jan 2025 21:29:21 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id WMZEtEP2w7R7JWMZEtypRn; Fri, 10 Jan 2025 21:29:20 +0000 X-Authority-Analysis: v=2.4 cv=Yaq75RRf c=1 sm=1 tr=0 ts=67819130 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=VdSt8ZQiCzkA:10 a=ItBw4LHWJt0A:10 a=J8VhU67wQ4pyfjdV97sA:9 a=QEXdDO2ut3YA:10 a=6Ogn3jAGHLSNbaov7Orx:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Cc:To:In-Reply-To:References:Message-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From:Sender: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=UAfWUdfBF1V3vCymfk9ME3/uM+z9KzsMhJgVtkzanj0=; b=oIrhwMu1UeUrf8zVsuNc5E9AvU vEOPqcmcUQnMsZuWH/mAJahiTARkM8oXqJh+vT4IiFbW7AyUkBgoIbb9FvdP44GozQL6gI/bXwEUg SaW9q3aj0nLPX84DHqtlLfT4C; Received: from 97-118-36-154.hlrn.qwest.net ([97.118.36.154]:40752 helo=prentzel.local) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1tWMZD-0042hb-2T; Fri, 10 Jan 2025 14:29:19 -0700 From: Tom Tromey Date: Fri, 10 Jan 2025 14:29:17 -0700 Subject: [PATCH v2 2/6] Use gdb::unordered_set in linespec_state MIME-Version: 1.0 Message-Id: <20250110-linespec-state-cxx-v2-2-a17144fa5c36@tromey.com> References: <20250110-linespec-state-cxx-v2-0-a17144fa5c36@tromey.com> In-Reply-To: <20250110-linespec-state-cxx-v2-0-a17144fa5c36@tromey.com> To: gdb-patches@sourceware.org Cc: Tom Tromey X-Mailer: b4 0.14.2 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.118.36.154 X-Source-L: No X-Exim-ID: 1tWMZD-0042hb-2T X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-118-36-154.hlrn.qwest.net (prentzel.local) [97.118.36.154]:40752 X-Source-Auth: tom+tromey.com X-Email-Count: 5 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfBsR5bOxtAzI6aePNKLFjNeOmFWn749F+/YCxp7kikhDGk+h9EAbGTYjTgM9paG0LeTTmBSh3DrYK5TBb9JU9fynYMhbs2hILs2uPvcOUgmx67XIRYSC o40SLMLWdkR9kzjwamb1n5GAGzeOilENdMkuUW9Lqh2Cpv7gmQEpbjnx6JaD77nNyeT7X8P5HPwu+sXPYSupbI9bYxmRNeo9bA8= X-Spam-Status: No, score=-3019.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org This patch changes linespec_state to use gdb::unordered_set. This simplifies the code a little and removes some manual management. It also replaces address_entry with a std::pair, which simplifies the code even more; and since this is a private type, IMO it doesn't reduce readability at all. --- gdb/linespec.c | 89 +++++++++++++++------------------------------------------- 1 file changed, 22 insertions(+), 67 deletions(-) diff --git a/gdb/linespec.c b/gdb/linespec.c index 86dbbacbba8859898b4fbe2e896f66f1256c8aeb..03a76ae6afd227c0b35dbd3307107f76f4feb29a 100644 --- a/gdb/linespec.c +++ b/gdb/linespec.c @@ -76,16 +76,6 @@ enum class linespec_complete_what KEYWORD, }; -/* An address entry is used to ensure that any given location is only - added to the result a single time. It holds an address and the - program space from which the address came. */ - -struct address_entry -{ - struct program_space *pspace; - CORE_ADDR addr; -}; - /* A linespec. Elements of this structure are filled in by a parser (either parse_linespec or some other function). The structure is then converted into SALs by convert_linespec_to_sals. */ @@ -132,29 +122,6 @@ struct linespec_canonical_name struct symtab *symtab; }; -/* A hash function for address_entry. */ - -static hashval_t -hash_address_entry (const void *p) -{ - const address_entry *aep = (const address_entry *) p; - hashval_t hash; - - hash = iterative_hash_object (aep->pspace, 0); - return iterative_hash_object (aep->addr, hash); -} - -/* An equality function for address_entry. */ - -static int -eq_address_entry (const void *a, const void *b) -{ - const address_entry *aea = (const address_entry *) a; - const address_entry *aeb = (const address_entry *) b; - - return aea->pspace == aeb->pspace && aea->addr == aeb->addr; -} - /* An instance of this is used to keep all state while linespec operates. This instance is passed around as a 'this' pointer to the various implementation methods. */ @@ -174,9 +141,7 @@ struct linespec_state default_line (default_line), funfirstline ((flags & DECODE_LINE_FUNFIRSTLINE) ? 1 : 0), list_mode ((flags & DECODE_LINE_LIST_MODE) ? 1 : 0), - canonical (canonical), - addr_set (htab_create_alloc (10, hash_address_entry, eq_address_entry, - xfree, xcalloc, xfree)) + canonical (canonical) { } @@ -187,12 +152,18 @@ struct linespec_state ~linespec_state () { - htab_delete (addr_set); xfree (canonical_names); } DISABLE_COPY_AND_ASSIGN (linespec_state); + /* Add ADDR to the address set. Return true if this is a new + entry. */ + bool maybe_add_address (program_space *pspace, CORE_ADDR addr) + { + return addr_set.emplace (pspace, addr).second; + } + /* The language in use during linespec processing. */ const struct language_defn *language; @@ -222,12 +193,19 @@ struct linespec_state /* Canonical strings that mirror the std::vector result. */ linespec_canonical_name *canonical_names = nullptr; - /* This is a set of address_entry objects which is used to prevent - duplicate symbols from being entered into the result. */ - htab_t addr_set; - /* Are we building a linespec? */ int is_linespec = 0; + +private: + + /* An address entry is used to ensure that any given location is + only added to the result a single time. It holds an address and + the program space from which the address came. */ + using address_entry = std::pair<::program_space *, CORE_ADDR>; + + /* This is a set of address_entry objects which is used to prevent + duplicate symbols from being entered into the result. */ + gdb::unordered_set addr_set; }; /* This is a helper object that is used when collecting symbols into a @@ -1139,29 +1117,6 @@ add_sal_to_sals (struct linespec_state *self, } } -/* Check whether the address, represented by PSPACE and ADDR, is - already in the set. If so, return 0. Otherwise, add it and return - 1. */ - -static int -maybe_add_address (htab_t set, struct program_space *pspace, CORE_ADDR addr) -{ - struct address_entry e, *p; - void **slot; - - e.pspace = pspace; - e.addr = addr; - slot = htab_find_slot (set, &e, INSERT); - if (*slot) - return 0; - - p = XNEW (struct address_entry); - memcpy (p, &e, sizeof (struct address_entry)); - *slot = p; - - return 1; -} - /* A helper that walks over all matching symtabs in all objfiles and calls CALLBACK for each symbol matching NAME. If SEARCH_PSPACE is not NULL, then the search is restricted to just that program @@ -2243,7 +2198,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls) = sym.symbol->symtab ()->compunit ()->objfile ()->pspace (); if (symbol_to_sal (&sal, state->funfirstline, sym.symbol) - && maybe_add_address (state->addr_set, pspace, sal.pc)) + && state->maybe_add_address (pspace, sal.pc)) add_sal_to_sals (state, &sals, &sal, sym.symbol->natural_name (), 0); } @@ -2308,7 +2263,7 @@ convert_linespec_to_sals (struct linespec_state *state, linespec *ls) { symtab_and_line sal; if (symbol_to_sal (&sal, state->funfirstline, sym.symbol) - && maybe_add_address (state->addr_set, pspace, sal.pc)) + && state->maybe_add_address (pspace, sal.pc)) add_sal_to_sals (state, &sals, &sal, sym.symbol->natural_name (), 0); } @@ -4157,7 +4112,7 @@ minsym_found (struct linespec_state *self, struct objfile *objfile, sal.section = msymbol->obj_section (objfile); - if (maybe_add_address (self->addr_set, objfile->pspace (), sal.pc)) + if (self->maybe_add_address (objfile->pspace (), sal.pc)) add_sal_to_sals (self, result, &sal, msymbol->natural_name (), 0); }