gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / gdbsupport / scoped_mmap.h
CommitLineData
84696f37
MM
1/* scoped_mmap, automatically unmap files
2
b811d2c2 3 Copyright (C) 2018-2020 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
1a5c2598
TT
20#ifndef COMMON_SCOPED_MMAP_H
21#define COMMON_SCOPED_MMAP_H
84696f37 22
84696f37
MM
23#ifdef HAVE_SYS_MMAN_H
24
25#include <sys/mman.h>
26
27/* A smart-pointer-like class to mmap() and automatically munmap() a memory
28 mapping. */
29
30class scoped_mmap
31{
32public:
33 scoped_mmap () noexcept : m_mem (MAP_FAILED), m_length (0) {}
34 scoped_mmap (void *addr, size_t length, int prot, int flags, int fd,
35 off_t offset) noexcept : m_length (length)
4b17aefe
SM
36 {
37 m_mem = mmap (addr, m_length, prot, flags, fd, offset);
38 }
39
84696f37 40 ~scoped_mmap ()
4b17aefe 41 {
5c831bb1
SM
42 destroy ();
43 }
44
0fa7617d
TT
45 scoped_mmap (scoped_mmap &&rhs) noexcept
46 : m_mem (rhs.m_mem),
47 m_length (rhs.m_length)
5c831bb1 48 {
5c831bb1
SM
49 rhs.m_mem = MAP_FAILED;
50 rhs.m_length = 0;
4b17aefe 51 }
84696f37
MM
52
53 DISABLE_COPY_AND_ASSIGN (scoped_mmap);
54
083eef1f 55 ATTRIBUTE_UNUSED_RESULT void *release () noexcept
4b17aefe
SM
56 {
57 void *mem = m_mem;
58 m_mem = MAP_FAILED;
59 m_length = 0;
60 return mem;
61 }
84696f37
MM
62
63 void reset (void *addr, size_t length, int prot, int flags, int fd,
64 off_t offset) noexcept
4b17aefe 65 {
5c831bb1 66 destroy ();
84696f37 67
4b17aefe
SM
68 m_length = length;
69 m_mem = mmap (addr, m_length, prot, flags, fd, offset);
70 }
84696f37
MM
71
72 size_t size () const noexcept { return m_length; }
73 void *get () const noexcept { return m_mem; }
74
75private:
5c831bb1
SM
76 void destroy ()
77 {
78 if (m_mem != MAP_FAILED)
79 munmap (m_mem, m_length);
80 }
81
84696f37
MM
82 void *m_mem;
83 size_t m_length;
84};
85
5c831bb1
SM
86/* Map FILENAME in memory. Throw an error if anything goes wrong. */
87scoped_mmap mmap_file (const char *filename);
88
84696f37 89#endif /* HAVE_SYS_MMAN_H */
1a5c2598
TT
90
91#endif /* COMMON_SCOPED_MMAP_H */
This page took 0.199014 seconds and 4 git commands to generate.