[RFC,4/5] 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 | 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
Comments
>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
Simon> + void reset (int new_fd)
Simon> + {
Simon> + destroy ();
Simon> +
Simon> + m_fd = new_fd;
Simon> + }
I was wondering if this should only destroy() when new_fd!=m_fd.
That way self-resetting would not cause a bug.
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, 0, O_RDONLY));
... however, then I thought perhaps this question could be avoided by
just initializing m_fd directly, like:
scoped_mmapped_file (const char *filename)
: m_fd (open (filename, 0, O_RDONLY))
Then "reset" wouldn't be needed at all.
Also I think that should be "open (filename, O_RDONLY)" -- no "0, ".
Tom
On 2018-05-10 16:59, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
>
> Simon> + void reset (int new_fd)
> Simon> + {
> Simon> + destroy ();
> Simon> +
> Simon> + m_fd = new_fd;
> Simon> + }
>
> I was wondering if this should only destroy() when new_fd!=m_fd.
> That way self-resetting would not cause a bug.
It probably should, indeed.
> 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, 0, O_RDONLY));
>
> ... however, then I thought perhaps this question could be avoided by
> just initializing m_fd directly, like:
>
> scoped_mmapped_file (const char *filename)
> : m_fd (open (filename, 0, O_RDONLY))
>
> Then "reset" wouldn't be needed at all.
I can do that, but I am using reset() in the following patch anyway.
Though if you find a way of avoiding that usage, then we can remove the
method.
> Also I think that should be "open (filename, O_RDONLY)" -- no "0, ".
Right, thanks.
Simon
>>>>> "Simon" == Simon Marchi <simon.marchi@polymtl.ca> writes:
>> ... however, then I thought perhaps this question could be avoided by
>> just initializing m_fd directly, like:
>>
>> scoped_mmapped_file (const char *filename)
>> : m_fd (open (filename, 0, O_RDONLY))
>>
>> Then "reset" wouldn't be needed at all.
Simon> I can do that, but I am using reset() in the following patch anyway.
Oops, I didn't notice that. FWIW I think it's fine to have a reset
method.
Tom
On 2018-05-10 05:10 PM, Simon Marchi wrote:
> On 2018-05-10 16:59, Tom Tromey wrote:
>>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
>>
>> Simon> + void reset (int new_fd)
>> Simon> + {
>> Simon> + destroy ();
>> Simon> +
>> Simon> + m_fd = new_fd;
>> Simon> + }
>>
>> I was wondering if this should only destroy() when new_fd!=m_fd.
>> That way self-resetting would not cause a bug.
>
> It probably should, indeed.
I looked into this a bit more, and I think I'll leave it like this. This is
how unique_ptr works (at least libstdc++'s and libc++'s implementations of it),
and that's how our other scoped_* implementations work (scoped_mmap amd ref_ptr).
Simon
@@ -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 \
@@ -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, 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 */
new file mode 100644
@@ -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) */
+}
+
+