Skip to content

Commit

Permalink
gdbsupport: add gdb::string_view_hash
Browse files Browse the repository at this point in the history
Add the string_view_hash type, which will be useful to be able to use
gdb::string_view as std::unordered_map keys.

Use it in gdb/symtab.c, to exercise it.

Change-Id: Id69a466ab19a9f6620b5df8a2dd29b5cddd94c00
Approved-By: Andrew Burgess <[email protected]>
  • Loading branch information
simark committed Jan 5, 2023
1 parent 72127b1 commit 1a8605a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gdb/symtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ hash_demangled_name_entry (const void *data)
const struct demangled_name_entry *e
= (const struct demangled_name_entry *) data;

return fast_hash (e->mangled.data (), e->mangled.length ());
return gdb::string_view_hash () (e->mangled);
}

/* Equality function for the demangled name hash. */
Expand Down
17 changes: 17 additions & 0 deletions gdbsupport/common-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,21 @@ fast_hash (const void *ptr, size_t len, unsigned int start_value = 0)
#endif
}

namespace gdb
{

/* Hash type for gdb::string_view.
Even after we switch to C++17 and dump our string_view implementation, we
might want to keep this hash implementation if it's faster than std::hash
for std::string_view. */

struct string_view_hash
{
std::size_t operator() (gdb::string_view view) const
{ return fast_hash (view.data (), view.length ()); }
};

} /* namespace gdb */

#endif /* COMMON_COMMON_UTILS_H */

0 comments on commit 1a8605a

Please sign in to comment.