Skip to content

Commit

Permalink
Merge pull request #20 from lukewilliamboswell/trace-alloc
Browse files Browse the repository at this point in the history
make trace-allocs configurable at build time
  • Loading branch information
lukewilliamboswell authored Jan 24, 2024
2 parents b4df269 + b20fa2f commit 17a7a63
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn build(b: *std.Build) !void {
// Enable this if you hit any sort of memory corruption.
// It will cost performance.
const zero_on_alloc = b.option(bool, "zero-on-alloc", "zeros all newly allocated memory") orelse false;
const trace_allocs = b.option(bool, "trace-allocs", "debug print on every allocation and deallocation") orelse false;

const build_roc = b.addExecutable(.{
.name = "build_roc",
Expand Down Expand Up @@ -52,6 +53,7 @@ pub fn build(b: *std.Build) !void {
const options = b.addOptions();
options.addOption(usize, "mem_size", mem_size);
options.addOption(bool, "zero_on_alloc", zero_on_alloc);
options.addOption(bool, "trace_allocs", trace_allocs);
lib.addOptions("config", options);

lib.import_memory = true;
Expand Down
8 changes: 4 additions & 4 deletions platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ comptime {
}
}

const TRACE_ALLOC = false;
const TRACE_ALLOCS = config.trace_allocs;

const MEM_SIZE = config.mem_size;
const MEM: [MEM_SIZE]u8 align(ALIGN) = undefined;
Expand All @@ -40,7 +40,7 @@ export fn roc_alloc(requested_size: usize, alignment: u32) callconv(.C) *anyopaq
_ = alignment;
// Leave extra space to store allocation size.
const size = requested_size + MEM_CHUNK_SIZE;
if (TRACE_ALLOC) {
if (TRACE_ALLOCS) {
w4.tracef("alloc -> requested size %d, full size %d, chunks %d", requested_size, size, size / MEM_CHUNK_SIZE);
}

Expand All @@ -62,7 +62,7 @@ export fn roc_alloc(requested_size: usize, alignment: u32) callconv(.C) *anyopaq

const exclusive_end_index = current_index;
const range = .{ .start = start_index, .end = exclusive_end_index };
if (TRACE_ALLOC) {
if (TRACE_ALLOCS) {
w4.tracef("alloc -> start %d, end %d", start_index, exclusive_end_index);
}
free_set.setRangeValue(range, false);
Expand Down Expand Up @@ -111,7 +111,7 @@ export fn roc_dealloc(c_ptr: *anyopaque, alignment: u32) callconv(.C) void {
const range_ptr: *Range = @ptrFromInt(base_addr);
const range = range_ptr.*;

if (TRACE_ALLOC) {
if (TRACE_ALLOCS) {
w4.tracef("free -> start %d, end %d", range.start, range.end);
}
free_set.setRangeValue(range, true);
Expand Down

0 comments on commit 17a7a63

Please sign in to comment.