From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 13/19] Convert gnu-v3-abi.c to new hash table
Date: Fri, 07 Apr 2023 09:25:45 -0600 [thread overview]
Message-ID: <20230407-t-robin-hood-hash-v1-13-900d93ef1510@tromey.com> (raw)
In-Reply-To: <20230407-t-robin-hood-hash-v1-0-900d93ef1510@tromey.com>
This converts gnu-v3-abi.c to use the new hash table.
This change shows how a std::vector can easily be made directly from
the hash table, simplifying the earlier approach of constructing a
vector and a hash table at the same time.
---
gdb/gnu-v3-abi.c | 101 ++++++++++++++++++++-----------------------------------
1 file changed, 36 insertions(+), 65 deletions(-)
diff --git a/gdb/gnu-v3-abi.c b/gdb/gnu-v3-abi.c
index 12bc5b09a93..ebd9097b28d 100644
--- a/gdb/gnu-v3-abi.c
+++ b/gdb/gnu-v3-abi.c
@@ -33,6 +33,7 @@
#include "cli/cli-style.h"
#include "dwarf2/loc.h"
#include "inferior.h"
+#include "gdbsupport/hash-table.h"
static struct cp_abi_ops gnu_v3_abi_ops;
@@ -786,51 +787,36 @@ gnuv3_method_ptr_to_value (struct value **this_p, struct value *method_ptr)
return value_from_pointer (lookup_pointer_type (method_type), ptr_value);
}
-/* Objects of this type are stored in a hash table and a vector when
- printing the vtables for a class. */
+/* Trait type used for the keys of the hash map used when printing
+ vtables for a class. */
-struct value_and_voffset
+struct value_traits
{
- /* The value representing the object. */
- struct value *value;
+ typedef value *value_type;
- /* The maximum vtable offset we've found for any object at this
- offset in the outermost object. */
- int max_voffset;
-};
-
-/* Hash function for value_and_voffset. */
+ static bool is_empty (const value_type &val)
+ { return val == nullptr; }
-static hashval_t
-hash_value_and_voffset (const void *p)
-{
- const struct value_and_voffset *o = (const struct value_and_voffset *) p;
+ static bool equals (const value_type &lhs, const value_type &rhs)
+ {
+ return (lhs->address () + lhs->embedded_offset ()
+ == rhs->address () + rhs->embedded_offset ());
+ }
- return o->value->address () + o->value->embedded_offset ();
-}
-
-/* Equality function for value_and_voffset. */
-
-static int
-eq_value_and_voffset (const void *a, const void *b)
-{
- const struct value_and_voffset *ova = (const struct value_and_voffset *) a;
- const struct value_and_voffset *ovb = (const struct value_and_voffset *) b;
+ static size_t hash (const value_type &val)
+ { return val->address () + val->embedded_offset (); }
+};
- return (ova->value->address () + ova->value->embedded_offset ()
- == ovb->value->address () + ovb->value->embedded_offset ());
-}
+typedef gdb::traited_hash_map<value_traits, int> vtable_hash_t;
-/* Comparison function for value_and_voffset. */
+/* Comparison function used for sorting the vtable entries. */
static bool
-compare_value_and_voffset (const struct value_and_voffset *va,
- const struct value_and_voffset *vb)
+compare_value_and_voffset (const std::pair<value *, int> &va,
+ const std::pair<value *, int> &vb)
{
- CORE_ADDR addra = (va->value->address ()
- + va->value->embedded_offset ());
- CORE_ADDR addrb = (vb->value->address ()
- + vb->value->embedded_offset ());
+ CORE_ADDR addra = va.first->address () + va.first->embedded_offset ();
+ CORE_ADDR addrb = vb.first->address () + vb.first->embedded_offset ();
return addra < addrb;
}
@@ -838,18 +824,14 @@ compare_value_and_voffset (const struct value_and_voffset *va,
/* A helper function used when printing vtables. This determines the
key (most derived) sub-object at each address and also computes the
maximum vtable offset seen for the corresponding vtable. Updates
- OFFSET_HASH and OFFSET_VEC with a new value_and_voffset object, if
- needed. VALUE is the object to examine. */
+ OFFSET_HASH with a new value_and_voffset object, if needed. VALUE
+ is the object to examine. */
static void
-compute_vtable_size (htab_t offset_hash,
- std::vector<value_and_voffset *> *offset_vec,
- struct value *value)
+compute_vtable_size (vtable_hash_t &offset_hash, struct value *value)
{
int i;
struct type *type = check_typedef (value->type ());
- void **slot;
- struct value_and_voffset search_vo, *current_vo;
gdb_assert (type->code () == TYPE_CODE_STRUCT);
@@ -859,18 +841,8 @@ compute_vtable_size (htab_t offset_hash,
return;
/* Update the hash and the vec, if needed. */
- search_vo.value = value;
- slot = htab_find_slot (offset_hash, &search_vo, INSERT);
- if (*slot)
- current_vo = (struct value_and_voffset *) *slot;
- else
- {
- current_vo = XNEW (struct value_and_voffset);
- current_vo->value = value;
- current_vo->max_voffset = -1;
- *slot = current_vo;
- offset_vec->push_back (current_vo);
- }
+ auto insert_pair = offset_hash.insert (value, -1);
+ int ¤t_max_voffset = insert_pair.first->second;
/* Update the value_and_voffset object with the highest vtable
offset from this class. */
@@ -885,15 +857,15 @@ compute_vtable_size (htab_t offset_hash,
{
int voffset = TYPE_FN_FIELD_VOFFSET (fn, j);
- if (voffset > current_vo->max_voffset)
- current_vo->max_voffset = voffset;
+ if (voffset > current_max_voffset)
+ current_max_voffset = voffset;
}
}
}
/* Recurse into base classes. */
for (i = 0; i < TYPE_N_BASECLASSES (type); ++i)
- compute_vtable_size (offset_hash, offset_vec, value_field (value, i));
+ compute_vtable_size (offset_hash, value_field (value, i));
}
/* Helper for gnuv3_print_vtable that prints a single vtable. */
@@ -994,23 +966,22 @@ gnuv3_print_vtable (struct value *value)
return;
}
- htab_up offset_hash (htab_create_alloc (1, hash_value_and_voffset,
- eq_value_and_voffset,
- xfree, xcalloc, xfree));
- std::vector<value_and_voffset *> result_vec;
+ vtable_hash_t offset_hash;
+ compute_vtable_size (offset_hash, value);
- compute_vtable_size (offset_hash.get (), &result_vec, value);
+ std::vector<std::pair<struct value *, int>> result_vec (offset_hash.begin (),
+ offset_hash.end ());
std::sort (result_vec.begin (), result_vec.end (),
compare_value_and_voffset);
count = 0;
- for (value_and_voffset *iter : result_vec)
+ for (auto &item : result_vec)
{
- if (iter->max_voffset >= 0)
+ if (item.second >= 0)
{
if (count > 0)
gdb_printf ("\n");
- print_one_vtable (gdbarch, iter->value, iter->max_voffset, &opts);
+ print_one_vtable (gdbarch, item.first, item.second, &opts);
++count;
}
}
--
2.39.2
next prev parent reply other threads:[~2023-04-07 15:25 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-07 15:25 [PATCH 00/19] Add hash table to gdbsupport Tom Tromey
2023-04-07 15:25 ` [PATCH 01/19] Add a " Tom Tromey
2023-04-07 15:41 ` Tom Tromey
2023-04-07 15:25 ` [PATCH 02/19] Convert compile-c-symbols.c to new hash table Tom Tromey
2023-04-07 15:25 ` [PATCH 03/19] Convert filename-seen-cache.h " Tom Tromey
2023-04-07 15:25 ` [PATCH 04/19] Convert linespec.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 05/19] Convert target-descriptions.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 06/19] Convert dwarf2/macro.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 07/19] Convert breakpoint.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 08/19] Convert py-framefilter.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 09/19] Convert disasm.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 10/19] Convert compile/compile.c " Tom Tromey
2023-04-07 15:25 ` [PATCH 11/19] Convert type copying " Tom Tromey
2023-04-07 15:25 ` [PATCH 12/19] Convert static links " Tom Tromey
2023-04-07 15:25 ` Tom Tromey [this message]
2023-04-07 15:25 ` [PATCH 14/19] Convert abbrev cache " Tom Tromey
2023-04-07 15:25 ` [PATCH 15/19] Convert abbrevs " Tom Tromey
2023-04-07 15:25 ` [PATCH 16/19] Convert typedef hash " Tom Tromey
2023-04-07 15:25 ` [PATCH 17/19] Convert all_bfds " Tom Tromey
2023-04-07 15:25 ` [PATCH 18/19] Convert more DWARF code " Tom Tromey
2023-04-07 15:25 ` [PATCH 19/19] Convert gdb_bfd.c " Tom Tromey
2023-04-10 19:45 ` [PATCH 00/19] Add hash table to gdbsupport John Baldwin
2023-11-03 18:54 ` Tom Tromey
2023-12-08 18:28 ` Tom Tromey
2024-01-11 18:07 ` Tom Tromey
2024-01-11 19:35 ` John Baldwin
2024-01-12 2:57 ` Simon Marchi
2024-01-12 18:22 ` Tom Tromey
2024-01-12 19:12 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230407-t-robin-hood-hash-v1-13-900d93ef1510@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).