[Mammoth] Move all callers of FromCapability to OwnedMemoryObject.
This commit is contained in:
parent
337126cabb
commit
8e827a5dfb
24 changed files with 175 additions and 87 deletions
|
|
@ -7,6 +7,7 @@
|
|||
#include "glacier/memory/ref_ptr.h"
|
||||
#include "glacier/memory/reference.h"
|
||||
#include "glacier/memory/unique_ptr.h"
|
||||
#include "glacier/string/str_format.h"
|
||||
|
||||
namespace glcr {
|
||||
|
||||
|
|
@ -26,6 +27,8 @@ class BinaryTree {
|
|||
|
||||
Optional<Ref<V>> Find(K key);
|
||||
|
||||
void DebugTreeIntoStr(StringBuilder& builder) const;
|
||||
|
||||
private:
|
||||
// TODO: Consider adding a sharedptr type to
|
||||
// avoid making this "RefCounted".
|
||||
|
|
@ -45,6 +48,9 @@ class BinaryTree {
|
|||
// If this node exists, return it. Otherwise, this
|
||||
// will be the parent of where this node would be inserted.
|
||||
RefPtr<BinaryNode> FindOrInsertionParent(K key);
|
||||
|
||||
static void DebugNodeIntoString(StringBuilder& builder, uint64_t indent_level,
|
||||
const RefPtr<BinaryNode>& node);
|
||||
};
|
||||
|
||||
template <typename K, typename V>
|
||||
|
|
@ -102,6 +108,9 @@ void BinaryTree<K, V>::Delete(K key) {
|
|||
node->parent->left = new_child;
|
||||
}
|
||||
}
|
||||
if (new_child) {
|
||||
new_child->parent = node->parent;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
|
|
@ -211,4 +220,36 @@ BinaryTree<K, V>::FindOrInsertionParent(K key) {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void StrFormatValue(StringBuilder& builder, const BinaryTree<K, V>& value,
|
||||
StringView opts) {
|
||||
value.DebugTreeIntoStr(builder);
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void BinaryTree<K, V>::DebugTreeIntoStr(StringBuilder& builder) const {
|
||||
DebugNodeIntoString(builder, 0, root_);
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void BinaryTree<K, V>::DebugNodeIntoString(StringBuilder& builder,
|
||||
uint64_t indent_level,
|
||||
const RefPtr<BinaryNode>& node) {
|
||||
if (node.empty()) {
|
||||
return;
|
||||
}
|
||||
for (uint64_t i = 0; i < indent_level; i++) {
|
||||
builder.PushBack('\t');
|
||||
}
|
||||
StrFormatValue(builder, node->value, "");
|
||||
builder.PushBack('\n');
|
||||
if (node->left) {
|
||||
builder.PushBack('L');
|
||||
DebugNodeIntoString(builder, indent_level + 1, node->left);
|
||||
}
|
||||
if (node->right) {
|
||||
builder.PushBack('R');
|
||||
DebugNodeIntoString(builder, indent_level + 1, node->right);
|
||||
}
|
||||
}
|
||||
} // namespace glcr
|
||||
|
|
|
|||
|
|
@ -11,10 +11,12 @@ class RefCounted {
|
|||
virtual ~RefCounted() {}
|
||||
// FIXME: Rethink error handling in these cases now that we can't panic the
|
||||
// kernel.
|
||||
void Adopt() { ref_count_ = 1; }
|
||||
void AdoptPtr() { ref_count_ = 1; }
|
||||
|
||||
void Acquire() { ref_count_++; }
|
||||
bool Release() { return (--ref_count_) == 0; }
|
||||
void AcquirePtr() { ref_count_++; }
|
||||
bool ReleasePtr() { return (--ref_count_) == 0; }
|
||||
|
||||
uint64_t ref_count() { return ref_count_; }
|
||||
|
||||
private:
|
||||
// FIXME: This should be an atomic type.
|
||||
|
|
|
|||
|
|
@ -18,16 +18,16 @@ class RefPtr {
|
|||
RefPtr(decltype(nullptr)) : ptr_(nullptr) {}
|
||||
RefPtr(const RefPtr& other) : ptr_(other.ptr_) {
|
||||
if (ptr_) {
|
||||
ptr_->Acquire();
|
||||
ptr_->AcquirePtr();
|
||||
}
|
||||
}
|
||||
RefPtr& operator=(const RefPtr& other) {
|
||||
T* old = ptr_;
|
||||
ptr_ = other.ptr_;
|
||||
if (ptr_) {
|
||||
ptr_->Acquire();
|
||||
ptr_->AcquirePtr();
|
||||
}
|
||||
if (old && old->Release()) {
|
||||
if (old && old->ReleasePtr()) {
|
||||
delete old;
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +46,15 @@ class RefPtr {
|
|||
enum DontAdoptTag {
|
||||
DontAdopt,
|
||||
};
|
||||
RefPtr(T* ptr, DontAdoptTag) : ptr_(ptr) { ptr->Acquire(); }
|
||||
RefPtr(T* ptr, DontAdoptTag) : ptr_(ptr) { ptr->AcquirePtr(); }
|
||||
|
||||
~RefPtr() {
|
||||
if (ptr_) {
|
||||
if (ptr_->ReleasePtr()) {
|
||||
delete ptr_;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
T* get() const { return ptr_; };
|
||||
T& operator*() const { return *ptr_; }
|
||||
|
|
@ -65,7 +73,7 @@ class RefPtr {
|
|||
T* ptr_;
|
||||
|
||||
friend RefPtr<T> AdoptPtr<T>(T* ptr);
|
||||
RefPtr(T* ptr) : ptr_(ptr) { ptr->Adopt(); }
|
||||
RefPtr(T* ptr) : ptr_(ptr) { ptr->AdoptPtr(); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
|
|
|||
|
|
@ -20,27 +20,32 @@ void StrFormatNumber(StringBuilder& builder, uint64_t value, uint64_t base) {
|
|||
} // namespace
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint8_t value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const uint8_t& value,
|
||||
StringView opts) {
|
||||
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
||||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint16_t value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const uint16_t& value,
|
||||
StringView opts) {
|
||||
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
||||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, int32_t value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const int32_t& value,
|
||||
StringView opts) {
|
||||
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
||||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint32_t value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const uint32_t& value,
|
||||
StringView opts) {
|
||||
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
||||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint64_t value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const uint64_t& value,
|
||||
StringView opts) {
|
||||
if (opts.find('x') != opts.npos) {
|
||||
builder.PushBack("0x");
|
||||
StrFormatNumber(builder, value, 16);
|
||||
|
|
@ -50,23 +55,26 @@ void StrFormatValue(StringBuilder& builder, uint64_t value, StringView opts) {
|
|||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, ErrorCode value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const ErrorCode& value,
|
||||
StringView opts) {
|
||||
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
||||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, char value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const char& value,
|
||||
StringView opts) {
|
||||
builder.PushBack(value);
|
||||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, const char* value,
|
||||
void StrFormatValue(StringBuilder& builder, char const* const& value,
|
||||
StringView opts) {
|
||||
StrFormatValue(builder, StringView(value), opts);
|
||||
StrFormatInternal(builder, StringView(value));
|
||||
}
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, StringView value, StringView opts) {
|
||||
void StrFormatValue(StringBuilder& builder, const StringView& value,
|
||||
StringView opts) {
|
||||
StrFormatInternal(builder, value);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,41 +7,51 @@
|
|||
|
||||
namespace glcr {
|
||||
|
||||
// FIXME: We need some meta-programming here to allow pass-by-value for pointers
|
||||
// and primitives.
|
||||
template <typename T>
|
||||
void StrFormatValue(StringBuilder& builder, T value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const T& value, StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint8_t value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const uint8_t& value,
|
||||
StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint16_t value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const uint16_t& value,
|
||||
StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, int32_t value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const int32_t& value,
|
||||
StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint32_t value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const uint32_t& value,
|
||||
StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, uint64_t value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const uint64_t& value,
|
||||
StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, ErrorCode value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const ErrorCode& value,
|
||||
StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, char value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const char& value, StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, const char* value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, char const* const& value,
|
||||
StringView opts);
|
||||
|
||||
template <>
|
||||
void StrFormatValue(StringBuilder& builder, StringView value, StringView opts);
|
||||
void StrFormatValue(StringBuilder& builder, const StringView& value,
|
||||
StringView opts);
|
||||
|
||||
void StrFormatInternal(StringBuilder& builder, StringView format);
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void StrFormatInternal(StringBuilder& builder, StringView format, T value,
|
||||
Args... args) {
|
||||
void StrFormatInternal(StringBuilder& builder, StringView format,
|
||||
const T& value, Args&&... args) {
|
||||
uint64_t posl = format.find('{');
|
||||
uint64_t posr = format.find('}', posl);
|
||||
if (posl == format.npos || posr == format.npos) {
|
||||
|
|
@ -56,7 +66,7 @@ void StrFormatInternal(StringBuilder& builder, StringView format, T value,
|
|||
}
|
||||
|
||||
template <typename... Args>
|
||||
String StrFormat(StringView format, Args... args) {
|
||||
String StrFormat(StringView format, Args&&... args) {
|
||||
VariableStringBuilder builder;
|
||||
StrFormatInternal(builder, format, args...);
|
||||
return builder.ToString();
|
||||
|
|
@ -64,7 +74,7 @@ String StrFormat(StringView format, Args... args) {
|
|||
|
||||
template <typename... Args>
|
||||
void StrFormatIntoBuffer(StringBuilder& builder, StringView format,
|
||||
Args... args) {
|
||||
Args&&... args) {
|
||||
StrFormatInternal(builder, format, args...);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ class MappedMemoryRegion {
|
|||
static MappedMemoryRegion DirectPhysical(uint64_t phys_addr, uint64_t size);
|
||||
static MappedMemoryRegion ContiguousPhysical(uint64_t size);
|
||||
static MappedMemoryRegion Default(uint64_t size);
|
||||
static MappedMemoryRegion FromCapability(z_cap_t vmmo_cap);
|
||||
|
||||
MappedMemoryRegion() {}
|
||||
// TODO: Disallow copy before doing any cleanup here.
|
||||
|
|
|
|||
|
|
@ -36,14 +36,6 @@ MappedMemoryRegion MappedMemoryRegion::Default(uint64_t size) {
|
|||
return MappedMemoryRegion(vmmo_cap, 0, vaddr, size);
|
||||
}
|
||||
|
||||
MappedMemoryRegion MappedMemoryRegion::FromCapability(z_cap_t vmmo_cap) {
|
||||
uint64_t vaddr;
|
||||
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
|
||||
|
||||
// FIXME: get the size here.
|
||||
return MappedMemoryRegion(vmmo_cap, 0, vaddr, 0);
|
||||
}
|
||||
|
||||
OwnedMemoryRegion::OwnedMemoryRegion(OwnedMemoryRegion&& other)
|
||||
: OwnedMemoryRegion(other.vmmo_cap_, other.vaddr_, other.size_) {
|
||||
other.vmmo_cap_ = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue