From: Simon Marchi Date: Sun, 8 Feb 2026 22:04:11 +0000 (-0500) Subject: gdb/registry: add registry::key::try_emplace X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f3965c24048e2aadef19549a2608debf680315a4;p=thirdparty%2Fbinutils-gdb.git 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 --- diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index eaac2816880..c7533921b77 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -307,13 +307,7 @@ static const registry::key ada_inferior_data; static struct ada_inferior_data * get_ada_inferior_data (struct inferior *inf) { - struct ada_inferior_data *data; - - data = ada_inferior_data.get (inf); - if (data == NULL) - data = &ada_inferior_data.emplace (inf); - - return data; + return &ada_inferior_data.try_emplace (inf); } /* Perform all necessary cleanups regarding our module's inferior data @@ -397,11 +391,7 @@ static const registry::key static cache_entry_set & get_ada_pspace_data (struct program_space *pspace) { - cache_entry_set *data = ada_pspace_data_handle.get (pspace); - if (data == nullptr) - data = &ada_pspace_data_handle.emplace (pspace); - - return *data; + return ada_pspace_data_handle.try_emplace (pspace); } /* Utilities */ diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c index 22ba47c262d..0e124e1a2a2 100644 --- a/gdb/ada-tasks.c +++ b/gdb/ada-tasks.c @@ -296,13 +296,7 @@ task_to_str (int taskno, const ada_task_info *task_info) static struct ada_tasks_pspace_data * get_ada_tasks_pspace_data (struct program_space *pspace) { - struct ada_tasks_pspace_data *data; - - data = ada_tasks_pspace_data_handle.get (pspace); - if (data == NULL) - data = &ada_tasks_pspace_data_handle.emplace (pspace); - - return data; + return &ada_tasks_pspace_data_handle.try_emplace (pspace); } /* Return the ada-tasks module's data for the given inferior (INF). @@ -320,13 +314,7 @@ get_ada_tasks_pspace_data (struct program_space *pspace) static struct ada_tasks_inferior_data * get_ada_tasks_inferior_data (struct inferior *inf) { - struct ada_tasks_inferior_data *data; - - data = ada_tasks_inferior_data_handle.get (inf); - if (data == NULL) - data = &ada_tasks_inferior_data_handle.emplace (inf); - - return data; + return &ada_tasks_inferior_data_handle.try_emplace (inf); } /* Return the task number of the task whose thread is THREAD, or zero diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c index 120b032fcf4..823fb13cb29 100644 --- a/gdb/aix-thread.c +++ b/gdb/aix-thread.c @@ -205,13 +205,7 @@ get_aix_thread_variables_data (struct inferior *inf) if (inf == NULL) return NULL; - struct aix_thread_variables* data; - - data = aix_thread_variables_handle.get (inf); - if (data == NULL) - data = &aix_thread_variables_handle.emplace (inf); - - return data; + return &aix_thread_variables_handle.try_emplace (inf); } /* Helper to get data for ptid in a function. */ diff --git a/gdb/amd-dbgapi-target.c b/gdb/amd-dbgapi-target.c index d636f1a39ed..655dddd36ed 100644 --- a/gdb/amd-dbgapi-target.c +++ b/gdb/amd-dbgapi-target.c @@ -369,12 +369,7 @@ static const registry::key static amd_dbgapi_inferior_info & get_amd_dbgapi_inferior_info (inferior *inferior) { - amd_dbgapi_inferior_info *info = amd_dbgapi_inferior_data.get (inferior); - - if (info == nullptr) - info = &amd_dbgapi_inferior_data.emplace (inferior, inferior); - - return *info; + return amd_dbgapi_inferior_data.try_emplace (inferior, inferior); } /* The async event handler registered with the event loop, indicating that we diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c index 7d6aa06f233..7f33786f2ef 100644 --- a/gdb/arm-tdep.c +++ b/gdb/arm-tdep.c @@ -9743,10 +9743,8 @@ arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile, if (name[1] != 'a' && name[1] != 't' && name[1] != 'd') return; - data = arm_bfd_data_key.get (objfile->obfd.get ()); - if (data == NULL) - data = &arm_bfd_data_key.emplace (objfile->obfd.get (), - objfile->obfd->section_count); + data = &arm_bfd_data_key.try_emplace (objfile->obfd.get (), + objfile->obfd->section_count); arm_mapping_symbol_vec &map = data->section_maps[bfd_asymbol_section (sym)->index]; diff --git a/gdb/auto-load.c b/gdb/auto-load.c index b7e62626ab7..b5fec13743c 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -608,13 +608,7 @@ static const registry::key static struct auto_load_pspace_info * get_auto_load_pspace_data (struct program_space *pspace) { - struct auto_load_pspace_info *info; - - info = auto_load_pspace_data.get (pspace); - if (info == NULL) - info = &auto_load_pspace_data.emplace (pspace); - - return info; + return &auto_load_pspace_data.try_emplace (pspace); } /* Hash function for the loaded script hash. */ diff --git a/gdb/break-catch-syscall.c b/gdb/break-catch-syscall.c index 43f12862052..1a0bb931080 100644 --- a/gdb/break-catch-syscall.c +++ b/gdb/break-catch-syscall.c @@ -82,13 +82,7 @@ static const registry::key static struct catch_syscall_inferior_data * get_catch_syscall_inferior_data (struct inferior *inf) { - struct catch_syscall_inferior_data *inf_data; - - inf_data = catch_syscall_inferior_data.get (inf); - if (inf_data == NULL) - inf_data = &catch_syscall_inferior_data.emplace (inf); - - return inf_data; + return &catch_syscall_inferior_data.try_emplace (inf); } /* Implement the "insert" method for syscall catchpoints. */ diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 82514384d98..8f20a1792c8 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -3653,12 +3653,7 @@ msym_not_found_p (const struct minimal_symbol *msym) static struct breakpoint_objfile_data * get_breakpoint_objfile_data (struct objfile *objfile) { - struct breakpoint_objfile_data *bp_objfile_data; - - bp_objfile_data = breakpoint_objfile_key.get (objfile); - if (bp_objfile_data == NULL) - bp_objfile_data = &breakpoint_objfile_key.emplace (objfile); - return bp_objfile_data; + return &breakpoint_objfile_key.try_emplace (objfile); } static void diff --git a/gdb/bsd-uthread.c b/gdb/bsd-uthread.c index 9d91893c6cf..e4c5c9ee3e3 100644 --- a/gdb/bsd-uthread.c +++ b/gdb/bsd-uthread.c @@ -84,10 +84,7 @@ static const registry::key bsd_uthread_data; static struct bsd_uthread_ops * get_bsd_uthread (struct gdbarch *gdbarch) { - struct bsd_uthread_ops *ops = bsd_uthread_data.get (gdbarch); - if (ops == nullptr) - ops = &bsd_uthread_data.emplace (gdbarch); - return ops; + return &bsd_uthread_data.try_emplace (gdbarch); } /* Set the function that supplies registers from an inactive thread diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c index a5026e133c9..9f4a5bc7ede 100644 --- a/gdb/dwarf2/expr.c +++ b/gdb/dwarf2/expr.c @@ -709,9 +709,7 @@ struct type * dwarf_expr_context::address_type () const { gdbarch *arch = this->m_per_objfile->objfile->arch (); - dwarf_gdbarch_types *types = dwarf_arch_cookie.get (arch); - if (types == nullptr) - types = &dwarf_arch_cookie.emplace (arch); + dwarf_gdbarch_types *types = &dwarf_arch_cookie.try_emplace (arch); int ndx; if (this->m_addr_size == 2) diff --git a/gdb/dwarf2/frame.c b/gdb/dwarf2/frame.c index a4dada8a789..94586b06ef3 100644 --- a/gdb/dwarf2/frame.c +++ b/gdb/dwarf2/frame.c @@ -608,10 +608,7 @@ static const registry::key dwarf2_frame_data; static dwarf2_frame_ops * get_frame_ops (struct gdbarch *gdbarch) { - dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch); - if (result == nullptr) - result = &dwarf2_frame_data.emplace (gdbarch); - return result; + return &dwarf2_frame_data.try_emplace (gdbarch); } /* Default architecture-specific register state initialization diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c index 2ddba1660de..2e3e2c80210 100644 --- a/gdb/fbsd-tdep.c +++ b/gdb/fbsd-tdep.c @@ -496,10 +496,7 @@ static const registry::key static struct fbsd_gdbarch_data * get_fbsd_gdbarch_data (struct gdbarch *gdbarch) { - struct fbsd_gdbarch_data *result = fbsd_gdbarch_data_handle.get (gdbarch); - if (result == nullptr) - result = &fbsd_gdbarch_data_handle.emplace (gdbarch); - return result; + return &fbsd_gdbarch_data_handle.try_emplace (gdbarch); } struct fbsd_pspace_data @@ -524,13 +521,7 @@ static const registry::key static struct fbsd_pspace_data * get_fbsd_pspace_data (struct program_space *pspace) { - struct fbsd_pspace_data *data; - - data = fbsd_pspace_data_handle.get (pspace); - if (data == NULL) - data = &fbsd_pspace_data_handle.emplace (pspace); - - return data; + return &fbsd_pspace_data_handle.try_emplace (pspace); } /* This is how we want PTIDs from core files to be printed. */ diff --git a/gdb/frame-base.c b/gdb/frame-base.c index a732e1efc9a..e51e47430e4 100644 --- a/gdb/frame-base.c +++ b/gdb/frame-base.c @@ -70,10 +70,7 @@ static const registry::key frame_base_data; static struct frame_base_table * get_frame_base_table (struct gdbarch *gdbarch) { - struct frame_base_table *table = frame_base_data.get (gdbarch); - if (table == nullptr) - table = &frame_base_data.emplace (gdbarch); - return table; + return &frame_base_data.try_emplace (gdbarch); } void diff --git a/gdb/frame-unwind.c b/gdb/frame-unwind.c index 49ade0043a1..a942ed8c207 100644 --- a/gdb/frame-unwind.c +++ b/gdb/frame-unwind.c @@ -74,12 +74,9 @@ static const registry::key> static std::vector & get_frame_unwind_table (struct gdbarch *gdbarch) { - std::vector *table = frame_unwind_data.get (gdbarch); - if (table == nullptr) - table = &frame_unwind_data.emplace (gdbarch, + return frame_unwind_data.try_emplace (gdbarch, standard_unwinders.begin (), standard_unwinders.end ()); - return *table; } static const char * diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index cbf8e705479..98ea4667749 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -1254,13 +1254,7 @@ static const registry::key bfd_inferior_data_key; static struct bfd_inferior_data * get_bfd_inferior_data (struct inferior *inf) { - struct bfd_inferior_data *data; - - data = bfd_inferior_data_key.get (inf); - if (data == nullptr) - data = &bfd_inferior_data_key.emplace (inf); - - return data; + return &bfd_inferior_data_key.try_emplace (inf); } /* Increment the BFD error count for STR and return the updated diff --git a/gdb/ia64-libunwind-tdep.c b/gdb/ia64-libunwind-tdep.c index 41c07160cb9..52902dc17f2 100644 --- a/gdb/ia64-libunwind-tdep.c +++ b/gdb/ia64-libunwind-tdep.c @@ -126,10 +126,7 @@ static const char *find_dyn_list_name = STRINGIFY(UNW_OBJ(find_dyn_list)); static struct libunwind_descr * libunwind_descr (struct gdbarch *gdbarch) { - struct libunwind_descr *result = libunwind_descr_handle.get (gdbarch); - if (result == nullptr) - result = &libunwind_descr_handle.emplace (gdbarch); - return result; + return &libunwind_descr_handle.try_emplace (gdbarch); } void diff --git a/gdb/inflow.c b/gdb/inflow.c index d8271f9caa8..ff118b558ca 100644 --- a/gdb/inflow.c +++ b/gdb/inflow.c @@ -639,13 +639,7 @@ terminal_info::~terminal_info () static struct terminal_info * get_inflow_inferior_data (struct inferior *inf) { - struct terminal_info *info; - - info = inflow_inferior_data.get (inf); - if (info == NULL) - info = &inflow_inferior_data.emplace (inf); - - return info; + return &inflow_inferior_data.try_emplace (inf); } /* This is a "inferior_exit" observer. Releases the TERMINAL_INFO member diff --git a/gdb/jit.c b/gdb/jit.c index db9d18f1507..21e8667a7af 100644 --- a/gdb/jit.c +++ b/gdb/jit.c @@ -1151,9 +1151,7 @@ static const registry::key jit_gdbarch_data; static void jit_prepend_unwinder (struct gdbarch *gdbarch) { - struct jit_gdbarch_data_type *data = jit_gdbarch_data.get (gdbarch); - if (data == nullptr) - data = &jit_gdbarch_data.emplace (gdbarch); + struct jit_gdbarch_data_type *data = &jit_gdbarch_data.try_emplace (gdbarch); if (!data->unwinder_registered) { diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c index 59d3eea6069..960995d94f1 100644 --- a/gdb/linux-fork.c +++ b/gdb/linux-fork.c @@ -113,13 +113,7 @@ static const registry::key static struct checkpoint_inferior_data * get_checkpoint_inferior_data (struct inferior *inf) { - struct checkpoint_inferior_data *data; - - data = checkpoint_inferior_data_key.get (inf); - if (data == nullptr) - data = &checkpoint_inferior_data_key.emplace (inf); - - return data; + return &checkpoint_inferior_data_key.try_emplace (inf); } /* Return a reference to the per-inferior fork list. */ diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 9ce28d53283..edcf4ea2401 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -210,10 +210,7 @@ static const registry::key static struct linux_gdbarch_data * get_linux_gdbarch_data (struct gdbarch *gdbarch) { - struct linux_gdbarch_data *result = linux_gdbarch_data_handle.get (gdbarch); - if (result == nullptr) - result = &linux_gdbarch_data_handle.emplace (gdbarch); - return result; + return &linux_gdbarch_data_handle.try_emplace (gdbarch); } /* Linux-specific cached data. This is used by GDB for caching @@ -263,12 +260,7 @@ linux_inferior_execd (inferior *exec_inf, inferior *follow_inf) static struct linux_info * get_linux_inferior_data (inferior *inf) { - linux_info *info = linux_inferior_data.get (inf); - - if (info == nullptr) - info = &linux_inferior_data.emplace (inf); - - return info; + return &linux_inferior_data.try_emplace (inf); } /* See linux-tdep.h. */ diff --git a/gdb/netbsd-tdep.c b/gdb/netbsd-tdep.c index 1d97a7a8b9a..e369622ae7f 100644 --- a/gdb/netbsd-tdep.c +++ b/gdb/netbsd-tdep.c @@ -367,10 +367,7 @@ static const registry::key static struct nbsd_gdbarch_data * get_nbsd_gdbarch_data (struct gdbarch *gdbarch) { - struct nbsd_gdbarch_data *result = nbsd_gdbarch_data_handle.get (gdbarch); - if (result == nullptr) - result = &nbsd_gdbarch_data_handle.emplace (gdbarch); - return result; + return &nbsd_gdbarch_data_handle.try_emplace (gdbarch); } /* Implement the "get_siginfo_type" gdbarch method. */ diff --git a/gdb/objfiles.c b/gdb/objfiles.c index e33368ba19d..14c07371f15 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -88,13 +88,7 @@ objfile_pspace_info::~objfile_pspace_info () static struct objfile_pspace_info * get_objfile_pspace_data (struct program_space *pspace) { - struct objfile_pspace_info *info; - - info = objfiles_pspace_data.get (pspace); - if (info == NULL) - info = &objfiles_pspace_data.emplace (pspace); - - return info; + return &objfiles_pspace_data.try_emplace (pspace); } 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::key 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 Storage *get_storage (O *owner, const StorageKey &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 diff --git a/gdb/reggroups.c b/gdb/reggroups.c index 2bc8e52cb2e..cf377ddee76 100644 --- a/gdb/reggroups.c +++ b/gdb/reggroups.c @@ -111,10 +111,7 @@ static const registry::key reggroups_data; static reggroups * get_reggroups (struct gdbarch *gdbarch) { - struct reggroups *groups = reggroups_data.get (gdbarch); - if (groups == nullptr) - groups = ®groups_data.emplace (gdbarch); - return groups; + return ®groups_data.try_emplace (gdbarch); } /* See reggroups.h. */ diff --git a/gdb/registry.h b/gdb/registry.h index 3738d4226bb..d76d3a80be8 100644 --- a/gdb/registry.h +++ b/gdb/registry.h @@ -135,6 +135,20 @@ public: return *result; } + /* If this key uses the default deleter, then this method is + available. It returns the data associated with OBJ and this + key. If no such data has been attached, a new instance is + constructed using ARGS and attached to OBJ. */ + template + DATA & + try_emplace (T *obj, Args &&...args) const + { + DATA *result = get (obj); + if (result == nullptr) + result = &emplace (obj, std::forward (args)...); + return *result; + } + /* Clear the data attached to OBJ that is associated with this KEY. Any existing data is destroyed using the deleter, and the data is reset to nullptr. */ diff --git a/gdb/remote.c b/gdb/remote.c index 8bdc4951035..edabc2c9ee5 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -1727,11 +1727,7 @@ static const registry::key static remote_per_progspace & get_remote_progspace_info (program_space *pspace) { - remote_per_progspace *info = remote_pspace_data.get (pspace); - if (info == nullptr) - info = &remote_pspace_data.emplace (pspace); - gdb_assert (info != nullptr); - return *info; + return remote_pspace_data.try_emplace (pspace); } /* The size to align memory write packets, when practical. The protocol @@ -13098,11 +13094,7 @@ static const registry::key static struct remote_g_packet_data * get_g_packet_data (struct gdbarch *gdbarch) { - struct remote_g_packet_data *data - = remote_g_packet_data_handle.get (gdbarch); - if (data == nullptr) - data = &remote_g_packet_data_handle.emplace (gdbarch); - return data; + return &remote_g_packet_data_handle.try_emplace (gdbarch); } void diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c index 6c403f9685d..8aa155e1f36 100644 --- a/gdb/rs6000-tdep.c +++ b/gdb/rs6000-tdep.c @@ -162,12 +162,7 @@ static const registry::key ppc_inferior_data_key; ppc_inferior_data * get_ppc_per_inferior (inferior *inf) { - ppc_inferior_data *per_inf = ppc_inferior_data_key.get (inf); - - if (per_inf == nullptr) - per_inf = &ppc_inferior_data_key.emplace (inf); - - return per_inf; + return &ppc_inferior_data_key.try_emplace (inf); } /* To be used by skip_prologue. */ diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c index 8f210312342..b7daec8ff90 100644 --- a/gdb/solib-aix.c +++ b/gdb/solib-aix.c @@ -96,13 +96,7 @@ static const registry::key static struct solib_aix_inferior_data * get_solib_aix_inferior_data (struct inferior *inf) { - struct solib_aix_inferior_data *data; - - data = solib_aix_inferior_data_handle.get (inf); - if (data == NULL) - data = &solib_aix_inferior_data_handle.emplace (inf); - - return data; + return &solib_aix_inferior_data_handle.try_emplace (inf); } #if !defined(HAVE_LIBEXPAT) diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c index 0a6e023128e..aaa34ec0764 100644 --- a/gdb/solib-darwin.c +++ b/gdb/solib-darwin.c @@ -102,11 +102,7 @@ static const registry::key static darwin_info * get_darwin_info (program_space *pspace) { - darwin_info *info = solib_darwin_pspace_data.get (pspace); - if (info != nullptr) - return info; - - return &solib_darwin_pspace_data.emplace (pspace); + return &solib_darwin_pspace_data.try_emplace (pspace); } /* Return non-zero if the version in dyld_all_image is known. */ diff --git a/gdb/solib-dsbt.c b/gdb/solib-dsbt.c index aed7e4ea28e..3ad6f5ab2f3 100644 --- a/gdb/solib-dsbt.c +++ b/gdb/solib-dsbt.c @@ -198,11 +198,7 @@ static const registry::key solib_dsbt_pspace_data; static dsbt_info * get_dsbt_info (program_space *pspace) { - dsbt_info *info = solib_dsbt_pspace_data.get (pspace); - if (info != nullptr) - return info; - - return &solib_dsbt_pspace_data.emplace (pspace); + return &solib_dsbt_pspace_data.try_emplace (pspace); } diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c index 2ffccb69be2..c17d5932b55 100644 --- a/gdb/solib-rocm.c +++ b/gdb/solib-rocm.c @@ -232,12 +232,7 @@ private: static struct solib_info * get_solib_info (inferior *inf) { - solib_info *info = rocm_solib_data.get (inf); - - if (info == nullptr) - info = &rocm_solib_data.emplace (inf, inf); - - return info; + return &rocm_solib_data.try_emplace (inf, inf); } /* Relocate section addresses. */ diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c index 49b005beb1a..c8d78fbfc1d 100644 --- a/gdb/solib-svr4.c +++ b/gdb/solib-svr4.c @@ -468,12 +468,7 @@ svr4_solib_ops::free_probes_table (svr4_info *info) const static struct svr4_info * get_svr4_info (program_space *pspace) { - struct svr4_info *info = solib_svr4_pspace_data.get (pspace); - - if (info == NULL) - info = &solib_svr4_pspace_data.emplace (pspace); - - return info; + return &solib_svr4_pspace_data.try_emplace (pspace); } /* Local function prototypes */ diff --git a/gdb/source.c b/gdb/source.c index 825f09f5947..f890533b931 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -223,11 +223,7 @@ get_lines_to_list (void) static current_source_location * get_source_location (program_space *pspace) { - current_source_location *loc - = current_source_key.get (pspace); - if (loc == nullptr) - loc = ¤t_source_key.emplace (pspace); - return loc; + return ¤t_source_key.try_emplace (pspace); } /* See source.h. */ diff --git a/gdb/svr4-tls-tdep.c b/gdb/svr4-tls-tdep.c index 163985918a6..b39623b324c 100644 --- a/gdb/svr4-tls-tdep.c +++ b/gdb/svr4-tls-tdep.c @@ -42,10 +42,7 @@ static const registry::key static struct svr4_tls_gdbarch_data * get_svr4_tls_gdbarch_data (struct gdbarch *gdbarch) { - struct svr4_tls_gdbarch_data *result = svr4_tls_gdbarch_data_handle.get (gdbarch); - if (result == nullptr) - result = &svr4_tls_gdbarch_data_handle.emplace (gdbarch); - return result; + return &svr4_tls_gdbarch_data_handle.try_emplace (gdbarch); } /* When true, force internal TLS address lookup instead of lookup via diff --git a/gdb/symtab.c b/gdb/symtab.c index 37a9fe62f40..5d02d328694 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -6280,20 +6280,13 @@ make_source_files_completion_list (const char *text) static main_info * get_main_info (program_space *pspace) { - main_info *info = main_progspace_key.get (pspace); - - if (info == NULL) - { - /* It may seem strange to store the main name in the progspace - and also in whatever objfile happens to see a main name in - its debug info. The reason for this is mainly historical: - gdb returned "main" as the name even if no function named - "main" was defined the program; and this approach lets us - keep compatibility. */ - info = &main_progspace_key.emplace (pspace); - } - - return info; + /* It may seem strange to store the main name in the progspace + and also in whatever objfile happens to see a main name in + its debug info. The reason for this is mainly historical: + gdb returned "main" as the name even if no function named + "main" was defined the program; and this approach lets us + keep compatibility. */ + return &main_progspace_key.try_emplace (pspace); } static void diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c index 1c4bbe297c0..3464af80dec 100644 --- a/gdb/target-descriptions.c +++ b/gdb/target-descriptions.c @@ -443,10 +443,7 @@ static const registry::key tdesc_data; static tdesc_arch_data * get_arch_data (struct gdbarch *gdbarch) { - tdesc_arch_data *result = tdesc_data.get (gdbarch); - if (result == nullptr) - result = &tdesc_data.emplace (gdbarch); - return result; + return &tdesc_data.try_emplace (gdbarch); } /* Fetch the current target's description, and switch the current diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c index 6a8f26bf3e6..c41b500afc1 100644 --- a/gdb/windows-tdep.c +++ b/gdb/windows-tdep.c @@ -190,10 +190,7 @@ static const registry::key static struct windows_gdbarch_data * get_windows_gdbarch_data (struct gdbarch *gdbarch) { - windows_gdbarch_data *result = windows_gdbarch_data_handle.get (gdbarch); - if (result == nullptr) - result = &windows_gdbarch_data_handle.emplace (gdbarch); - return result; + return &windows_gdbarch_data_handle.try_emplace (gdbarch); } /* Define Thread Local Base pointer type. */