-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrewired_memory.hpp
98 lines (83 loc) · 3.36 KB
/
rewired_memory.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Copyright (C) 2018 Dean De Leo, email: dleo[at]cwi.nl
*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef REWIRED_MEMORY_HPP_
#define REWIRED_MEMORY_HPP_
#include <cinttypes>
#include <cstddef>
#include <vector>
#include "errorhandling.hpp"
DEFINE_EXCEPTION(RewiredMemoryException);
/**
* It represents a single large section of memory mapped memory. The memory is split in extents, multiple
* of a virtual page. Extents within the mapped memory can be rewired, exchanging the mapping
* between their virtual addresses and the underlying physical memory.
*/
class RewiredMemory{
const size_t m_page_size; // virtual memory page size, for the underlying architecture
const size_t m_num_pages_per_extent; // number of pages that compose an extent
void* m_start_address; // the start address in virtual memory of the reserved region
int m_handle_physical_memory; // the handle to the allocated physical memory, as file descriptor
std::vector<uint32_t> m_translation_map; // an array, given an offset in virtual memory, returns the offset
const size_t m_max_memory; // the maximum amount of virtual memory reserved for the memory mapping, in bytes
/**
* Raise an exception if the given address is not valid:
* - it's not aligned to an extent
* - it is not part of the memory space handled by this instance
*/
void validate_address(void* address);
public:
/**
* Allocate a single segment of mapped memory
* @param pages_per_extent it defines the size of a single extents, in terms of virtual pages
* @param the amount of extents to allocate
* @param max_memory the maximum amount of virtual memory that can be reserved by this instance, in bytes
*/
RewiredMemory(size_t pages_per_extent, size_t num_extents, size_t max_memory = (1ull << 35) /* 2^35 = 32 GB */);
/**
* Destructor. Release the managed resources
*/
~RewiredMemory();
/**
* Retrieve the pointer to the virtual memory space
*/
void* get_start_address() const noexcept;
/**
* Extent the amount of allocated memory
*/
void extend(size_t num_extents);
/**
* Rewires the memory of addr1 and addr2, swapping their physical addresses
*/
void swap(void* addr1, void* addr2);
/**
* The size of a single extent, in bytes
*/
size_t get_extent_size() const noexcept;
/**
* Retrieve the amount of allocated memory, in bytes
*/
size_t get_allocated_memory_size() const noexcept;
/**
* Retrieve the amount of allocated extents
*/
size_t get_allocated_extents() const noexcept;
/**
* Retrieve the maximum amount of memory that can be allocated, in bytes
*/
size_t get_max_memory() const noexcept;
};
#endif /* REWIRED_MEMORY_HPP_ */