[zion/glacier] Move RefCounted to glacier.

This commit is contained in:
Drew Galbraith 2023-06-21 14:52:40 -07:00
parent 56eae3d4e5
commit 8bcb574677
4 changed files with 33 additions and 44 deletions

View File

@ -0,0 +1,29 @@
#pragma once
#include <stdint.h>
namespace glcr {
template <typename T>
class RefCounted {
public:
RefCounted() {}
virtual ~RefCounted() {}
// FIXME: Rethink error handling in these cases now that we can't panic the
// kernel.
void Adopt() { ref_count_ = 1; }
void Acquire() { ref_count_++; }
bool Release() { return (--ref_count_) == 0; }
private:
// FIXME: This should be an atomic type.
uint64_t ref_count_ = -1;
// Disallow copy and move.
RefCounted(RefCounted&) = delete;
RefCounted(RefCounted&&) = delete;
RefCounted& operator=(RefCounted&) = delete;
RefCounted& operator=(RefCounted&&) = delete;
};
} // namespace glcr

View File

@ -1,5 +1,6 @@
#pragma once
#include <glacier/memory/ref_counted.h>
#include <stdint.h>
#include "lib/ref_ptr.h"
@ -8,7 +9,7 @@
class Process;
class Thread;
class Capability : public RefCounted<Capability> {
class Capability : public glcr::RefCounted<Capability> {
public:
Capability(const RefPtr<KernelObject>& obj, uint64_t permissions)
: obj_(obj), permissions_(permissions) {}

View File

@ -1,41 +0,0 @@
#pragma once
#include <stdint.h>
#include "debug/debug.h"
template <typename T>
class RefCounted {
public:
RefCounted() {}
~RefCounted() { dbgln("RefCounted object destroyed"); }
void Adopt() {
if (ref_count_ != -1) {
panic("Adopting owned ptr");
} else {
ref_count_ = 1;
}
}
void Acquire() {
if (ref_count_ == -1) {
panic("Acquiring unowned ptr");
}
ref_count_++;
}
bool Release() {
if (ref_count_ == -1 || ref_count_ == 0) {
panic("Releasing unowned ptr");
}
return (--ref_count_) == 0;
}
private:
// FIXME: This should be an atomic type.
uint64_t ref_count_ = -1;
// Disallow copy and move.
RefCounted(RefCounted&) = delete;
RefCounted(RefCounted&&) = delete;
RefCounted& operator=(RefCounted&) = delete;
RefCounted& operator=(RefCounted&&) = delete;
};

View File

@ -1,8 +1,8 @@
#pragma once
#include "lib/ref_counted.h"
#include <glacier/memory/ref_counted.h>
class KernelObject : public RefCounted<KernelObject> {
class KernelObject : public glcr::RefCounted<KernelObject> {
public:
enum ObjectType {
INVALID = 0x0,