From 07bdd3fbdb7d1fcbda5c148cb8428da5f5e9de97 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 20 Apr 2023 23:17:34 -0500 Subject: [PATCH] changed: better memory management --- compiler/src/onyx.c | 16 ++++++++++++++-- compiler/src/parser.c | 4 +++- compiler/src/polymorph.h | 2 ++ compiler/src/wasm_emit.c | 1 + shared/include/bh.h | 15 ++++++++++++--- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/compiler/src/onyx.c b/compiler/src/onyx.c index 28c79a5d..6996d6a0 100644 --- a/compiler/src/onyx.c +++ b/compiler/src/onyx.c @@ -1,4 +1,12 @@ // #define BH_DEBUG + +extern struct bh_allocator global_heap_allocator; + +#define STBDS_REALLOC(_,p,s) (bh_resize(global_heap_allocator, p, s)) +#define STBDS_FREE(_,p) (bh_free(global_heap_allocator, p)) + +#define BH_INTERNAL_ALLOCATOR (global_heap_allocator) + #define BH_DEFINE #define BH_NO_TABLE #define STB_DS_IMPLEMENTATION @@ -898,6 +906,8 @@ static b32 onyx_run() { } #endif +bh_managed_heap mh; + int main(int argc, char *argv[]) { bh_scratch_init(&global_scratch, bh_heap_allocator(), 256 * 1024); // NOTE: 256 KiB @@ -907,7 +917,9 @@ int main(int argc, char *argv[]) { // were tracked and would be automatically freed at the end of execution. // I don't know why I ever did that because that is the job of the operating // system when a process exits. - global_heap_allocator = bh_heap_allocator(); + // global_heap_allocator = bh_heap_allocator(); + bh_managed_heap_init(&mh); + global_heap_allocator = bh_managed_heap_allocator(&mh); CompileOptions compile_opts = compile_opts_parse(global_heap_allocator, argc, argv); context_init(&compile_opts); @@ -971,7 +983,7 @@ int main(int argc, char *argv[]) { context_free(); bh_scratch_free(&global_scratch); - // bh_managed_heap_free(&global_heap); + bh_managed_heap_free(&mh); return compiler_progress != ONYX_COMPILER_PROGRESS_SUCCESS; } diff --git a/compiler/src/parser.c b/compiler/src/parser.c index e456b8a7..62559ee0 100644 --- a/compiler/src/parser.c +++ b/compiler/src/parser.c @@ -3,9 +3,9 @@ // such as procedure definitions, string literals, struct definitions // and declarations to be introduced into scopes. +#include "parser.h" #include "lex.h" #include "errors.h" -#include "parser.h" #include "utils.h" #define make_node(nclass, kind) onyx_ast_node_new(parser->allocator, sizeof(nclass), kind) @@ -3793,6 +3793,7 @@ OnyxParser onyx_parser_create(bh_allocator alloc, OnyxTokenizer *tokenizer) { bh_arr_new(global_heap_allocator, parser.current_symbol_stack, 4); bh_arr_new(global_heap_allocator, parser.scope_flags, 4); bh_arr_new(global_heap_allocator, parser.stored_tags, 4); + bh_arr_new(global_heap_allocator, parser.current_function_stack, 4); return parser; } @@ -3802,6 +3803,7 @@ void onyx_parser_free(OnyxParser* parser) { bh_arr_free(parser->current_symbol_stack); bh_arr_free(parser->scope_flags); bh_arr_free(parser->stored_tags); + bh_arr_free(parser->current_function_stack); } void onyx_parse(OnyxParser *parser) { diff --git a/compiler/src/polymorph.h b/compiler/src/polymorph.h index a79c9373..de83db13 100644 --- a/compiler/src/polymorph.h +++ b/compiler/src/polymorph.h @@ -991,6 +991,8 @@ b32 potentially_convert_function_to_polyproc(AstFunction *func) { if (bh_arr_length(auto_vars) == 0) return 0; + bh_arr_new(global_heap_allocator, func->poly_params, bh_arr_length(auto_vars)); + param_idx = 0; bh_arr_each(AutoPolymorphVariable, apv, auto_vars) { AstPolyParam pp; diff --git a/compiler/src/wasm_emit.c b/compiler/src/wasm_emit.c index 445b9f2f..1f90205e 100644 --- a/compiler/src/wasm_emit.c +++ b/compiler/src/wasm_emit.c @@ -4715,6 +4715,7 @@ OnyxWasmModule onyx_wasm_module_create(bh_allocator alloc) { bh_arr_new(alloc, module.elems, 4); bh_arr_new(alloc, module.libraries, 4); bh_arr_new(alloc, module.library_paths, 4); + bh_arr_new(alloc, module.for_remove_info, 4); bh_arr_new(global_heap_allocator, module.return_location_stack, 4); bh_arr_new(global_heap_allocator, module.structured_jump_target, 16); diff --git a/shared/include/bh.h b/shared/include/bh.h index a519f5a6..71e168db 100644 --- a/shared/include/bh.h +++ b/shared/include/bh.h @@ -11,6 +11,10 @@ #endif #endif +#ifndef BH_INTERNAL_ALLOCATOR + #define BH_INTERNAL_ALLOCATOR (bh_heap_allocator()) +#endif + // NOTE: For lseek64 #define _LARGEFILE64_SOURCE @@ -481,6 +485,11 @@ bh_dir bh_dir_open(char* path); b32 bh_dir_read(bh_dir dir, bh_dirent* out); void bh_dir_close(bh_dir dir); + + +b32 bh_wait_for_changes_in_directories(u32 count, char ** paths); + + #endif @@ -602,7 +611,7 @@ typedef struct bh__arr { #define bh_arr(T) T* #define bh__arrhead(arr) (((bh__arr *)(arr)) - 1) -#define bh_arr_allocator(arr) (arr ? bh__arrhead(arr)->allocator : bh_heap_allocator()) +#define bh_arr_allocator(arr) (arr ? bh__arrhead(arr)->allocator : BH_INTERNAL_ALLOCATOR) #define bh_arr_length(arr) (arr ? bh__arrhead(arr)->length : 0) #define bh_arr_capacity(arr) (arr ? bh__arrhead(arr)->capacity : 0) #define bh_arr_size(arr) (arr ? bh__arrhead(arr)->capacity * sizeof(*(arr)) : 0) @@ -1943,7 +1952,7 @@ char* bh_lookup_file(char* filename, char* relative_to, char *suffix, b32 add_su else bh_snprintf(path, 512, "%s%s", relative_to, fn + 2); - if (bh_file_exists(path)) return bh_path_get_full_name(path, bh_heap_allocator()); + if (bh_file_exists(path)) return bh_path_get_full_name(path, BH_INTERNAL_ALLOCATOR); return fn; } @@ -1955,7 +1964,7 @@ char* bh_lookup_file(char* filename, char* relative_to, char *suffix, b32 add_su else bh_snprintf(path, 512, "%s%s", *folder, fn); - if (bh_file_exists(path)) return bh_path_get_full_name(path, bh_heap_allocator()); + if (bh_file_exists(path)) return bh_path_get_full_name(path, BH_INTERNAL_ALLOCATOR); } } -- 2.25.1