Merge remote-tracking branch 'origin/main' into upgrade-llvm-zig

This commit is contained in:
Brendan Hansknecht 2024-12-11 16:38:34 -08:00
commit 0a573ca557
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
818 changed files with 15185 additions and 4951 deletions

View file

@ -73,19 +73,29 @@ comptime {
}
}
fn testing_roc_alloc(size: usize, _: u32) callconv(.C) ?*anyopaque {
// We store an extra usize which is the size of the full allocation.
const full_size = size + @sizeOf(usize);
var raw_ptr = (std.testing.allocator.alloc(u8, full_size) catch unreachable).ptr;
@as([*]usize, @alignCast(@ptrCast(raw_ptr)))[0] = full_size;
raw_ptr += @sizeOf(usize);
const ptr = @as(?*anyopaque, @ptrCast(raw_ptr));
fn testing_roc_alloc(size: usize, nominal_alignment: u32) callconv(.C) ?*anyopaque {
const real_alignment = 16;
if (nominal_alignment > real_alignment) {
@panic("alignments larger than that of usize are not currently supported");
}
// We store an extra usize which is the size of the data plus the size of the size, directly before the data.
// We need enough clocks of the alignment size to fit this (usually this will be one)
const size_of_size = @sizeOf(usize);
const alignments_needed = size_of_size / real_alignment + comptime if (size_of_size % real_alignment == 0) 0 else 1;
const extra_bytes = alignments_needed * size_of_size;
const full_size = size + extra_bytes;
const whole_ptr = (std.testing.allocator.alignedAlloc(u8, real_alignment, full_size) catch unreachable).ptr;
const written_to_size = size + size_of_size;
@as([*]align(real_alignment) usize, @ptrCast(whole_ptr))[extra_bytes - size_of_size] = written_to_size;
const data_ptr = @as(?*anyopaque, @ptrCast(whole_ptr + extra_bytes));
if (DEBUG_TESTING_ALLOC and builtin.target.cpu.arch != .wasm32) {
std.debug.print("+ alloc {*}: {} bytes\n", .{ ptr, size });
std.debug.print("+ alloc {*}: {} bytes\n", .{ data_ptr, size });
}
return ptr;
return data_ptr;
}
fn testing_roc_realloc(c_ptr: *anyopaque, new_size: usize, old_size: usize, _: u32) callconv(.C) ?*anyopaque {
@ -106,9 +116,16 @@ fn testing_roc_realloc(c_ptr: *anyopaque, new_size: usize, old_size: usize, _: u
}
fn testing_roc_dealloc(c_ptr: *anyopaque, _: u32) callconv(.C) void {
const raw_ptr = @as([*]u8, @ptrCast(c_ptr)) - @sizeOf(usize);
const full_size = @as([*]usize, @alignCast(@ptrCast(raw_ptr)))[0];
const slice = raw_ptr[0..full_size];
const alignment = 16;
const size_of_size = @sizeOf(usize);
const alignments_needed = size_of_size / alignment + comptime if (size_of_size % alignment == 0) 0 else 1;
const extra_bytes = alignments_needed * size_of_size;
const byte_array = @as([*]u8, @ptrCast(c_ptr)) - extra_bytes;
const allocation_ptr = @as([*]align(alignment) u8, @alignCast(byte_array));
const offset_from_allocation_to_size = extra_bytes - size_of_size;
const size_of_data_and_size = @as([*]usize, @alignCast(@ptrCast(allocation_ptr)))[offset_from_allocation_to_size];
const full_size = size_of_data_and_size + offset_from_allocation_to_size;
const slice = allocation_ptr[0..full_size];
if (DEBUG_TESTING_ALLOC and builtin.target.cpu.arch != .wasm32) {
std.debug.print("💀 dealloc {*}\n", .{slice.ptr});