From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 509823858D33; Fri, 7 Apr 2023 15:49:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 509823858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680882548; bh=M/JksLa3/TAZga4ppY8ZBFE4OtQXWBklrGV5IByCY7s=; h=From:To:Subject:Date:From; b=y4vHtpj9ey71vkvqs7kI/GzvHNfWcyAVvWFhxiRmUFfEACVai2tjGMG8jmsdN0r1q fsQ/br+OjJ1H6FM10JqY4NuYettHYYB4b6f80PZgYO4c1vizgY5yu++WpjRgMpVlw0 jcnNIa6JVAFqMzCAOGLn7LzNeV/WAuSAwx0jtqZ0= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Rewrite Ada symbol cache X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 565059a2828112f10a6ef8395c4855a5b072e32f X-Git-Newrev: 9d1c303d52d7aeb2ad0217e53e2bf0a840b93316 Message-Id: <20230407154908.509823858D33@sourceware.org> Date: Fri, 7 Apr 2023 15:49:08 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D9d1c303d52d7= aeb2ad0217e53e2bf0a840b93316 commit 9d1c303d52d7aeb2ad0217e53e2bf0a840b93316 Author: Tom Tromey Date: Wed Mar 15 11:47:32 2023 -0600 Rewrite Ada symbol cache =20 In an experiment I'm trying, I needed Ada symbol cache entries to be allocated with 'new'. This patch reimplements the symbol cache to use the libiberty hash table and to use new and delete. A couple of other minor cleanups are done. Diff: --- gdb/ada-lang.c | 186 +++++++++++++++++++++++++----------------------------= ---- 1 file changed, 82 insertions(+), 104 deletions(-) diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 5301c72d514..d2d7dca5c1e 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -235,44 +235,6 @@ static const utf8_entry ada_case_fold[] =3D =20 =0C =20 -/* The result of a symbol lookup to be stored in our symbol cache. */ - -struct cache_entry -{ - /* The name used to perform the lookup. */ - const char *name; - /* The namespace used during the lookup. */ - domain_enum domain; - /* The symbol returned by the lookup, or NULL if no matching symbol - was found. */ - struct symbol *sym; - /* The block where the symbol was found, or NULL if no matching - symbol was found. */ - const struct block *block; - /* A pointer to the next entry with the same hash. */ - struct cache_entry *next; -}; - -/* The Ada symbol cache, used to store the result of Ada-mode symbol - lookups in the course of executing the user's commands. - - The cache is implemented using a simple, fixed-sized hash. - The size is fixed on the grounds that there are not likely to be - all that many symbols looked up during any given session, regardless - of the size of the symbol table. If we decide to go to a resizable - table, let's just use the stuff from libiberty instead. */ - -#define HASH_SIZE 1009 - -struct ada_symbol_cache -{ - /* An obstack used to store the entries in our cache. */ - struct auto_obstack cache_space; - - /* The root of the hash table used to implement our symbol cache. */ - struct cache_entry *root[HASH_SIZE] {}; -}; - static const char ada_completer_word_break_characters[] =3D #ifdef VMS " \t\n!@#%^&*()+=3D|~`}{[]\";:?/,-"; @@ -361,15 +323,58 @@ ada_inferior_exit (struct inferior *inf) =20 /* program-space-specific data. */ =20 -/* This module's per-program-space data. */ -struct ada_pspace_data +/* The result of a symbol lookup to be stored in our symbol cache. */ + +struct cache_entry { - /* The Ada symbol cache. */ - std::unique_ptr sym_cache; + /* The name used to perform the lookup. */ + std::string name; + /* The namespace used during the lookup. */ + domain_enum domain =3D UNDEF_DOMAIN; + /* The symbol returned by the lookup, or NULL if no matching symbol + was found. */ + struct symbol *sym =3D nullptr; + /* The block where the symbol was found, or NULL if no matching + symbol was found. */ + const struct block *block =3D nullptr; +}; + +/* The symbol cache uses this type when searching. */ + +struct cache_entry_search +{ + const char *name; + domain_enum domain; + + hashval_t hash () const + { + /* This must agree with hash_cache_entry, below. */ + return htab_hash_string (name); + } }; =20 +/* Hash function for cache_entry. */ + +static hashval_t +hash_cache_entry (const void *v) +{ + const cache_entry *entry =3D (const cache_entry *) v; + return htab_hash_string (entry->name.c_str ()); +} + +/* Equality function for cache_entry. */ + +static int +eq_cache_entry (const void *a, const void *b) +{ + const cache_entry *entrya =3D (const cache_entry *) a; + const cache_entry_search *entryb =3D (const cache_entry_search *) b; + + return entrya->domain =3D=3D entryb->domain && entrya->name =3D=3D entry= b->name; +} + /* Key to our per-program-space data. */ -static const registry::key +static const registry::key ada_pspace_data_handle; =20 /* Return this module's data for the given program space (PSPACE). @@ -377,14 +382,17 @@ static const registry::key =20 This function always returns a valid object. */ =20 -static struct ada_pspace_data * +static htab_t get_ada_pspace_data (struct program_space *pspace) { - struct ada_pspace_data *data; - - data =3D ada_pspace_data_handle.get (pspace); - if (data =3D=3D NULL) - data =3D ada_pspace_data_handle.emplace (pspace); + htab_t data =3D ada_pspace_data_handle.get (pspace); + if (data =3D=3D nullptr) + { + data =3D htab_create_alloc (10, hash_cache_entry, eq_cache_entry, + htab_delete_entry, + xcalloc, xfree); + ada_pspace_data_handle.set (pspace, data); + } =20 return data; } @@ -4635,49 +4643,12 @@ make_array_descriptor (struct type *type, struct va= lue *arr) even in this case, some expensive name-based symbol searches are still sometimes necessary - to find an XVZ variable, mostly. */ =20 -/* Return the symbol cache associated to the given program space PSPACE. - If not allocated for this PSPACE yet, allocate and initialize one. */ - -static struct ada_symbol_cache * -ada_get_symbol_cache (struct program_space *pspace) -{ - struct ada_pspace_data *pspace_data =3D get_ada_pspace_data (pspace); - - if (pspace_data->sym_cache =3D=3D nullptr) - pspace_data->sym_cache.reset (new ada_symbol_cache); - - return pspace_data->sym_cache.get (); -} - /* Clear all entries from the symbol cache. */ =20 static void ada_clear_symbol_cache () { - struct ada_pspace_data *pspace_data - =3D get_ada_pspace_data (current_program_space); - - if (pspace_data->sym_cache !=3D nullptr) - pspace_data->sym_cache.reset (); -} - -/* Search our cache for an entry matching NAME and DOMAIN. - Return it if found, or NULL otherwise. */ - -static struct cache_entry ** -find_entry (const char *name, domain_enum domain) -{ - struct ada_symbol_cache *sym_cache - =3D ada_get_symbol_cache (current_program_space); - int h =3D msymbol_hash (name) % HASH_SIZE; - struct cache_entry **e; - - for (e =3D &sym_cache->root[h]; *e !=3D NULL; e =3D &(*e)->next) - { - if (domain =3D=3D (*e)->domain && strcmp (name, (*e)->name) =3D=3D 0) - return e; - } - return NULL; + ada_pspace_data_handle.clear (current_program_space); } =20 /* Search the symbol cache for an entry matching NAME and DOMAIN. @@ -4690,14 +4661,19 @@ static int lookup_cached_symbol (const char *name, domain_enum domain, struct symbol **sym, const struct block **block) { - struct cache_entry **e =3D find_entry (name, domain); + htab_t tab =3D get_ada_pspace_data (current_program_space); + cache_entry_search search; + search.name =3D name; + search.domain =3D domain; =20 - if (e =3D=3D NULL) + cache_entry *e =3D (cache_entry *) htab_find_with_hash (tab, &search, + search.hash ()); + if (e =3D=3D nullptr) return 0; - if (sym !=3D NULL) - *sym =3D (*e)->sym; - if (block !=3D NULL) - *block =3D (*e)->block; + if (sym !=3D nullptr) + *sym =3D e->sym; + if (block !=3D nullptr) + *block =3D e->block; return 1; } =20 @@ -4708,11 +4684,6 @@ static void cache_symbol (const char *name, domain_enum domain, struct symbol *sym, const struct block *block) { - struct ada_symbol_cache *sym_cache - =3D ada_get_symbol_cache (current_program_space); - int h; - struct cache_entry *e; - /* Symbols for builtin types don't have a block. For now don't cache such symbols. */ if (sym !=3D NULL && !sym->is_objfile_owned ()) @@ -4730,14 +4701,21 @@ cache_symbol (const char *name, domain_enum domain,= struct symbol *sym, return; } =20 - h =3D msymbol_hash (name) % HASH_SIZE; - e =3D XOBNEW (&sym_cache->cache_space, cache_entry); - e->next =3D sym_cache->root[h]; - sym_cache->root[h] =3D e; - e->name =3D obstack_strdup (&sym_cache->cache_space, name); - e->sym =3D sym; + htab_t tab =3D get_ada_pspace_data (current_program_space); + cache_entry_search search; + search.name =3D name; + search.domain =3D domain; + + void **slot =3D htab_find_slot_with_hash (tab, &search, + search.hash (), INSERT); + + cache_entry *e =3D new cache_entry; + e->name =3D name; e->domain =3D domain; + e->sym =3D sym; e->block =3D block; + + *slot =3D e; } =0C /* Symbol Lookup */