[zion/glacier] Move RefPtr to glacier.

This commit is contained in:
Drew Galbraith 2023-06-21 15:07:40 -07:00
parent 8bcb574677
commit e1af79b975
26 changed files with 130 additions and 106 deletions

View file

@ -56,7 +56,8 @@ z_err_t UnboundedMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
return Z_OK;
}
void UnboundedMessageQueue::WriteKernel(uint64_t init, RefPtr<Capability> cap) {
void UnboundedMessageQueue::WriteKernel(uint64_t init,
glcr::RefPtr<Capability> cap) {
auto msg = glcr::MakeShared<Message>();
msg->bytes = new uint8_t[8];
msg->num_bytes = sizeof(init);

View file

@ -1,7 +1,9 @@
#pragma once
#include <glacier/memory/ref_ptr.h>
#include <glacier/memory/shared_ptr.h>
#include "capability/capability.h"
#include "glacier/memory/shared_ptr.h"
#include "include/ztypes.h"
#include "lib/linked_list.h"
@ -27,7 +29,7 @@ class UnboundedMessageQueue : public MessageQueue {
z_err_t PopFront(uint64_t* num_bytes, void* bytes, uint64_t* num_caps,
z_cap_t* caps) override;
void WriteKernel(uint64_t init, RefPtr<Capability> cap);
void WriteKernel(uint64_t init, glcr::RefPtr<Capability> cap);
uint64_t size() { return pending_messages_.size(); }
bool empty() { return size() == 0; }
@ -37,7 +39,7 @@ class UnboundedMessageQueue : public MessageQueue {
uint64_t num_bytes;
uint8_t* bytes;
LinkedList<RefPtr<Capability>> caps;
LinkedList<glcr::RefPtr<Capability>> caps;
};
LinkedList<glcr::SharedPtr<Message>> pending_messages_;

View file

@ -1,92 +0,0 @@
#pragma once
#include "debug/debug.h"
template <typename T>
class RefPtr;
template <typename T>
RefPtr<T> AdoptPtr(T* ptr);
template <typename T, typename U>
RefPtr<T> StaticCastRefPtr(const RefPtr<U>& ref);
template <typename T>
class RefPtr {
public:
RefPtr() : ptr_(nullptr) {}
RefPtr(decltype(nullptr)) : ptr_(nullptr) {}
RefPtr(const RefPtr& other) : ptr_(other.ptr_) {
if (ptr_) {
ptr_->Acquire();
}
}
RefPtr& operator=(const RefPtr& other) {
T* old = ptr_;
ptr_ = other.ptr_;
if (ptr_) {
ptr_->Acquire();
}
if (old && old->Release()) {
dbgln("Deleting obj %m", old);
delete old;
}
return *this;
}
RefPtr(RefPtr&& other) : ptr_(other.ptr_) { other.ptr_ = nullptr; }
RefPtr& operator=(RefPtr&& other) {
// Swap
T* ptr = ptr_;
ptr_ = other.ptr_;
other.ptr_ = ptr;
return *this;
}
enum DontAdoptTag {
DontAdopt,
};
RefPtr(T* ptr, DontAdoptTag) : ptr_(ptr) { ptr->Acquire(); }
T* get() const { return ptr_; };
T& operator*() const { return *ptr_; }
T* operator->() const { return ptr_; }
operator bool() const { return ptr_ != nullptr; }
bool operator==(decltype(nullptr)) const { return (ptr_ == nullptr); }
bool operator!=(decltype(nullptr)) const { return (ptr_ != nullptr); }
bool operator==(const RefPtr<T>& other) const { return (ptr_ == other.ptr_); }
bool operator!=(const RefPtr<T>& other) const { return (ptr_ != other.ptr_); }
private:
T* ptr_;
friend RefPtr<T> AdoptPtr<T>(T* ptr);
RefPtr(T* ptr) : ptr_(ptr) { ptr->Adopt(); }
};
template <typename T>
class MakeRefCountedFriend final {
public:
template <typename... Args>
static RefPtr<T> Make(Args&&... args) {
return AdoptPtr(new T(args...));
}
};
template <typename T, typename... Args>
RefPtr<T> MakeRefCounted(Args&&... args) {
return MakeRefCountedFriend<T>::Make(args...);
}
template <typename T>
RefPtr<T> AdoptPtr(T* ptr) {
return RefPtr(ptr);
}
template <typename T, typename U>
RefPtr<T> StaticCastRefPtr(const RefPtr<U>& ref) {
return RefPtr(static_cast<T*>(ref.get()), RefPtr<T>::DontAdopt);
}