Add Leak Detection to Ona Heap Allocator #15
|
@ -7,7 +7,7 @@ const ext = @import("./ext.zig");
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
/// Recorded allocation info state.
|
||||||
///
|
///
|
||||||
const AllocationInfo = struct {
|
const AllocationInfo = struct {
|
||||||
trace: AllocationTrace,
|
trace: AllocationTrace,
|
||||||
|
@ -16,7 +16,9 @@ const AllocationInfo = struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
///
|
///
|
||||||
|
/// Recorded stack trace of allocation call site.
|
||||||
///
|
///
|
||||||
|
/// *Note* this structure is reduced to zero bytes in released builds optimized for speed or size.
|
||||||
///
|
///
|
||||||
const AllocationTrace = std.debug.ConfigurableTrace(2, 4, switch (builtin.mode) {
|
const AllocationTrace = std.debug.ConfigurableTrace(2, 4, switch (builtin.mode) {
|
||||||
.Debug, .ReleaseSafe => true,
|
.Debug, .ReleaseSafe => true,
|
||||||
|
@ -24,7 +26,7 @@ const AllocationTrace = std.debug.ConfigurableTrace(2, 4, switch (builtin.mode)
|
||||||
});
|
});
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
/// Heap allocation context.
|
||||||
///
|
///
|
||||||
const Context = struct {
|
const Context = struct {
|
||||||
allocation_info_head: ?*AllocationInfo = null,
|
allocation_info_head: ?*AllocationInfo = null,
|
||||||
|
@ -69,7 +71,7 @@ const Context = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
///
|
/// Returns the assumed pointer to the [AllocationInfo] address of `allocation`.
|
||||||
///
|
///
|
||||||
fn allocation_info_of(allocation: [*]u8) *AllocationInfo {
|
fn allocation_info_of(allocation: [*]u8) *AllocationInfo {
|
||||||
return @intToPtr(*AllocationInfo, @ptrToInt(allocation) - @sizeOf(AllocationInfo));
|
return @intToPtr(*AllocationInfo, @ptrToInt(allocation) - @sizeOf(AllocationInfo));
|
||||||
|
@ -159,9 +161,12 @@ const Context = struct {
|
||||||
if (target_allocation_info == allocation_info_head) {
|
if (target_allocation_info == allocation_info_head) {
|
||||||
self.allocation_info_head = allocation_info_head.next_info;
|
self.allocation_info_head = allocation_info_head.next_info;
|
||||||
|
|
||||||
return @ptrCast([*]u8, ext.SDL_realloc(target_allocation_info, size) orelse {
|
const allocation_address = ext.SDL_realloc(target_allocation_info, size) orelse return null;
|
||||||
return null;
|
|
||||||
})[allocation_info_size .. allocation_info_size + size];
|
target_allocation_info.size = size;
|
||||||
|
|
||||||
|
return @ptrCast([*]u8, allocation_address)[
|
||||||
|
allocation_info_size .. (allocation_info_size + size)];
|
||||||
}
|
}
|
||||||
|
|
||||||
var previous_allocation_info = allocation_info_head;
|
var previous_allocation_info = allocation_info_head;
|
||||||
|
@ -171,9 +176,12 @@ const Context = struct {
|
||||||
if (allocation_info == target_allocation_info) {
|
if (allocation_info == target_allocation_info) {
|
||||||
previous_allocation_info.next_info = allocation_info.next_info;
|
previous_allocation_info.next_info = allocation_info.next_info;
|
||||||
|
|
||||||
return @ptrCast([*]u8, ext.SDL_realloc(target_allocation_info, size) orelse {
|
const allocation_address = ext.SDL_realloc(target_allocation_info, size) orelse return null;
|
||||||
return null;
|
|
||||||
})[allocation_info_size .. allocation_info_size + size];
|
target_allocation_info.size = size;
|
||||||
|
|
||||||
|
return @ptrCast([*]u8, allocation_address)[
|
||||||
|
allocation_info_size .. (allocation_info_size + size)];
|
||||||
}
|
}
|
||||||
|
|
||||||
previous_allocation_info = allocation_info;
|
previous_allocation_info = allocation_info;
|
||||||
|
|
Loading…
Reference in New Issue