Created
August 9, 2021 03:19
-
-
Save ityonemo/fb1f9aca32feb56ad46dd5caab76a765 to your computer and use it in GitHub Desktop.
not_colored.zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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