Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure
@ 2026-02-08 22:04 simon.marchi
  2026-02-08 22:04 ` [PATCH 2/3] gdb/registry: make registry::key::emplace return a reference simon.marchi
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: simon.marchi @ 2026-02-08 22:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

scm-arch.c uses "void" as a registry data type here:

    /* Use a 'void *' here because it isn't guaranteed that SCM is a
       pointer.  */
    static const registry<gdbarch>::key<void, gdb::noop_deleter<void>>
         arch_object_data;

This conflicts with my subsequent patch that makes the emplace method
return a reference because it's not valid to have `void &`.

Circumvent the problem by defining a structure type to use in the
registry, instead of storing the SCM directly.  I think that makes the
code more straightforward and less hacky anyway (at the cost of one
extra allocate per gdbarch that you would use in your Guile code...).

Change-Id: I8d92234a9b0384098fa066dc79a42195dee7ca04
---
 gdb/guile/scm-arch.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/gdb/guile/scm-arch.c b/gdb/guile/scm-arch.c
index 58cacb6f8d7e..db99be81de4d 100644
--- a/gdb/guile/scm-arch.c
+++ b/gdb/guile/scm-arch.c
@@ -40,10 +40,12 @@ static const char arch_smob_name[] = "gdb:arch";
 /* The tag Guile knows the arch smob by.  */
 static scm_t_bits arch_smob_tag;
 
-/* Use a 'void *' here because it isn't guaranteed that SCM is a
-   pointer.  */
-static const registry<gdbarch>::key<void, gdb::noop_deleter<void>>
-     arch_object_data;
+struct arch_object_data_type
+{
+  SCM arch_scm;
+};
+
+static const registry<gdbarch>::key<arch_object_data_type> arch_object_data;
 
 static int arscm_is_arch (SCM);
 \f
@@ -113,22 +115,20 @@ gdbscm_arch_p (SCM scm)
 SCM
 arscm_scm_from_arch (struct gdbarch *gdbarch)
 {
-  SCM arch_scm;
-  void *data = arch_object_data.get (gdbarch);
+  arch_object_data_type *data = arch_object_data.get (gdbarch);
   if (data == nullptr)
     {
-      arch_scm = arscm_make_arch_smob (gdbarch);
+      SCM arch_scm = arscm_make_arch_smob (gdbarch);
 
       /* This object lasts the duration of the GDB session, so there
 	 is no call to scm_gc_unprotect_object for it.  */
       scm_gc_protect_object (arch_scm);
 
-      arch_object_data.set (gdbarch, (void *) arch_scm);
+      data = arch_object_data.emplace (gdbarch);
+      data->arch_scm = arch_scm;
     }
-  else
-    arch_scm = (SCM) data;
 
-  return arch_scm;
+  return data->arch_scm;
 }
 
 /* Return the <gdb:arch> smob in SELF.

base-commit: 7619807a1fdea3bf984437c6c8f8f1859b474fd0
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/3] gdb/registry: make registry::key::emplace return a reference
  2026-02-08 22:04 [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure simon.marchi
@ 2026-02-08 22:04 ` simon.marchi
  2026-02-09 16:12   ` Tom Tromey
  2026-02-08 22:04 ` [PATCH 3/3] gdb/registry: add registry::key::try_emplace simon.marchi
  2026-02-09 16:11 ` [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure Tom Tromey
  2 siblings, 1 reply; 8+ messages in thread
From: simon.marchi @ 2026-02-08 22:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@polymtl.ca>

Since we use C++ and not C, I think that we should gradually move to
using references for things that can never be nullptr.  One example of
this is the return value of the emplace method.

Change it to return a reference, and (to keep the patch straightforward)
update all callers to take the address.  More patches could follow to
propagate the use of references further.

Change-Id: I725539694cf496f8288918cc29d7aaae9aca2292
---
 gdb/ada-lang.c            | 4 ++--
 gdb/ada-tasks.c           | 4 ++--
 gdb/aix-thread.c          | 2 +-
 gdb/amd-dbgapi-target.c   | 2 +-
 gdb/arm-tdep.c            | 6 +++---
 gdb/auto-load.c           | 2 +-
 gdb/auxv.c                | 2 +-
 gdb/break-catch-syscall.c | 2 +-
 gdb/breakpoint.c          | 2 +-
 gdb/bsd-uthread.c         | 2 +-
 gdb/dwarf2/expr.c         | 2 +-
 gdb/dwarf2/frame.c        | 2 +-
 gdb/dwarf2/read.c         | 3 ++-
 gdb/elfread.c             | 2 +-
 gdb/fbsd-tdep.c           | 4 ++--
 gdb/frame-base.c          | 2 +-
 gdb/frame-unwind.c        | 6 +++---
 gdb/gdb_bfd.c             | 2 +-
 gdb/gdbtypes.c            | 2 +-
 gdb/guile/scm-arch.c      | 2 +-
 gdb/guile/scm-symbol.c    | 2 +-
 gdb/hppa-tdep.c           | 2 +-
 gdb/ia64-libunwind-tdep.c | 2 +-
 gdb/inflow.c              | 2 +-
 gdb/jit.c                 | 2 +-
 gdb/linux-fork.c          | 2 +-
 gdb/linux-tdep.c          | 4 ++--
 gdb/netbsd-tdep.c         | 2 +-
 gdb/objc-lang.c           | 2 +-
 gdb/objfiles.c            | 2 +-
 gdb/python/py-registers.c | 2 +-
 gdb/python/py-unwind.c    | 2 +-
 gdb/reggroups.c           | 2 +-
 gdb/registry.h            | 4 ++--
 gdb/remote-sim.c          | 2 +-
 gdb/remote.c              | 4 ++--
 gdb/rs6000-tdep.c         | 2 +-
 gdb/solib-aix.c           | 2 +-
 gdb/solib-darwin.c        | 2 +-
 gdb/solib-dsbt.c          | 2 +-
 gdb/solib-rocm.c          | 2 +-
 gdb/solib-svr4.c          | 2 +-
 gdb/source.c              | 2 +-
 gdb/svr4-tls-tdep.c       | 2 +-
 gdb/symtab.c              | 4 ++--
 gdb/target-descriptions.c | 2 +-
 gdb/windows-tdep.c        | 2 +-
 47 files changed, 59 insertions(+), 58 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 4c89776777f2..eaac2816880e 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -311,7 +311,7 @@ get_ada_inferior_data (struct inferior *inf)
 
   data = ada_inferior_data.get (inf);
   if (data == NULL)
-    data = ada_inferior_data.emplace (inf);
+    data = &ada_inferior_data.emplace (inf);
 
   return data;
 }
@@ -399,7 +399,7 @@ 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);
+    data = &ada_pspace_data_handle.emplace (pspace);
 
   return *data;
 }
diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c
index 798d9c4cb325..22ba47c262d0 100644
--- a/gdb/ada-tasks.c
+++ b/gdb/ada-tasks.c
@@ -300,7 +300,7 @@ get_ada_tasks_pspace_data (struct program_space *pspace)
 
   data = ada_tasks_pspace_data_handle.get (pspace);
   if (data == NULL)
-    data = ada_tasks_pspace_data_handle.emplace (pspace);
+    data = &ada_tasks_pspace_data_handle.emplace (pspace);
 
   return data;
 }
@@ -324,7 +324,7 @@ get_ada_tasks_inferior_data (struct inferior *inf)
 
   data = ada_tasks_inferior_data_handle.get (inf);
   if (data == NULL)
-    data = ada_tasks_inferior_data_handle.emplace (inf);
+    data = &ada_tasks_inferior_data_handle.emplace (inf);
 
   return data;
 }
diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c
index 196942f064c8..120b032fcf4e 100644
--- a/gdb/aix-thread.c
+++ b/gdb/aix-thread.c
@@ -209,7 +209,7 @@ get_aix_thread_variables_data (struct inferior *inf)
 
   data = aix_thread_variables_handle.get (inf);
   if (data == NULL)
-    data = aix_thread_variables_handle.emplace (inf);
+    data = &aix_thread_variables_handle.emplace (inf);
 
   return data;
 }
diff --git a/gdb/amd-dbgapi-target.c b/gdb/amd-dbgapi-target.c
index 4e52683dc55a..d636f1a39ed6 100644
--- a/gdb/amd-dbgapi-target.c
+++ b/gdb/amd-dbgapi-target.c
@@ -372,7 +372,7 @@ 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);
+    info = &amd_dbgapi_inferior_data.emplace (inferior, inferior);
 
   return *info;
 }
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index e4c8b78de030..7d6aa06f2337 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -2571,7 +2571,7 @@ arm_exidx_new_objfile (struct objfile *objfile)
     }
 
   /* Allocate exception table data structure.  */
-  data = arm_exidx_data_key.emplace (objfile->obfd.get ());
+  data = &arm_exidx_data_key.emplace (objfile->obfd.get ());
   data->section_maps.resize (objfile->obfd->section_count);
 
   /* Fill in exception table.  */
@@ -9745,8 +9745,8 @@ arm_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile,
 
   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.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 ca9799c36824..b7e62626ab7d 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -612,7 +612,7 @@ get_auto_load_pspace_data (struct program_space *pspace)
 
   info = auto_load_pspace_data.get (pspace);
   if (info == NULL)
-    info = auto_load_pspace_data.emplace (pspace);
+    info = &auto_load_pspace_data.emplace (pspace);
 
   return info;
 }
diff --git a/gdb/auxv.c b/gdb/auxv.c
index 59f38d3cf49f..1a58fd8c844e 100644
--- a/gdb/auxv.c
+++ b/gdb/auxv.c
@@ -365,7 +365,7 @@ target_read_auxv ()
 
   if (info == nullptr)
     {
-      info = auxv_inferior_data.emplace (inf);
+      info = &auxv_inferior_data.emplace (inf);
       info->data = target_read_auxv_raw (inf->top_target ());
     }
 
diff --git a/gdb/break-catch-syscall.c b/gdb/break-catch-syscall.c
index 0cf594e6fdc6..43f128620522 100644
--- a/gdb/break-catch-syscall.c
+++ b/gdb/break-catch-syscall.c
@@ -86,7 +86,7 @@ get_catch_syscall_inferior_data (struct inferior *inf)
 
   inf_data = catch_syscall_inferior_data.get (inf);
   if (inf_data == NULL)
-    inf_data = catch_syscall_inferior_data.emplace (inf);
+    inf_data = &catch_syscall_inferior_data.emplace (inf);
 
   return inf_data;
 }
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index f8fa7bcb1420..82514384d98e 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -3657,7 +3657,7 @@ get_breakpoint_objfile_data (struct objfile *objfile)
 
   bp_objfile_data = breakpoint_objfile_key.get (objfile);
   if (bp_objfile_data == NULL)
-    bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
+    bp_objfile_data = &breakpoint_objfile_key.emplace (objfile);
   return bp_objfile_data;
 }
 
diff --git a/gdb/bsd-uthread.c b/gdb/bsd-uthread.c
index 2da1197ef8b0..9d91893c6cf3 100644
--- a/gdb/bsd-uthread.c
+++ b/gdb/bsd-uthread.c
@@ -86,7 +86,7 @@ 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);
+    ops = &bsd_uthread_data.emplace (gdbarch);
   return ops;
 }
 
diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index b0eac5c6bf44..a5026e133c91 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -711,7 +711,7 @@ 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);
+    types = &dwarf_arch_cookie.emplace (arch);
   int ndx;
 
   if (this->m_addr_size == 2)
diff --git a/gdb/dwarf2/frame.c b/gdb/dwarf2/frame.c
index b6d5aac8a76a..a4dada8a7899 100644
--- a/gdb/dwarf2/frame.c
+++ b/gdb/dwarf2/frame.c
@@ -610,7 +610,7 @@ get_frame_ops (struct gdbarch *gdbarch)
 {
   dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch);
   if (result == nullptr)
-    result = dwarf2_frame_data.emplace (gdbarch);
+    result = &dwarf2_frame_data.emplace (gdbarch);
   return result;
 }
 
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 7a977a97a465..6061df0ecb8e 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -1213,7 +1213,8 @@ dwarf2_has_info (struct objfile *objfile,
 	  just_created = true;
 	}
 
-      per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
+      per_objfile
+	= &dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
     }
 
   /* Virtual sections are created from DWP files.  It's not clear those
diff --git a/gdb/elfread.c b/gdb/elfread.c
index 60785ace56d1..722f21bf7753 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -1295,7 +1295,7 @@ elf_get_probes (struct objfile *objfile)
 
   if (probes_per_bfd == NULL)
     {
-      probes_per_bfd = probe_key.emplace (objfile->obfd.get ());
+      probes_per_bfd = &probe_key.emplace (objfile->obfd.get ());
 
       /* Here we try to gather information about all types of probes from the
 	 objfile.  */
diff --git a/gdb/fbsd-tdep.c b/gdb/fbsd-tdep.c
index ae11e2e85ee9..2ddba1660ded 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -498,7 +498,7 @@ 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);
+    result = &fbsd_gdbarch_data_handle.emplace (gdbarch);
   return result;
 }
 
@@ -528,7 +528,7 @@ get_fbsd_pspace_data (struct program_space *pspace)
 
   data = fbsd_pspace_data_handle.get (pspace);
   if (data == NULL)
-    data = fbsd_pspace_data_handle.emplace (pspace);
+    data = &fbsd_pspace_data_handle.emplace (pspace);
 
   return data;
 }
diff --git a/gdb/frame-base.c b/gdb/frame-base.c
index 905c15e5047d..a732e1efc9a5 100644
--- a/gdb/frame-base.c
+++ b/gdb/frame-base.c
@@ -72,7 +72,7 @@ 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);
+    table = &frame_base_data.emplace (gdbarch);
   return table;
 }
 
diff --git a/gdb/frame-unwind.c b/gdb/frame-unwind.c
index 16d1fb1d62e4..49ade0043a10 100644
--- a/gdb/frame-unwind.c
+++ b/gdb/frame-unwind.c
@@ -76,9 +76,9 @@ get_frame_unwind_table (struct gdbarch *gdbarch)
 {
   std::vector<const frame_unwind *> *table = frame_unwind_data.get (gdbarch);
   if (table == nullptr)
-    table = frame_unwind_data.emplace (gdbarch,
-				       standard_unwinders.begin (),
-				       standard_unwinders.end ());
+    table = &frame_unwind_data.emplace (gdbarch,
+					standard_unwinders.begin (),
+					standard_unwinders.end ());
   return *table;
 }
 
diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 7f4c302b759a..cbf8e7054794 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -1258,7 +1258,7 @@ get_bfd_inferior_data (struct inferior *inf)
 
   data = bfd_inferior_data_key.get (inf);
   if (data == nullptr)
-    data = bfd_inferior_data_key.emplace (inf);
+    data = &bfd_inferior_data_key.emplace (inf);
 
   return data;
 }
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 848c5f6153a3..ebba0b2329b0 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -5741,7 +5741,7 @@ allocate_fixed_point_type_info (struct type *type)
       fixed_point_type_storage *storage
 	= fixed_point_objfile_key.get (type->objfile_owner ());
       if (storage == nullptr)
-	storage = fixed_point_objfile_key.emplace (type->objfile_owner ());
+	storage = &fixed_point_objfile_key.emplace (type->objfile_owner ());
       info = up.get ();
       storage->push_back (std::move (up));
     }
diff --git a/gdb/guile/scm-arch.c b/gdb/guile/scm-arch.c
index db99be81de4d..1bd010c023eb 100644
--- a/gdb/guile/scm-arch.c
+++ b/gdb/guile/scm-arch.c
@@ -124,7 +124,7 @@ arscm_scm_from_arch (struct gdbarch *gdbarch)
 	 is no call to scm_gc_unprotect_object for it.  */
       scm_gc_protect_object (arch_scm);
 
-      data = arch_object_data.emplace (gdbarch);
+      data = &arch_object_data.emplace (gdbarch);
       data->arch_scm = arch_scm;
     }
 
diff --git a/gdb/guile/scm-symbol.c b/gdb/guile/scm-symbol.c
index f128a45dafd2..e70afa820f2c 100644
--- a/gdb/guile/scm-symbol.c
+++ b/gdb/guile/scm-symbol.c
@@ -136,7 +136,7 @@ syscm_get_symbol_map (struct symbol *symbol)
       struct syscm_gdbarch_data *data = syscm_gdbarch_data_key.get (gdbarch);
       if (data == nullptr)
 	{
-	  data = syscm_gdbarch_data_key.emplace (gdbarch);
+	  data = &syscm_gdbarch_data_key.emplace (gdbarch);
 	  data->htab
 	    = gdbscm_create_eqable_gsmob_ptr_map (syscm_hash_symbol_smob,
 						  syscm_eq_symbol_smob);
diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c
index ae5f5328484e..7cd6f2c11b69 100644
--- a/gdb/hppa-tdep.c
+++ b/gdb/hppa-tdep.c
@@ -463,7 +463,7 @@ read_unwind_info (struct objfile *objfile)
   /* Keep a pointer to the unwind information.  */
   obj_private = hppa_objfile_priv_data.get (objfile);
   if (obj_private == NULL)
-    obj_private = hppa_objfile_priv_data.emplace (objfile);
+    obj_private = &hppa_objfile_priv_data.emplace (objfile);
 
   obj_private->unwind_info = ui;
 }
diff --git a/gdb/ia64-libunwind-tdep.c b/gdb/ia64-libunwind-tdep.c
index 06a9752e443a..41c07160cb9c 100644
--- a/gdb/ia64-libunwind-tdep.c
+++ b/gdb/ia64-libunwind-tdep.c
@@ -128,7 +128,7 @@ libunwind_descr (struct gdbarch *gdbarch)
 {
   struct libunwind_descr *result = libunwind_descr_handle.get (gdbarch);
   if (result == nullptr)
-    result = libunwind_descr_handle.emplace (gdbarch);
+    result = &libunwind_descr_handle.emplace (gdbarch);
   return result;
 }
 
diff --git a/gdb/inflow.c b/gdb/inflow.c
index 4c6c70dfee9c..d8271f9caa84 100644
--- a/gdb/inflow.c
+++ b/gdb/inflow.c
@@ -643,7 +643,7 @@ get_inflow_inferior_data (struct inferior *inf)
 
   info = inflow_inferior_data.get (inf);
   if (info == NULL)
-    info = inflow_inferior_data.emplace (inf);
+    info = &inflow_inferior_data.emplace (inf);
 
   return info;
 }
diff --git a/gdb/jit.c b/gdb/jit.c
index 4add2fa48612..db9d18f15071 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -1153,7 +1153,7 @@ 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);
+    data = &jit_gdbarch_data.emplace (gdbarch);
 
   if (!data->unwinder_registered)
     {
diff --git a/gdb/linux-fork.c b/gdb/linux-fork.c
index b6429ebb3a1a..59d3eea60690 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -117,7 +117,7 @@ get_checkpoint_inferior_data (struct inferior *inf)
 
   data = checkpoint_inferior_data_key.get (inf);
   if (data == nullptr)
-    data = checkpoint_inferior_data_key.emplace (inf);
+    data = &checkpoint_inferior_data_key.emplace (inf);
 
   return data;
 }
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index ccc7fa7cf9df..9ce28d532836 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -212,7 +212,7 @@ 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);
+    result = &linux_gdbarch_data_handle.emplace (gdbarch);
   return result;
 }
 
@@ -266,7 +266,7 @@ get_linux_inferior_data (inferior *inf)
   linux_info *info = linux_inferior_data.get (inf);
 
   if (info == nullptr)
-    info = linux_inferior_data.emplace (inf);
+    info = &linux_inferior_data.emplace (inf);
 
   return info;
 }
diff --git a/gdb/netbsd-tdep.c b/gdb/netbsd-tdep.c
index 8ad15de08750..1d97a7a8b9ad 100644
--- a/gdb/netbsd-tdep.c
+++ b/gdb/netbsd-tdep.c
@@ -369,7 +369,7 @@ 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);
+    result = &nbsd_gdbarch_data_handle.emplace (gdbarch);
   return result;
 }
 
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index 9d543005370c..f14d7f16d376 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -1085,7 +1085,7 @@ find_methods (char type, const char *theclass, const char *category,
 	}
 
       if (objc_csym == NULL)
-	objc_csym = objc_objfile_data.emplace (&objfile, objfile_csym);
+	objc_csym = &objc_objfile_data.emplace (&objfile, objfile_csym);
       else
 	/* Count of ObjC methods in this objfile should be constant.  */
 	gdb_assert (*objc_csym == objfile_csym);
diff --git a/gdb/objfiles.c b/gdb/objfiles.c
index db96a1761f8d..e33368ba19dc 100644
--- a/gdb/objfiles.c
+++ b/gdb/objfiles.c
@@ -92,7 +92,7 @@ get_objfile_pspace_data (struct program_space *pspace)
 
   info = objfiles_pspace_data.get (pspace);
   if (info == NULL)
-    info = objfiles_pspace_data.emplace (pspace);
+    info = &objfiles_pspace_data.emplace (pspace);
 
   return info;
 }
diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index 0ec708364ddc..b345229ea619 100644
--- a/gdb/python/py-registers.c
+++ b/gdb/python/py-registers.c
@@ -143,7 +143,7 @@ gdbpy_get_register_descriptor (struct gdbarch *gdbarch,
 {
   gdbpy_register_type *vecp = gdbpy_register_object_data.get (gdbarch);
   if (vecp == nullptr)
-    vecp = gdbpy_register_object_data.emplace (gdbarch);
+    vecp = &gdbpy_register_object_data.emplace (gdbarch);
   gdbpy_register_type &vec = *vecp;
 
   /* Ensure that we have enough entries in the vector.  */
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index 7c0779a82708..c2579f3c35a6 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -996,7 +996,7 @@ 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);
+    data = &pyuw_gdbarch_data.emplace (newarch);
 
   if (!data->unwinder_registered)
     {
diff --git a/gdb/reggroups.c b/gdb/reggroups.c
index ad22e4c17d40..2bc8e52cb2ea 100644
--- a/gdb/reggroups.c
+++ b/gdb/reggroups.c
@@ -113,7 +113,7 @@ get_reggroups (struct gdbarch *gdbarch)
 {
   struct reggroups *groups = reggroups_data.get (gdbarch);
   if (groups == nullptr)
-    groups = reggroups_data.emplace (gdbarch);
+    groups = &reggroups_data.emplace (gdbarch);
   return groups;
 }
 
diff --git a/gdb/registry.h b/gdb/registry.h
index c1aaadce5125..3738d4226bb8 100644
--- a/gdb/registry.h
+++ b/gdb/registry.h
@@ -124,7 +124,7 @@ class registry
        available.  It emplaces a new instance of the associated data
        type and attaches it to OBJ using this key.  The arguments, if
        any, are forwarded to the constructor.  */
-    template<typename Dummy = DATA *, typename... Args>
+    template<typename Dummy = DATA &, typename... Args>
     typename std::enable_if<std::is_same<Deleter,
 					 std::default_delete<DATA>>::value,
 			    Dummy>::type
@@ -132,7 +132,7 @@ class registry
     {
       DATA *result = new DATA (std::forward<Args> (args)...);
       set (obj, result);
-      return result;
+      return *result;
     }
 
     /* Clear the data attached to OBJ that is associated with this KEY.
diff --git a/gdb/remote-sim.c b/gdb/remote-sim.c
index ee2a1b373701..95ee9841fe07 100644
--- a/gdb/remote-sim.c
+++ b/gdb/remote-sim.c
@@ -247,7 +247,7 @@ _("Inferior %d and inferior %d would have identical simulator state.\n"
 
   if (sim_data == NULL)
     {
-      sim_data = sim_inferior_data_key.emplace (inf, sim_desc);
+      sim_data = &sim_inferior_data_key.emplace (inf, sim_desc);
     }
   else if (sim_desc)
     {
diff --git a/gdb/remote.c b/gdb/remote.c
index 1358e1c06b92..8bdc4951035a 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1729,7 +1729,7 @@ 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);
+    info = &remote_pspace_data.emplace (pspace);
   gdb_assert (info != nullptr);
   return *info;
 }
@@ -13101,7 +13101,7 @@ 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);
+    data = &remote_g_packet_data_handle.emplace (gdbarch);
   return data;
 }
 
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
index 1dd8e7080098..6c403f9685d5 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -165,7 +165,7 @@ 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);
+    per_inf = &ppc_inferior_data_key.emplace (inf);
 
   return per_inf;
 }
diff --git a/gdb/solib-aix.c b/gdb/solib-aix.c
index 3c452e0d8055..8f210312342d 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -100,7 +100,7 @@ get_solib_aix_inferior_data (struct inferior *inf)
 
   data = solib_aix_inferior_data_handle.get (inf);
   if (data == NULL)
-    data = solib_aix_inferior_data_handle.emplace (inf);
+    data = &solib_aix_inferior_data_handle.emplace (inf);
 
   return data;
 }
diff --git a/gdb/solib-darwin.c b/gdb/solib-darwin.c
index c23b8e6eb972..0a6e023128e4 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -106,7 +106,7 @@ get_darwin_info (program_space *pspace)
   if (info != nullptr)
     return info;
 
-  return solib_darwin_pspace_data.emplace (pspace);
+  return &solib_darwin_pspace_data.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 17c8e565ee1d..aed7e4ea28e3 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -202,7 +202,7 @@ get_dsbt_info (program_space *pspace)
   if (info != nullptr)
     return info;
 
-  return solib_dsbt_pspace_data.emplace (pspace);
+  return &solib_dsbt_pspace_data.emplace (pspace);
 }
 
 
diff --git a/gdb/solib-rocm.c b/gdb/solib-rocm.c
index ced7c6449992..2ffccb69be2a 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -235,7 +235,7 @@ get_solib_info (inferior *inf)
   solib_info *info = rocm_solib_data.get (inf);
 
   if (info == nullptr)
-    info = rocm_solib_data.emplace (inf, inf);
+    info = &rocm_solib_data.emplace (inf, inf);
 
   return info;
 }
diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c
index 8beb545fd9a7..49b005beb1a2 100644
--- a/gdb/solib-svr4.c
+++ b/gdb/solib-svr4.c
@@ -471,7 +471,7 @@ 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);
+    info = &solib_svr4_pspace_data.emplace (pspace);
 
   return info;
 }
diff --git a/gdb/source.c b/gdb/source.c
index ade1bb2789f9..825f09f59479 100644
--- a/gdb/source.c
+++ b/gdb/source.c
@@ -226,7 +226,7 @@ get_source_location (program_space *pspace)
   current_source_location *loc
     = current_source_key.get (pspace);
   if (loc == nullptr)
-    loc = current_source_key.emplace (pspace);
+    loc = &current_source_key.emplace (pspace);
   return loc;
 }
 
diff --git a/gdb/svr4-tls-tdep.c b/gdb/svr4-tls-tdep.c
index eac0357abd50..163985918a65 100644
--- a/gdb/svr4-tls-tdep.c
+++ b/gdb/svr4-tls-tdep.c
@@ -44,7 +44,7 @@ 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);
+    result = &svr4_tls_gdbarch_data_handle.emplace (gdbarch);
   return result;
 }
 
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 35e380980b56..37a9fe62f40a 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -1318,7 +1318,7 @@ get_symbol_cache (struct program_space *pspace)
 
   if (cache == NULL)
     {
-      cache = symbol_cache_key.emplace (pspace);
+      cache = &symbol_cache_key.emplace (pspace);
       resize_symbol_cache (cache, symbol_cache_size);
     }
 
@@ -6290,7 +6290,7 @@ get_main_info (program_space *pspace)
 	 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);
+      info = &main_progspace_key.emplace (pspace);
     }
 
   return info;
diff --git a/gdb/target-descriptions.c b/gdb/target-descriptions.c
index 0096263547ad..1c4bbe297c08 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -445,7 +445,7 @@ get_arch_data (struct gdbarch *gdbarch)
 {
   tdesc_arch_data *result = tdesc_data.get (gdbarch);
   if (result == nullptr)
-    result = tdesc_data.emplace (gdbarch);
+    result = &tdesc_data.emplace (gdbarch);
   return result;
 }
 
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index ad4c35ff8c6e..6a8f26bf3e65 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -192,7 +192,7 @@ 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);
+    result = &windows_gdbarch_data_handle.emplace (gdbarch);
   return result;
 }
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/3] gdb/registry: add registry::key::try_emplace
  2026-02-08 22:04 [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure simon.marchi
  2026-02-08 22:04 ` [PATCH 2/3] gdb/registry: make registry::key::emplace return a reference simon.marchi
@ 2026-02-08 22:04 ` simon.marchi
  2026-02-09 16:16   ` Tom Tromey
  2026-02-09 16:11 ` [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure Tom Tromey
  2 siblings, 1 reply; 8+ messages in thread
From: simon.marchi @ 2026-02-08 22:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

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
---
 gdb/ada-lang.c               | 14 ++------------
 gdb/ada-tasks.c              | 16 ++--------------
 gdb/aix-thread.c             |  8 +-------
 gdb/amd-dbgapi-target.c      |  7 +------
 gdb/arm-tdep.c               |  6 ++----
 gdb/auto-load.c              |  8 +-------
 gdb/break-catch-syscall.c    |  8 +-------
 gdb/breakpoint.c             |  7 +------
 gdb/bsd-uthread.c            |  5 +----
 gdb/dwarf2/expr.c            |  4 +---
 gdb/dwarf2/frame.c           |  5 +----
 gdb/fbsd-tdep.c              | 13 ++-----------
 gdb/frame-base.c             |  5 +----
 gdb/frame-unwind.c           |  5 +----
 gdb/gdb_bfd.c                |  8 +-------
 gdb/ia64-libunwind-tdep.c    |  5 +----
 gdb/inflow.c                 |  8 +-------
 gdb/jit.c                    |  4 +---
 gdb/linux-fork.c             |  8 +-------
 gdb/linux-tdep.c             | 12 ++----------
 gdb/netbsd-tdep.c            |  5 +----
 gdb/objfiles.c               |  8 +-------
 gdb/python/py-registers.c    |  5 +----
 gdb/python/py-unwind.c       |  4 +---
 gdb/python/python-internal.h |  8 +-------
 gdb/reggroups.c              |  5 +----
 gdb/registry.h               | 14 ++++++++++++++
 gdb/remote.c                 | 12 ++----------
 gdb/rs6000-tdep.c            |  7 +------
 gdb/solib-aix.c              |  8 +-------
 gdb/solib-darwin.c           |  6 +-----
 gdb/solib-dsbt.c             |  6 +-----
 gdb/solib-rocm.c             |  7 +------
 gdb/solib-svr4.c             |  7 +------
 gdb/source.c                 |  6 +-----
 gdb/svr4-tls-tdep.c          |  5 +----
 gdb/symtab.c                 | 21 +++++++--------------
 gdb/target-descriptions.c    |  5 +----
 gdb/windows-tdep.c           |  5 +----
 39 files changed, 64 insertions(+), 236 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index eaac2816880e..c7533921b778 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -307,13 +307,7 @@ static const registry<inferior>::key<ada_inferior_data> 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<program_space>::key<cache_entry_set>
 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 22ba47c262d0..0e124e1a2a21 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 120b032fcf4e..823fb13cb291 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 d636f1a39ed6..655dddd36ed5 100644
--- a/gdb/amd-dbgapi-target.c
+++ b/gdb/amd-dbgapi-target.c
@@ -369,12 +369,7 @@ static const registry<inferior>::key<amd_dbgapi_inferior_info>
 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 7d6aa06f2337..7f33786f2efe 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 b7e62626ab7d..b5fec13743cb 100644
--- a/gdb/auto-load.c
+++ b/gdb/auto-load.c
@@ -608,13 +608,7 @@ static const registry<program_space>::key<auto_load_pspace_info>
 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 43f128620522..1a0bb9310801 100644
--- a/gdb/break-catch-syscall.c
+++ b/gdb/break-catch-syscall.c
@@ -82,13 +82,7 @@ static const registry<inferior>::key<catch_syscall_inferior_data>
 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 82514384d98e..8f20a1792c8b 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 9d91893c6cf3..e4c5c9ee3e37 100644
--- a/gdb/bsd-uthread.c
+++ b/gdb/bsd-uthread.c
@@ -84,10 +84,7 @@ static const registry<gdbarch>::key<struct bsd_uthread_ops> 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 a5026e133c91..9f4a5bc7ede4 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 a4dada8a7899..94586b06ef30 100644
--- a/gdb/dwarf2/frame.c
+++ b/gdb/dwarf2/frame.c
@@ -608,10 +608,7 @@ static const registry<gdbarch>::key<dwarf2_frame_ops> 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 2ddba1660ded..2e3e2c802106 100644
--- a/gdb/fbsd-tdep.c
+++ b/gdb/fbsd-tdep.c
@@ -496,10 +496,7 @@ static const registry<gdbarch>::key<fbsd_gdbarch_data>
 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<program_space>::key<fbsd_pspace_data>
 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 a732e1efc9a5..e51e47430e44 100644
--- a/gdb/frame-base.c
+++ b/gdb/frame-base.c
@@ -70,10 +70,7 @@ static const registry<gdbarch>::key<struct frame_base_table> 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 49ade0043a10..a942ed8c2071 100644
--- a/gdb/frame-unwind.c
+++ b/gdb/frame-unwind.c
@@ -74,12 +74,9 @@ static const registry<gdbarch>::key<std::vector<const frame_unwind *>>
 static std::vector<const frame_unwind *> &
 get_frame_unwind_table (struct gdbarch *gdbarch)
 {
-  std::vector<const frame_unwind *> *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 cbf8e7054794..98ea46677493 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -1254,13 +1254,7 @@ static const registry<inferior>::key<bfd_inferior_data> 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 41c07160cb9c..52902dc17f22 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 d8271f9caa84..ff118b558caf 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 db9d18f15071..21e8667a7af6 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -1151,9 +1151,7 @@ static const registry<gdbarch>::key<jit_gdbarch_data_type> 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 59d3eea60690..960995d94f1c 100644
--- a/gdb/linux-fork.c
+++ b/gdb/linux-fork.c
@@ -113,13 +113,7 @@ static const registry<inferior>::key<checkpoint_inferior_data>
 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 9ce28d532836..edcf4ea2401c 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -210,10 +210,7 @@ static const registry<gdbarch>::key<linux_gdbarch_data>
 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 1d97a7a8b9ad..e369622ae7f7 100644
--- a/gdb/netbsd-tdep.c
+++ b/gdb/netbsd-tdep.c
@@ -367,10 +367,7 @@ static const registry<gdbarch>::key<nbsd_gdbarch_data>
 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 e33368ba19dc..14c07371f15e 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);
 }
 
 \f
diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index b345229ea619..caf7bcc0f9d5 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 c2579f3c35a6..d4c35e17f145 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 722edbd1a1cf..8925714f3c11 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -1209,13 +1209,7 @@ class gdbpy_registry
   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
diff --git a/gdb/reggroups.c b/gdb/reggroups.c
index 2bc8e52cb2ea..cf377ddee766 100644
--- a/gdb/reggroups.c
+++ b/gdb/reggroups.c
@@ -111,10 +111,7 @@ static const registry<gdbarch>::key<reggroups> reggroups_data;
 static reggroups *
 get_reggroups (struct gdbarch *gdbarch)
 {
-  struct reggroups *groups = reggroups_data.get (gdbarch);
-  if (groups == nullptr)
-    groups = &reggroups_data.emplace (gdbarch);
-  return groups;
+  return &reggroups_data.try_emplace (gdbarch);
 }
 
 /* See reggroups.h.  */
diff --git a/gdb/registry.h b/gdb/registry.h
index 3738d4226bb8..d76d3a80be8f 100644
--- a/gdb/registry.h
+++ b/gdb/registry.h
@@ -135,6 +135,20 @@ class registry
       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<typename... Args>
+    DATA &
+    try_emplace (T *obj, Args &&...args) const
+    {
+      DATA *result = get (obj);
+      if (result == nullptr)
+	result = &emplace (obj, std::forward<Args> (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 8bdc4951035a..edabc2c9ee53 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1727,11 +1727,7 @@ static const registry<program_space>::key<remote_per_progspace>
 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<gdbarch>::key<struct remote_g_packet_data>
 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 6c403f9685d5..8aa155e1f364 100644
--- a/gdb/rs6000-tdep.c
+++ b/gdb/rs6000-tdep.c
@@ -162,12 +162,7 @@ static const registry<inferior>::key<ppc_inferior_data> 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 8f210312342d..b7daec8ff90b 100644
--- a/gdb/solib-aix.c
+++ b/gdb/solib-aix.c
@@ -96,13 +96,7 @@ static const registry<inferior>::key<solib_aix_inferior_data>
 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 0a6e023128e4..aaa34ec0764e 100644
--- a/gdb/solib-darwin.c
+++ b/gdb/solib-darwin.c
@@ -102,11 +102,7 @@ static const registry<program_space>::key<darwin_info>
 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 aed7e4ea28e3..3ad6f5ab2f3e 100644
--- a/gdb/solib-dsbt.c
+++ b/gdb/solib-dsbt.c
@@ -198,11 +198,7 @@ static const registry<program_space>::key<dsbt_info> 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 2ffccb69be2a..c17d5932b553 100644
--- a/gdb/solib-rocm.c
+++ b/gdb/solib-rocm.c
@@ -232,12 +232,7 @@ struct rocm_solib_ops : public solib_ops
 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 49b005beb1a2..c8d78fbfc1dd 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 825f09f59479..f890533b9317 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 = &current_source_key.emplace (pspace);
-  return loc;
+  return &current_source_key.try_emplace (pspace);
 }
 
 /* See source.h.  */
diff --git a/gdb/svr4-tls-tdep.c b/gdb/svr4-tls-tdep.c
index 163985918a65..b39623b324ca 100644
--- a/gdb/svr4-tls-tdep.c
+++ b/gdb/svr4-tls-tdep.c
@@ -42,10 +42,7 @@ static const registry<gdbarch>::key<svr4_tls_gdbarch_data>
 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 37a9fe62f40a..5d02d328694b 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 1c4bbe297c08..3464af80dec9 100644
--- a/gdb/target-descriptions.c
+++ b/gdb/target-descriptions.c
@@ -443,10 +443,7 @@ static const registry<gdbarch>::key<tdesc_arch_data> 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 6a8f26bf3e65..c41b500afc1e 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -190,10 +190,7 @@ static const registry<gdbarch>::key<windows_gdbarch_data>
 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.  */
-- 
2.53.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure
  2026-02-08 22:04 [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure simon.marchi
  2026-02-08 22:04 ` [PATCH 2/3] gdb/registry: make registry::key::emplace return a reference simon.marchi
  2026-02-08 22:04 ` [PATCH 3/3] gdb/registry: add registry::key::try_emplace simon.marchi
@ 2026-02-09 16:11 ` Tom Tromey
  2 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2026-02-09 16:11 UTC (permalink / raw)
  To: simon.marchi; +Cc: gdb-patches, Simon Marchi

>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:

Simon> From: Simon Marchi <simon.marchi@efficios.com>
Simon> scm-arch.c uses "void" as a registry data type here:

Simon>     /* Use a 'void *' here because it isn't guaranteed that SCM is a
Simon>        pointer.  */
Simon>     static const registry<gdbarch>::key<void, gdb::noop_deleter<void>>
Simon>          arch_object_data;

Simon> This conflicts with my subsequent patch that makes the emplace method
Simon> return a reference because it's not valid to have `void &`.

Simon> Circumvent the problem by defining a structure type to use in the
Simon> registry, instead of storing the SCM directly.  I think that makes the
Simon> code more straightforward and less hacky anyway (at the cost of one
Simon> extra allocate per gdbarch that you would use in your Guile code...).

Ok.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 2/3] gdb/registry: make registry::key::emplace return a reference
  2026-02-08 22:04 ` [PATCH 2/3] gdb/registry: make registry::key::emplace return a reference simon.marchi
@ 2026-02-09 16:12   ` Tom Tromey
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2026-02-09 16:12 UTC (permalink / raw)
  To: simon.marchi; +Cc: gdb-patches

>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:

Simon> From: Simon Marchi <simon.marchi@polymtl.ca>
Simon> Since we use C++ and not C, I think that we should gradually move to
Simon> using references for things that can never be nullptr.  One example of
Simon> this is the return value of the emplace method.

Seems reasonable.

Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] gdb/registry: add registry::key::try_emplace
  2026-02-08 22:04 ` [PATCH 3/3] gdb/registry: add registry::key::try_emplace simon.marchi
@ 2026-02-09 16:16   ` Tom Tromey
  2026-02-09 18:18     ` Simon Marchi
  0 siblings, 1 reply; 8+ messages in thread
From: Tom Tromey @ 2026-02-09 16:16 UTC (permalink / raw)
  To: simon.marchi; +Cc: gdb-patches, Simon Marchi

>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:

Simon> From: Simon Marchi <simon.marchi@efficios.com>
Simon> We have many times the pattern:

Simon>     current_source_location *loc
Simon>       = current_source_key.get (pspace);
Simon>     if (loc == nullptr)
Simon>       loc = current_source_key.emplace (pspace);
Simon>     return loc;

I like it.

Simon> I thought it would be nice to have it directly part of registry::key.
Simon> Add a try_emplace method, which is like emplace, except that it returns
Simon> the existing data, if any.  The try_emplace name comes from similar
Simon> methods in std::map or gdb::unordered_map
Simon> (ankerl::unordered_dense::map).

emplace still just leaks if you call it twice.  That's maybe something
to fix someday.  Though probably doesn't happen in practice.

I think in the past I balked at implementing this fix but it seems to me
that since emplace is only available when the default deleter is in use,
it really shouldn't be a problem at all.

Anyway this looks good to me.  Thanks.
Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] gdb/registry: add registry::key::try_emplace
  2026-02-09 16:16   ` Tom Tromey
@ 2026-02-09 18:18     ` Simon Marchi
  2026-02-09 18:28       ` Tom Tromey
  0 siblings, 1 reply; 8+ messages in thread
From: Simon Marchi @ 2026-02-09 18:18 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, Simon Marchi

On 2/9/26 11:16 AM, Tom Tromey wrote:
>>>>>> "Simon" == simon marchi <simon.marchi@polymtl.ca> writes:
> 
> Simon> From: Simon Marchi <simon.marchi@efficios.com>
> Simon> We have many times the pattern:
> 
> Simon>     current_source_location *loc
> Simon>       = current_source_key.get (pspace);
> Simon>     if (loc == nullptr)
> Simon>       loc = current_source_key.emplace (pspace);
> Simon>     return loc;
> 
> I like it.
> 
> Simon> I thought it would be nice to have it directly part of registry::key.
> Simon> Add a try_emplace method, which is like emplace, except that it returns
> Simon> the existing data, if any.  The try_emplace name comes from similar
> Simon> methods in std::map or gdb::unordered_map
> Simon> (ankerl::unordered_dense::map).
> 
> emplace still just leaks if you call it twice.  That's maybe something
> to fix someday.  Though probably doesn't happen in practice.
> 
> I think in the past I balked at implementing this fix but it seems to me
> that since emplace is only available when the default deleter is in use,
> it really shouldn't be a problem at all.

I was wondering about that too, if we have any legitimate use case where
we set a registry value to overwrite an existing one.  If not, we could
have an assert.  Or if we do, then perhaps that setting a value to
overwrite an existing one should call the deleter on the old one
first.  Hard to say without a concrete use case.

> 
> Anyway this looks good to me.  Thanks.
> Approved-By: Tom Tromey <tom@tromey.com>

Thanks, series pushed.

Simon

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 3/3] gdb/registry: add registry::key::try_emplace
  2026-02-09 18:18     ` Simon Marchi
@ 2026-02-09 18:28       ` Tom Tromey
  0 siblings, 0 replies; 8+ messages in thread
From: Tom Tromey @ 2026-02-09 18:28 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches, Simon Marchi

>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:

>> I think in the past I balked at implementing this fix but it seems to me
>> that since emplace is only available when the default deleter is in use,
>> it really shouldn't be a problem at all.

Simon> I was wondering about that too, if we have any legitimate use case where
Simon> we set a registry value to overwrite an existing one.  If not, we could
Simon> have an assert.  Or if we do, then perhaps that setting a value to
Simon> overwrite an existing one should call the deleter on the old one
Simon> first.  Hard to say without a concrete use case.

I think an assert would be fine.  I don't believe there are any cases
where a slot is overwritten without first clearing it.  Most code should
use try_emplace anyhow.

Tom

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-02-09 18:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-08 22:04 [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure simon.marchi
2026-02-08 22:04 ` [PATCH 2/3] gdb/registry: make registry::key::emplace return a reference simon.marchi
2026-02-09 16:12   ` Tom Tromey
2026-02-08 22:04 ` [PATCH 3/3] gdb/registry: add registry::key::try_emplace simon.marchi
2026-02-09 16:16   ` Tom Tromey
2026-02-09 18:18     ` Simon Marchi
2026-02-09 18:28       ` Tom Tromey
2026-02-09 16:11 ` [PATCH 1/3] gdb/guile: wrap scm-arch.c per-arch data in a structure Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox