diff options
| author | Simon Marchi <simon.marchi@efficios.com> | 2026-02-08 17:04:11 -0500 |
|---|---|---|
| committer | Simon Marchi <simon.marchi@efficios.com> | 2026-02-09 12:48:36 -0500 |
| commit | f3965c24048e2aadef19549a2608debf680315a4 (patch) | |
| tree | 23808cd94f6d55a52670152f3c0a27ebd9a9c12e /gdb/python | |
| parent | ea71a4081d98075998e297a6d95683f837889720 (diff) | |
| download | binutils-f3965c24048e2aadef19549a2608debf680315a4.tar.gz binutils-f3965c24048e2aadef19549a2608debf680315a4.tar.bz2 binutils-f3965c24048e2aadef19549a2608debf680315a4.zip | |
gdb/registry: add registry::key::try_emplace
We have many times the pattern:
current_source_location *loc
= current_source_key.get (pspace);
if (loc == nullptr)
loc = current_source_key.emplace (pspace);
return loc;
I thought it would be nice to have it directly part of registry::key.
Add a try_emplace method, which is like emplace, except that it returns
the existing data, if any. The try_emplace name comes from similar
methods in std::map or gdb::unordered_map
(ankerl::unordered_dense::map).
Replace as many callers as I could find to use it.
Change-Id: I21f922ca065a354f041caaf70484d6cfbcbb76ed
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/python')
| -rw-r--r-- | gdb/python/py-registers.c | 5 | ||||
| -rw-r--r-- | gdb/python/py-unwind.c | 4 | ||||
| -rw-r--r-- | gdb/python/python-internal.h | 8 |
3 files changed, 3 insertions, 14 deletions
diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c index b345229ea61..caf7bcc0f9d 100644 --- a/gdb/python/py-registers.c +++ b/gdb/python/py-registers.c @@ -141,10 +141,7 @@ static gdbpy_ref<> gdbpy_get_register_descriptor (struct gdbarch *gdbarch, int regnum) { - gdbpy_register_type *vecp = gdbpy_register_object_data.get (gdbarch); - if (vecp == nullptr) - vecp = &gdbpy_register_object_data.emplace (gdbarch); - gdbpy_register_type &vec = *vecp; + gdbpy_register_type &vec = gdbpy_register_object_data.try_emplace (gdbarch); /* Ensure that we have enough entries in the vector. */ if (vec.size () <= regnum) diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index c2579f3c35a..d4c35e17f14 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -994,9 +994,7 @@ static const registry<gdbarch>::key<pyuw_gdbarch_data_type> pyuw_gdbarch_data; static void pyuw_on_new_gdbarch (gdbarch *newarch) { - struct pyuw_gdbarch_data_type *data = pyuw_gdbarch_data.get (newarch); - if (data == nullptr) - data = &pyuw_gdbarch_data.emplace (newarch); + struct pyuw_gdbarch_data_type *data = &pyuw_gdbarch_data.try_emplace (newarch); if (!data->unwinder_registered) { diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 722edbd1a1c..8925714f3c1 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -1209,13 +1209,7 @@ private: template<typename O> Storage *get_storage (O *owner, const StorageKey<O> &key) const { - Storage *r = key.get (owner); - if (r == nullptr) - { - r = new Storage(); - key.set (owner, r); - } - return r; + return &key.try_emplace (owner); } Storage *get_storage (struct objfile* objf) const |
