[binutils-gdb] gdb/ctf: add unique_ptr types

Simon Marchi via Gdb-cvs <[email protected]>
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=11f28f49d6b820d4b2bae6a9605573e61881a94a

commit 11f28f49d6b820d4b2bae6a9605573e61881a94a
Author: Simon Marchi <[email protected]>
Date:   Fri Feb 27 22:51:49 2026 -0500

    gdb/ctf: add unique_ptr types
    
    Add ctf_archive_up and ctf_dict_up to automatically manage the lifetime
    of a ctf_archive_t or ctf_dict_t and use them.
    
    Change-Id: I51b3548b1bb28941c7bad776a11a238eb25789e4
    Approved-By: Tom Tromey <[email protected]>

Diff:
---
 gdb/ctfread.c | 56 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 35 insertions(+), 21 deletions(-)

diff --git a/gdb/ctfread.c b/gdb/ctfread.c
index 141f51f621b..6c1bbebcf2c 100644
--- a/gdb/ctfread.c
+++ b/gdb/ctfread.c
@@ -125,28 +125,40 @@ ctf_kind_str (uint32_t kind)
 
 using ctf_type_map = gdb::unordered_map<ctf_id_t, struct type *>;
 
-struct ctf_dict_info
+struct ctf_archive_closer
 {
-  explicit ctf_dict_info (ctf_dict_t *dict) : dict (dict) {}
-  ~ctf_dict_info ();
+  void operator() (ctf_archive_t *arc) const noexcept
+  {
+    ctf_close (arc);
+  }
+};
 
-  /* Map from IDs to types.  */
-  ctf_type_map type_map;
+using ctf_archive_up = std::unique_ptr<ctf_archive_t, ctf_archive_closer>;
 
-  /* The dictionary.  */
-  ctf_dict_t *dict;
+struct ctf_dict_closer
+{
+  void operator() (ctf_dict_t *dict) const noexcept
+  {
+    ctf_dict_close (dict);
+  }
 };
 
-/* Cleanup function for the ctf_dict_key data.  */
-ctf_dict_info::~ctf_dict_info ()
+using ctf_dict_up = std::unique_ptr<ctf_dict_t, ctf_dict_closer>;
+
+struct ctf_dict_info
 {
-  if (dict == nullptr)
-    return;
+  explicit ctf_dict_info (ctf_archive_up archive, ctf_dict_up dict)
+    : archive (std::move (archive)),
+      dict (std::move (dict))
+  {}
 
-  ctf_archive_t *arc = ctf_get_arc (dict);
-  ctf_dict_close (dict);
-  ctf_close (arc);
-}
+  /* Map from IDs to types.  */
+  ctf_type_map type_map;
+
+  /* The archive and dictionary.  */
+  ctf_archive_up archive;
+  ctf_dict_up dict;
+};
 
 static const registry<objfile>::key<ctf_dict_info> ctf_dict_key;
 
@@ -1542,26 +1554,28 @@ elfctf_build_psymtabs (struct objfile *of)
   CTF_SCOPED_DEBUG_START_END ("building psymtabs for %s",
 			      bfd_get_filename (abfd));
 
-  ctf_archive_t *arc = ctf_bfdopen (abfd, &err);
+  ctf_archive_up arc (ctf_bfdopen (abfd, &err));
   if (arc == nullptr)
     error (_("ctf_bfdopen failed on %s - %s"),
 	   bfd_get_filename (abfd), ctf_errmsg (err));
 
-  ctf_dict_t *dict = ctf_dict_open (arc, NULL, &err);
+  ctf_dict_up dict (ctf_dict_open (arc.get (), NULL, &err));
   if (dict == nullptr)
     error (_("ctf_dict_open failed on %s - %s"),
 	   bfd_get_filename (abfd), ctf_errmsg (err));
-  ctf_dict_key.emplace (of, dict);
 
-  pcu.dict = dict;
+  ctf_dict_info &dict_info
+    = ctf_dict_key.emplace (of, std::move (arc), std::move (dict));
+
+  pcu.dict = dict_info.dict.get ();
   pcu.of = of;
-  pcu.arc = arc;
+  pcu.arc = dict_info.archive.get ();
 
   psymbol_functions *psf = new psymbol_functions ();
   of->qf.emplace_front (psf);
   pcu.psf = psf;
 
-  if (ctf_archive_iter (arc, build_ctf_archive_member, &pcu) < 0)
+  if (ctf_archive_iter (pcu.arc, build_ctf_archive_member, &pcu) < 0)
     error (_("ctf_archive_iter failed in input file %s: - %s"),
 	   bfd_get_filename (abfd), ctf_errmsg (err));
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.