Skip to content

Instantly share code, notes, and snippets.

@ityonemo
Created August 9, 2021 03:19
Show Gist options
  • Save ityonemo/fb1f9aca32feb56ad46dd5caab76a765 to your computer and use it in GitHub Desktop.
Save ityonemo/fb1f9aca32feb56ad46dd5caab76a765 to your computer and use it in GitHub Desktop.
not_colored.zig
// for testing purposes only, a mock of the c stdlib that provides calloc and malloc backed by the
// testing allocator. Stores last length in a global: Definitely not threadsafe.
var test_allocator_total_length: usize = undefined;
const MockedLibC = struct {
pub fn calloc(count: usize, size: usize) ?[*]u8 {
test_allocator_total_length = count * size;
var slice = std.testing.allocator.alloc(u8, test_allocator_total_length) catch return null;
const result_pointer = @ptrCast([*]u8, slice.ptr);
@memset(result_pointer, 0, test_allocator_total_length);
return result_pointer;
}
pub fn malloc(byte_length: usize) ?[*]u8 {
test_allocator_total_length = byte_length;
var slice = std.testing.allocator.alloc(u8, test_allocator_total_length) catch return null;
return @ptrCast([*]u8, slice.ptr);
}
pub fn free(memory: [*]u8) void {
std.testing.allocator.free(memory[0..test_allocator_total_length]);
test_allocator_total_length = undefined;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment