diff options
author | Simon Marchi <simon.marchi@efficios.com> | 2024-11-04 13:27:53 -0500 |
---|---|---|
committer | Simon Marchi <simon.marchi@polymtl.ca> | 2024-11-25 22:07:04 -0500 |
commit | 9c0818db31b7380779ec7904514a699cd22d8dbb (patch) | |
tree | 578ba3370b0f560f0e71ceb8eccde38667186978 /gdb | |
parent | e4ed0965e371f1eb18d5d87b1a6af9fb64d07839 (diff) | |
download | binutils-9c0818db31b7380779ec7904514a699cd22d8dbb.zip binutils-9c0818db31b7380779ec7904514a699cd22d8dbb.tar.gz binutils-9c0818db31b7380779ec7904514a699cd22d8dbb.tar.bz2 |
Convert gdb_bfd.c to new hash table
This converts the BFD cache in gdb_bfd.c to use the new hash table.
Change-Id: Ib6257fe9d4f7f8ef793a2c82d53935a8d2c245a3
Co-Authored-By: Tom Tromey <tom@tromey.com>
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb')
-rw-r--r-- | gdb/gdb_bfd.c | 112 |
1 files changed, 47 insertions, 65 deletions
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index 3213234..3683eeb 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -167,10 +167,6 @@ registry_accessor<bfd>::get (bfd *abfd) return &gdata->registry_fields; } -/* A hash table storing all the BFDs maintained in the cache. */ - -static htab_t gdb_bfd_cache; - /* When true gdb will reuse an existing bfd object if the filename, modification time, and file size all match. */ @@ -216,34 +212,42 @@ struct gdb_bfd_cache_search dev_t device_id; }; -/* A hash function for BFDs. */ - -static hashval_t -hash_bfd (const void *b) +struct bfd_cache_hash { - const bfd *abfd = (const struct bfd *) b; + using is_transparent = void; - /* It is simplest to just hash the filename. */ - return htab_hash_string (bfd_get_filename (abfd)); -} + std::size_t operator() (bfd *abfd) const noexcept + { + /* It is simplest to just hash the filename. */ + return htab_hash_string (bfd_get_filename (abfd)); + } -/* An equality function for BFDs. Note that this expects the caller - to search using struct gdb_bfd_cache_search only, not BFDs. */ + std::size_t operator() (const gdb_bfd_cache_search &search) const noexcept + { return htab_hash_string (search.filename); } +}; -static int -eq_bfd (const void *a, const void *b) +struct bfd_cache_eq { - const bfd *abfd = (const struct bfd *) a; - const struct gdb_bfd_cache_search *s - = (const struct gdb_bfd_cache_search *) b; - struct gdb_bfd_data *gdata = (struct gdb_bfd_data *) bfd_usrdata (abfd); + using is_transparent = void; - return (gdata->mtime == s->mtime - && gdata->size == s->size - && gdata->inode == s->inode - && gdata->device_id == s->device_id - && strcmp (bfd_get_filename (abfd), s->filename) == 0); -} + bool operator() (bfd *lhs, bfd *rhs) const noexcept + { return lhs == rhs; } + + bool operator() (const gdb_bfd_cache_search &s, bfd *abfd) const noexcept + { + auto gdata = static_cast<gdb_bfd_data *> (bfd_usrdata (abfd)); + + return (gdata->mtime == s.mtime + && gdata->size == s.size + && gdata->inode == s.inode + && gdata->device_id == s.device_id + && strcmp (bfd_get_filename (abfd), s.filename) == 0); + } +}; + +/* A hash set storing all the BFDs maintained in the cache. */ + +static gdb::unordered_set<bfd *, bfd_cache_hash, bfd_cache_eq> gdb_bfd_cache; /* See gdb_bfd.h. */ @@ -516,10 +520,7 @@ gdb_bfd_ref_ptr gdb_bfd_open (const char *name, const char *target, int fd, bool warn_if_slow) { - hashval_t hash; - void **slot; bfd *abfd; - struct gdb_bfd_cache_search search; struct stat st; if (is_target_filename (name)) @@ -544,10 +545,6 @@ gdb_bfd_open (const char *name, const char *target, int fd, std::lock_guard<std::recursive_mutex> guard (gdb_bfd_mutex); #endif - if (gdb_bfd_cache == NULL) - gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL, - xcalloc, xfree); - if (fd == -1) { fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release (); @@ -568,25 +565,26 @@ gdb_bfd_open (const char *name, const char *target, int fd, return gdb_bfd_ref_ptr::new_reference (abfd); } + gdb_bfd_cache_search search; + search.filename = name; search.mtime = st.st_mtime; search.size = st.st_size; search.inode = st.st_ino; search.device_id = st.st_dev; - /* Note that this must compute the same result as hash_bfd. */ - hash = htab_hash_string (name); - /* Note that we cannot use htab_find_slot_with_hash here, because - opening the BFD may fail; and this would violate hashtab - invariants. */ - abfd = (struct bfd *) htab_find_with_hash (gdb_bfd_cache, &search, hash); - if (bfd_sharing && abfd != NULL) + if (bfd_sharing) { - bfd_cache_debug_printf ("Reusing cached bfd %s for %s", - host_address_to_string (abfd), - bfd_get_filename (abfd)); - close (fd); - return gdb_bfd_ref_ptr::new_reference (abfd); + if (auto iter = gdb_bfd_cache.find (search); + iter != gdb_bfd_cache.end ()) + { + abfd = *iter; + bfd_cache_debug_printf ("Reusing cached bfd %s for %s", + host_address_to_string (abfd), + bfd_get_filename (abfd)); + close (fd); + return gdb_bfd_ref_ptr::new_reference (abfd); + } } abfd = bfd_fopen (name, target, FOPEN_RB, fd); @@ -609,9 +607,8 @@ gdb_bfd_open (const char *name, const char *target, int fd, if (bfd_sharing) { - slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT); - gdb_assert (!*slot); - *slot = abfd; + bool inserted = gdb_bfd_cache.emplace (abfd).second; + gdb_assert (inserted); } return gdb_bfd_ref_ptr (abfd); @@ -700,7 +697,6 @@ void gdb_bfd_unref (struct bfd *abfd) { struct gdb_bfd_data *gdata; - struct gdb_bfd_cache_search search; bfd *archive_bfd; if (abfd == NULL) @@ -727,23 +723,9 @@ gdb_bfd_unref (struct bfd *abfd) bfd_get_filename (abfd)); archive_bfd = gdata->archive_bfd; - search.filename = bfd_get_filename (abfd); - if (gdb_bfd_cache && search.filename) - { - hashval_t hash = htab_hash_string (search.filename); - void **slot; - - search.mtime = gdata->mtime; - search.size = gdata->size; - search.inode = gdata->inode; - search.device_id = gdata->device_id; - slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, - NO_INSERT); - - if (slot && *slot) - htab_clear_slot (gdb_bfd_cache, slot); - } + if (bfd_get_filename (abfd) != nullptr) + gdb_bfd_cache.erase (abfd); delete gdata; bfd_set_usrdata (abfd, NULL); /* Paranoia. */ |