public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@ericsson.com>
To: <gdb-patches@sourceware.org>
Cc: Simon Marchi <simon.marchi@ericsson.com>
Subject: [PATCH RFC 4/5] Introduce scoped_mmapped_file
Date: Wed, 09 May 2018 21:27:00 -0000	[thread overview]
Message-ID: <1525901216-15031-5-git-send-email-simon.marchi@ericsson.com> (raw)
In-Reply-To: <1525901216-15031-1-git-send-email-simon.marchi@ericsson.com>

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

  parent reply	other threads:[~2018-05-09 21:27 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-09 21:27 [PATCH RFC 0/5] Add a DWARF index cache Simon Marchi
2018-05-09 21:27 ` [PATCH RFC 1/5] Rename some functions, index -> gdb_index Simon Marchi
2018-05-10 20:40   ` Tom Tromey
2018-06-12  2:03     ` Simon Marchi
2018-05-09 21:27 ` [PATCH RFC 2/5] Remove mapped_index::total_size Simon Marchi
2018-05-09 23:08   ` Simon Marchi
2018-05-10 20:54   ` Tom Tromey
2018-05-18 20:26     ` Simon Marchi
2018-05-09 21:27 ` [PATCH RFC 3/5] Make index reading functions more modular Simon Marchi
2018-05-10 21:02   ` Tom Tromey
2018-05-10 21:18     ` Simon Marchi
2018-05-09 21:27 ` [PATCH RFC 5/5] Add DWARF index cache Simon Marchi
2018-05-10 22:24   ` Tom Tromey
2018-07-09 20:56     ` Simon Marchi
2018-05-09 21:27 ` Simon Marchi [this message]
2018-05-10 21:04   ` [PATCH RFC 4/5] Introduce scoped_mmapped_file Tom Tromey
2018-05-10 21:27     ` Simon Marchi
2018-05-12 15:49       ` Tom Tromey
2018-06-13  1:36       ` Simon Marchi
2018-05-09 21:57 ` [PATCH RFC 0/5] Add a DWARF index cache Simon Marchi

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=1525901216-15031-5-git-send-email-simon.marchi@ericsson.com \
    --to=simon.marchi@ericsson.com \
    --cc=gdb-patches@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).