This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[binutils-gdb] Remove target_fileio_close_cleanup


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

commit 770623f79f7fdff38f62c641438759748cf3f138
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Feb 23 14:42:26 2018 -0700

    Remove target_fileio_close_cleanup
    
    This removes target_fileio_close_cleanup in favor of a new RAII class.
    The new class is similar to scoped_fd but calls
    target_fileio_close_cleanup rather than close.
    
    Regression tested by the buildbot.
    
    gdb/ChangeLog
    2018-03-17  Tom Tromey  <tom@tromey.com>
    
    	* target.c (class scoped_target_fd): New.
    	(target_fileio_close_cleanup): Remove.
    	(target_fileio_read_alloc_1): Use scoped_target_fd.

Diff:
---
 gdb/ChangeLog |  6 ++++++
 gdb/target.c  | 48 ++++++++++++++++++++++++++++++++----------------
 2 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 53f05c7..086d117 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2018-03-17  Tom Tromey  <tom@tromey.com>
+
+	* target.c (class scoped_target_fd): New.
+	(target_fileio_close_cleanup): Remove.
+	(target_fileio_read_alloc_1): Use scoped_target_fd.
+
 2018-03-16  Simon Marchi  <simon.marchi@polymtl.ca>
 
 	* silent-rules.mk: New.
diff --git a/gdb/target.c b/gdb/target.c
index 2b97118..84f5228 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -3087,14 +3087,36 @@ target_fileio_readlink (struct inferior *inf, const char *filename,
   return {};
 }
 
-static void
-target_fileio_close_cleanup (void *opaque)
+/* Like scoped_fd, but specific to target fileio.  */
+
+class scoped_target_fd
 {
-  int fd = *(int *) opaque;
-  int target_errno;
+public:
+  explicit scoped_target_fd (int fd) noexcept
+    : m_fd (fd)
+  {
+  }
 
-  target_fileio_close (fd, &target_errno);
-}
+  ~scoped_target_fd ()
+  {
+    if (m_fd >= 0)
+      {
+	int target_errno;
+
+	target_fileio_close (m_fd, &target_errno);
+      }
+  }
+
+  DISABLE_COPY_AND_ASSIGN (scoped_target_fd);
+
+  int get () const noexcept
+  {
+    return m_fd;
+  }
+
+private:
+  int m_fd;
+};
 
 /* Read target file FILENAME, in the filesystem as seen by INF.  If
    INF is NULL, use the filesystem seen by the debugger (GDB or, for
@@ -3108,20 +3130,16 @@ static LONGEST
 target_fileio_read_alloc_1 (struct inferior *inf, const char *filename,
 			    gdb_byte **buf_p, int padding)
 {
-  struct cleanup *close_cleanup;
   size_t buf_alloc, buf_pos;
   gdb_byte *buf;
   LONGEST n;
-  int fd;
   int target_errno;
 
-  fd = target_fileio_open (inf, filename, FILEIO_O_RDONLY, 0700,
-			   &target_errno);
-  if (fd == -1)
+  scoped_target_fd fd (target_fileio_open (inf, filename, FILEIO_O_RDONLY,
+					   0700, &target_errno));
+  if (fd.get () == -1)
     return -1;
 
-  close_cleanup = make_cleanup (target_fileio_close_cleanup, &fd);
-
   /* Start by reading up to 4K at a time.  The target will throttle
      this number down if necessary.  */
   buf_alloc = 4096;
@@ -3129,20 +3147,18 @@ target_fileio_read_alloc_1 (struct inferior *inf, const char *filename,
   buf_pos = 0;
   while (1)
     {
-      n = target_fileio_pread (fd, &buf[buf_pos],
+      n = target_fileio_pread (fd.get (), &buf[buf_pos],
 			       buf_alloc - buf_pos - padding, buf_pos,
 			       &target_errno);
       if (n < 0)
 	{
 	  /* An error occurred.  */
-	  do_cleanups (close_cleanup);
 	  xfree (buf);
 	  return -1;
 	}
       else if (n == 0)
 	{
 	  /* Read all there was.  */
-	  do_cleanups (close_cleanup);
 	  if (buf_pos == 0)
 	    xfree (buf);
 	  else


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]