[2/3] Introduce scoped_mmapped_file
Commit Message
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 | 96 +++++++++++++++++++++++++++
4 files changed, 182 insertions(+), 2 deletions(-)
create mode 100644 gdb/common/scoped_mmapped_file.h
create mode 100644 gdb/unittests/scoped_mmapped_file-selftests.c
Comments
>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
Simon> We already have scoped_mmap, which can is a thin RAII layer over mmap.
Simon> If one simply wants to mmap an entire file for reading, it takes a bit
Simon> of boilerplate. This patch introduces the scoped_mmapped_file class to
Simon> make this easier.
Simon> + /* Map FILENAME in memory. Throw an error if anything goes wrong. */
Simon> + scoped_mmapped_file (const char *filename)
Simon> + {
Simon> + m_fd.reset (open (filename, O_RDONLY));
Simon> + if (m_fd.get () < 0)
Simon> + perror_with_name ("open");
Simon> +
Simon> + off_t size = lseek (m_fd.get (), 0, SEEK_END);
Simon> + if (size < 0)
Simon> + perror_with_name ("lseek");
Simon> +
Simon> + /* We can't map an empty file. */
Simon> + if (size == 0)
Simon> + error (_("file to mmap is empty"));
Simon> +
Simon> + m_mmap.reset (nullptr, size, PROT_READ, MAP_PRIVATE, m_fd.get (), 0);
Simon> + if (m_mmap.get () == MAP_FAILED)
Simon> + perror_with_name ("mmap");
It seems to me that there's no need to keep the fd open after the mmap,
so this could be done by just having the scoped_fd as a local variable,
and then I suppose the scoped_fd changes wouldn't be needed either.
Tom
On 2018-07-23 10:17 PM, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
>
> Simon> We already have scoped_mmap, which can is a thin RAII layer over mmap.
> Simon> If one simply wants to mmap an entire file for reading, it takes a bit
> Simon> of boilerplate. This patch introduces the scoped_mmapped_file class to
> Simon> make this easier.
>
> Simon> + /* Map FILENAME in memory. Throw an error if anything goes wrong. */
> Simon> + scoped_mmapped_file (const char *filename)
> Simon> + {
> Simon> + m_fd.reset (open (filename, O_RDONLY));
> Simon> + if (m_fd.get () < 0)
> Simon> + perror_with_name ("open");
> Simon> +
> Simon> + off_t size = lseek (m_fd.get (), 0, SEEK_END);
> Simon> + if (size < 0)
> Simon> + perror_with_name ("lseek");
> Simon> +
> Simon> + /* We can't map an empty file. */
> Simon> + if (size == 0)
> Simon> + error (_("file to mmap is empty"));
> Simon> +
> Simon> + m_mmap.reset (nullptr, size, PROT_READ, MAP_PRIVATE, m_fd.get (), 0);
> Simon> + if (m_mmap.get () == MAP_FAILED)
> Simon> + perror_with_name ("mmap");
>
> It seems to me that there's no need to keep the fd open after the mmap,
> so this could be done by just having the scoped_fd as a local variable,
> and then I suppose the scoped_fd changes wouldn't be needed either.
>
> Tom
>
Ah, I didn't know the fd didn't need to be kept open, but you're right:
https://stackoverflow.com/questions/17490033/do-i-need-to-keep-a-file-open-after-calling-mmap-on-it
I'll try that.
Simon
@@ -434,6 +434,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 \
@@ -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;
};
new file mode 100644
@@ -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, 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 */
new file mode 100644
@@ -0,0 +1,96 @@
+/* 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"
+#include "common/gdb_unlinker.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);
+
+ gdb::unlinker unlink_test_file (filename);
+
+ {
+ ::scoped_mmapped_file f (filename);
+
+ SELF_CHECK (f.get () != MAP_FAILED);
+ SELF_CHECK (f.size () == 7);
+ SELF_CHECK (0 == strcmp ((char *) f.get (), "Hello!"));
+ }
+}
+
+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) */
+}
+
+