aboutsummaryrefslogtreecommitdiff
path: root/core/log
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2025-10-10 09:47:31 +0100
committergingerBill <gingerBill@users.noreply.github.com>2025-10-10 09:47:31 +0100
commit4723ec75adfc8bcea4477d46838f3b966c7ecea5 (patch)
tree3cb0239d088043a1bb9184047dbc58cf62490b73 /core/log
parent7a9ea3ee6d02d8eade6d7988498bd69716391563 (diff)
Use `sync.Mutex` instead of a boolean-check for `log.Log_Allocator`
Diffstat (limited to 'core/log')
-rw-r--r--core/log/log_allocator.odin135
1 files changed, 67 insertions, 68 deletions
diff --git a/core/log/log_allocator.odin b/core/log/log_allocator.odin
index 16f1abe31..3ef5b4e25 100644
--- a/core/log/log_allocator.odin
+++ b/core/log/log_allocator.odin
@@ -2,26 +2,30 @@ package log
import "base:runtime"
import "core:fmt"
+import "core:sync"
Log_Allocator_Format :: enum {
Bytes, // Actual number of bytes.
Human, // Bytes in human units like bytes, kibibytes, etc. as appropriate.
}
+
+// Log_Allocator is an allocator which calls `context.logger` on each of its allocations operations.
+// The format can be changed by setting the `size_fmt: Log_Allocator_Format` field to either `Bytes` or `Human`.
Log_Allocator :: struct {
allocator: runtime.Allocator,
level: Level,
prefix: string,
- locked: bool,
+ lock: sync.Mutex,
size_fmt: Log_Allocator_Format,
}
log_allocator_init :: proc(la: ^Log_Allocator, level: Level, size_fmt := Log_Allocator_Format.Bytes,
allocator := context.allocator, prefix := "") {
la.allocator = allocator
- la.level = level
- la.prefix = prefix
- la.locked = false
+ la.level = level
+ la.prefix = prefix
+ la.lock = {}
la.size_fmt = size_fmt
}
@@ -46,83 +50,78 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
buf: [256]byte = ---
- if !la.locked {
- la.locked = true
- defer la.locked = false
+ sync.lock(&la.lock)
+ switch mode {
+ case .Alloc:
+ format: string
+ switch la.size_fmt {
+ case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)"
+ case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%m, alignment=%d)"
+ }
+ str := fmt.bprintf(buf[:], format, la.prefix, padding, size, alignment)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
+
+ case .Alloc_Non_Zeroed:
+ format: string
+ switch la.size_fmt {
+ case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)"
+ case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%m, alignment=%d)"
+ }
+ str := fmt.bprintf(buf[:], format, la.prefix, padding, size, alignment)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- switch mode {
- case .Alloc:
+ case .Free:
+ if old_size != 0 {
format: string
switch la.size_fmt {
- case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)"
- case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%m, alignment=%d)"
+ case .Bytes: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)"
+ case .Human: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%m)"
}
- str := fmt.bprintf(buf[:], format, la.prefix, padding, size, alignment)
+ str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size)
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
-
- case .Alloc_Non_Zeroed:
- format: string
- switch la.size_fmt {
- case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)"
- case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%m, alignment=%d)"
- }
- str := fmt.bprintf(buf[:], format, la.prefix, padding, size, alignment)
+ } else {
+ str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", la.prefix, padding, old_memory)
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
+ }
- case .Free:
- if old_size != 0 {
- format: string
- switch la.size_fmt {
- case .Bytes: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)"
- case .Human: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%m)"
- }
- str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- } else {
- str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", la.prefix, padding, old_memory)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- }
-
- case .Free_All:
- str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free_All)", la.prefix, padding)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
+ case .Free_All:
+ str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free_All)", la.prefix, padding)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- case .Resize:
- format: string
- switch la.size_fmt {
- case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)"
- case .Human: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%m, size=%m, alignment=%d)"
- }
- str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size, size, alignment)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
-
- case .Resize_Non_Zeroed:
- format: string
- switch la.size_fmt {
- case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%d, size=%d, alignment=%d)"
- case .Human: format = "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%m, size=%m, alignment=%d)"
- }
- str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size, size, alignment)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
+ case .Resize:
+ format: string
+ switch la.size_fmt {
+ case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)"
+ case .Human: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%m, size=%m, alignment=%d)"
+ }
+ str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size, size, alignment)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
+
+ case .Resize_Non_Zeroed:
+ format: string
+ switch la.size_fmt {
+ case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%d, size=%d, alignment=%d)"
+ case .Human: format = "%s%s>>> ALLOCATOR(mode=.Resize_Non_Zeroed, ptr=%p, old_size=%m, size=%m, alignment=%d)"
+ }
+ str := fmt.bprintf(buf[:], format, la.prefix, padding, old_memory, old_size, size, alignment)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- case .Query_Features:
- str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Features)", la.prefix, padding)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
+ case .Query_Features:
+ str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Features)", la.prefix, padding)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- case .Query_Info:
- str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Info)", la.prefix, padding)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- }
+ case .Query_Info:
+ str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Info)", la.prefix, padding)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
}
+ sync.unlock(&la.lock)
data, err := la.allocator.procedure(la.allocator.data, mode, size, alignment, old_memory, old_size, location)
- if !la.locked {
- la.locked = true
- defer la.locked = false
- if err != nil {
- str := fmt.bprintf(buf[:], "%s%sALLOCATOR ERROR=%v", la.prefix, padding, err)
- context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
- }
+ if err != nil {
+ sync.lock(&la.lock)
+ str := fmt.bprintf(buf[:], "%s%sALLOCATOR ERROR=%v", la.prefix, padding, err)
+ context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
+ sync.unlock(&la.lock)
}
return data, err
}