public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom Tromey <tromey@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Use unique_ptr in CLI logging code
Date: Mon, 28 Mar 2022 20:19:39 +0000 (GMT)	[thread overview]
Message-ID: <20220328201939.A1D0B3857C44@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8b1931b39443acab9d1f8272a8a81b261f7ef29b

commit 8b1931b39443acab9d1f8272a8a81b261f7ef29b
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Dec 31 11:34:59 2021 -0700

    Use unique_ptr in CLI logging code
    
    This changes the CLI logging code to avoid manual memory management
    (to the extent possible) by using unique_ptr in a couple of spots.
    This will come in handy in a later patch.

Diff:
---
 gdb/cli/cli-interp.c | 43 +++++++++++++++++--------------------------
 1 file changed, 17 insertions(+), 26 deletions(-)

diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c
index b310ef2b050..d36715d1d8f 100644
--- a/gdb/cli/cli-interp.c
+++ b/gdb/cli/cli-interp.c
@@ -396,9 +396,9 @@ struct saved_output_files
   ui_file *log;
   ui_file *targ;
   ui_file *targerr;
-  ui_file *file_to_delete;
+  ui_file_up file_to_delete;
 };
-static saved_output_files saved_output;
+static std::unique_ptr<saved_output_files> saved_output;
 
 /* See cli-interp.h.  */
 
@@ -408,12 +408,12 @@ cli_interp_base::set_logging (ui_file_up logfile, bool logging_redirect,
 {
   if (logfile != nullptr)
     {
-      saved_output.out = gdb_stdout;
-      saved_output.err = gdb_stderr;
-      saved_output.log = gdb_stdlog;
-      saved_output.targ = gdb_stdtarg;
-      saved_output.targerr = gdb_stdtargerr;
-      gdb_assert (saved_output.file_to_delete == nullptr);
+      saved_output.reset (new saved_output_files);
+      saved_output->out = gdb_stdout;
+      saved_output->err = gdb_stderr;
+      saved_output->log = gdb_stdlog;
+      saved_output->targ = gdb_stdtarg;
+      saved_output->targerr = gdb_stdtargerr;
 
       /* If something is not being redirected, then a tee containing both the
 	 logfile and stdout.  */
@@ -422,10 +422,10 @@ cli_interp_base::set_logging (ui_file_up logfile, bool logging_redirect,
       if (!logging_redirect || !debug_redirect)
 	{
 	  tee = new tee_file (gdb_stdout, std::move (logfile));
-	  saved_output.file_to_delete = tee;
+	  saved_output->file_to_delete.reset (tee);
 	}
       else
-	saved_output.file_to_delete = logfile.release ();
+	saved_output->file_to_delete = std::move (logfile);
 
       gdb_stdout = logging_redirect ? logfile_p : tee;
       gdb_stdlog = debug_redirect ? logfile_p : tee;
@@ -435,22 +435,13 @@ cli_interp_base::set_logging (ui_file_up logfile, bool logging_redirect,
     }
   else
     {
-      /* Delete the correct file.  If it's the tee then the logfile will also
-	 be deleted.  */
-      delete saved_output.file_to_delete;
-
-      gdb_stdout = saved_output.out;
-      gdb_stderr = saved_output.err;
-      gdb_stdlog = saved_output.log;
-      gdb_stdtarg = saved_output.targ;
-      gdb_stdtargerr = saved_output.targerr;
-
-      saved_output.out = nullptr;
-      saved_output.err = nullptr;
-      saved_output.log = nullptr;
-      saved_output.targ = nullptr;
-      saved_output.targerr = nullptr;
-      saved_output.file_to_delete = nullptr;
+      gdb_stdout = saved_output->out;
+      gdb_stderr = saved_output->err;
+      gdb_stdlog = saved_output->log;
+      gdb_stdtarg = saved_output->targ;
+      gdb_stdtargerr = saved_output->targerr;
+
+      saved_output.reset (nullptr);
     }
 }


                 reply	other threads:[~2022-03-28 20:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220328201939.A1D0B3857C44@sourceware.org \
    --to=tromey@sourceware.org \
    --cc=gdb-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).