aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-02-24 17:53:46 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commita27a3e6ae09e8396584c95a3567b145e86d8bc40 (patch)
tree6b56721fb64f31e4b731e75942f81838d10b0612 /src
parenta307f17f44b461f58441926fcbf87883f17ebe61 (diff)
Fixed CHECK_RESULT macro, use scoped allocator
Scoped allocator gives us better performance and cleanup code for error cases is much cleaner
Diffstat (limited to 'src')
-rw-r--r--src/ast.c17
-rw-r--r--src/buffer.c28
-rw-r--r--src/parser.c9
-rw-r--r--src/scoped_allocator.c4
4 files changed, 22 insertions, 36 deletions
diff --git a/src/ast.c b/src/ast.c
index 719d48e..2802c3b 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -7,18 +7,9 @@ Ast ast_none() {
return ast;
}
-void ast_deinit(Ast *ast) {
- /* TODO: Cleanup the different types of ast */
- (void)ast;
-}
-
-void funcdecl_init(FunctionDecl *self) {
+int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator) {
self->name = create_buffer_view_null();
- buffer_init(&self->body);
-}
-
-void funcdecl_deinit(FunctionDecl *self) {
- buffer_deinit(&self->body);
+ return buffer_init(&self->body, allocator, sizeof(Ast) * 4);
}
int funcdecl_add_to_body(FunctionDecl *self, Ast ast) {
@@ -34,8 +25,4 @@ void lhsexpr_init(LhsExpr *self, int isConst, BufferView var_name) {
self->isConst = isConst;
self->var_name = var_name;
self->rhs_expr = ast_none();
-}
-
-void lhsexpr_deinit(LhsExpr *self) {
- ast_deinit(&self->rhs_expr);
} \ No newline at end of file
diff --git a/src/buffer.c b/src/buffer.c
index 972e23a..490418a 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -3,12 +3,14 @@
#include "../include/mem.h"
#include <assert.h>
-void buffer_init(Buffer *self) {
- self->data = NULL;
+int buffer_init(Buffer *self, ScopedAllocator *allocator, usize initial_capacity) {
+ self->allocator = allocator;
self->size = 0;
- self->capacity = 0;
+ self->capacity = initial_capacity;
+ return scoped_allocator_alloc(self->allocator, initial_capacity, (void**)&self->data);
}
+<<<<<<< HEAD
void buffer_deinit(Buffer *self) {
am_free(self->data);
self->data = NULL;
@@ -18,6 +20,9 @@ void buffer_deinit(Buffer *self) {
static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) {
usize capacity;
+=======
+static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) {
+>>>>>>> Fixed CHECK_RESULT macro, use scoped allocator
void *new_mem;
int alloc_result;
usize new_capacity;
@@ -26,21 +31,16 @@ static CHECK_RESULT int buffer_ensure_capacity_for(Buffer *self, usize size) {
if(self->capacity >= new_capacity)
return BUFFER_OK;
- capacity = self->capacity;
- if(capacity == 0) {
- capacity = new_capacity;
- } else {
- while(capacity < new_capacity) {
- capacity *= 1.5;
- }
- }
-
- alloc_result = am_realloc(self->data, capacity, &new_mem);
+ /*
+ We are using alloc here instead of realloc because we are using scoped allocator
+ which doesn't reallocate. TODO: Verify if scoped allocator node size is enough to hold buffers...
+ */
+ alloc_result = scoped_allocator_alloc(self->allocator, new_capacity, &new_mem);
if(alloc_result != ALLOC_OK)
return BUFFER_ALLOC_FAIL;
self->data = new_mem;
- self->capacity = capacity;
+ self->capacity = new_capacity;
return BUFFER_OK;
}
diff --git a/src/parser.c b/src/parser.c
index 875c0bc..6a1c796 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -7,17 +7,12 @@
static CHECK_RESULT int parser_parse_body(Parser *self, Ast *ast);
int parser_init(Parser *self) {
- buffer_init(&self->ast_objects);
return_if_error(scoped_allocator_init(&self->allocator));
+ return_if_error(buffer_init(&self->ast_objects, &self->allocator, sizeof(Ast) * 8));
return PARSER_OK;
}
void parser_deinit(Parser *self) {
- usize i;
- for(i = 0; i < self->ast_objects.size / sizeof(Ast); ++i) {
- ast_deinit((Ast*)&self->ast_objects.data[i]);
- }
- buffer_deinit(&self->ast_objects);
scoped_allocator_deinit(&self->allocator);
}
@@ -62,7 +57,7 @@ static CHECK_RESULT int parser_parse_function_decl(Parser *self, FunctionDecl **
return_if_error(tokenizer_accept(&self->tokenizer, TOK_OPEN_BRACE));
return_if_error(scoped_allocator_alloc(&self->allocator, sizeof(FunctionDecl), (void**)func_decl));
- funcdecl_init(*func_decl);
+ cleanup_if_error(funcdecl_init(*func_decl, &self->allocator));
for(;;) {
Ast body_obj;
diff --git a/src/scoped_allocator.c b/src/scoped_allocator.c
index ea99774..bd14206 100644
--- a/src/scoped_allocator.c
+++ b/src/scoped_allocator.c
@@ -2,7 +2,11 @@
#include "../include/alloc.h"
#include <assert.h>
+<<<<<<< HEAD
#define ALLOC_NODE_SIZE 4096
+=======
+#define ALLOC_NODE_SIZE 65536
+>>>>>>> Fixed CHECK_RESULT macro, use scoped allocator
int scoped_allocator_node_init(ScopedAllocatorNode *self) {
self->data = NULL;