From 10d838ebc4f4ffc628805c733ca0e0bca958fc9f Mon Sep 17 00:00:00 2001 From: Nunuhara Cabbage Date: Wed, 12 May 2021 19:36:48 -0700 Subject: [PATCH] Fix bug when loading heap Need to grow as needed. --- include/vm/heap.h | 1 + src/heap.c | 4 +++- src/resume.c | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/vm/heap.h b/include/vm/heap.h index f067777..281a25b 100644 --- a/include/vm/heap.h +++ b/include/vm/heap.h @@ -50,6 +50,7 @@ extern size_t heap_size; void heap_init(void); void heap_delete(void); +void heap_grow(size_t new_size); int32_t heap_alloc_slot(enum vm_pointer_type type); void heap_ref(int slot); diff --git a/src/heap.c b/src/heap.c index c00d825..47e2050 100644 --- a/src/heap.c +++ b/src/heap.c @@ -18,6 +18,7 @@ #include #include +#include #include "system4/string.h" #include "vm.h" #include "vm/heap.h" @@ -45,8 +46,9 @@ static const char *vm_ptrtype_string(enum vm_pointer_type type) { return "INVALID POINTER TYPE"; } -static void heap_grow(size_t new_size) +void heap_grow(size_t new_size) { + assert(new_size > heap_size); heap = xrealloc(heap, sizeof(struct vm_pointer) * new_size); heap_free_stack = xrealloc(heap_free_stack, sizeof(int32_t) * new_size); for (size_t i = heap_size; i < new_size; i++) { diff --git a/src/resume.c b/src/resume.c index 24263cf..c9b1f15 100644 --- a/src/resume.c +++ b/src/resume.c @@ -256,6 +256,9 @@ static void delete_heap(void) // Out-of-order allocations would break the above assumption! static void alloc_heap_slot(int slot) { + if ((size_t)slot >= heap_size) { + heap_grow(heap_size * 2); + } heap_free_stack[slot] = heap_free_stack[heap_free_ptr]; heap_free_ptr++; }