Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make trace-allocs configurable at build time #20

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading