From 8bcb57467755b1e2ab1bd3897b773c50038af93a Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 21 Jun 2023 14:52:40 -0700 Subject: [PATCH] [zion/glacier] Move RefCounted to glacier. --- lib/glacier/memory/ref_counted.h | 29 ++++++++++++++++++++++ zion/capability/capability.h | 3 ++- zion/lib/ref_counted.h | 41 -------------------------------- zion/object/kernel_object.h | 4 ++-- 4 files changed, 33 insertions(+), 44 deletions(-) create mode 100644 lib/glacier/memory/ref_counted.h delete mode 100644 zion/lib/ref_counted.h diff --git a/lib/glacier/memory/ref_counted.h b/lib/glacier/memory/ref_counted.h new file mode 100644 index 0000000..a141de4 --- /dev/null +++ b/lib/glacier/memory/ref_counted.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +namespace glcr { + +template +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 diff --git a/zion/capability/capability.h b/zion/capability/capability.h index 1e41fef..44b5f84 100644 --- a/zion/capability/capability.h +++ b/zion/capability/capability.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "lib/ref_ptr.h" @@ -8,7 +9,7 @@ class Process; class Thread; -class Capability : public RefCounted { +class Capability : public glcr::RefCounted { public: Capability(const RefPtr& obj, uint64_t permissions) : obj_(obj), permissions_(permissions) {} diff --git a/zion/lib/ref_counted.h b/zion/lib/ref_counted.h deleted file mode 100644 index 1926ba1..0000000 --- a/zion/lib/ref_counted.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include - -#include "debug/debug.h" - -template -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; -}; diff --git a/zion/object/kernel_object.h b/zion/object/kernel_object.h index eda05da..d3a425e 100644 --- a/zion/object/kernel_object.h +++ b/zion/object/kernel_object.h @@ -1,8 +1,8 @@ #pragma once -#include "lib/ref_counted.h" +#include -class KernelObject : public RefCounted { +class KernelObject : public glcr::RefCounted { public: enum ObjectType { INVALID = 0x0,