From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway20.websitewelcome.com (gateway20.websitewelcome.com [192.185.46.107]) by sourceware.org (Postfix) with ESMTPS id 70FD0386F435 for ; Sat, 24 Oct 2020 21:28:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 70FD0386F435 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tom@tromey.com Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway20.websitewelcome.com (Postfix) with ESMTP id 54916400DB67F for ; Sat, 24 Oct 2020 16:07:03 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id WQmYka48QLFNkWQmZkT4MM; Sat, 24 Oct 2020 16:08:59 -0500 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: 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=EUZTUn1kQW26SVZx08/q9GhDDadua/T9VZvxsmm0bDk=; b=pKPksYhFcYP2qR+n+U9/KgIlXU 4fDmjy7uUVbYrz4kbC9SD/j3lXqbz4wk8O6jcm9uHRciOwLlLy3WpFX2JeE3ebjbLOpQpLw8c96zi Bq84+/ZYluMvZr/06Bclix64P; Received: from 174-16-123-164.hlrn.qwest.net ([174.16.123.164]:36770 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1kWQmY-003Xst-QE; Sat, 24 Oct 2020 15:08:58 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 01/11] Use htab_t in varobj Date: Sat, 24 Oct 2020 15:08:46 -0600 Message-Id: <20201024210856.12021-2-tom@tromey.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20201024210856.12021-1-tom@tromey.com> References: <20201024210856.12021-1-tom@tromey.com> 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: 174.16.123.164 X-Source-L: No X-Exim-ID: 1kWQmY-003Xst-QE X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 174-16-123-164.hlrn.qwest.net (bapiya.Home) [174.16.123.164]:36770 X-Source-Auth: tom+tromey.com X-Email-Count: 4 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3033.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_ABUSEAT, RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 Oct 2020 21:28:08 -0000 varobj.c currently has its own hash table implementation. This patch replaces it with htab_t, simplifying the code. gdb/ChangeLog 2020-10-24 Tom Tromey * varobj.c (VAROBJ_TABLE_SIZE): Remove. (varobj_table): Now htab_t. (varobj_get_handle, install_variable, uninstall_variable): Update. (hash_varobj, eq_varobj_and_string): New functions. (hash_varobj): Update. --- gdb/ChangeLog | 9 +++++ gdb/varobj.c | 109 ++++++++++++++++---------------------------------- 2 files changed, 44 insertions(+), 74 deletions(-) diff --git a/gdb/varobj.c b/gdb/varobj.c index 85530e3f3cf..3af1ef22d7e 100644 --- a/gdb/varobj.c +++ b/gdb/varobj.c @@ -200,12 +200,8 @@ static int format_code[] = { 0, 't', 'd', 'x', 'o', 'z' }; /* Header of the list of root variable objects. */ static struct varobj_root *rootlist; -/* Prime number indicating the number of buckets in the hash table. */ -/* A prime large enough to avoid too many collisions. */ -#define VAROBJ_TABLE_SIZE 227 - /* Pointer to the varobj hash table (built at run time). */ -static struct vlist **varobj_table; +static htab_t varobj_table; @@ -432,24 +428,13 @@ varobj_gen_name (void) struct varobj * varobj_get_handle (const char *objname) { - struct vlist *cv; - const char *chp; - unsigned int index = 0; - unsigned int i = 1; - - for (chp = objname; *chp; chp++) - { - index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; - } + varobj *var = (varobj *) htab_find_with_hash (varobj_table, objname, + htab_hash_string (objname)); - cv = *(varobj_table + index); - while (cv != NULL && cv->var->obj_name != objname) - cv = cv->next; - - if (cv == NULL) + if (var == NULL) error (_("Variable object not found")); - return cv->var; + return var; } /* Given the handle, return the name of the object. */ @@ -1793,29 +1778,15 @@ delete_variable_1 (int *delcountp, struct varobj *var, bool only_children_p, static bool install_variable (struct varobj *var) { - struct vlist *cv; - struct vlist *newvl; - const char *chp; - unsigned int index = 0; - unsigned int i = 1; - - for (chp = var->obj_name.c_str (); *chp; chp++) - { - index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; - } - - cv = *(varobj_table + index); - while (cv != NULL && cv->var->obj_name != var->obj_name) - cv = cv->next; - - if (cv != NULL) + hashval_t hash = htab_hash_string (var->obj_name.c_str ()); + void **slot = htab_find_slot_with_hash (varobj_table, + var->obj_name.c_str (), + hash, INSERT); + if (*slot != nullptr) error (_("Duplicate variable object name")); /* Add varobj to hash table. */ - newvl = XNEW (struct vlist); - newvl->next = *(varobj_table + index); - newvl->var = var; - *(varobj_table + index) = newvl; + *slot = var; /* If root, add varobj to root list. */ if (is_root_p (var)) @@ -1835,46 +1806,15 @@ install_variable (struct varobj *var) static void uninstall_variable (struct varobj *var) { - struct vlist *cv; - struct vlist *prev; struct varobj_root *cr; struct varobj_root *prer; - const char *chp; - unsigned int index = 0; - unsigned int i = 1; - - /* Remove varobj from hash table. */ - for (chp = var->obj_name.c_str (); *chp; chp++) - { - index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; - } - cv = *(varobj_table + index); - prev = NULL; - while (cv != NULL && cv->var->obj_name != var->obj_name) - { - prev = cv; - cv = cv->next; - } + hashval_t hash = htab_hash_string (var->obj_name.c_str ()); + htab_remove_elt_with_hash (varobj_table, var->obj_name.c_str (), hash); if (varobjdebug) fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name.c_str ()); - if (cv == NULL) - { - warning - ("Assertion failed: Could not find variable object \"%s\" to delete", - var->obj_name.c_str ()); - return; - } - - if (prev == NULL) - *(varobj_table + index) = cv->next; - else - prev->next = cv->next; - - xfree (cv); - /* If root, remove varobj from root list. */ if (is_root_p (var)) { @@ -2520,11 +2460,32 @@ varobj_invalidate (void) all_root_varobjs (varobj_invalidate_iter, NULL); } +/* A hash function for a varobj. */ + +static hashval_t +hash_varobj (const void *a) +{ + const varobj *obj = (const varobj *) a; + return htab_hash_string (obj->obj_name.c_str ()); +} + +/* A hash table equality function for varobjs. */ + +static int +eq_varobj_and_string (const void *a, const void *b) +{ + const varobj *obj = (const varobj *) a; + const char *name = (const char *) b; + + return obj->obj_name == name; +} + void _initialize_varobj (); void _initialize_varobj () { - varobj_table = XCNEWVEC (struct vlist *, VAROBJ_TABLE_SIZE); + varobj_table = htab_create_alloc (5, hash_varobj, eq_varobj_and_string, + nullptr, xcalloc, xfree); add_setshow_zuinteger_cmd ("varobj", class_maintenance, &varobjdebug, -- 2.17.2