aboutsummaryrefslogtreecommitdiff
path: root/src/compiler.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler.c')
-rw-r--r--src/compiler.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/compiler.c b/src/compiler.c
index c10ccc9..7fd4426 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -180,22 +180,23 @@ typedef struct {
static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compiler, FileScopeReference *file_scope) {
Parser *parser;
- int result;
BufferView filepath;
ArenaAllocator *parser_allocator;
+ int result;
+
+ parser = NULL;
+ parser_allocator = NULL;
result = AMAL_COMPILER_ERR;
- cleanup_if_error(amal_mutex_lock(&compiler->mutex, "amal_compiler_load_in_this_thread, create arena allocator"));
- cleanup_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(ArenaAllocator), (void**)&parser_allocator));
- amal_mutex_tryunlock(&compiler->mutex);
- return_if_error(arena_allocator_init(parser_allocator));
+ return_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(ArenaAllocator), (void**)&parser_allocator));
+ cleanup_if_error(arena_allocator_init(parser_allocator));
filepath = create_buffer_view(file_scope->canonical_path.data, file_scope->canonical_path.size);
amal_log_info("Started parsing %.*s", filepath.size, filepath.data);
- return_if_error(arena_allocator_alloc(parser_allocator, sizeof(Parser), (void**)&parser));
- return_if_error(parser_init(parser, compiler, parser_allocator));
+ cleanup_if_error(arena_allocator_alloc(parser_allocator, sizeof(Parser), (void**)&parser));
+ cleanup_if_error(parser_init(parser, compiler, parser_allocator));
file_scope->parser = parser;
- return_if_error(parser_parse_file(parser, filepath));
+ cleanup_if_error(parser_parse_file(parser, filepath));
cleanup_if_error(amal_mutex_lock(&compiler->mutex, "amal_compiler_load_in_this_thread, add parser"));
parser->index = buffer_get_size(&compiler->parsers, Parser*);
cleanup_if_error(buffer_append(&compiler->parsers, &parser, sizeof(parser)));
@@ -204,6 +205,13 @@ static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compile
cleanup:
amal_mutex_tryunlock(&compiler->mutex);
+ /*
+ This also free's the parser, since it's allocated inside the allocator.
+ The allocator itself is allocated inside the compiler allocator so that will be free'd
+ when the compiler allocator is free'd
+ */
+ if(result != AMAL_COMPILER_OK)
+ arena_allocator_deinit(parser_allocator);
return result;
}