Compare commits
2 Commits
26b61db021
...
a16dcc2aa9
Author | SHA1 | Date |
---|---|---|
|
a16dcc2aa9 | |
|
98f029ae23 |
|
@ -0,0 +1,173 @@
|
|||
#pragma once
|
||||
|
||||
#include "glacier/container/optional.h"
|
||||
#include "glacier/container/pair.h"
|
||||
#include "glacier/memory/move.h"
|
||||
#include "glacier/memory/ref_counted.h"
|
||||
#include "glacier/memory/ref_ptr.h"
|
||||
#include "glacier/memory/reference.h"
|
||||
#include "glacier/memory/unique_ptr.h"
|
||||
|
||||
namespace glcr {
|
||||
|
||||
template <typename K, typename V>
|
||||
class BinaryTree {
|
||||
public:
|
||||
BinaryTree() = default;
|
||||
BinaryTree(const BinaryTree&) = delete;
|
||||
// FIXME: Implement move.
|
||||
BinaryTree(BinaryTree&&) = delete;
|
||||
|
||||
void Insert(K key, V&& value);
|
||||
void Delete(K key);
|
||||
|
||||
Optional<Ref<V>> Predecessor(K key);
|
||||
Optional<Ref<V>> Successor(K key);
|
||||
|
||||
Optional<Ref<V>> Find(K key);
|
||||
|
||||
private:
|
||||
// TODO: Consider adding a sharedptr type to
|
||||
// avoid making this "RefCounted".
|
||||
struct BinaryNode : public RefCounted<BinaryNode> {
|
||||
K key;
|
||||
V value;
|
||||
|
||||
RefPtr<BinaryNode> left;
|
||||
RefPtr<BinaryNode> right;
|
||||
RefPtr<BinaryNode> parent;
|
||||
|
||||
BinaryNode(K k, V v) : key(k), value(Move(v)) {}
|
||||
};
|
||||
|
||||
RefPtr<BinaryNode> root_;
|
||||
|
||||
// If this node exists, return it. Otherwise, this
|
||||
// will be the parent of where this node would be inserted.
|
||||
RefPtr<BinaryNode> FindOrInsertionParent(K key);
|
||||
};
|
||||
|
||||
template <typename K, typename V>
|
||||
void BinaryTree<K, V>::Insert(K key, V&& value) {
|
||||
auto parent = FindOrInsertionParent(key);
|
||||
if (parent.empty()) {
|
||||
root_ = AdoptPtr(new BinaryNode(key, Move(value)));
|
||||
return;
|
||||
}
|
||||
|
||||
if (parent->key > key) {
|
||||
parent->left = AdoptPtr(new BinaryNode(key, Move(value)));
|
||||
parent->left->parent = parent;
|
||||
} else if (parent->key < key) {
|
||||
parent->right = AdoptPtr(new BinaryNode(key, Move(value)));
|
||||
parent->right->parent = parent;
|
||||
} else {
|
||||
parent->value = Move(value);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void BinaryTree<K, V>::Delete(K key) {
|
||||
// TODO: Implement Delete.
|
||||
return;
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
Optional<Ref<V>> BinaryTree<K, V>::Predecessor(K key) {
|
||||
auto current = FindOrInsertionParent(key);
|
||||
|
||||
// The case where the current is the insertion parent and
|
||||
// the predecessor is unique. If the key was going to be
|
||||
// inserted as the left child, it shares its predecessor with the parent.
|
||||
if (current->key < key) {
|
||||
return Optional<Ref<V>>(current->value);
|
||||
}
|
||||
|
||||
// Case where the predecessor is below us in the tree.
|
||||
if (current->left) {
|
||||
current = current->left;
|
||||
while (current->right) {
|
||||
current = current->right;
|
||||
}
|
||||
return {current->value};
|
||||
}
|
||||
|
||||
// Case where the predecessor is above us in the tree.
|
||||
auto parent = current->parent;
|
||||
while (parent && (parent->left == current)) {
|
||||
current = parent;
|
||||
parent = current->parent;
|
||||
}
|
||||
if (parent) {
|
||||
return {parent->value};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
Optional<Ref<V>> BinaryTree<K, V>::Successor(K key) {
|
||||
auto current = FindOrInsertionParent(key);
|
||||
|
||||
// The case where the current is the insertion parent and
|
||||
// the predecessor is unique. If the key was going to be
|
||||
// inserted as the left child, it shares its predecessor with the parent.
|
||||
if (current->key > key) {
|
||||
return Optional<Ref<V>>(current->value);
|
||||
}
|
||||
|
||||
// Case where the predecessor is below us in the tree.
|
||||
if (current->right) {
|
||||
current = current->right;
|
||||
while (current->left) {
|
||||
current = current->left;
|
||||
}
|
||||
return {current->value};
|
||||
}
|
||||
|
||||
// Case where the predecessor is above us in the tree.
|
||||
auto parent = current->parent;
|
||||
while (parent && (parent->right == current)) {
|
||||
current = parent;
|
||||
parent = current->parent;
|
||||
}
|
||||
if (parent) {
|
||||
return {parent->value};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
Optional<Ref<V>> BinaryTree<K, V>::Find(K key) {
|
||||
auto current = FindOrInsertionParent(key);
|
||||
if (current->key == key) {
|
||||
return Optional<Ref<V>>(current->value);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
RefPtr<typename BinaryTree<K, V>::BinaryNode>
|
||||
BinaryTree<K, V>::FindOrInsertionParent(K key) {
|
||||
if (root_.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto current = root_;
|
||||
while (true) {
|
||||
if (key == current->key) {
|
||||
return current;
|
||||
} else if (key < current->key) {
|
||||
if (!current->left) {
|
||||
return current;
|
||||
}
|
||||
current = current->left;
|
||||
} else {
|
||||
if (!current->right) {
|
||||
return current;
|
||||
}
|
||||
current = current->right;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace glcr
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include "glacier/memory/move.h"
|
||||
|
||||
namespace glcr {
|
||||
|
||||
template <typename T>
|
||||
class Optional {
|
||||
public:
|
||||
Optional() : empty_(nullptr), has_value_(false) {}
|
||||
Optional(const T& value) : value_(value), has_value_(true) {}
|
||||
Optional(T&& value) : value_(Move(value)), has_value_(true) {}
|
||||
Optional(const Optional&) = default;
|
||||
Optional(Optional&&) = default;
|
||||
~Optional() {
|
||||
if (has_value_) {
|
||||
value_.~T();
|
||||
}
|
||||
}
|
||||
|
||||
bool empty() const { return !has_value_; }
|
||||
explicit operator bool() { return has_value_; }
|
||||
|
||||
const T& value() const { return value_; }
|
||||
T&& release_value() {
|
||||
has_value_ = false;
|
||||
return Move(value_);
|
||||
}
|
||||
|
||||
T* operator->() { return &value_; }
|
||||
T& operator*() { return value_; }
|
||||
|
||||
private:
|
||||
union {
|
||||
T value_;
|
||||
void* empty_;
|
||||
};
|
||||
bool has_value_;
|
||||
};
|
||||
|
||||
} // namespace glcr
|
|
@ -51,6 +51,8 @@ class RefPtr {
|
|||
T* get() const { return ptr_; };
|
||||
T& operator*() const { return *ptr_; }
|
||||
T* operator->() const { return ptr_; }
|
||||
|
||||
bool empty() const { return ptr_ == nullptr; }
|
||||
operator bool() const { return ptr_ != nullptr; }
|
||||
|
||||
bool operator==(decltype(nullptr)) const { return (ptr_ == nullptr); }
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
namespace glcr {
|
||||
|
||||
template <typename T>
|
||||
class Ref {
|
||||
public:
|
||||
Ref(T& ref) : ref_(ref) {}
|
||||
Ref(const Ref& other) = default;
|
||||
Ref(Ref&& other) = default;
|
||||
|
||||
operator T&() const { return ref_; }
|
||||
|
||||
private:
|
||||
T& ref_;
|
||||
};
|
||||
|
||||
} // namespace glcr
|
|
@ -39,13 +39,13 @@ uint64_t AddressSpace::GetNextMemMapAddr(uint64_t size) {
|
|||
|
||||
void AddressSpace::MapInMemoryObject(
|
||||
uint64_t vaddr, const glcr::RefPtr<MemoryObject>& mem_obj) {
|
||||
memory_mappings_.PushBack({.vaddr = vaddr, .mem_obj = mem_obj});
|
||||
memory_mappings_.Insert(vaddr, {.vaddr = vaddr, .mem_obj = mem_obj});
|
||||
}
|
||||
|
||||
uint64_t AddressSpace::MapInMemoryObject(
|
||||
const glcr::RefPtr<MemoryObject>& mem_obj) {
|
||||
uint64_t vaddr = GetNextMemMapAddr(mem_obj->size());
|
||||
memory_mappings_.PushBack({.vaddr = vaddr, .mem_obj = mem_obj});
|
||||
memory_mappings_.Insert(vaddr, {.vaddr = vaddr, .mem_obj = mem_obj});
|
||||
return vaddr;
|
||||
}
|
||||
|
||||
|
@ -62,12 +62,13 @@ bool AddressSpace::HandlePageFault(uint64_t vaddr) {
|
|||
return true;
|
||||
}
|
||||
|
||||
MemoryMapping* mapping = GetMemoryMappingForAddr(vaddr);
|
||||
if (mapping == nullptr) {
|
||||
auto mapping_or = GetMemoryMappingForAddr(vaddr);
|
||||
if (!mapping_or) {
|
||||
return false;
|
||||
}
|
||||
uint64_t offset = vaddr - mapping->vaddr;
|
||||
uint64_t physical_addr = mapping->mem_obj->PhysicalPageAtOffset(offset);
|
||||
MemoryMapping& mapping = mapping_or.value();
|
||||
uint64_t offset = vaddr - mapping.vaddr;
|
||||
uint64_t physical_addr = mapping.mem_obj->PhysicalPageAtOffset(offset);
|
||||
if (physical_addr == 0) {
|
||||
dbgln("WARN: Memory object returned invalid physical addr.");
|
||||
return false;
|
||||
|
@ -79,16 +80,15 @@ bool AddressSpace::HandlePageFault(uint64_t vaddr) {
|
|||
return true;
|
||||
}
|
||||
|
||||
AddressSpace::MemoryMapping* AddressSpace::GetMemoryMappingForAddr(
|
||||
uint64_t vaddr) {
|
||||
auto iter = memory_mappings_.begin();
|
||||
while (iter != memory_mappings_.end()) {
|
||||
if ((vaddr >= (*iter).vaddr) &&
|
||||
(vaddr < ((*iter).vaddr + (*iter).mem_obj->size()))) {
|
||||
return &(*iter);
|
||||
glcr::Optional<glcr::Ref<AddressSpace::MemoryMapping>>
|
||||
AddressSpace::GetMemoryMappingForAddr(uint64_t vaddr) {
|
||||
auto mapping_or = memory_mappings_.Predecessor(vaddr + 1);
|
||||
if (!mapping_or) {
|
||||
return mapping_or;
|
||||
}
|
||||
++iter;
|
||||
MemoryMapping& mapping = mapping_or.value();
|
||||
if (mapping.vaddr + mapping.mem_obj->size() <= vaddr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return 0;
|
||||
return mapping_or;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/container/linked_list.h>
|
||||
#include <glacier/container/binary_tree.h>
|
||||
#include <glacier/memory/ref_ptr.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
@ -92,7 +92,8 @@ class AddressSpace : public KernelObject {
|
|||
uint64_t vaddr;
|
||||
glcr::RefPtr<MemoryObject> mem_obj;
|
||||
};
|
||||
glcr::LinkedList<MemoryMapping> memory_mappings_;
|
||||
glcr::BinaryTree<uint64_t, MemoryMapping> memory_mappings_;
|
||||
|
||||
MemoryMapping* GetMemoryMappingForAddr(uint64_t vaddr);
|
||||
glcr::Optional<glcr::Ref<MemoryMapping>> GetMemoryMappingForAddr(
|
||||
uint64_t vaddr);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue