diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-02-24 17:53:46 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | a27a3e6ae09e8396584c95a3567b145e86d8bc40 (patch) | |
tree | 6b56721fb64f31e4b731e75942f81838d10b0612 | |
parent | a307f17f44b461f58441926fcbf87883f17ebe61 (diff) |
Fixed CHECK_RESULT macro, use scoped allocator
Scoped allocator gives us better performance and cleanup code for error
cases is much cleaner
-rw-r--r-- | include/ast.h | 5 | ||||
-rw-r--r-- | include/buffer.h | 7 | ||||
-rw-r--r-- | src/ast.c | 17 | ||||
-rw-r--r-- | src/buffer.c | 28 | ||||
-rw-r--r-- | src/parser.c | 9 | ||||
-rw-r--r-- | src/scoped_allocator.c | 4 |
6 files changed, 26 insertions, 44 deletions
diff --git a/include/ast.h b/include/ast.h index b14c26d..ef06257 100644 --- a/include/ast.h +++ b/include/ast.h @@ -43,15 +43,12 @@ struct LhsExpr { }; Ast ast_none(); -void ast_deinit(Ast *ast); -void funcdecl_init(FunctionDecl *self); -void funcdecl_deinit(FunctionDecl *self); +CHECK_RESULT int funcdecl_init(FunctionDecl *self, ScopedAllocator *allocator); CHECK_RESULT int funcdecl_add_to_body(FunctionDecl *self, Ast ast); void funccall_init(FunctionCall *self, BufferView name); void lhsexpr_init(LhsExpr *self, int isConst, BufferView var_name); -void lhsexpr_deinit(LhsExpr *self); #endif diff --git a/include/buffer.h b/include/buffer.h index c45fbde..700ae7b 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -1,20 +1,19 @@ #ifndef AMALGAM_BUFFER_H #define AMALGAM_BUFFER_H -#include "types.h" -#include "misc.h" +#include "scoped_allocator.h" #define BUFFER_OK 0 #define BUFFER_ALLOC_FAIL -1 typedef struct { + ScopedAllocator *allocator; char* data; usize size; usize capacity; } Buffer; -void buffer_init(Buffer *self); -void buffer_deinit(Buffer *self); +CHECK_RESULT int buffer_init(Buffer *self, ScopedAllocator *allocator, usize initial_capacity); CHECK_RESULT int buffer_append(Buffer *self, void *data, usize size); void* buffer_get(Buffer *self, usize index, usize type_size); @@ -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; |