-
Notifications
You must be signed in to change notification settings - Fork 10
/
buffered_rewired_memory.cpp
201 lines (167 loc) · 8.6 KB
/
buffered_rewired_memory.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* 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/>.
*/
#include "buffered_rewired_memory.hpp"
#include <algorithm>
#include <cassert>
#include <iostream>
#include "errorhandling.hpp"
using namespace std;
#define RAISE(msg) RAISE_EXCEPTION(RewiredMemoryException, msg)
/*****************************************************************************
* *
* DEBUG *
* *
*****************************************************************************/
//#define DEBUG
#define COUT_DEBUG_FORCE(msg) std::cout << "[BufferedRewiredMemory::" << __FUNCTION__ << "] " << msg << std::endl
#if defined(DEBUG)
#define COUT_DEBUG(msg) COUT_DEBUG_FORCE(msg)
#else
#define COUT_DEBUG(msg)
#endif
/*****************************************************************************
* *
* Initialisation *
* *
*****************************************************************************/
BufferedRewiredMemory::BufferedRewiredMemory(size_t pages_per_extent, size_t num_extents) :
m_instance(pages_per_extent, num_extents),
m_buffer_start_address(static_cast<char*>(m_instance.get_start_address()) + m_instance.get_allocated_memory_size()),
m_allocated_buffers(0)
{ }
/*****************************************************************************
* *
* Handle buffer space *
* *
*****************************************************************************/
void BufferedRewiredMemory::add_buffers(size_t num_extents){
m_instance.extend(num_extents);
// register the new buffers
char* buffer_space = static_cast<char*>(m_buffer_start_address) + get_extent_size() * get_total_buffers();
for(size_t i = 0; i < num_extents; i++){
m_buffers.push_front(buffer_space + i * get_extent_size());
}
// update the state of the data structure
m_allocated_buffers += num_extents;
COUT_DEBUG("acquired " << num_extents << " extents. Total buffer capacity: " << get_total_buffers() << " extents");
}
void* BufferedRewiredMemory::acquire_buffer(){
if(m_buffers.empty()){ add_buffers(max<size_t>(4, m_allocated_buffers * 0.5)); }
assert(!m_buffers.empty());
void* address = m_buffers.back();
m_buffers.pop_back();
COUT_DEBUG("address: " << address);
return address;
}
void BufferedRewiredMemory::swap_and_release(void* addr1, void* addr2){
// check whether addr1 or addr2 is the pointer to the buffer
char* ptr_bufferspace (nullptr);
char* ptr_userspace (nullptr);
char* vmem1 = (char*) addr1;
char* vmem2 = (char*) addr2;
char* start_address_buffers = (char*) m_buffer_start_address;
if(vmem1 >= start_address_buffers){
ptr_bufferspace = vmem1;
ptr_userspace = vmem2;
}
if(vmem2 >= start_address_buffers){
// already set
if(ptr_bufferspace != nullptr){
RAISE("both pointers refer to buffers: addr1: " << addr1 << ", addr2: " << addr2 << ", buffer start address: " << (void*) start_address_buffers);
}
ptr_bufferspace = vmem2;
ptr_userspace = vmem1;
}
// both addresses do not refer to the buffer space
if(ptr_bufferspace == nullptr){
RAISE("both pointers do not refer to a buffer: addr1: " << addr1 << ", addr2: " << addr2 << ", buffer start address: " << (void*) start_address_buffers);
}
COUT_DEBUG("userspace: " << (void*) ptr_userspace << ", bufferspace: " << (void*) ptr_bufferspace);
m_instance.swap(ptr_userspace, ptr_bufferspace);
m_buffers.push_back(ptr_bufferspace);
}
/*****************************************************************************
* *
* Resize *
* *
*****************************************************************************/
void BufferedRewiredMemory::extend(size_t num_extents){
if(num_extents == 0) RAISE("The amount of extents specified is zero");
assert(get_used_buffers() == 0 && "There are buffers in use!");
if(get_used_buffers() != 0) RAISE("There are buffers in use: " << get_used_buffers() << "/" << get_total_buffers());
// the buffers are at the end of
int64_t num_extents_buffer = get_total_buffers();
int64_t additional_phys_memory = static_cast<int64_t>(num_extents) - num_extents_buffer;
if(additional_phys_memory < 0){ // there is no need to extent the physical memory, just cover it with the buffer space
const size_t extent_size = get_extent_size();
m_buffer_start_address = ((char*) m_buffer_start_address) + num_extents * extent_size;
m_allocated_buffers = num_extents_buffer - num_extents;
m_buffers.clear(); // rebuild the deque
char* buffer_address = (char*) m_buffer_start_address;
for(size_t i = 0; i < m_allocated_buffers; i++){
m_buffers.push_front(buffer_address);
buffer_address += extent_size;
}
} else { // we need to acquire more physical memory
m_instance.extend(additional_phys_memory);
// all the space previously occupied by the buffer space is now in use for the user data
m_allocated_buffers = 0;
m_buffers.clear();
m_buffer_start_address = static_cast<char*>(m_instance.get_start_address()) + m_instance.get_allocated_memory_size();
}
}
void BufferedRewiredMemory::shrink(size_t num_extents){
if(num_extents == 0) RAISE("The amount of extents specified is zero");
assert(get_used_buffers() == 0 && "There are buffers in use!");
if(get_used_buffers() != 0) RAISE("There are buffers in use: " << get_used_buffers() << "/" << get_total_buffers());
if(num_extents > get_allocated_extents() - get_total_buffers()) RAISE("Releasing more memory than acquired");
char* buffer_address = (char*) m_buffer_start_address;
const size_t extent_size = get_extent_size();
for(size_t i = 0; i < num_extents; i++){
buffer_address -= extent_size; // in bytes
m_buffers.push_front(buffer_address);
}
m_allocated_buffers += num_extents;
m_buffer_start_address = buffer_address;
}
/*****************************************************************************
* *
* Observers *
* *
*****************************************************************************/
void* BufferedRewiredMemory::get_start_address() const noexcept {
return m_instance.get_start_address();
}
size_t BufferedRewiredMemory::get_extent_size() const noexcept{
return m_instance.get_extent_size();
}
size_t BufferedRewiredMemory::get_allocated_extents() const noexcept {
return m_instance.get_allocated_extents();
}
size_t BufferedRewiredMemory::get_allocated_memory_size() const noexcept{
return m_instance.get_allocated_memory_size();
}
size_t BufferedRewiredMemory::get_total_buffers() const noexcept {
return m_allocated_buffers;
}
size_t BufferedRewiredMemory::get_used_buffers() const noexcept{
assert(m_buffers.size() <= m_allocated_buffers && "The total number of free buffers must be less or equal those allocated");
return m_allocated_buffers - m_buffers.size();
}
size_t BufferedRewiredMemory::get_max_memory() const noexcept{
return m_instance.get_max_memory();
}