Commit | Line | Data |
---|---|---|
84696f37 MM |
1 | /* scoped_mmap, automatically unmap files |
2 | ||
42a4f53d | 3 | Copyright (C) 2018-2019 Free Software Foundation, Inc. |
84696f37 MM |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #ifndef SCOPED_MMAP_H | |
21 | #define SCOPED_MMAP_H | |
22 | ||
23 | #include "config.h" | |
24 | ||
25 | #ifdef HAVE_SYS_MMAN_H | |
26 | ||
27 | #include <sys/mman.h> | |
28 | ||
29 | /* A smart-pointer-like class to mmap() and automatically munmap() a memory | |
30 | mapping. */ | |
31 | ||
32 | class scoped_mmap | |
33 | { | |
34 | public: | |
35 | scoped_mmap () noexcept : m_mem (MAP_FAILED), m_length (0) {} | |
36 | scoped_mmap (void *addr, size_t length, int prot, int flags, int fd, | |
37 | off_t offset) noexcept : m_length (length) | |
4b17aefe SM |
38 | { |
39 | m_mem = mmap (addr, m_length, prot, flags, fd, offset); | |
40 | } | |
41 | ||
84696f37 | 42 | ~scoped_mmap () |
4b17aefe | 43 | { |
5c831bb1 SM |
44 | destroy (); |
45 | } | |
46 | ||
47 | scoped_mmap (scoped_mmap &&rhs) | |
48 | { | |
49 | destroy (); | |
50 | ||
51 | m_mem = rhs.m_mem; | |
52 | m_length = rhs.m_length; | |
53 | ||
54 | rhs.m_mem = MAP_FAILED; | |
55 | rhs.m_length = 0; | |
4b17aefe | 56 | } |
84696f37 MM |
57 | |
58 | DISABLE_COPY_AND_ASSIGN (scoped_mmap); | |
59 | ||
60 | void *release () noexcept | |
4b17aefe SM |
61 | { |
62 | void *mem = m_mem; | |
63 | m_mem = MAP_FAILED; | |
64 | m_length = 0; | |
65 | return mem; | |
66 | } | |
84696f37 MM |
67 | |
68 | void reset (void *addr, size_t length, int prot, int flags, int fd, | |
69 | off_t offset) noexcept | |
4b17aefe | 70 | { |
5c831bb1 | 71 | destroy (); |
84696f37 | 72 | |
4b17aefe SM |
73 | m_length = length; |
74 | m_mem = mmap (addr, m_length, prot, flags, fd, offset); | |
75 | } | |
84696f37 MM |
76 | |
77 | size_t size () const noexcept { return m_length; } | |
78 | void *get () const noexcept { return m_mem; } | |
79 | ||
80 | private: | |
5c831bb1 SM |
81 | void destroy () |
82 | { | |
83 | if (m_mem != MAP_FAILED) | |
84 | munmap (m_mem, m_length); | |
85 | } | |
86 | ||
84696f37 MM |
87 | void *m_mem; |
88 | size_t m_length; | |
89 | }; | |
90 | ||
5c831bb1 SM |
91 | /* Map FILENAME in memory. Throw an error if anything goes wrong. */ |
92 | scoped_mmap mmap_file (const char *filename); | |
93 | ||
84696f37 MM |
94 | #endif /* HAVE_SYS_MMAN_H */ |
95 | #endif /* SCOPED_MMAP_H */ |