From a27a3e6ae09e8396584c95a3567b145e86d8bc40 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 24 Feb 2019 17:53:46 +0100 Subject: Fixed CHECK_RESULT macro, use scoped allocator Scoped allocator gives us better performance and cleanup code for error cases is much cleaner --- src/ast.c | 17 ++--------------- src/buffer.c | 28 ++++++++++++++-------------- src/parser.c | 9 ++------- src/scoped_allocator.c | 4 ++++ 4 files changed, 22 insertions(+), 36 deletions(-) (limited to 'src') 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 -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 +<<<<<<< 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; -- cgit v1.2.3