update zig to 0.11 and update build script

Also update memcpy to avoid recursive memcpy due to optimizations.
This commit is contained in:
Brendan Hansknecht 2023-09-20 14:42:11 -07:00
parent 0aaf8ebf8a
commit 2e2e609547
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
18 changed files with 535 additions and 507 deletions

View file

@ -1,6 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const always_inline = std.builtin.CallOptions.Modifier.always_inline;
const Monotonic = std.builtin.AtomicOrder.Monotonic;
const DEBUG_INCDEC = false;
@ -62,7 +61,12 @@ comptime {
}
fn testing_roc_alloc(size: usize, _: u32) callconv(.C) ?*anyopaque {
const ptr = @ptrCast(?*anyopaque, std.testing.allocator.alloc(u8, size) catch unreachable);
// 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));
if (DEBUG_TESTING_ALLOC and builtin.target.cpu.arch != .wasm32) {
std.debug.print("+ alloc {*}: {} bytes\n", .{ ptr, size });
@ -72,26 +76,32 @@ fn testing_roc_alloc(size: usize, _: u32) callconv(.C) ?*anyopaque {
}
fn testing_roc_realloc(c_ptr: *anyopaque, new_size: usize, old_size: usize, _: u32) callconv(.C) ?*anyopaque {
const ptr = @ptrCast([*]u8, @alignCast(2 * @alignOf(usize), c_ptr));
const slice = ptr[0..old_size];
const raw_ptr = @as([*]u8, @ptrCast(c_ptr)) - @sizeOf(usize);
const slice = raw_ptr[0..(old_size + @sizeOf(usize))];
const new = @ptrCast(?*anyopaque, std.testing.allocator.realloc(slice, new_size) catch unreachable);
const new_full_size = new_size + @sizeOf(usize);
var new_raw_ptr = (std.testing.allocator.realloc(slice, new_full_size) catch unreachable).ptr;
@as([*]usize, @alignCast(@ptrCast(new_raw_ptr)))[0] = new_full_size;
new_raw_ptr += @sizeOf(usize);
const new_ptr = @as(?*anyopaque, @ptrCast(new_raw_ptr));
if (DEBUG_TESTING_ALLOC and builtin.target.cpu.arch != .wasm32) {
std.debug.print("- realloc {*}\n", .{new});
std.debug.print("- realloc {*}\n", .{new_ptr});
}
return new;
return new_ptr;
}
fn testing_roc_dealloc(c_ptr: *anyopaque, _: u32) callconv(.C) void {
const ptr = @ptrCast([*]u8, @alignCast(2 * @alignOf(usize), c_ptr));
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];
if (DEBUG_TESTING_ALLOC and builtin.target.cpu.arch != .wasm32) {
std.debug.print("💀 dealloc {*}\n", .{ptr});
std.debug.print("💀 dealloc {*}\n", .{slice.ptr});
}
std.testing.allocator.destroy(ptr);
std.testing.allocator.free(slice);
}
fn testing_roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
@ -102,14 +112,14 @@ fn testing_roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
}
pub fn alloc(size: usize, alignment: u32) ?[*]u8 {
return @ptrCast(?[*]u8, roc_alloc(size, alignment));
return @as(?[*]u8, @ptrCast(roc_alloc(size, alignment)));
}
pub fn realloc(c_ptr: [*]u8, new_size: usize, old_size: usize, alignment: u32) [*]u8 {
if (DEBUG_INCDEC and builtin.target.cpu.arch != .wasm32) {
std.debug.print("- realloc {*}\n", .{c_ptr});
}
return @ptrCast([*]u8, roc_realloc(c_ptr, new_size, old_size, alignment));
return @as([*]u8, @ptrCast(roc_realloc(c_ptr, new_size, old_size, alignment)));
}
pub fn dealloc(c_ptr: [*]u8, alignment: u32) void {
@ -136,7 +146,7 @@ pub const Dec = fn (?[*]u8) callconv(.C) void;
const REFCOUNT_MAX_ISIZE: isize = 0;
pub const REFCOUNT_ONE_ISIZE: isize = std.math.minInt(isize);
pub const REFCOUNT_ONE: usize = @bitCast(usize, REFCOUNT_ONE_ISIZE);
pub const REFCOUNT_ONE: usize = @as(usize, @bitCast(REFCOUNT_ONE_ISIZE));
pub const IntWidth = enum(u8) {
U8 = 0,
@ -173,8 +183,8 @@ pub fn increfRcPtrC(ptr_to_refcount: *isize, amount: isize) callconv(.C) void {
switch (RC_TYPE) {
Refcount.normal => {
if (DEBUG_INCDEC and builtin.target.cpu.arch != .wasm32) {
const old = @bitCast(usize, ptr_to_refcount.*);
const new = old + @intCast(usize, amount);
const old = @as(usize, @bitCast(ptr_to_refcount.*));
const new = old + @as(usize, @intCast(amount));
const oldH = old - REFCOUNT_ONE + 1;
const newH = new - REFCOUNT_ONE + 1;
@ -200,9 +210,9 @@ pub fn decrefRcPtrC(
// (NOT the start of the data, or the start of the allocation)
// this is of course unsafe, but we trust what we get from the llvm side
var bytes = @ptrCast([*]isize, bytes_or_null);
var bytes = @as([*]isize, @ptrCast(bytes_or_null));
return @call(.{ .modifier = always_inline }, decref_ptr_to_refcount, .{ bytes, alignment });
return @call(.always_inline, decref_ptr_to_refcount, .{ bytes, alignment });
}
pub fn decrefCheckNullC(
@ -210,8 +220,8 @@ pub fn decrefCheckNullC(
alignment: u32,
) callconv(.C) void {
if (bytes_or_null) |bytes| {
const isizes: [*]isize = @ptrCast([*]isize, @alignCast(@sizeOf(isize), bytes));
return @call(.{ .modifier = always_inline }, decref_ptr_to_refcount, .{ isizes - 1, alignment });
const isizes: [*]isize = @as([*]isize, @ptrCast(@alignCast(bytes)));
return @call(.always_inline, decref_ptr_to_refcount, .{ isizes - 1, alignment });
}
}
@ -221,11 +231,11 @@ pub fn decrefDataPtrC(
) callconv(.C) void {
var bytes = bytes_or_null orelse return;
const data_ptr = @ptrToInt(bytes);
const data_ptr = @intFromPtr(bytes);
const tag_mask: usize = if (@sizeOf(usize) == 8) 0b111 else 0b11;
const unmasked_ptr = data_ptr & ~tag_mask;
const isizes: [*]isize = @intToPtr([*]isize, unmasked_ptr);
const isizes: [*]isize = @as([*]isize, @ptrFromInt(unmasked_ptr));
const rc_ptr = isizes - 1;
return decrefRcPtrC(rc_ptr, alignment);
@ -237,11 +247,11 @@ pub fn increfDataPtrC(
) callconv(.C) void {
var bytes = bytes_or_null orelse return;
const ptr = @ptrToInt(bytes);
const ptr = @intFromPtr(bytes);
const tag_mask: usize = if (@sizeOf(usize) == 8) 0b111 else 0b11;
const masked_ptr = ptr & ~tag_mask;
const isizes: *isize = @intToPtr(*isize, masked_ptr - @sizeOf(usize));
const isizes: *isize = @as(*isize, @ptrFromInt(masked_ptr - @sizeOf(usize)));
return increfRcPtrC(isizes, inc_amount);
}
@ -252,11 +262,11 @@ pub fn freeDataPtrC(
) callconv(.C) void {
var bytes = bytes_or_null orelse return;
const ptr = @ptrToInt(bytes);
const ptr = @intFromPtr(bytes);
const tag_mask: usize = if (@sizeOf(usize) == 8) 0b111 else 0b11;
const masked_ptr = ptr & ~tag_mask;
const isizes: [*]isize = @intToPtr([*]isize, masked_ptr);
const isizes: [*]isize = @as([*]isize, @ptrFromInt(masked_ptr));
return freeRcPtrC(isizes - 1, alignment);
}
@ -280,7 +290,7 @@ pub fn decref(
var bytes = bytes_or_null orelse return;
const isizes: [*]isize = @ptrCast([*]isize, @alignCast(@alignOf(isize), bytes));
const isizes: [*]isize = @as([*]isize, @ptrCast(@alignCast(bytes)));
decref_ptr_to_refcount(isizes - 1, alignment);
}
@ -290,8 +300,8 @@ inline fn free_ptr_to_refcount(
alignment: u32,
) void {
if (RC_TYPE == Refcount.none) return;
const extra_bytes = std.math.max(alignment, @sizeOf(usize));
const allocation_ptr = @ptrCast([*]u8, refcount_ptr) - (extra_bytes - @sizeOf(usize));
const extra_bytes = @max(alignment, @sizeOf(usize));
const allocation_ptr = @as([*]u8, @ptrCast(refcount_ptr)) - (extra_bytes - @sizeOf(usize));
// NOTE: we don't even check whether the refcount is "infinity" here!
dealloc(allocation_ptr, alignment);
@ -316,9 +326,9 @@ inline fn decref_ptr_to_refcount(
if (refcount != REFCOUNT_MAX_ISIZE) {
switch (RC_TYPE) {
Refcount.normal => {
const old = @bitCast(usize, refcount);
const old = @as(usize, @bitCast(refcount));
refcount_ptr[0] = refcount -% 1;
const new = @bitCast(usize, refcount -% 1);
const new = @as(usize, @bitCast(refcount -% 1));
if (DEBUG_INCDEC and builtin.target.cpu.arch != .wasm32) {
const oldH = old - REFCOUNT_ONE + 1;
@ -347,11 +357,11 @@ pub fn isUnique(
) callconv(.C) bool {
var bytes = bytes_or_null orelse return true;
const ptr = @ptrToInt(bytes);
const ptr = @intFromPtr(bytes);
const tag_mask: usize = if (@sizeOf(usize) == 8) 0b111 else 0b11;
const masked_ptr = ptr & ~tag_mask;
const isizes: [*]isize = @intToPtr([*]isize, masked_ptr);
const isizes: [*]isize = @as([*]isize, @ptrFromInt(masked_ptr));
const refcount = (isizes - 1)[0];
@ -407,7 +417,7 @@ pub inline fn calculateCapacity(
new_capacity = (old_capacity * 3 + 1) / 2;
}
return std.math.max(new_capacity, requested_length);
return @max(new_capacity, requested_length);
}
pub fn allocateWithRefcountC(
@ -422,7 +432,7 @@ pub fn allocateWithRefcount(
element_alignment: u32,
) [*]u8 {
const ptr_width = @sizeOf(usize);
const alignment = std.math.max(ptr_width, element_alignment);
const alignment = @max(ptr_width, element_alignment);
const length = alignment + data_bytes;
var new_bytes: [*]u8 = alloc(length, alignment) orelse unreachable;
@ -432,7 +442,7 @@ pub fn allocateWithRefcount(
}
const data_ptr = new_bytes + alignment;
const refcount_ptr = @ptrCast([*]usize, @alignCast(ptr_width, data_ptr) - ptr_width);
const refcount_ptr = @as([*]usize, @ptrCast(@as([*]align(ptr_width) u8, @alignCast(data_ptr)) - ptr_width));
refcount_ptr[0] = if (RC_TYPE == Refcount.none) REFCOUNT_MAX_ISIZE else REFCOUNT_ONE;
return data_ptr;
@ -450,7 +460,7 @@ pub fn unsafeReallocate(
new_length: usize,
element_width: usize,
) [*]u8 {
const align_width: usize = std.math.max(alignment, @sizeOf(usize));
const align_width: usize = @max(alignment, @sizeOf(usize));
const old_width = align_width + old_length * element_width;
const new_width = align_width + new_length * element_width;
@ -464,7 +474,7 @@ pub fn unsafeReallocate(
const old_allocation = source_ptr - align_width;
const new_allocation = realloc(old_allocation, new_width, old_width, alignment);
const new_source = @ptrCast([*]u8, new_allocation) + align_width;
const new_source = @as([*]u8, @ptrCast(new_allocation)) + align_width;
return new_source;
}
@ -502,5 +512,5 @@ test "increfC, static data" {
// In wasm, the value will be constant to the build as a whole.
// Either way, it can not be know by an attacker unless they get access to the executable.
pub fn dictPseudoSeed() callconv(.C) u64 {
return @intCast(u64, @ptrToInt(dictPseudoSeed));
return @as(u64, @intCast(@intFromPtr(&dictPseudoSeed)));
}