From 74b7e42e13354eab6df0c9f69f03170ec6965688 Mon Sep 17 00:00:00 2001 From: jacopodl Date: Tue, 16 Jan 2024 17:03:32 +0100 Subject: [PATCH] refactor --- argon/vm/datatype/arobject.h | 13 ++----------- argon/vm/memory/gc.cpp | 17 +++++++++++------ argon/vm/memory/gc.h | 2 +- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/argon/vm/datatype/arobject.h b/argon/vm/datatype/arobject.h index c3eecb60..f469b749 100644 --- a/argon/vm/datatype/arobject.h +++ b/argon/vm/datatype/arobject.h @@ -104,21 +104,12 @@ namespace argon::vm::datatype { template T *MakeGCObject(const TypeInfo *type, bool track) { - // N.B: It's risky to track an object before initializing its ReferenceCounter, - // but we can do it because the GC doesn't cycle until all worker threads have stopped, - // including the one currently allocating a new object. - auto *ret = memory::GCNew(type->size, track); - - AR_UNSAFE_GET_RC(ret) = (ArSize) memory::RCType::GC; - AR_GET_TYPE(ret) = type; - AR_UNSAFE_GET_MON(ret) = nullptr; - - return (T *) ret; + return (T*) memory::GCNew(type, track); } template T *MakeGCObject(TypeInfo *type, bool track) { - auto *ret = MakeGCObject((const TypeInfo *) type, track); + auto *ret = (T*) memory::GCNew(type, track); if (ret != nullptr) IncRef(type); diff --git a/argon/vm/memory/gc.cpp b/argon/vm/memory/gc.cpp index 403a8385..7e0bfe0f 100644 --- a/argon/vm/memory/gc.cpp +++ b/argon/vm/memory/gc.cpp @@ -168,11 +168,17 @@ void SearchRoots(GCGeneration *generation) { // PUBLIC -argon::vm::datatype::ArObject *argon::vm::memory::GCNew(ArSize length, bool track) { - auto *head = (GCHead *) memory::Alloc(sizeof(GCHead) + length); +argon::vm::datatype::ArObject *argon::vm::memory::GCNew(const datatype::TypeInfo *type, bool track) { + auto *head = (GCHead *) memory::Alloc(sizeof(GCHead) + type->size); if (head != nullptr) { memory::MemoryZero(head, sizeof(GCHead)); + auto *ret = (argon::vm::datatype::ArObject *) (((unsigned char *) head) + sizeof(GCHead)); + + AR_UNSAFE_GET_RC(ret) = (ArSize) RCType::GC; + AR_GET_TYPE(ret) = type; + AR_UNSAFE_GET_MON(ret) = nullptr; + if (track) { std::unique_lock lock(track_lock); @@ -181,7 +187,7 @@ argon::vm::datatype::ArObject *argon::vm::memory::GCNew(ArSize length, bool trac allocations++; } - return (argon::vm::datatype::ArObject *) (((unsigned char *) head) + sizeof(GCHead)); + return ret; } return nullptr; @@ -247,10 +253,10 @@ GCHead *argon::vm::memory::GCGetHead(datatype::ArObject *object) { void argon::vm::memory::GCFree(datatype::ArObject *object) { auto *head = GCGetHead(object); - if(head == nullptr) + if (head == nullptr) return; - if(head->IsTracked()) { + if (head->IsTracked()) { AR_GET_RC(object).DecStrong(nullptr); return; } @@ -311,7 +317,6 @@ void argon::vm::memory::ThresholdCollect() { void argon::vm::memory::Track(datatype::ArObject *object) { auto *head = GCGetHead(object); - if (head == nullptr) return; diff --git a/argon/vm/memory/gc.h b/argon/vm/memory/gc.h index 21584731..9aa8c198 100644 --- a/argon/vm/memory/gc.h +++ b/argon/vm/memory/gc.h @@ -70,7 +70,7 @@ namespace argon::vm::memory { int times; }; - datatype::ArObject *GCNew(datatype::ArSize length, bool track); + datatype::ArObject *GCNew(const datatype::TypeInfo *type, bool track); datatype::ArSize Collect(unsigned short generation);