KWin
Loading...
Searching...
No Matches
memorymap.h
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <sys/mman.h>
10#include <utility>
11
12namespace KWin
13{
14
16{
17public:
19 : m_data(MAP_FAILED)
20 , m_size(0)
21 {
22 }
23
24 MemoryMap(int size, int prot, int flags, int fd, off_t offset)
25 : m_data(mmap(nullptr, size, prot, flags, fd, offset))
26 , m_size(size)
27 {
28 }
29
31 : m_data(std::exchange(other.m_data, MAP_FAILED))
32 , m_size(std::exchange(other.m_size, 0))
33 {
34 }
35
37 {
38 if (m_data != MAP_FAILED) {
39 munmap(m_data, m_size);
40 }
41 }
42
44 {
45 if (m_data != MAP_FAILED) {
46 munmap(m_data, m_size);
47 }
48 m_data = std::exchange(other.m_data, MAP_FAILED);
49 m_size = std::exchange(other.m_size, 0);
50 return *this;
51 }
52
53 inline bool isValid() const
54 {
55 return m_data != MAP_FAILED;
56 }
57
58 inline void *data() const
59 {
60 return m_data;
61 }
62
63 inline int size() const
64 {
65 return m_size;
66 }
67
68private:
69 void *m_data;
70 int m_size;
71};
72
73} // namespace KWin
void * data() const
Definition memorymap.h:58
MemoryMap & operator=(MemoryMap &&other)
Definition memorymap.h:43
bool isValid() const
Definition memorymap.h:53
MemoryMap(MemoryMap &&other)
Definition memorymap.h:30
MemoryMap(int size, int prot, int flags, int fd, off_t offset)
Definition memorymap.h:24
int size() const
Definition memorymap.h:63