-
Notifications
You must be signed in to change notification settings - Fork 395
Expand file tree
/
Copy pathmachine.hpp
More file actions
283 lines (230 loc) · 7 KB
/
Copy pathmachine.hpp
File metadata and controls
283 lines (230 loc) · 7 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2018 IncludeOS AS, Oslo, Norway
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OS_DETAIL_MACHINE_HPP
#define OS_DETAIL_MACHINE_HPP
#include <typeindex>
#include <unordered_map>
#include <hal/machine.hpp>
#include <hal/machine_memory.hpp>
#include <util/typename.hpp>
#include <set>
#include <info>
#include <hw/device.hpp>
namespace os::detail {
using namespace util;
class Machine_access_error : public std::runtime_error {
using runtime_error::runtime_error;
};
class Machine {
public:
using Memory = os::Machine::Memory;
template <typename T>
using Allocator = os::Machine::Allocator<T>;
template <typename T>
using Vec = std::vector<T,Allocator<T>>;
template <typename K, typename V>
using Map = std::unordered_map<K, V,
std::hash<K>,
std::equal_to<K>,
Allocator<std::pair<const K,V>>>;
struct Part {
enum class Storage : uint8_t {
machine, heap
};
void* ptr;
Storage storage;
std::type_index t_idx;
};
using Parts_vec = Vec<Part>;
using Parts_ent = std::pair<std::type_index, Parts_vec>;
using Parts_alloc = const Allocator<Parts_ent>;
using Parts_map = Map<std::type_index, Parts_vec>;
using Ptr_alloc = const Allocator<void*>;
using Device_types = std::set<std::type_index, std::less<std::type_index>,
Allocator<std::type_index>>;
template <typename T>
struct Deleter {
void operator()(T* obj) noexcept {
os::machine().memory().deallocate(obj, sizeof(T));
}
};
template <typename T>
using Unique_ptr = std::unique_ptr<T,Deleter<T>>;
using Arch = os::Arch;
Machine(void* mem, size_t size);
const char* arch();
virtual const char* name() { return "PC"; }
template <typename T>
Parts_vec& get_vector() {
const std::type_index t_idx = std::type_index(typeid(T));
auto vec_it = parts_.find(t_idx);
if (vec_it == parts_.end()) {
throw Machine_access_error("Requested machine parts not found");
}
return vec_it->second;
}
template <typename T>
T& get(int i) {
const std::type_index t_idx = std::type_index(typeid(T));
auto& vec = get_vector<T>();
auto& part = vec.at(i);
Expects(part.t_idx == t_idx);
T* tptr = (T*)part.ptr;
return *tptr;
}
template <typename T>
os::Machine::Vector<T> get() {
auto& vec = get_vector<T>();
// Populate new vector of ref-wrappers
os::Machine::Vector<T> new_parts(ptr_alloc_);
for (auto& part : vec) {
auto ref = std::ref(*(reinterpret_cast<T*>(part.ptr)));
new_parts.emplace_back(ref);
}
return new_parts;
}
template <typename T>
ssize_t add(T* part, Part::Storage storage) {
const std::type_index t_idx = std::type_index(typeid(T));
auto vec_it = parts_.find(t_idx);
if (vec_it == parts_.end()) {
// Create vector for T if it doesn't exist
auto res = parts_.emplace(t_idx, Parts_vec(ptr_alloc_));
if (! res.second) {
return -1;
}
vec_it = res.first;
}
// Add part to T vector
auto& vec = vec_it->second;
Part p {part, storage, t_idx};
vec.push_back(p);
// Mark type as "Device" if needed
if constexpr(std::is_base_of<hw::Device,T>::value)
add_device_type(t_idx);
return vec.size() - 1;
}
template <typename T, typename... Args>
ssize_t add_new(Args&&... args) {
auto* mem = memory().allocate(sizeof(T));
auto* ptr (new (mem) T{std::forward<Args>(args)...});
return add<T>(ptr, Part::Storage::machine);
}
template <typename T>
ssize_t count() {
if (parts_.count(std::type_index(typeid(T))) == 0)
return 0;
auto& vec = get_vector<T>();
return vec.size();
}
template <typename T>
void remove(size_t i) {
auto& vec = get_vector<T>();
if(UNLIKELY(vec.size() < i))
throw Machine_access_error{"Requested machine part not found: " + std::to_string(i)};
vec.erase(vec.begin() + i);
}
inline Memory& memory() {
return mem_;
}
void init();
inline void deactivate_devices();
inline void print_devices() const;
private:
Memory mem_;
Ptr_alloc ptr_alloc_;
Parts_map parts_;
Device_types device_types_;
void add_device_type(const std::type_index t_idx)
{
if(device_types_.find(t_idx) == device_types_.end())
device_types_.insert(t_idx);
}
};
} // namespace detail
// Machine wrappers
namespace os {
template <typename T>
ssize_t Machine::add(std::unique_ptr<T> part) noexcept {
try {
return impl->add<T>(part.release(), detail::Machine::Part::Storage::heap);
}
catch(const std::bad_alloc&) {
//printf("Bad alloc on insert free=%zu alloced=%zu\n",
// memory().bytes_free(), memory().bytes_allocated());
return -1; // do we rather wanna throw here?
}
}
template <typename T, typename... Args>
ssize_t Machine::add_new(Args&&... args) {
return impl->add_new<T>(args...);
}
template <typename T>
T& Machine::get(int id) {
return impl->get<T>(id);
}
template <typename T>
Machine::Vector<T> Machine::get() {
return impl->get<T>();
}
template <typename T>
void Machine::remove(int i) {
impl->remove<T>(i);
};
template <typename T>
ssize_t Machine::count() {
return impl->count<T>();
}
inline void Machine::deactivate_devices() {
impl->deactivate_devices();
}
inline void Machine::print_devices() const {
impl->print_devices();
}
}
namespace os::detail
{
inline void Machine::deactivate_devices()
{
INFO("Machine", "Deactivating devices");
for(const auto idx : device_types_)
{
for(auto& part : parts_.at(idx))
{
auto& dev = *reinterpret_cast<hw::Device*>(part.ptr);
dev.deactivate();
}
}
}
inline void Machine::print_devices() const
{
INFO("Machine", "Listing registered devices");
#ifndef NO_INFO
for(const auto idx : device_types_)
{
INFO2("|");
for(const auto& part : parts_.at(idx))
{
const auto& dev = *reinterpret_cast<const hw::Device*>(part.ptr);
INFO2("+--+ %s", dev.to_string().c_str());
}
}
#endif
INFO2("|");
INFO2("o");
}
}
#endif // OS_MACHINE_HPP