mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 23:58:50 +00:00
Use sync.Mutex instead of a boolean-check for log.Log_Allocator
This commit is contained in:
+10
-11
@@ -2,17 +2,21 @@ package log
|
|||||||
|
|
||||||
import "base:runtime"
|
import "base:runtime"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
|
import "core:sync"
|
||||||
|
|
||||||
Log_Allocator_Format :: enum {
|
Log_Allocator_Format :: enum {
|
||||||
Bytes, // Actual number of bytes.
|
Bytes, // Actual number of bytes.
|
||||||
Human, // Bytes in human units like bytes, kibibytes, etc. as appropriate.
|
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 {
|
Log_Allocator :: struct {
|
||||||
allocator: runtime.Allocator,
|
allocator: runtime.Allocator,
|
||||||
level: Level,
|
level: Level,
|
||||||
prefix: string,
|
prefix: string,
|
||||||
locked: bool,
|
lock: sync.Mutex,
|
||||||
size_fmt: Log_Allocator_Format,
|
size_fmt: Log_Allocator_Format,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,7 +25,7 @@ log_allocator_init :: proc(la: ^Log_Allocator, level: Level, size_fmt := Log_All
|
|||||||
la.allocator = allocator
|
la.allocator = allocator
|
||||||
la.level = level
|
la.level = level
|
||||||
la.prefix = prefix
|
la.prefix = prefix
|
||||||
la.locked = false
|
la.lock = {}
|
||||||
la.size_fmt = size_fmt
|
la.size_fmt = size_fmt
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,10 +50,7 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
|||||||
|
|
||||||
buf: [256]byte = ---
|
buf: [256]byte = ---
|
||||||
|
|
||||||
if !la.locked {
|
sync.lock(&la.lock)
|
||||||
la.locked = true
|
|
||||||
defer la.locked = false
|
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case .Alloc:
|
case .Alloc:
|
||||||
format: string
|
format: string
|
||||||
@@ -113,16 +114,14 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
|||||||
str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Info)", la.prefix, padding)
|
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)
|
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)
|
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 {
|
if err != nil {
|
||||||
|
sync.lock(&la.lock)
|
||||||
str := fmt.bprintf(buf[:], "%s%sALLOCATOR ERROR=%v", la.prefix, padding, err)
|
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)
|
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
|
||||||
}
|
sync.unlock(&la.lock)
|
||||||
}
|
}
|
||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user