From 50cd63aa387eb15f43215947777cba4254b38e4c Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Sun, 30 Jul 2023 04:33:14 +0200 Subject: [PATCH] some gc stuff --- src/runtime.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/runtime.h | 26 ++++++++--- 2 files changed, 135 insertions(+), 7 deletions(-) diff --git a/src/runtime.c b/src/runtime.c index ef3f730..a58f4c0 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -1,5 +1,6 @@ #include "runtime.h" #include "lexer.h" +#include #include void value_vec_construct(ValueVec* vec, Allocator* allocator) @@ -133,3 +134,118 @@ void value_stringify(const Value* value, String* acc) break; } } + +void gc_buffer_construct(GCBuffer* buffer, size_t capacity) +{ + void* data = malloc(capacity); + ASSERT(buffer != NULL); + *buffer = (GCBuffer) { + .data = data, + .capacity = capacity, + }; +} + +void gc_buffer_destroy(GCBuffer* buffer) { free(buffer->data); } + +void gc_buffer_resize(GCBuffer* buffer, size_t minimum_capacity) +{ + if (buffer->capacity >= minimum_capacity) { + return; + } + while (buffer->capacity < minimum_capacity) { + buffer->capacity *= 2; + } + void* new_buffer = realloc(buffer->data, buffer->capacity); + ASSERT(new_buffer); + buffer->data = new_buffer; +} + +void gc_construct(GC* allocator, size_t start_capacity) +{ + GCBuffer buffer_a; + gc_buffer_construct(&buffer_a, start_capacity); + GCBuffer buffer_b; + gc_buffer_construct(&buffer_b, start_capacity); + *allocator = (GC) { + .alloc = gc_alloc, + .realloc = gc_realloc, + .buffer_a = buffer_a, + .buffer_b = buffer_b, + .select = GCBufferA, + .index = 0, + }; +} + +void gc_destroy(GC* allocator) +{ + gc_buffer_destroy(&allocator->buffer_a); + gc_buffer_destroy(&allocator->buffer_b); +} + +void* gc_alloc(GC* allocator, size_t size) +{ + GCBuffer* buffer = gc_selected_buffer(allocator); + if (buffer->capacity < allocator->index + size) { + return NULL; + } + void* ptr = &buffer->data[allocator->index]; + allocator->index += size; + return ptr; +} + +void* gc_realloc(GC* allocator, void* data, size_t size) +{ + void* new_ptr = gc_alloc(allocator, size); + if (new_ptr == NULL) { + return NULL; + } + memcpy(new_ptr, data, size); + return new_ptr; +} + +void* gc_collect(GC* allocator, ValueVec* stack, size_t to_be_allocated) +{ + GCBuffer* selected = gc_selected_buffer(allocator); + GCBuffer* other = gc_other_buffer(allocator); +} + +GCBuffer* gc_selected_buffer(GC* allocator) +{ + switch (allocator->select) { + case GCBufferA: + return &allocator->buffer_a; + case GCBufferB: + return &allocator->buffer_b; + } +} + +GCBuffer* gc_other_buffer(GC* allocator) +{ + switch (allocator->select) { + case GCBufferA: + return &allocator->buffer_b; + case GCBufferB: + return &allocator->buffer_a; + } +} + +void gc_select_other(GC* allocator) +{ + switch (allocator->select) { + case GCBufferA: + allocator->select = GCBufferB; + break; + case GCBufferB: + allocator->select = GCBufferA; + break; + } +} + +void runtime_construct(Runtime* runtime, Allocator* allocator) +{ + *runtime = (Runtime) { + .allocator = allocator, + }; +} + +void runtime_destroy(Runtime* runtime) { } diff --git a/src/runtime.h b/src/runtime.h index 93fcda9..712f093 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -52,27 +52,39 @@ struct Value { void value_destroy(Value* value); void value_stringify(const Value* value, String* acc); +typedef struct { + void* data; + size_t capacity; +} GCBuffer; + +void gc_buffer_construct(GCBuffer* buffer, size_t capacity); +void gc_buffer_destroy(GCBuffer* buffer); +void gc_buffer_resize(GCBuffer* buffer, size_t minimum_capacity); + typedef enum { GCBufferA, GCBufferB, -} GCBuffer; +} SelectedGCBuffer; typedef struct GC GC; struct GC { void* (*alloc)(GC* allocator, size_t size); void* (*realloc)(GC* allocator, void* data, size_t size); - void* a_buffer; - size_t a_capacity; - void* b_buffer; - size_t b_capacity; - size_t alloc_ptr; - GCBuffer selected_buffer; + GCBuffer buffer_a; + GCBuffer buffer_b; + SelectedGCBuffer select; + size_t index; }; void gc_construct(GC* allocator, size_t start_capacity); void gc_destroy(GC* allocator); void* gc_alloc(GC* allocator, size_t size); void* gc_realloc(GC* allocator, void* data, size_t size); +void* gc_collect(GC* allocator, ValueVec* stack, size_t to_be_allocated); + +GCBuffer* gc_selected_buffer(GC* allocator); +GCBuffer* gc_other_buffer(GC* allocator); +void gc_select_other(GC* allocator); typedef struct { Allocator* allocator;