From 0488b6e9551267c4bda6f409d8db8e6539dd5e25 Mon Sep 17 00:00:00 2001 From: Phillip Stephens Date: Thu, 20 Oct 2022 07:28:06 -0700 Subject: [PATCH] Match and link CToken Former-commit-id: ff40d50c8d33ca3e476e29cc1924a162e185e7fd --- configure.py | 2 +- include/Kyoto/CObjectReference.hpp | 10 ++++- include/Kyoto/CToken.hpp | 5 ++- obj_files.mk | 2 +- src/Kyoto/CToken.cpp | 70 ++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/Kyoto/CToken.cpp diff --git a/configure.py b/configure.py index 92b6059b..cf7faba3 100755 --- a/configure.py +++ b/configure.py @@ -624,7 +624,7 @@ LIBS = [ ["Kyoto/Streams/CZipInputStream", True], ["Kyoto/Streams/CZipSupport", True], "Kyoto/CSimplePool", - "Kyoto/CToken", + ["Kyoto/CToken", True], ["Kyoto/IObj", True], ], }, diff --git a/include/Kyoto/CObjectReference.hpp b/include/Kyoto/CObjectReference.hpp index 76d37e02..5190d8d3 100644 --- a/include/Kyoto/CObjectReference.hpp +++ b/include/Kyoto/CObjectReference.hpp @@ -16,14 +16,20 @@ public: // , xc_objectStore(nullptr) // , x10_object(obj.release()) // , x14_params(CVParamTransfer::Null()) {} - + ~CObjectReference(); CObjectReference(IObjectStore* store, const rstl::auto_ptr< IObj >& obj, SObjectTag tag, CVParamTransfer xfer); bool IsLoaded() const { return x10_object != nullptr; } + void AddReference() { x0_refCount++; } + int RemoveReference(); + void Lock(); + void Unlock(); + IObj* GetObject(); + private: - ushort x0_refCount; + short x0_refCount; bool x2_locked : 1; ushort x2_lockCount : 15; SObjectTag x4_objTag; diff --git a/include/Kyoto/CToken.hpp b/include/Kyoto/CToken.hpp index cf1b3e18..5791d0b5 100644 --- a/include/Kyoto/CToken.hpp +++ b/include/Kyoto/CToken.hpp @@ -11,15 +11,18 @@ class CObjectReference; class CToken { public: CToken() {} + CToken(CObjectReference* ref); CToken(IObj* obj); // : x0_objRef(new CObjectReference(obj)), x4_lockHeld(false) {} CToken(const CToken& other); ~CToken(); - CToken& operator=(const CToken&); CObjOwnerDerivedFromIObjUntyped* GetObj(); void Lock(); + void Unlock(); bool IsLoaded() const { return x0_objRef->IsLoaded(); } + void RemoveRef(); + CToken& operator=(const CToken&); private: CObjectReference* x0_objRef; diff --git a/obj_files.mk b/obj_files.mk index 624b81e2..6f42e012 100644 --- a/obj_files.mk +++ b/obj_files.mk @@ -563,7 +563,7 @@ KYOTO_1 :=\ $(BUILD_DIR)/src/Kyoto/Streams/CZipInputStream.o\ $(BUILD_DIR)/src/Kyoto/Streams/CZipSupport.o\ $(BUILD_DIR)/asm/Kyoto/CSimplePool.o\ - $(BUILD_DIR)/asm/Kyoto/CToken.o\ + $(BUILD_DIR)/src/Kyoto/CToken.o\ $(BUILD_DIR)/src/Kyoto/IObj.o\ ZLIB_FILES :=\ diff --git a/src/Kyoto/CToken.cpp b/src/Kyoto/CToken.cpp new file mode 100644 index 00000000..19f9caa5 --- /dev/null +++ b/src/Kyoto/CToken.cpp @@ -0,0 +1,70 @@ +#include "Kyoto/CToken.hpp" +#include "Kyoto/Alloc/CMemory.hpp" + +CToken::CToken(CObjectReference* ref) : x0_objRef(ref), x4_lockHeld(false) { + x0_objRef->AddReference(); +} + +CToken::CToken(IObj* obj) +: x0_objRef(new ("??(??)", nullptr) CObjectReference(obj)), x4_lockHeld(false) { + x0_objRef->AddReference(); + Lock(); +} + +CToken::CToken(const CToken& other) : x0_objRef(other.x0_objRef), x4_lockHeld(false) { + x0_objRef->AddReference(); + if (other.x4_lockHeld) { + Lock(); + } +} + +CToken::~CToken() { + if (x4_lockHeld) { + x0_objRef->Unlock(); + } + RemoveRef(); +} + +CObjOwnerDerivedFromIObjUntyped* CToken::GetObj() { + Lock(); + + return reinterpret_cast< CObjOwnerDerivedFromIObjUntyped* >(x0_objRef->GetObject()); +} + +void CToken::RemoveRef() { + if (x0_objRef->RemoveReference() == 0) { + delete x0_objRef; + } +} + +void CToken::Lock() { + if (!x4_lockHeld) { + x0_objRef->Lock(); + x4_lockHeld = true; + } +} + +void CToken::Unlock() { + if (x4_lockHeld == 0) { + return; + } + + x0_objRef->Unlock(); + x4_lockHeld = false; +} + +CToken& CToken::operator=(const CToken& other) { + if (&other == const_cast< const CToken* >(this)) { + return *this; + } + + Unlock(); + RemoveRef(); + x0_objRef = other.x0_objRef; + x0_objRef->AddReference(); + if (other.x4_lockHeld) { + Lock(); + } + + return *this; +}