Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopodl committed Jan 16, 2024
1 parent daf3dcd commit 74b7e42
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
13 changes: 2 additions & 11 deletions argon/vm/datatype/arobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,12 @@ namespace argon::vm::datatype {

template<typename T>
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<typename T>
T *MakeGCObject(TypeInfo *type, bool track) {
auto *ret = MakeGCObject<T>((const TypeInfo *) type, track);
auto *ret = (T*) memory::GCNew(type, track);
if (ret != nullptr)
IncRef(type);

Expand Down
17 changes: 11 additions & 6 deletions argon/vm/memory/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion argon/vm/memory/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 74b7e42

Please sign in to comment.