This is the mail archive of the gdb-patches@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]

[PATCH RFC 4/5] Introduce scoped_mmapped_file


We already have scoped_mmap, which can is a thin RAII layer over mmap.
If one simply wants to mmap an entire file for reading, it takes a bit
of boilerplate.  This patch introduces the scoped_mmapped_file class to
make this easier.

gdb/ChangeLog:

	* common/scoped_mmapped_file.h: New file.
	* common/scoped_fd.h (class scoped_fd) <destroy>: New method.
	<reset>: New method.
	<~scoped_fd>: Use destroy.
	* unittests/scoped_mmapped_file-selftests.c: New file.
	* Makefile.in (SUBDIR_UNITTESTS_SRCS): Add
	unittests/scoped_mmapped_file-selftests.c.
---
 gdb/Makefile.in                               |  1 +
 gdb/common/scoped_fd.h                        | 17 ++++-
 gdb/common/scoped_mmapped_file.h              | 70 ++++++++++++++++++++
 gdb/unittests/scoped_mmapped_file-selftests.c | 95 +++++++++++++++++++++++++++
 4 files changed, 181 insertions(+), 2 deletions(-)
 create mode 100644 gdb/common/scoped_mmapped_file.h
 create mode 100644 gdb/unittests/scoped_mmapped_file-selftests.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 87d74a7..4e302c8 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -429,6 +429,7 @@ SUBDIR_UNITTESTS_SRCS = \
 	unittests/rsp-low-selftests.c \
 	unittests/scoped_fd-selftests.c \
 	unittests/scoped_mmap-selftests.c \
+	unittests/scoped_mmapped_file-selftests.c \
 	unittests/scoped_restore-selftests.c \
 	unittests/string_view-selftests.c \
 	unittests/tracepoint-selftests.c \
diff --git a/gdb/common/scoped_fd.h b/gdb/common/scoped_fd.h
index a6a8ab9..574c26e 100644
--- a/gdb/common/scoped_fd.h
+++ b/gdb/common/scoped_fd.h
@@ -34,8 +34,7 @@ public:
   explicit scoped_fd (int fd = -1) noexcept : m_fd (fd) {}
   ~scoped_fd ()
   {
-    if (m_fd >= 0)
-      close (m_fd);
+    destroy ();
   }
 
   DISABLE_COPY_AND_ASSIGN (scoped_fd);
@@ -52,7 +51,21 @@ public:
     return m_fd;
   }
 
+  void reset (int new_fd)
+  {
+    destroy ();
+
+    m_fd = new_fd;
+  }
+
 private:
+
+  void destroy ()
+  {
+    if (m_fd >= 0)
+      close (m_fd);
+  }
+
   int m_fd;
 };
 
diff --git a/gdb/common/scoped_mmapped_file.h b/gdb/common/scoped_mmapped_file.h
new file mode 100644
index 0000000..18bc26a
--- /dev/null
+++ b/gdb/common/scoped_mmapped_file.h
@@ -0,0 +1,70 @@
+/* scoped_mmapped_file, automatically map/unmap entire files.
+
+   Copyright (C) 2018 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef SCOPED_MMAPPED_FILE_H
+#define SCOPED_MMAPPED_FILE_H
+
+#if defined(HAVE_SYS_MMAN_H)
+
+#include "scoped_mmap.h"
+#include "scoped_fd.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+/* A smart-pointer-like class to automatically mmap an entire file.  */
+
+class scoped_mmapped_file
+{
+public:
+
+  scoped_mmapped_file () = default;
+
+  /* Map FILENAME in memory.  Throw an error if anything goes wrong.  */
+  scoped_mmapped_file (const char *filename)
+  {
+    m_fd.reset (open (filename, 0, O_RDONLY));
+    if (m_fd.get () < 0)
+      perror_with_name ("open");
+
+    off_t size = lseek (m_fd.get (), 0, SEEK_END);
+    if (size < 0)
+      perror_with_name ("lseek");
+
+    /* We can't map an empty file.  */
+    if (size == 0)
+      error (_("file to mmap is empty"));
+
+    m_mmap.reset (nullptr, size, PROT_READ, MAP_PRIVATE, m_fd.get (), 0);
+    if (m_mmap.get () == MAP_FAILED)
+      perror_with_name ("mmap");
+  }
+
+  size_t size () const noexcept { return m_mmap.size (); }
+  void *get () const noexcept { return m_mmap.get (); }
+
+private:
+  scoped_mmap m_mmap;
+  scoped_fd m_fd;
+};
+
+#endif /* defined(HAVE_SYS_MMAN_H) */
+
+#endif /* SCOPED_MMAPPED_FILE_H */
diff --git a/gdb/unittests/scoped_mmapped_file-selftests.c b/gdb/unittests/scoped_mmapped_file-selftests.c
new file mode 100644
index 0000000..8d8f517
--- /dev/null
+++ b/gdb/unittests/scoped_mmapped_file-selftests.c
@@ -0,0 +1,95 @@
+/* Self tests for scoped_mmapped_file for GDB, the GNU debugger.
+
+   Copyright (C) 2018 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "selftest.h"
+#include "common/scoped_mmapped_file.h"
+
+#if defined (HAVE_SYS_MMAN_H)
+
+namespace selftests {
+namespace scoped_mmapped_file {
+
+static void
+test_destroy ()
+{
+  char filename[] = "scoped_mmapped_file-selftest-XXXXXX";
+  int fd = mkstemp (filename);
+  SELF_CHECK (fd >= 0);
+
+  write (fd, "Hello!", 7);
+  close (fd);
+
+  {
+    ::scoped_mmapped_file f (filename);
+
+    SELF_CHECK (f.get () != MAP_FAILED);
+    SELF_CHECK (f.size () == 7);
+    SELF_CHECK (0 == strcmp ((char *) f.get (), "Hello!"));
+  }
+
+  unlink (filename);
+}
+
+static void
+test_default_constructor ()
+{
+  ::scoped_mmapped_file f;
+
+  SELF_CHECK (f.get () == MAP_FAILED);
+  SELF_CHECK (f.size () == 0);
+}
+
+static void
+test_invalid_filename ()
+{
+  bool threw = false;
+
+  try {
+      ::scoped_mmapped_file f ("/this/file/should/not/exist");
+  } catch (gdb_exception &e) {
+      threw = true;
+  }
+
+  SELF_CHECK (threw);
+}
+
+static void
+run_tests ()
+{
+  test_destroy ();
+  test_default_constructor ();
+  test_invalid_filename ();
+}
+
+} /* namespace scoped_mmapped_file */
+} /* namespace selftests */
+
+#endif /* defined (HAVE_SYS_MMAN_H) */
+
+void
+_initialize_scoped_mmapped_file_selftests ()
+{
+#if defined (HAVE_SYS_MMAN_H)
+  selftests::register_test ("scoped_mmapped_file",
+			    selftests::scoped_mmapped_file::run_tests);
+#endif /* defined (HAVE_SYS_MMAN_H) */
+}
+
+
-- 
2.7.4


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