Skip to content

Commit

Permalink
#111 trigger gc
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed Jun 14, 2024
1 parent 99e0097 commit 47ef30f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 2 additions & 0 deletions include/ChaiVM/interpreter/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class Executor {
&Executor::set_field,
};

void triggerGC();

private:
chsize_t acc_;
bool isAccRef_ = false;
Expand Down
11 changes: 6 additions & 5 deletions include/ChaiVM/memory/traced-allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct AllocationInfo final {
class TracedByteAllocator final {
public:
TracedByteAllocator(size_t size)
: allocator_(size, FreeListAllocator::PlacementPolicy::FIND_FIRST) {
: size_(size), allocator_(size, FreeListAllocator::PlacementPolicy::FIND_FIRST) {
allocator_.Init();
}

Expand All @@ -38,12 +38,13 @@ class TracedByteAllocator final {
// allocations_ are changed by GC
}

auto &allocated() noexcept { return allocated_; }
auto const &allocated() const noexcept { return allocated_; }
auto &allocations() noexcept { return allocations_; }
const auto &allocations() const noexcept { return allocations_; }
size_t &allocated() { return allocated_; }
size_t const &allocated() const noexcept { return allocated_; }
auto &allocations() { return allocations_; }
size_t size() { return size_; }

private:
size_t size_ = 0;
size_t allocated_ = 0;
std::list<AllocationInfo> allocations_;
FreeListAllocator allocator_;
Expand Down
10 changes: 8 additions & 2 deletions src/ChaiVM/interpreter/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace chai::interpreter {

#define DO_NEXT_INS() \
triggerGC(); \
Instruction newIns = \
decoder::parse(currentFrame_->func_.code[pc() / sizeof(bytecode_t)]); \
/*std::cout << "Next inst: " << OP_TO_STR[newIns.operation] << ", imm = " \
* << newIns.immidiate << std::endl; */ \
std::cout << "Next inst: " << OP_TO_STR[newIns.operation] << ", imm = " << newIns.immidiate << std::endl; \
(this->*HANDLER_ARR[newIns.operation])(newIns);

Executor::Executor(CodeManager *manager, memory::LinearBuffer &framesBuffer,
Expand Down Expand Up @@ -580,6 +580,12 @@ memory::TracedByteAllocator &interpreter::Executor::getObjectAllocator() {
const GarbageCollector &interpreter::Executor::getGC() const {
return gc_;
}
void interpreter::Executor::triggerGC() {
if (static_cast<double>(objectsAllocator_.allocated()) >
static_cast<double>(objectsAllocator_.size()) * 0.9) {
gc_.collect();
}
}

InvalidInstruction::InvalidInstruction(const char *msg) : runtime_error(msg) {}
InvalidInstruction::InvalidInstruction(const std::string &msg)
Expand Down

0 comments on commit 47ef30f

Please sign in to comment.