From 8ad8e72b3ae730d31aa1ad2a7c081c2792895152 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 9 Oct 2022 10:45:36 +0200 Subject: [PATCH] Getting rid of heap stuff completely but also naively --- base.cpp | 63 ----------------------------------------------- core_compiler.cpp | 20 ++++++--------- core_compiler.h | 1 - core_main.cpp | 6 ++++- os_windows.cpp | 53 --------------------------------------- 5 files changed, 13 insertions(+), 130 deletions(-) diff --git a/base.cpp b/base.cpp index 9086e5d..dbdb6bf 100644 --- a/base.cpp +++ b/base.cpp @@ -588,30 +588,6 @@ arena_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size return 0; } -force_inline void * -personal_arena_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size_t size){ - Arena *arena = (Arena *)a; - arena->alignment = 1; - - void *result = 0; - switch(kind){ - case Allocation_Resize: { - assert(arena->old_size); - assert(arena->old_size < size); - assert(arena->debug_prev_pointer == old_pointer); - result = arena_push_size(arena, size - arena->old_size); - result = old_pointer; - } break; - default: { - result = arena_allocator_proc(a, kind, old_pointer, size); - arena->debug_prev_pointer = result; - } - } - - arena->old_size = size; - return result; -} - function void arena_init(Arena *a, String debug_name){ a->memory = os_reserve(default_reserve_size); @@ -621,15 +597,6 @@ arena_init(Arena *a, String debug_name){ if(!a->proc) a->proc = arena_allocator_proc; } -function Arena -arena_make_personal(String debug_name){ - Arena arena = {}; - arena.proc = personal_arena_allocator_proc; - arena_init(&arena, debug_name); - arena.kind = Allocator_PersonalArena; - return arena; -} - enum Log_Kind{Log_Kind_Normal, Log_Kind_Error, Log_Kind_Trace}; typedef void Log_Proc(Log_Kind kind, String string, char *file, int line); //----------------------------------------------------------------------------- @@ -892,35 +859,6 @@ array_make(Allocator *a, S64 size = 16){ return result; } -function void -test_array(){ - Scratch scratch; - Array array = {scratch}; - - int size = 1000; - for(int i = 0; i < size; i++){ - array.add(i); - } - S32 i = 0; - For(array){ - assert(it == i++); - } - - Arena arena = arena_make_personal("Test personal arena"_s); - Array array2 = {&arena}; - for(int i = 0; i < size; i++){ - array2.add(i); - } - i=0; - For(array2){ - assert(it == i++); - } - exp_destroy(&arena); - assert(arena.memory.data == 0); - assert(thread_ctx.scratch->memory.data != 0); -} - - #include "base_string.cpp" //----------------------------------------------------------------------------- // Logging @@ -1042,7 +980,6 @@ function Map_Key_Value * map_base_get(Map *map, U64 key){ if(map->len == 0) return 0; assert(key); - // if(key == 0) key+=1; U64 hash = hash_u64(key); U64 index = wrap_around_pow2(hash, map->cap); diff --git a/core_compiler.cpp b/core_compiler.cpp index c4f2e66..9e7174c 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -2,8 +2,8 @@ function void lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){ l->arena = token_string_arena; - l->tokens = array_make(token_string_arena, 1024*2); - l->interns= intern_table_make(token_string_arena, map_allocator, 1024); + l->tokens = array_make(token_string_arena, 4096*4); + l->interns= intern_table_make(token_string_arena, map_allocator, 2048); /*#import meta for i in meta.keywords: @@ -71,18 +71,17 @@ op_info_table[19].op = l->intern("!"_s); } function void -parse_init(Parse_Ctx *ctx, Arena *perm_allocator, Allocator *heap_allocator){ +parse_init(Parse_Ctx *ctx, Arena *perm_allocator){ pctx = ctx; ctx->perm = perm_allocator; - ctx->heap = heap_allocator; - ctx->type_map = {ctx->heap}; + ctx->type_map = map_make(ctx->perm, 2048); ctx->gen = {ctx->perm}; ctx->helper_builder= {ctx->perm}; - ctx->scope_ids = 1; + ctx->scope_ids = 1; bigint_allocator = ctx->perm; arena_init(&ctx->stage_arena, "Compiler stage arena"_s); - lex_init(ctx->perm, ctx->heap, ctx); + lex_init(ctx->perm, ctx->perm, ctx); init_type(); // Init paths @@ -97,18 +96,15 @@ parse_init(Parse_Ctx *ctx, Arena *perm_allocator, Allocator *heap_allocator){ function void begin_compilation(){ init_ctx_time_begin = os_time(); - OS_Heap *heap = exp_alloc_type(&pernament_arena, OS_Heap, AF_ZeroMemory); - *heap = win32_os_heap_create(false, mib(4), 0, "Compiler heap"_s); Parse_Ctx *ctx = exp_alloc_type(&pernament_arena, Parse_Ctx, AF_ZeroMemory); - parse_init(ctx, &pernament_arena, heap); + parse_init(ctx, &pernament_arena); init_ctx_time_end = os_time(); } function void destroy_compiler(){ - exp_destroy(pctx->heap); exp_free_all(pctx->perm); - exp_destroy(&pctx->stage_arena); + exp_free_all(&pctx->stage_arena); } function void diff --git a/core_compiler.h b/core_compiler.h index b3b8337..f44c0cb 100644 --- a/core_compiler.h +++ b/core_compiler.h @@ -155,7 +155,6 @@ struct Lexer{ struct Parse_Ctx:Lexer{ Arena *perm; // Stores: AST, tokens, interns - Allocator *heap; Arena stage_arena; List all_types; diff --git a/core_main.cpp b/core_main.cpp index 89b46e7..202e5ba 100644 --- a/core_main.cpp +++ b/core_main.cpp @@ -10,6 +10,11 @@ Current: - [ ] Imports are leaking names ! Multimedia leaks windows stuff - [ ] Test and bulletproof any, slices + +Memory: +- [ ] Redesign Type map to use List and reduce wasting space +- [ ] Redesign lexing to minimize memory usage, we got rid of heap but in a naive way! + In the future - [ ] Cleanup @@ -270,7 +275,6 @@ int main(int argument_count, char **arguments){ test_unicode(); map_test(); - test_array(); test_string_builder(); test_intern_table(); diff --git a/os_windows.cpp b/os_windows.cpp index 72ba939..8cb2caa 100644 --- a/os_windows.cpp +++ b/os_windows.cpp @@ -248,56 +248,3 @@ os_list_dir(Allocator *a, String dir, U32 flags = LIST_NO_FLAGS){ return result; } - -//----------------------------------------------------------------------------- -// OS Heap allocator -//----------------------------------------------------------------------------- -struct OS_Heap:Allocator{ - void *handle; -}; - -function void * -os_heap_allocator_proc(Allocator *a, Allocation_Kind kind, void *old_pointer, size_t size){ - OS_Heap *heap = (OS_Heap *)a; - switch(kind){ - case Allocation_FreeAll:{ - invalid_codepath; - return 0; - } - case Allocation_Destroy:{ - BOOL result = HeapDestroy(heap->handle); - assert(result != 0); - heap->handle = 0; - heap->proc = 0; - return 0; - } - case Allocation_Free:{ - BOOL result = HeapFree(heap->handle, 0, old_pointer); - assert(result != 0); - return 0; - } - case Allocation_Alloc:{ - void *result = HeapAlloc(heap->handle, 0, size); - assert(result); - return result; - } - case Allocation_Resize:{ - void *result = HeapReAlloc(heap->handle, 0, old_pointer, size); - assert(result); - return result; - } - default: invalid_codepath; - } - return 0; -} - -function OS_Heap // max_size == 0 == growing heap -win32_os_heap_create(B32 multithreaded, size_t initial_size, size_t max_size, String debug_name){ - OS_Heap result = {}; - result.debug_name = debug_name; - result.proc = os_heap_allocator_proc; - result.kind = Allocator_OSHeap; - result.handle = HeapCreate(multithreaded ? 0 : HEAP_NO_SERIALIZE, initial_size, max_size); - assert(result.handle); - return result; -} \ No newline at end of file