From 838554460bbf0d591bc325359ad6d62011654a71 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 8 Aug 2022 15:25:11 +0100 Subject: [PATCH] Add basic "lock" around `Log_Allocator` to minimize errors with allocation logging loops --- core/log/log_allocator.odin | 93 ++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 43 deletions(-) diff --git a/core/log/log_allocator.odin b/core/log/log_allocator.odin index ea0ec37ba..eb7a6b377 100644 --- a/core/log/log_allocator.odin +++ b/core/log/log_allocator.odin @@ -6,12 +6,14 @@ Log_Allocator :: struct { allocator: runtime.Allocator, level: Level, prefix: string, + locked: bool, } log_allocator_init :: proc(la: ^Log_Allocator, level: Level, allocator := context.allocator, prefix := "") { la.allocator = allocator la.level = level la.prefix = prefix + la.locked = false } @@ -27,58 +29,63 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, runtime.Allocator_Error) { la := (^Log_Allocator)(allocator_data) - switch mode { - case .Alloc: - logf( - level=la.level, - fmt_str = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)", - args = {la.prefix, " " if la.prefix != "" else "", size, alignment}, - location = location, - ) - case .Free: - if old_size != 0 { + if !la.locked { + la.locked = true + defer la.locked = false + + switch mode { + case .Alloc: logf( level=la.level, - fmt_str = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)", - args = {la.prefix, " " if la.prefix != "" else "", old_memory, old_size}, + fmt_str = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)", + args = {la.prefix, " " if la.prefix != "" else "", size, alignment}, location = location, ) - } else { + case .Free: + if old_size != 0 { + logf( + level=la.level, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)", + args = {la.prefix, " " if la.prefix != "" else "", old_memory, old_size}, + location = location, + ) + } else { + logf( + level=la.level, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", + args = {la.prefix, " " if la.prefix != "" else "", old_memory}, + location = location, + ) + } + case .Free_All: logf( level=la.level, - fmt_str = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", - args = {la.prefix, " " if la.prefix != "" else "", old_memory}, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free_All)", + args = {la.prefix, " " if la.prefix != "" else ""}, + location = location, + ) + case .Resize: + logf( + level=la.level, + fmt_str = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)", + args = {la.prefix, " " if la.prefix != "" else "", old_memory, old_size, size, alignment}, + location = location, + ) + case .Query_Features: + logf( + level=la.level, + fmt_str = "%s%ALLOCATOR(mode=.Query_Features)", + args = {la.prefix, " " if la.prefix != "" else ""}, + location = location, + ) + case .Query_Info: + logf( + level=la.level, + fmt_str = "%s%ALLOCATOR(mode=.Query_Info)", + args = {la.prefix, " " if la.prefix != "" else ""}, location = location, ) } - case .Free_All: - logf( - level=la.level, - fmt_str = "%s%s<<< ALLOCATOR(mode=.Free_All)", - args = {la.prefix, " " if la.prefix != "" else ""}, - location = location, - ) - case .Resize: - logf( - level=la.level, - fmt_str = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)", - args = {la.prefix, " " if la.prefix != "" else "", old_memory, old_size, size, alignment}, - location = location, - ) - case .Query_Features: - logf( - level=la.level, - fmt_str = "%s%ALLOCATOR(mode=.Query_Features)", - args = {la.prefix, " " if la.prefix != "" else ""}, - location = location, - ) - case .Query_Info: - logf( - level=la.level, - fmt_str = "%s%ALLOCATOR(mode=.Query_Info)", - args = {la.prefix, " " if la.prefix != "" else ""}, - location = location, - ) } return la.allocator.procedure(la.allocator.data, mode, size, alignment, old_memory, old_size, location)