Renamed str_fmt_alloc to str_fmt, str_fmt to str_fmt_out, allocator convention enforcement in context

I'm enforcing aprintf it as the default formatter.
I changed up the context allocator assignment to reflect how I've been intending to allocation in startup & platform events vs tick.
Tick uses the frame slab's by default with transient on temp. Startup & platform events use the transient by default & for temp, with any use of the persistent allocator being explicit.
This commit is contained in:
Edward R. Gonzalez 2024-05-22 17:13:56 -04:00
parent 368abefccf
commit cf81d3f9bd
18 changed files with 56 additions and 51 deletions

View File

@ -265,7 +265,7 @@ ui_screen_settings_menu :: proc( captures : rawptr = nil ) -> ( should_raise : b
else
{
array_clear( value_str)
array_append( & value_str, to_runes(str_fmt_alloc("%v", config.engine_refresh_hz)))
array_append( & value_str, to_runes(str_fmt("%v", config.engine_refresh_hz)))
}
ui_parent(input_box)

View File

@ -52,7 +52,7 @@ startup :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem
transient = transient_mem
files_buffer = files_buffer_mem
context.allocator = persistent_allocator()
context.allocator = transient_allocator()
context.temp_allocator = transient_allocator()
// TODO(Ed) : Put on the transient allocator a slab allocator (transient slab)
}
@ -224,7 +224,7 @@ startup :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem
ui_startup( & workspace.ui, cache_allocator = persistent_slab_allocator() )
}
debug.path_lorem = str_fmt_alloc("C:/projects/SectrPrototype/examples/Lorem Ipsum.txt", allocator = persistent_slab_allocator())
debug.path_lorem = str_fmt("C:/projects/SectrPrototype/examples/Lorem Ipsum.txt", allocator = persistent_slab_allocator())
alloc_error : AllocatorError; success : bool
debug.lorem_content, success = os.read_entire_file( debug.path_lorem, persistent_slab_allocator() )
@ -393,7 +393,8 @@ tick :: proc( host_delta_time : f64, host_delta_ns : Duration ) -> b32
fps_avg = 1 / (frametime_avg_ms * MS_To_S)
if frametime_elapsed_ms > 60.0 {
log( str_fmt_tmp("Big tick! %v ms", frametime_elapsed_ms), LogLevel.Warning )
context.allocator = transient_allocator()
log( str_fmt("Big tick! %v ms", frametime_elapsed_ms), LogLevel.Warning )
}
}
return should_close

View File

@ -53,9 +53,10 @@ startup :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem
transient = transient_mem
files_buffer = files_buffer_mem
context.allocator = persistent_allocator()
// The policy for startup & any other persistent scopes is that the default allocator is transient.
// Any persistent allocations are explicitly specified.
context.allocator = transient_allocator()
context.temp_allocator = transient_allocator()
// TODO(Ed) : Put on the transient allocator a slab allocator (transient slab)
}
state := new( State, persistent_allocator() )
@ -197,7 +198,7 @@ startup :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem
// path_squidgy_slimes := strings.concatenate( { Path_Assets, "Squidgy Slimes.ttf" } )
// font_squidgy_slimes = font_load( path_squidgy_slimes, 24.0, "Squidgy_Slime" )
path_firacode := strings.concatenate( { Path_Assets, "FiraCode-Regular.ttf" }, transient_allocator() )
path_firacode := strings.concatenate( { Path_Assets, "FiraCode-Regular.ttf" } )
font_firacode = font_load( path_firacode, 24.0, "FiraCode" )
default_font = font_firacode
log( "Default font loaded" )
@ -245,7 +246,7 @@ startup :: proc( prof : ^SpallProfiler, persistent_mem, frame_mem, transient_mem
ui_startup( & workspace.ui, cache_allocator = persistent_slab_allocator() )
}
debug.path_lorem = str_fmt_alloc("C:/projects/SectrPrototype/examples/Lorem Ipsum.txt", allocator = persistent_slab_allocator())
debug.path_lorem = str_fmt("C:/projects/SectrPrototype/examples/Lorem Ipsum.txt", allocator = persistent_slab_allocator())
alloc_error : AllocatorError; success : bool
debug.lorem_content, success = os.read_entire_file( debug.path_lorem, persistent_slab_allocator() )
@ -367,6 +368,8 @@ tick_work_frame :: #force_inline proc()
verify( alloc_error == .None, "Failed to allocate frame slab" )
}
// The policy for the work tick is that the default allocator is the frame's slab.
// Transient's is the temp allocator.
context.allocator = frame_slab_allocator()
context.temp_allocator = transient_allocator()
@ -390,6 +393,8 @@ tick_work_frame :: #force_inline proc()
tick_frametime :: #force_inline proc( client_tick : ^time.Tick, host_delta_time_ms : f64, host_delta_ns : Duration )
{
state := get_state(); using state
context.allocator = frame_slab_allocator()
context.temp_allocator = transient_allocator()
// profile("Client tick timing processing")
// config.engine_refresh_hz = uint(monitor_refresh_hz)
@ -433,7 +438,7 @@ tick_frametime :: #force_inline proc( client_tick : ^time.Tick, host_delta_time_
fps_avg = 1 / (frametime_avg_ms * MS_To_S)
if frametime_elapsed_ms > 60.0 {
log( str_fmt_tmp("Big tick! %v ms", frametime_elapsed_ms), LogLevel.Warning )
log( str_fmt("Big tick! %v ms", frametime_elapsed_ms), LogLevel.Warning )
}
profile_begin("sokol_app: post_client_tick")

View File

@ -245,7 +245,7 @@ render_mode_screenspace :: proc ()
if debug.debug_text_vis
{
fps_msg := str_fmt_tmp( "FPS: %f", fps_avg)
fps_msg := str_fmt( "FPS: %f", fps_avg)
fps_msg_width := measure_text_size( fps_msg, default_font, 12.0, 0.0 ).x
fps_msg_pos := screen_get_corners().top_right - { fps_msg_width, 0 } - { 5, 5 }
debug_draw_text( fps_msg, fps_msg_pos, 12.0, color = rl.GREEN )

View File

@ -97,11 +97,11 @@ font_load :: proc( path_file : string,
) -> FontID
{
profile(#procedure)
log( str_fmt_tmp("Loading font: %v", path_file))
log( str_fmt("Loading font: %v", path_file))
font_provider_data := & get_state().font_provider_data; using font_provider_data
font_data, read_succeded : = os.read_entire_file( path_file, context.temp_allocator )
verify( b32(read_succeded), str_fmt_tmp("Failed to read font file for: %v", path_file) )
font_data, read_succeded : = os.read_entire_file( path_file )
verify( b32(read_succeded), str_fmt("Failed to read font file for: %v", path_file) )
font_data_size := cast(i32) len(font_data)
desired_id := desired_id
@ -145,7 +145,7 @@ font_load :: proc( path_file : string,
codepoints = nil,
codepointCount = count,
type = rl.FontType.DEFAULT )
verify( glyphs != nil, str_fmt_tmp("Failed to load glyphs for font: %v at desired size: %v", desired_id, size ) )
verify( glyphs != nil, str_fmt("Failed to load glyphs for font: %v at desired size: %v", desired_id, size ) )
atlas := rl.GenImageFontAtlas( glyphs, & recs, count, size, padding, i32(Font_Atlas_Packing_Method.Raylib_Basic) )
texture = rl.LoadTextureFromImage( atlas )
@ -164,7 +164,6 @@ font_load :: proc( path_file : string,
rl.UnloadImage( atlas )
}
free_all( context.temp_allocator )
return { key, desired_id }
}

View File

@ -27,9 +27,9 @@ import "core:hash"
import "core:hash/xxhash"
xxh32 :: xxhash.XXH32
import fmt_io "core:fmt"
str_fmt :: fmt_io.printf
str_fmt_out :: fmt_io.printf
str_fmt_tmp :: fmt_io.tprintf
str_fmt_alloc :: fmt_io.aprintf
str_fmt :: fmt_io.aprintf // Decided to make aprintf the default. (It will always be the default allocator)
str_fmt_builder :: fmt_io.sbprintf
str_fmt_buffer :: fmt_io.bprintf
str_to_file_ln :: fmt_io.fprintln

View File

@ -73,7 +73,7 @@ hmap_chained_init :: proc( $Type : typeid, lookup_capacity : uint, allocator : A
bucket_reserve_num = pool_bucket_reserve_num,
alignment = pool_alignment,
allocator = allocator,
dbg_name = str_intern(str_fmt_tmp("%v: pool", dbg_name)).str
dbg_name = str_intern(str_fmt("%v: pool", dbg_name)).str
)
data := transmute([^] ^HMapChainedSlot(Type)) (transmute( [^]HMapChained(Type)) table.header)[1:]
table.lookup = slice_ptr( data, int(lookup_capacity) )

View File

@ -51,7 +51,7 @@ zpl_hmap_init :: proc( $ Type : typeid, allocator : Allocator ) -> ( HMapZPL( Ty
zpl_hmap_init_reserve :: proc
( $ Type : typeid, allocator : Allocator, num : u64, dbg_name : string = "" ) -> ( HMapZPL( Type), AllocatorError )
{
result : HMapZPL(Type)
result : HMapZPL(Type)
table_result, entries_result : AllocatorError
result.table, table_result = array_init_reserve( i64, allocator, num, dbg_name = dbg_name )

View File

@ -73,17 +73,17 @@ memtracker_register :: proc( tracker : ^MemoryTracker, new_entry : MemoryTracker
if (entry.end < new_entry.start)
{
msg := str_fmt_tmp("Memory tracker(%v) detected a collision:\nold_entry: %v\nnew_entry: %v", tracker.name, entry, new_entry)
msg := str_fmt("Memory tracker(%v) detected a collision:\nold_entry: %v\nnew_entry: %v", tracker.name, entry, new_entry)
ensure( false, msg )
memtracker_dump_entries(tracker ^)
}
array_append_at( & tracker.entries, new_entry, idx )
log(str_fmt_tmp("%v : Registered: %v", tracker.name, new_entry) )
log(str_fmt("%v : Registered: %v", tracker.name, new_entry) )
return
}
array_append( & tracker.entries, new_entry )
log(str_fmt_tmp("%v : Registered: %v", tracker.name, new_entry) )
log(str_fmt("%v : Registered: %v", tracker.name, new_entry) )
}
memtracker_register_auto_name :: proc( tracker : ^MemoryTracker, start, end : rawptr )
@ -119,17 +119,17 @@ memtracker_unregister :: proc( tracker : MemoryTracker, to_remove : MemoryTracke
entry := & entries[idx]
if entry.start == to_remove.start {
if (entry.end == to_remove.end || to_remove.end == nil) {
log(str_fmt_tmp("%v: Unregistered: %v", tracker.name, to_remove));
log(str_fmt("%v: Unregistered: %v", tracker.name, to_remove));
array_remove_at(tracker.entries, idx)
return
}
ensure(false, str_fmt_tmp("%v: Found an entry with the same start address but end address was different:\nentry : %v\nto_remove: %v", tracker.name, entry, to_remove))
ensure(false, str_fmt("%v: Found an entry with the same start address but end address was different:\nentry : %v\nto_remove: %v", tracker.name, entry, to_remove))
memtracker_dump_entries(tracker)
}
}
ensure(false, str_fmt_tmp("%v: Attempted to unregister an entry that was not tracked: %v", tracker.name, to_remove))
ensure(false, str_fmt("%v: Attempted to unregister an entry that was not tracked: %v", tracker.name, to_remove))
memtracker_dump_entries(tracker)
}
@ -150,7 +150,7 @@ memtracker_check_for_collisions :: proc ( tracker : MemoryTracker )
collided := left.start > right.start || left.end > right.end
if collided {
msg := str_fmt_tmp("%v: Memory tracker detected a collision:\nleft: %v\nright: %v", tracker.name, left, right)
msg := str_fmt("%v: Memory tracker detected a collision:\nleft: %v\nright: %v", tracker.name, left, right)
memtracker_dump_entries(tracker)
}
}
@ -167,6 +167,6 @@ memtracker_dump_entries :: proc( tracker : MemoryTracker )
log( "Dumping Memory Tracker:")
for idx in 0 ..< tracker.entries.num {
entry := & tracker.entries.data[idx]
log( str_fmt_tmp("%v", entry) )
log( str_fmt("%v", entry) )
}
}

View File

@ -172,7 +172,7 @@ pool_grab :: proc( pool : Pool, zero_memory := false ) -> ( block : []byte, allo
pool := pool
if pool.current_bucket != nil {
if ( pool.current_bucket.blocks == nil ) {
ensure( false, str_fmt_tmp("(corruption) current_bucket was wiped %p", pool.current_bucket) )
ensure( false, str_fmt("(corruption) current_bucket was wiped %p", pool.current_bucket) )
}
// verify( pool.current_bucket.blocks != nil, str_fmt_tmp("(corruption) current_bucket was wiped %p", pool.current_bucket) )
}
@ -313,17 +313,17 @@ pool_validate :: proc( pool : Pool )
bucket : ^PoolBucket = pool.bucket_list.first
if bucket != nil && uintptr(bucket) < 0x10000000000 {
ensure(false, str_fmt_tmp("Found a corrupted bucket %p", bucket ))
ensure(false, str_fmt("Found a corrupted bucket %p", bucket ))
}
// Compiler bug ^^ same as pool_reset
for ; bucket != nil; bucket = bucket.next
{
if bucket != nil && uintptr(bucket) < 0x10000000000 {
ensure(false, str_fmt_tmp("Found a corrupted bucket %p", bucket ))
ensure(false, str_fmt("Found a corrupted bucket %p", bucket ))
}
if ( bucket.blocks == nil ) {
ensure(false, str_fmt_tmp("Found a corrupted bucket %p", bucket ))
ensure(false, str_fmt("Found a corrupted bucket %p", bucket ))
}
}
}
@ -346,7 +346,7 @@ pool_validate_ownership :: proc( using self : Pool, block : [] byte ) -> b32
misalignment := (block_address - start) % uintptr(block_size)
if misalignment != 0 {
ensure(false, "pool_validate_ownership: This data is within this pool's buckets, however its not aligned to the start of a block")
log(str_fmt_tmp("Block address: %p Misalignment: %p closest: %p",
log(str_fmt("Block address: %p Misalignment: %p closest: %p",
transmute(rawptr)block_address,
transmute(rawptr)misalignment,
rawptr(block_address - misalignment)))

View File

@ -84,7 +84,7 @@ slab_init_pools :: proc ( using self : Slab, policy : ^SlabPolicy, bucket_reserv
for id in 0 ..< policy.idx {
using size_class := policy.items[id]
pool_dbg_name := str_fmt_alloc("%v pool[%v]", dbg_name, block_size, allocator = backing)
pool_dbg_name := str_fmt("%v pool[%v]", dbg_name, block_size, allocator = backing)
pool, alloc_error := pool_init( should_zero_buckets, block_size, bucket_capacity, bucket_reserve_num, block_alignment, backing, pool_dbg_name )
if alloc_error != .None do return alloc_error

View File

@ -113,7 +113,7 @@ str_intern :: proc( content : string ) -> StrRunesPair
}
str_intern_fmt :: #force_inline proc( format : string, args : ..any, allocator := context.allocator ) -> StrRunesPair {
return str_intern(str_fmt_alloc(format, args, allocator = allocator))
return str_intern(str_fmt(format, args, allocator = allocator))
}
// runes_intern :: proc( content : []rune ) -> StrRunesPair

View File

@ -20,7 +20,7 @@ thread__highres_wait :: proc( desired_ms : f64, loc := #caller_location ) -> b32
timer := win32.CreateWaitableTimerExW( nil, nil, win32.CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, win32.TIMER_ALL_ACCESS )
if timer == nil {
msg := str_fmt_tmp("Failed to create win32 timer - ErrorCode: %v", win32.GetLastError() )
msg := str_fmt("Failed to create win32 timer - ErrorCode: %v", win32.GetLastError() )
log( msg, LogLevel.Warning, loc)
return false
}
@ -28,7 +28,7 @@ thread__highres_wait :: proc( desired_ms : f64, loc := #caller_location ) -> b32
due_time := win32.LARGE_INTEGER(desired_ms * MS_To_NS)
result := win32.SetWaitableTimerEx( timer, & due_time, 0, nil, nil, nil, 0 )
if ! result {
msg := str_fmt_tmp("Failed to set win32 timer - ErrorCode: %v", win32.GetLastError() )
msg := str_fmt("Failed to set win32 timer - ErrorCode: %v", win32.GetLastError() )
log( msg, LogLevel.Warning, loc)
return false
}
@ -43,22 +43,22 @@ thread__highres_wait :: proc( desired_ms : f64, loc := #caller_location ) -> b32
switch wait_result
{
case WAIT_ABANDONED:
msg := str_fmt_tmp("Failed to wait for win32 timer - Error: WAIT_ABANDONED" )
msg := str_fmt("Failed to wait for win32 timer - Error: WAIT_ABANDONED" )
log( msg, LogLevel.Error, loc)
return false
case WAIT_IO_COMPLETION:
msg := str_fmt_tmp("Waited for win32 timer: Ended by APC queued to the thread" )
msg := str_fmt("Waited for win32 timer: Ended by APC queued to the thread" )
log( msg, LogLevel.Error, loc)
return false
case WAIT_OBJECT_0:
msg := str_fmt_tmp("Waited for win32 timer- Reason : WAIT_OBJECT_0" )
msg := str_fmt("Waited for win32 timer- Reason : WAIT_OBJECT_0" )
log( msg, loc = loc)
return false
case WAIT_FAILED:
msg := str_fmt_tmp("Waited for win32 timer failed - ErrorCode: $v", win32.GetLastError() )
msg := str_fmt("Waited for win32 timer failed - ErrorCode: $v", win32.GetLastError() )
log( msg, LogLevel.Error, loc)
return false
}

View File

@ -120,7 +120,7 @@ PWS_LexerData :: struct {
pws_parser_lex :: proc ( text : string, allocator : Allocator ) -> ( PWS_LexResult, AllocatorError )
{
bytes := transmute([]byte) text
log( str_fmt_tmp( "lexing: %v ...", (len(text) > 30 ? transmute(string) bytes[ :30] : text) ))
log( str_fmt( "lexing: %v ...", (len(text) > 30 ? transmute(string) bytes[ :30] : text) ))
profile(#procedure)
using lexer : PWS_LexerData
@ -256,7 +256,7 @@ pws_parser_parse :: proc( text : string, allocator : Allocator ) -> ( PWS_ParseR
tokens = lex.tokens
log( str_fmt_tmp( "parsing: %v ...", (len(text) > 30 ? transmute(string) bytes[ :30] : text) ))
log( str_fmt( "parsing: %v ...", (len(text) > 30 ? transmute(string) bytes[ :30] : text) ))
// TODO(Ed): Change this to use a node pool
nodes, alloc_error = array_init_reserve( PWS_AST, allocator, PWS_NodeArray_ReserveSize )

View File

@ -180,7 +180,7 @@ ui_graph_build_begin :: proc( ui : ^ UI_State, bounds : Vec2 = {} )
}
ui.built_box_count = 0
root = ui_box_make( {}, str_intern(str_fmt_tmp("%s: root#001", ui == & state.screen_ui ? "Screen" : "Workspace" )).str)
root = ui_box_make( {}, str_intern(str_fmt("%s: root#001", ui == & state.screen_ui ? "Screen" : "Workspace" )).str)
if ui == & state.screen_ui {
root.layout.size = range2(Vec2(state.app_window.extent) * 2, {})
}

View File

@ -21,7 +21,7 @@ ui_floating_startup :: proc( self : ^UI_FloatingManager, allocator : Allocator,
{
error : AllocatorError
queue_dbg_name := str_intern(str_fmt_tmp("%s: build_queue", dbg_name))
queue_dbg_name := str_intern(str_fmt("%s: build_queue", dbg_name))
self.build_queue, error = array_init_reserve( UI_Floating, allocator, build_queue_cap, dbg_name = queue_dbg_name.str )
if error != AllocatorError.None
{
@ -29,7 +29,7 @@ ui_floating_startup :: proc( self : ^UI_FloatingManager, allocator : Allocator,
return error
}
tracked_dbg_name := str_intern(str_fmt_tmp("%s: tracked", dbg_name))
tracked_dbg_name := str_intern(str_fmt("%s: tracked", dbg_name))
self.tracked, error = hmap_chained_init(UI_Floating, uint(tracked_cap), allocator, dbg_name = tracked_dbg_name.str )
if error != AllocatorError.None
{

View File

@ -61,7 +61,7 @@ test_draggable :: proc()
draggable.layout.pos = debug.draggable_box_pos
draggable.layout.size.min = debug.draggable_box_size
draggable.text = { str_fmt_alloc("%v", debug.draggable_box_pos), {} }
draggable.text = { str_fmt("%v", debug.draggable_box_pos), {} }
draggable.text.runes = to_runes(draggable.text.str)
}
@ -203,7 +203,7 @@ test_whitespace_ast :: proc( default_layout : ^UI_Layout, frame_style_default :
}
ui_layout( text_layout )
line_hbox := ui_widget(str_fmt_alloc( "line %v", line_id ), {.Mouse_Clickable})
line_hbox := ui_widget(str_fmt( "line %v", line_id ), {.Mouse_Clickable})
if line_hbox.key == ui.hot
{
@ -228,14 +228,14 @@ test_whitespace_ast :: proc( default_layout : ^UI_Layout, frame_style_default :
#partial switch head.type
{
case .Visible:
label := str_intern( str_fmt_alloc( "%v %v", head.content.str, label_id ))
label := str_intern( str_fmt( "%v %v", head.content.str, label_id ))
widget = ui_text( label.str, head.content )
label_id += 1
chunk_layout.pos.x += size_range2( widget.computed.bounds ).x
case .Spaces:
label := str_intern( str_fmt_alloc( "%v %v", "space", label_id ))
label := str_intern( str_fmt( "%v %v", "space", label_id ))
widget = ui_text_spaces( label.str )
label_id += 1
@ -247,7 +247,7 @@ test_whitespace_ast :: proc( default_layout : ^UI_Layout, frame_style_default :
chunk_layout.pos.x += size_range2( widget.computed.bounds ).x
case .Tabs:
label := str_intern( str_fmt_alloc( "%v %v", "tab", label_id ))
label := str_intern( str_fmt( "%v %v", "tab", label_id ))
widget = ui_text_tabs( label.str )
label_id += 1

View File

@ -307,7 +307,7 @@ ui_resizable_handles :: proc( parent : ^UI_Widget, pos : ^Vec2, size : ^Vec2,
name :: proc( label : string ) -> string {
parent_label := (transmute(^string) context.user_ptr) ^
return str_intern(str_fmt_alloc("%v: %v", parent_label, label )).str
return str_intern(str_fmt("%v: %v", parent_label, label )).str
}
context.user_ptr = & parent.label