Skip to content

Commit

Permalink
Merge a598b93 into 201ac22
Browse files Browse the repository at this point in the history
  • Loading branch information
andychu committed Sep 30, 2022
2 parents 201ac22 + a598b93 commit 6ec77e7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mycpp/gc_dict.h
Expand Up @@ -48,7 +48,7 @@ class _DummyDict {
void* values_;
};

// A list has one Slab pointer which we need to follow.
// A dict has 3 pointers the GC needs to follow.
constexpr uint16_t maskof_Dict() {
return maskbit(offsetof(_DummyDict, entry_)) |
maskbit(offsetof(_DummyDict, keys_)) |
Expand Down
37 changes: 37 additions & 0 deletions mycpp/marksweep_gc_test.cc
Expand Up @@ -82,6 +82,40 @@ TEST list_collection_test() {
PASS();
}

class Node : Obj {
public:
Node();
Node* next_;
};

constexpr uint16_t maskof_Node() {
return maskbit(offsetof(Node, next_));
}

Node::Node() :
Obj(Tag::FixedSize, maskof_Node(), sizeof(Node)),
next_(nullptr) {
}

TEST cycle_collection_test() {
// Dict<Str*, int>* d = NewDict<Str*, int>();

Node* n1 = nullptr;
Node* n2 = nullptr;
StackRoots _roots({&n1, &n2});
n1 = Alloc<Node>();
n2 = Alloc<Node>();

gHeap.Collect();

n1->next_ = n2;
n2->next_ = n1;

gHeap.Collect();

PASS();
}

GREATEST_MAIN_DEFS();

int main(int argc, char **argv) {
Expand All @@ -93,6 +127,9 @@ int main(int argc, char **argv) {

RUN_TEST(string_collection_test);
RUN_TEST(list_collection_test);
RUN_TEST(cycle_collection_test);

gHeap.Collect();

GREATEST_MAIN_END(); /* display results */
return 0;
Expand Down
7 changes: 7 additions & 0 deletions mycpp/marksweep_heap.cc
Expand Up @@ -53,6 +53,13 @@ void* MarkSweepHeap::Allocate(int byte_count) {
void MarkSweepHeap::MarkAllReferences(Obj* obj) {
auto header = ObjHeader(obj);

auto marked_alloc = marked_allocations_.find((Obj*)obj);
bool alloc_is_marked = marked_alloc != marked_allocations_.end();
if (alloc_is_marked)
{
return;
}

this->marked_allocations_.insert(static_cast<void*>(obj));

switch (header->heap_tag_) {
Expand Down

0 comments on commit 6ec77e7

Please sign in to comment.