Skip to content

Commit

Permalink
Always take pending limit into account (#1354)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperl authored Jan 19, 2023
1 parent 21a2269 commit 5ab8627
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
10 changes: 0 additions & 10 deletions src/heap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,6 @@ Stack* ObjectHeap::allocate_stack(int length) {
return result;
}

#ifdef TOIT_GC_LOGGING
static word format(word n) {
return (n > 9999) ? (n >> KB_LOG2) : n;
}
static const char* format_unit(word n) {
return (n > 9999) ? "K" : "";
}
#define FORMAT(n) format(n), format_unit(n)
#endif

void ObjectHeap::iterate_roots(RootCallback* callback) {
// Process the roots in the object heap.
callback->do_root(reinterpret_cast<Object**>(&task_));
Expand Down
2 changes: 2 additions & 0 deletions src/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ class ObjectHeap {
word max_external_allocation();
void register_external_allocation(word size);
void unregister_external_allocation(word size);

bool has_max_heap_size() const { return max_heap_size_ != 0; }
bool has_pending_limit() const { return limit_ != pending_limit_; }

void check_install_heap_limit() {
if (limit_ != pending_limit_) install_heap_limit();
Expand Down
7 changes: 7 additions & 0 deletions src/interpreter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ Object** Interpreter::push_error(Object** sp, Object* type, const char* message)
sp = gc(sp, false, attempts, false);
instance = process->object_heap()->allocate_instance(process->program()->exception_class_id());
}
process->object_heap()->check_install_heap_limit();

if (instance == null) {
DROP(1);
return push_out_of_memory_error(sp);
Expand All @@ -180,6 +182,8 @@ Object** Interpreter::push_error(Object** sp, Object* type, const char* message)
sp = gc(sp, true, attempts, false);
buffer.allocate(STACK_ENCODING_BUFFER_SIZE);
}
process->object_heap()->check_install_heap_limit();

if (!buffer.has_content()) {
DROP(2);
return push_out_of_memory_error(sp);
Expand All @@ -196,6 +200,8 @@ Object** Interpreter::push_error(Object** sp, Object* type, const char* message)
sp = gc(sp, false, attempts, false);
trace = process->allocate_byte_array(buffer.size());
}
process->object_heap()->check_install_heap_limit();

if (trace == null) {
DROP(2);
return push_out_of_memory_error(sp);
Expand Down Expand Up @@ -263,6 +269,7 @@ Object** Interpreter::handle_stack_overflow(Object** sp, OverflowState* state, M
sp = gc(sp, false, attempts, false);
new_stack = process->object_heap()->allocate_stack(new_length);
}
process->object_heap()->check_install_heap_limit();

// Then check for out of memory.
if (new_stack == null) {
Expand Down
17 changes: 15 additions & 2 deletions src/interpreter_run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Method Program::find_method(Object* receiver, int offset) {
#define DISPATCH(n) \
{ ASSERT(program->bytecodes.data() <= bcp + n); \
ASSERT(bcp + n < program->bytecodes.data() + program->bytecodes.length()); \
ASSERT(!process_->object_heap()->has_pending_limit()); \
Opcode next = static_cast<Opcode>(bcp[n]); \
bcp += n; \
OPCODE_TRACE() \
Expand Down Expand Up @@ -275,6 +276,13 @@ Interpreter::Result Interpreter::run() {
};
#undef LABEL

// We sometimes suspend processes and collect their garbage
// from the outside. In that case, we are not calling the GC
// after a failed allocation attempt, so we do not get the
// pending heap limit installed. Do it here before starting
// the interpretation.
process_->object_heap()->check_install_heap_limit();

// Interpretation state.
Program* program = process_->program();
#ifdef TOIT_CHECK_PROPAGATED_TYPES
Expand Down Expand Up @@ -531,6 +539,8 @@ Interpreter::Result Interpreter::run() {
sp = gc(sp, false, attempts, false);
result = process_->object_heap()->allocate_instance(Smi::from(class_index));
}
process_->object_heap()->check_install_heap_limit();

if (result == null) {
sp = push_error(sp, program->allocation_failed(), "");
goto THROW_IMPLEMENTATION;
Expand All @@ -541,8 +551,10 @@ Interpreter::Result Interpreter::run() {
instance->at_put(i, program->null_object());
}
PUSH(result);
if (Flags::gcalot) sp = gc(sp, false, 1, false);
process_->object_heap()->check_install_heap_limit();
if (Flags::gcalot) {
sp = gc(sp, false, 1, false);
process_->object_heap()->check_install_heap_limit();
}
OPCODE_END();

OPCODE_BEGIN_WITH_WIDE(IS_CLASS, encoded);
Expand Down Expand Up @@ -1071,6 +1083,7 @@ Interpreter::Result Interpreter::run() {

// GC might have taken place in object heap but local "method" is from program heap.
PUSH(result);
process_->object_heap()->check_install_heap_limit();
DISPATCH(PRIMITIVE_LENGTH);

done:
Expand Down

0 comments on commit 5ab8627

Please sign in to comment.