[zion/glacier] Move SharedPtr to glacier

This commit is contained in:
Drew Galbraith 2023-06-21 14:48:29 -07:00
parent f3443cf4de
commit 56eae3d4e5
5 changed files with 13 additions and 38 deletions

View file

@ -10,7 +10,7 @@ z_err_t UnboundedMessageQueue::PushBack(uint64_t num_bytes, const void* bytes,
return Z_ERR_UNIMPLEMENTED;
}
auto message = MakeShared<Message>();
auto message = glcr::MakeShared<Message>();
message->num_bytes = num_bytes;
message->bytes = new uint8_t[num_bytes];
for (uint64_t i = 0; i < num_bytes; i++) {
@ -57,7 +57,7 @@ z_err_t UnboundedMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
}
void UnboundedMessageQueue::WriteKernel(uint64_t init, RefPtr<Capability> cap) {
auto msg = MakeShared<Message>();
auto msg = glcr::MakeShared<Message>();
msg->bytes = new uint8_t[8];
msg->num_bytes = sizeof(init);

View file

@ -1,9 +1,9 @@
#pragma once
#include "capability/capability.h"
#include "glacier/memory/shared_ptr.h"
#include "include/ztypes.h"
#include "lib/linked_list.h"
#include "lib/shared_ptr.h"
class MessageQueue {
public:
@ -40,5 +40,5 @@ class UnboundedMessageQueue : public MessageQueue {
LinkedList<RefPtr<Capability>> caps;
};
LinkedList<SharedPtr<Message>> pending_messages_;
LinkedList<glcr::SharedPtr<Message>> pending_messages_;
};

View file

@ -1,88 +0,0 @@
#pragma once
#include <stdint.h>
#include "debug/debug.h"
template <typename T>
class SharedPtr {
public:
SharedPtr() : init_(false), ptr_(0), ref_cnt_(0) {}
// Takes ownership.
SharedPtr(T* ptr) {
ptr_ = ptr;
ref_cnt_ = new uint64_t(1);
}
SharedPtr(const SharedPtr<T>& other)
: init_(other.init_), ptr_(other.ptr_), ref_cnt_(other.ref_cnt_) {
(*ref_cnt_)++;
}
SharedPtr& operator=(const SharedPtr<T>& other) {
Cleanup();
init_ = other.init_;
ptr_ = other.ptr_;
ref_cnt_ = other.ref_cnt_;
(*ref_cnt_)++;
return *this;
}
~SharedPtr() { Cleanup(); }
T& operator*() {
CheckValid();
return *ptr_;
}
const T& operator*() const {
CheckValid();
return *ptr_;
}
T* operator->() {
CheckValid();
return ptr_;
}
const T* operator->() const {
CheckValid();
return ptr_;
}
T* ptr() {
CheckValid();
return ptr_;
}
bool operator==(const SharedPtr<T>& other) {
CheckValid();
other.CheckValid();
return ptr_ == other.ptr_;
}
bool empty() { return !init_; }
private:
bool init_ = true;
T* ptr_;
uint64_t* ref_cnt_;
void Cleanup() {
if (!init_) {
return;
}
if (--(*ref_cnt_) == 0) {
delete ptr_;
delete ref_cnt_;
}
}
void CheckValid() const {
if (!init_) {
panic("Accessing invalid shared ptr");
}
}
};
template <typename T, class... A>
SharedPtr<T> MakeShared(A... args) {
return {new T(args...)};
}

View file

@ -7,7 +7,6 @@
#include "lib/mutex.h"
#include "lib/pair.h"
#include "lib/ref_ptr.h"
#include "lib/shared_ptr.h"
#include "object/kernel_object.h"
#include "usr/zcall_internal.h"

View file

@ -4,7 +4,6 @@
#include "lib/linked_list.h"
#include "lib/message_queue.h"
#include "lib/mutex.h"
#include "lib/shared_ptr.h"
#include "object/kernel_object.h"
#include "object/thread.h"
#include "usr/zcall_internal.h"