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>
This commit is contained in:
Simon Marchi
2024-08-19 15:03:52 +00:00
parent 340e6b6185
commit fd85b25156

View File

@@ -154,10 +154,6 @@ registry_accessor<bfd>::get (bfd *abfd)
return &gdata->registry_fields; 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, /* When true gdb will reuse an existing bfd object if the filename,
modification time, and file size all match. */ modification time, and file size all match. */
@@ -203,34 +199,39 @@ struct gdb_bfd_cache_search
dev_t device_id; dev_t device_id;
}; };
/* A hash function for BFDs. */ struct bfd_cache_hash
static hashval_t
hash_bfd (const void *b)
{ {
const bfd *abfd = (const struct bfd *) b; using is_transparent = void;
/* It is simplest to just hash the filename. */ std::size_t operator() (bfd *abfd) const noexcept
return htab_hash_string (bfd_get_filename (abfd)); { return htab_hash_string (bfd_get_filename (abfd)); }
}
/* An equality function for BFDs. Note that this expects the caller std::size_t operator() (const gdb_bfd_cache_search &search) const noexcept
to search using struct gdb_bfd_cache_search only, not BFDs. */ { return htab_hash_string (search.filename); }
};
static int struct bfd_cache_eq
eq_bfd (const void *a, const void *b)
{ {
const bfd *abfd = (const struct bfd *) a; using is_transparent = void;
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);
return (gdata->mtime == s->mtime bool operator() (bfd *lhs, bfd *rhs) const noexcept
&& gdata->size == s->size { return lhs == rhs; }
&& gdata->inode == s->inode
&& gdata->device_id == s->device_id bool operator() (const gdb_bfd_cache_search &s, bfd *abfd) const noexcept
&& strcmp (bfd_get_filename (abfd), s->filename) == 0); {
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. */ /* See gdb_bfd.h. */
@@ -503,8 +504,6 @@ gdb_bfd_ref_ptr
gdb_bfd_open (const char *name, const char *target, int fd, gdb_bfd_open (const char *name, const char *target, int fd,
bool warn_if_slow) bool warn_if_slow)
{ {
hashval_t hash;
void **slot;
bfd *abfd; bfd *abfd;
struct gdb_bfd_cache_search search; struct gdb_bfd_cache_search search;
struct stat st; struct stat st;
@@ -531,10 +530,6 @@ gdb_bfd_open (const char *name, const char *target, int fd,
std::lock_guard<std::recursive_mutex> guard (gdb_bfd_mutex); std::lock_guard<std::recursive_mutex> guard (gdb_bfd_mutex);
#endif #endif
if (gdb_bfd_cache == NULL)
gdb_bfd_cache = htab_create_alloc (1, hash_bfd, eq_bfd, NULL,
xcalloc, xfree);
if (fd == -1) if (fd == -1)
{ {
fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release (); fd = gdb_open_cloexec (name, O_RDONLY | O_BINARY, 0).release ();
@@ -561,20 +556,19 @@ gdb_bfd_open (const char *name, const char *target, int fd,
search.inode = st.st_ino; search.inode = st.st_ino;
search.device_id = st.st_dev; search.device_id = st.st_dev;
/* Note that this must compute the same result as hash_bfd. */ if (bfd_sharing)
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 (auto iter = gdb_bfd_cache.find (search);
iter != gdb_bfd_cache.end ())
{
abfd = *iter;
bfd_cache_debug_printf ("Reusing cached bfd %s for %s", bfd_cache_debug_printf ("Reusing cached bfd %s for %s",
host_address_to_string (abfd), host_address_to_string (abfd),
bfd_get_filename (abfd)); bfd_get_filename (abfd));
close (fd); close (fd);
return gdb_bfd_ref_ptr::new_reference (abfd); return gdb_bfd_ref_ptr::new_reference (abfd);
} }
}
abfd = bfd_fopen (name, target, FOPEN_RB, fd); abfd = bfd_fopen (name, target, FOPEN_RB, fd);
if (abfd == NULL) if (abfd == NULL)
@@ -588,9 +582,8 @@ gdb_bfd_open (const char *name, const char *target, int fd,
if (bfd_sharing) if (bfd_sharing)
{ {
slot = htab_find_slot_with_hash (gdb_bfd_cache, &search, hash, INSERT); bool inserted = gdb_bfd_cache.emplace (abfd).second;
gdb_assert (!*slot); gdb_assert (inserted);
*slot = abfd;
} }
/* It's important to pass the already-computed stat info here, /* It's important to pass the already-computed stat info here,
@@ -685,7 +678,6 @@ void
gdb_bfd_unref (struct bfd *abfd) gdb_bfd_unref (struct bfd *abfd)
{ {
struct gdb_bfd_data *gdata; struct gdb_bfd_data *gdata;
struct gdb_bfd_cache_search search;
bfd *archive_bfd; bfd *archive_bfd;
if (abfd == NULL) if (abfd == NULL)
@@ -712,23 +704,9 @@ gdb_bfd_unref (struct bfd *abfd)
bfd_get_filename (abfd)); bfd_get_filename (abfd));
archive_bfd = gdata->archive_bfd; archive_bfd = gdata->archive_bfd;
search.filename = bfd_get_filename (abfd);
if (gdb_bfd_cache && search.filename) if (bfd_get_filename (abfd) != nullptr)
{ gdb_bfd_cache.erase (abfd);
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);
}
delete gdata; delete gdata;
bfd_set_usrdata (abfd, NULL); /* Paranoia. */ bfd_set_usrdata (abfd, NULL); /* Paranoia. */