From c0b7dd7da60c4b7557e494049937af630bc25c33 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 13:05:19 +0100 Subject: [PATCH] Remove need for allocator and MAX_FRAMES in `trace.frames` --- core/debug/trace/trace.odin | 5 ++--- core/debug/trace/trace_linux.odin | 17 +++++++++-------- core/debug/trace/trace_windows.odin | 12 +++++------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/core/debug/trace/trace.odin b/core/debug/trace/trace.odin index 243232ddc..134609b05 100644 --- a/core/debug/trace/trace.odin +++ b/core/debug/trace/trace.odin @@ -4,7 +4,6 @@ import "base:intrinsics" import "base:runtime" Frame :: distinct uintptr -MAX_FRAMES :: 512 Frame_Location :: struct { using loc: runtime.Source_Code_Location, @@ -32,8 +31,8 @@ destroy :: proc(ctx: ^Context) -> bool { } @(require_results) -frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame { - return _frames(ctx, skip, allocator) +frames :: proc(ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame { + return _frames(ctx, skip, frames_buffer) } @(require_results) diff --git a/core/debug/trace/trace_linux.odin b/core/debug/trace/trace_linux.odin index c65a165f9..211c379ca 100644 --- a/core/debug/trace/trace_linux.odin +++ b/core/debug/trace/trace_linux.odin @@ -2,6 +2,7 @@ //+build linux package debug_trace +import "base:intrinsics" import "base:runtime" import "core:strings" import "core:fmt" @@ -92,17 +93,16 @@ _destroy :: proc(ctx: ^Context) -> bool { } @(private="package") -_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (frames: []Frame) { +_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> (frames: []Frame) { Backtrace_Context :: struct { ctx: ^Context, - rt_ctx: runtime.Context, - frames: [MAX_FRAMES]Frame, + frames: []Frame, frame_count: int, } btc := &Backtrace_Context{ ctx = ctx, - rt_ctx = context, + frames = frames_buffer, } backtrace_simple( ctx.impl.state, @@ -113,6 +113,9 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (fra if address == 0 { return 1 } + if btc.frame_count == len(btc.frames) { + return 1 + } btc.frames[btc.frame_count] = address btc.frame_count += 1 return 0 @@ -121,10 +124,8 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (fra btc, ) - res := btc.frames[:btc.frame_count] - if len(res) > 0 { - frames = make([]Frame, btc.frame_count, allocator) - copy(frames, res) + if btc.frame_count > 0 { + frames = btc.frames[:btc.frame_count] } return } diff --git a/core/debug/trace/trace_windows.odin b/core/debug/trace/trace_windows.odin index c7b4eeaa1..5535c09f5 100644 --- a/core/debug/trace/trace_windows.odin +++ b/core/debug/trace/trace_windows.odin @@ -28,16 +28,14 @@ _destroy :: proc "contextless" (ctx: ^Context) -> bool { return true } -_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame { - buffer: [MAX_FRAMES]rawptr - frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(buffer), &buffer[0], nil) - frames := make([]Frame, frame_count, allocator) - for &f, i in frames { +_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame { + frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(frames_buffer), &frames_buffer[0], nil) + for i in 0..