Make index reading functions more modular
[deliverable/binutils-gdb.git] / gdb / common / scoped_mmap.h
CommitLineData
84696f37
MM
1/* scoped_mmap, automatically unmap files
2
3 Copyright (C) 2018 Free Software Foundation, Inc.
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
32class scoped_mmap
33{
34public:
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
SM
43 {
44 if (m_mem != MAP_FAILED)
45 munmap (m_mem, m_length);
46 }
84696f37
MM
47
48 DISABLE_COPY_AND_ASSIGN (scoped_mmap);
49
50 void *release () noexcept
4b17aefe
SM
51 {
52 void *mem = m_mem;
53 m_mem = MAP_FAILED;
54 m_length = 0;
55 return mem;
56 }
84696f37
MM
57
58 void reset (void *addr, size_t length, int prot, int flags, int fd,
59 off_t offset) noexcept
4b17aefe
SM
60 {
61 if (m_mem != MAP_FAILED)
62 munmap (m_mem, m_length);
84696f37 63
4b17aefe
SM
64 m_length = length;
65 m_mem = mmap (addr, m_length, prot, flags, fd, offset);
66 }
84696f37
MM
67
68 size_t size () const noexcept { return m_length; }
69 void *get () const noexcept { return m_mem; }
70
71private:
72 void *m_mem;
73 size_t m_length;
74};
75
76#endif /* HAVE_SYS_MMAN_H */
77#endif /* SCOPED_MMAP_H */
This page took 0.067415 seconds and 4 git commands to generate.