mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
Merge pull request #3033 from laytan/use-stack-buffer-for-log-allocator
use stack buffer for log allocator to avoid logging it's own allocations
This commit is contained in:
+42
-46
@@ -1,6 +1,7 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
import "core:runtime"
|
import "core:runtime"
|
||||||
|
import "core:fmt"
|
||||||
|
|
||||||
Log_Allocator_Format :: enum {
|
Log_Allocator_Format :: enum {
|
||||||
Bytes, // Actual number of bytes.
|
Bytes, // Actual number of bytes.
|
||||||
@@ -37,72 +38,71 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
|||||||
old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, runtime.Allocator_Error) {
|
old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, runtime.Allocator_Error) {
|
||||||
la := (^Log_Allocator)(allocator_data)
|
la := (^Log_Allocator)(allocator_data)
|
||||||
|
|
||||||
|
if context.logger.procedure == nil || la.level < context.logger.lowest_level {
|
||||||
|
return la.allocator.procedure(la.allocator.data, mode, size, alignment, old_memory, old_size, location)
|
||||||
|
}
|
||||||
|
|
||||||
padding := " " if la.prefix != "" else ""
|
padding := " " if la.prefix != "" else ""
|
||||||
|
|
||||||
|
buf: [256]byte = ---
|
||||||
|
|
||||||
if !la.locked {
|
if !la.locked {
|
||||||
la.locked = true
|
la.locked = true
|
||||||
defer la.locked = false
|
defer la.locked = false
|
||||||
|
|
||||||
switch mode {
|
switch mode {
|
||||||
case .Alloc:
|
case .Alloc:
|
||||||
fmt: string
|
format: string
|
||||||
switch la.size_fmt {
|
switch la.size_fmt {
|
||||||
case .Bytes: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)"
|
case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)"
|
||||||
case .Human: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%m, alignment=%d)"
|
case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%m, alignment=%d)"
|
||||||
}
|
}
|
||||||
logf(la.level, fmt, la.prefix, padding, size, alignment, location = location)
|
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:
|
case .Alloc_Non_Zeroed:
|
||||||
fmt: string
|
format: string
|
||||||
switch la.size_fmt {
|
switch la.size_fmt {
|
||||||
case .Bytes: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)"
|
case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%d, alignment=%d)"
|
||||||
case .Human: fmt = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%m, alignment=%d)"
|
case .Human: format = "%s%s>>> ALLOCATOR(mode=.Alloc_Non_Zeroed, size=%m, alignment=%d)"
|
||||||
}
|
}
|
||||||
logf(la.level, fmt, la.prefix, padding, size, alignment, location = location)
|
str := fmt.bprintf(buf[:], format, la.prefix, padding, size, alignment)
|
||||||
|
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
|
||||||
|
|
||||||
case .Free:
|
case .Free:
|
||||||
if old_size != 0 {
|
if old_size != 0 {
|
||||||
fmt: string
|
format: string
|
||||||
switch la.size_fmt {
|
switch la.size_fmt {
|
||||||
case .Bytes: fmt = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)"
|
case .Bytes: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)"
|
||||||
case .Human: fmt = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%m)"
|
case .Human: format = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%m)"
|
||||||
}
|
}
|
||||||
logf(la.level, fmt, la.prefix, padding, old_memory, old_size, location = location)
|
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 {
|
} else {
|
||||||
logf(
|
str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", la.prefix, padding, old_memory)
|
||||||
la.level,
|
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
|
||||||
"%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)",
|
|
||||||
la.prefix, padding, old_memory,
|
|
||||||
location = location,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case .Free_All:
|
case .Free_All:
|
||||||
logf(
|
str := fmt.bprintf(buf[:], "%s%s<<< ALLOCATOR(mode=.Free_All)", la.prefix, padding)
|
||||||
la.level,
|
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
|
||||||
"%s%s<<< ALLOCATOR(mode=.Free_All)",
|
|
||||||
la.prefix, padding,
|
|
||||||
location = location,
|
|
||||||
)
|
|
||||||
case .Resize:
|
case .Resize:
|
||||||
fmt: string
|
format: string
|
||||||
switch la.size_fmt {
|
switch la.size_fmt {
|
||||||
case .Bytes: fmt = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)"
|
case .Bytes: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)"
|
||||||
case .Human: fmt = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%m, size=%m, alignment=%d)"
|
case .Human: format = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%m, size=%m, alignment=%d)"
|
||||||
}
|
}
|
||||||
logf(la.level, fmt, la.prefix, padding, old_memory, old_size, size, alignment, location = location)
|
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:
|
case .Query_Features:
|
||||||
logf(
|
str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Features)", la.prefix, padding)
|
||||||
la.level,
|
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
|
||||||
"%s%sALLOCATOR(mode=.Query_Features)",
|
|
||||||
la.prefix, padding,
|
|
||||||
location = location,
|
|
||||||
)
|
|
||||||
case .Query_Info:
|
case .Query_Info:
|
||||||
logf(
|
str := fmt.bprintf(buf[:], "%s%sALLOCATOR(mode=.Query_Info)", la.prefix, padding)
|
||||||
la.level,
|
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
|
||||||
"%s%sALLOCATOR(mode=.Query_Info)",
|
|
||||||
la.prefix, padding,
|
|
||||||
location = location,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,12 +111,8 @@ log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
|||||||
la.locked = true
|
la.locked = true
|
||||||
defer la.locked = false
|
defer la.locked = false
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf(
|
str := fmt.bprintf(buf[:], "%s%sALLOCATOR ERROR=%v", la.prefix, padding, err)
|
||||||
la.level,
|
context.logger.procedure(context.logger.data, la.level, str, context.logger.options, location)
|
||||||
"%s%sALLOCATOR ERROR=%v",
|
|
||||||
la.prefix, padding, err,
|
|
||||||
location = location,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return data, err
|
return data, err
|
||||||
|
|||||||
Reference in New Issue
Block a user