Add Leak Detection to Ona Heap Allocator #15

Merged
kayomn merged 12 commits from ona-allocator-safety-tracker into main 2023-06-04 16:07:47 +02:00
3 changed files with 19 additions and 9 deletions
Showing only changes of commit d63cfc23d6 - Show all commits

View File

@ -7,7 +7,7 @@ const std = @import("std");
/// ///
/// ///
/// ///
const stack_trace_frame_size = if (std.debug.sys_can_stack_trace) 4 else 0; const stack_trace_frame_size = if (std.debug.sys_can_stack_trace) 16 else 0;
/// ///
/// ///
@ -166,7 +166,8 @@ pub const allocator = coral.io.Allocator.bind(Context, &context, struct {
pub fn trace_allocations() void { pub fn trace_allocations() void {
var current_allocation_info = context.allocation_info_head; var current_allocation_info = context.allocation_info_head;
while (current_allocation_info) |allocation_info| : (current_allocation_info = allocation_info) { while (current_allocation_info) |allocation_info| : (current_allocation_info = allocation_info.next_info) {
std.debug.print("{d} byte leak detected", .{allocation_info.size});
std.debug.dumpStackTrace(allocation_info.stack_trace); std.debug.dumpStackTrace(allocation_info.stack_trace);
} }
} }

View File

@ -185,8 +185,17 @@ pub fn check(self: *Self, condition: bool, failure_message: []const u8) !void {
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
var global_data = self.heap.fetch(self.global_object);
if (global_data.release(self)) {
self.heap.remove(self.global_object);
} else {
self.heap.assign(self.global_object, global_data);
}
self.values.deinit(self.allocator); self.values.deinit(self.allocator);
self.calls.deinit(self.allocator); self.calls.deinit(self.allocator);
self.heap.deinit(self.allocator);
} }
pub fn discard(self: *Self, val: types.Val) void { pub fn discard(self: *Self, val: types.Val) void {

View File

@ -55,7 +55,9 @@ const AppManifest = struct {
} }
}; };
pub fn run_app(base_file_system: file.System) void { pub fn run_app(_: file.System) void {
defer heap.trace_allocations();
const Logger = struct { const Logger = struct {
const Self = @This(); const Self = @This();
@ -74,12 +76,12 @@ pub fn run_app(base_file_system: file.System) void {
defer script_environment.deinit(); defer script_environment.deinit();
const app_file_name = "app.ona"; // const app_file_name = "app.ona";
var app_manifest = AppManifest{}; var app_manifest = AppManifest{};
app_manifest.load_script(&script_environment, base_file_system, app_file_name) catch { // app_manifest.load_script(&script_environment, base_file_system, app_file_name) catch {
return ext.SDL_LogError(ext.SDL_LOG_CATEGORY_APPLICATION, "failed to load %s\n", app_file_name); // return ext.SDL_LogError(ext.SDL_LOG_CATEGORY_APPLICATION, "failed to load %s\n", app_file_name);
}; // };
if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) { if (ext.SDL_Init(ext.SDL_INIT_EVERYTHING) != 0) {
return ext.SDL_LogError(ext.SDL_LOG_CATEGORY_APPLICATION, "%s\n", ext.SDL_GetError()); return ext.SDL_LogError(ext.SDL_LOG_CATEGORY_APPLICATION, "%s\n", ext.SDL_GetError());
@ -135,6 +137,4 @@ pub fn run_app(base_file_system: file.System) void {
ext.SDL_RenderPresent(renderer); ext.SDL_RenderPresent(renderer);
} }
} }
heap.trace_allocations();
} }