From fb1a6063a7c0878c801d5c29f2a399fdadbe76e2 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 20 Mar 2024 01:23:50 -0400 Subject: [PATCH] fixed hot reload there is still memory issues T-T --- code/api.odin | 19 +++++++++++-------- code/grime_pool_allocator.odin | 4 ++-- code/grime_slab_allocator.odin | 10 ++++++---- code/grime_string_interning.odin | 4 +++- code/host/host.odin | 9 ++++----- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/code/api.odin b/code/api.odin index a3c9b4b..8dc8fd8 100644 --- a/code/api.odin +++ b/code/api.odin @@ -15,6 +15,10 @@ import rl "vendor:raylib" Path_Assets :: "../assets/" Path_Input_Replay :: "scratch.sectr_replay" +Persistent_Slab_DBG_Name :: "Peristent Slab" +Frame_Slab_DBG_Name :: "Frame Slab" +Transient_Slab_DBG_Name :: "Transient Slab" + ModuleAPI :: struct { lib : dynlib.Library, write_time : FileTime, @@ -103,10 +107,10 @@ startup :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem } alloc_error : AllocatorError - persistent_slab, alloc_error = slab_init( policy_ptr, allocator = persistent_allocator(), dbg_name = "persistent slab" ) + persistent_slab, alloc_error = slab_init( policy_ptr, allocator = persistent_allocator(), dbg_name = Persistent_Slab_DBG_Name ) verify( alloc_error == .None, "Failed to allocate the persistent slab" ) - transient_slab, alloc_error = slab_init( & default_slab_policy, allocator = transient_allocator(), dbg_name = "transient slab" ) + transient_slab, alloc_error = slab_init( & default_slab_policy, allocator = transient_allocator(), dbg_name = Transient_Slab_DBG_Name ) verify( alloc_error == .None, "Failed to allocate transient slab" ) transient_clear_time = 120 // Seconds, 2 Minutes @@ -269,7 +273,6 @@ reload :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem, transient = transient_mem files_buffer = files_buffer_mem - context.allocator = persistent_allocator() context.temp_allocator = transient_allocator() @@ -280,6 +283,8 @@ reload :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem, // Or as done below, correct containers using allocators on reload. // Thankfully persistent dynamic allocations are rare, and thus we know exactly which ones they are. + slab_reload( persistent_slab, persistent_allocator() ) + font_provider_data.font_cache.hashes.backing = persistent_slab_allocator() font_provider_data.font_cache.entries.backing = persistent_slab_allocator() @@ -287,7 +292,7 @@ reload :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem, string_cache.table.hashes.backing = persistent_slab_allocator() string_cache.table.entries.backing = persistent_slab_allocator() - // slab_reload( frame_slab, frame_allocator()) + slab_reload( frame_slab, frame_allocator()) slab_reload( transient_slab, transient_allocator()) ui_reload( & get_state().project.workspace.ui, cache_allocator = persistent_slab_allocator() ) @@ -295,8 +300,6 @@ reload :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem, log("Module reloaded") } - - @export tick :: proc( host_delta_time : f64, host_delta_ns : Duration ) -> b32 { @@ -313,7 +316,7 @@ tick :: proc( host_delta_time : f64, host_delta_ns : Duration ) -> b32 // Setup Frame Slab { alloc_error : AllocatorError - frame_slab, alloc_error = slab_init( & default_slab_policy, bucket_reserve_num = 0, allocator = frame_allocator(), dbg_name = "frame slab" ) + frame_slab, alloc_error = slab_init( & default_slab_policy, bucket_reserve_num = 0, allocator = frame_allocator(), dbg_name = Frame_Slab_DBG_Name ) verify( alloc_error == .None, "Failed to allocate frame slab" ) } @@ -394,7 +397,7 @@ clean_frame :: proc() free_all( transient_allocator() ) alloc_error : AllocatorError - transient_slab, alloc_error = slab_init( & default_slab_policy, allocator = transient_allocator(), dbg_name = "transient slab" ) + transient_slab, alloc_error = slab_init( & default_slab_policy, allocator = transient_allocator(), dbg_name = Transient_Slab_DBG_Name ) verify( alloc_error == .None, "Failed to allocate transient slab" ) } } diff --git a/code/grime_pool_allocator.odin b/code/grime_pool_allocator.odin index cc796bb..fb88609 100644 --- a/code/grime_pool_allocator.odin +++ b/code/grime_pool_allocator.odin @@ -71,8 +71,8 @@ pool_init :: proc ( return } -pool_reload :: proc( using self : Pool, allocator : Allocator ) { - self.backing = allocator +pool_reload :: proc( pool : Pool, allocator : Allocator ) { + pool.backing = allocator } pool_destroy :: proc ( using self : Pool ) diff --git a/code/grime_slab_allocator.odin b/code/grime_slab_allocator.odin index 8d31508..fb89365 100644 --- a/code/grime_slab_allocator.odin +++ b/code/grime_slab_allocator.odin @@ -86,11 +86,13 @@ slab_init_pools :: proc ( using self : Slab, policy : ^SlabPolicy, bucket_reserv return .None } -slab_reload :: proc ( using self : Slab, allocator : Allocator ) +slab_reload :: proc ( slab : Slab, allocator : Allocator ) { - for id in 0 ..< pools.idx { - pool := pools.items[id] - pool_reload( pool, allocator ) + slab.backing = allocator + + for id in 0 ..< slab.pools.idx { + pool := slab.pools.items[id] + pool_reload( pool, slab.backing ) } } diff --git a/code/grime_string_interning.odin b/code/grime_string_interning.odin index ef6ef1e..ef593db 100644 --- a/code/grime_string_interning.odin +++ b/code/grime_string_interning.odin @@ -52,8 +52,10 @@ str_cache_init :: proc( /*allocator : Allocator*/ ) -> ( cache : StringCache ) { header_size :: size_of( Slab ) + @static dbg_name := "StringCache slab" + alloc_error : AllocatorError - cache.slab, alloc_error = slab_init( & policy, allocator = persistent_allocator(), dbg_name = "StringCache slab" ) + cache.slab, alloc_error = slab_init( & policy, allocator = persistent_allocator(), dbg_name = dbg_name ) verify(alloc_error == .None, "Failed to initialize the string cache" ) cache.table, alloc_error = zpl_hmap_init_reserve( StringCached, persistent_slab_allocator(), 64 * Kilobyte ) diff --git a/code/host/host.odin b/code/host/host.odin index 1f86e27..840f50a 100644 --- a/code/host/host.odin +++ b/code/host/host.odin @@ -243,7 +243,6 @@ sync_sectr_api :: proc( sectr_api : ^sectr.ModuleAPI, memory : ^ClientMemory, lo } } - fmt_backing : [16 * Kilobyte] u8 persistent_backing : [2 * Megabyte] byte @@ -326,16 +325,16 @@ main :: proc() { spall.SCOPED_EVENT( & profiler.ctx, & profiler.buffer, "Host Tick" ) - // Hot-Reload - sync_sectr_api( & sectr_api, & memory, & logger, & profiler ) - running = sectr_api.tick( duration_seconds( delta_ns ), delta_ns ) sectr_api.clean_frame() delta_ns = time.tick_lap_time( & host_tick ) host_tick = time.tick_now() - // free_all( arena_allocator( & state.transient)) + free_all( arena_allocator( & state.transient)) + + // Hot-Reload + sync_sectr_api( & sectr_api, & memory, & logger, & profiler ) } // Determine how the run_cyle completed, if it failed due to an error,