changed: better memory management
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 21 Apr 2023 04:17:34 +0000 (23:17 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 21 Apr 2023 04:17:34 +0000 (23:17 -0500)
compiler/src/onyx.c
compiler/src/parser.c
compiler/src/polymorph.h
compiler/src/wasm_emit.c
shared/include/bh.h

index 28c79a5d8baa15b2535cc82c4d5e07f52beb0faf..6996d6a0cbe37bdb59a35033cbe642516b6b27fd 100644 (file)
@@ -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;
 }
index e456b8a7e96b4b0c41ec37ed64dc1c21ca964841..62559ee02037c2a36d783e55b4fdf7d7a336cebe 100644 (file)
@@ -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) {
index a79c9373199eafd6429bad091c23e17386b60a8f..de83db133ba682099d269ae2768ef302b48ece28 100644 (file)
@@ -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;
index 445b9f2f6b36c068bc3c303f8bdd6829be47f8e3..1f90205e0aa200ed9c0944100cac7db00b5507e3 100644 (file)
@@ -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);
index a519f5a67025e48894e83392b5011cf317a6d684..71e168dbdf721cab25612974584c9b22d2fef7aa 100644 (file)
     #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);
         }
     }