Fix bug when loading heap

Need to grow as needed.
This commit is contained in:
Nunuhara Cabbage
2021-05-12 19:36:48 -07:00
parent 2a9e7a417b
commit 10d838ebc4
3 changed files with 7 additions and 1 deletions
+1
View File
@@ -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);
+3 -1
View File
@@ -18,6 +18,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#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++) {
+3
View File
@@ -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++;
}