From 0b3715ebee5ab200df75ba17f851ffef295bf1c0 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Mon, 24 May 2021 20:27:04 -0400 Subject: [PATCH] Rearrange arg order for roc_alloc etc --- .../fixtures/multi-dep-str/platform/host.zig | 6 +- .../multi-dep-thunk/platform/host.zig | 6 +- compiler/builtins/bitcode/src/dict.zig | 22 +++---- compiler/builtins/bitcode/src/list.zig | 66 +++++++++---------- compiler/builtins/bitcode/src/str.zig | 6 +- compiler/builtins/bitcode/src/utils.zig | 40 +++++------ compiler/gen/src/llvm/build.rs | 29 ++++++-- compiler/gen/src/llvm/build_list.rs | 48 ++++++-------- compiler/gen/src/llvm/externs.rs | 8 +-- compiler/gen/src/llvm/refcounting.rs | 4 +- compiler/test_gen/src/gen_list.rs | 8 +-- examples/balance/platform/src/lib.rs | 8 +-- examples/benchmarks/platform/host.zig | 6 +- examples/cli/platform/src/lib.rs | 8 +-- examples/custom-malloc/platform/host.zig | 6 +- examples/effect/thing/platform-dir/src/lib.rs | 8 +-- examples/hello-world/platform/host.zig | 6 +- examples/quicksort/platform/host.zig | 6 +- examples/shared-quicksort/platform/src/lib.rs | 8 +-- examples/task/platform/host.zig | 6 +- examples/tea/platform/src/lib.rs | 8 +-- roc_std/src/lib.rs | 14 ++-- 22 files changed, 166 insertions(+), 161 deletions(-) diff --git a/cli/tests/fixtures/multi-dep-str/platform/host.zig b/cli/tests/fixtures/multi-dep-str/platform/host.zig index 6781abce8b..2d335ec0f7 100644 --- a/cli/tests/fixtures/multi-dep-str/platform/host.zig +++ b/cli/tests/fixtures/multi-dep-str/platform/host.zig @@ -28,15 +28,15 @@ extern fn malloc(size: usize) callconv(.C) ?*c_void; extern fn realloc(c_ptr: [*]align(@alignOf(u128)) u8, size: usize) callconv(.C) ?*c_void; extern fn free(c_ptr: [*]align(@alignOf(u128)) u8) callconv(.C) void; -export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { +export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void { return malloc(size); } -export fn roc_realloc(alignment: u32, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) ?*c_void { +export fn roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) ?*c_void { return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size); } -export fn roc_dealloc(alignment: u32, c_ptr: *c_void) callconv(.C) void { +export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { free(@alignCast(16, @ptrCast([*]u8, c_ptr))); } diff --git a/cli/tests/fixtures/multi-dep-thunk/platform/host.zig b/cli/tests/fixtures/multi-dep-thunk/platform/host.zig index 6781abce8b..2d335ec0f7 100644 --- a/cli/tests/fixtures/multi-dep-thunk/platform/host.zig +++ b/cli/tests/fixtures/multi-dep-thunk/platform/host.zig @@ -28,15 +28,15 @@ extern fn malloc(size: usize) callconv(.C) ?*c_void; extern fn realloc(c_ptr: [*]align(@alignOf(u128)) u8, size: usize) callconv(.C) ?*c_void; extern fn free(c_ptr: [*]align(@alignOf(u128)) u8) callconv(.C) void; -export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { +export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void { return malloc(size); } -export fn roc_realloc(alignment: u32, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) ?*c_void { +export fn roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) ?*c_void { return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size); } -export fn roc_dealloc(alignment: u32, c_ptr: *c_void) callconv(.C) void { +export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { free(@alignCast(16, @ptrCast([*]u8, c_ptr))); } diff --git a/compiler/builtins/bitcode/src/dict.zig b/compiler/builtins/bitcode/src/dict.zig index c250d866c8..0d20f9a947 100644 --- a/compiler/builtins/bitcode/src/dict.zig +++ b/compiler/builtins/bitcode/src/dict.zig @@ -73,7 +73,7 @@ const Alignment = packed enum(u8) { Align8KeyFirst, Align8ValueFirst, - fn toUsize(self: Alignment) usize { + fn toU32(self: Alignment) u32 { switch (self) { .Align16KeyFirst => return 16, .Align16ValueFirst => return 16, @@ -97,14 +97,14 @@ pub fn decref( bytes_or_null: ?[*]u8, data_bytes: usize, ) void { - return utils.decref(alignment.toUsize(), bytes_or_null, data_bytes); + return utils.decref(alignment.toU32(), bytes_or_null, data_bytes); } pub fn allocateWithRefcount( - alignment: Alignment, data_bytes: usize, + alignment: Alignment, ) [*]u8 { - return utils.allocateWithRefcount(alignment.toUsize(), data_bytes); + return utils.allocateWithRefcount(data_bytes, alignment.toU32()); } pub const RocDict = extern struct { @@ -132,7 +132,7 @@ pub const RocDict = extern struct { const data_bytes = number_of_slots * slot_size; return RocDict{ - .dict_bytes = allocateWithRefcount(alignment, data_bytes), + .dict_bytes = allocateWithRefcount(data_bytes, alignment), .number_of_levels = number_of_levels, .dict_entries_len = number_of_entries, }; @@ -152,7 +152,7 @@ pub const RocDict = extern struct { const delta_capacity = new_capacity - old_capacity; const data_bytes = new_capacity * slot_size; - const first_slot = allocateWithRefcount(alignment, data_bytes); + const first_slot = allocateWithRefcount(data_bytes, alignment); // transfer the memory @@ -570,7 +570,7 @@ pub fn dictKeys(dict: RocDict, alignment: Alignment, key_width: usize, value_wid } const data_bytes = length * key_width; - var ptr = allocateWithRefcount(alignment, data_bytes); + var ptr = allocateWithRefcount(data_bytes, alignment); var offset = blk: { if (alignment.keyFirst()) { @@ -619,7 +619,7 @@ pub fn dictValues(dict: RocDict, alignment: Alignment, key_width: usize, value_w } const data_bytes = length * value_width; - var ptr = allocateWithRefcount(alignment, data_bytes); + var ptr = allocateWithRefcount(data_bytes, alignment); var offset = blk: { if (alignment.keyFirst()) { @@ -772,10 +772,10 @@ pub fn dictWalk( inc_value: Inc, output: Opaque, ) callconv(.C) void { - const alignment_usize = alignment.toUsize(); + const alignment_u32 = alignment.toU32(); // allocate space to write the result of the stepper into // experimentally aliasing the accum and output pointers is not a good idea - const bytes_ptr: [*]u8 = utils.alloc(alignment_usize, accum_width); + const bytes_ptr: [*]u8 = utils.alloc(accum_width, alignment_u32); var b1 = output orelse unreachable; var b2 = bytes_ptr; @@ -804,5 +804,5 @@ pub fn dictWalk( } @memcpy(output orelse unreachable, b2, accum_width); - utils.dealloc(alignment_usize, bytes_ptr); + utils.dealloc(bytes_ptr, alignment_u32); } diff --git a/compiler/builtins/bitcode/src/list.zig b/compiler/builtins/bitcode/src/list.zig index 4fd548ccae..ce17aefd07 100644 --- a/compiler/builtins/bitcode/src/list.zig +++ b/compiler/builtins/bitcode/src/list.zig @@ -41,19 +41,19 @@ pub const RocList = extern struct { } pub fn allocate( - alignment: usize, + alignment: u32, length: usize, element_size: usize, ) RocList { const data_bytes = length * element_size; return RocList{ - .bytes = utils.allocateWithRefcount(alignment, data_bytes), + .bytes = utils.allocateWithRefcount(data_bytes, alignment), .length = length, }; } - pub fn makeUnique(self: RocList, alignment: usize, element_width: usize) RocList { + pub fn makeUnique(self: RocList, alignment: u32, element_width: usize) RocList { if (self.isEmpty()) { return self; } @@ -80,7 +80,7 @@ pub const RocList = extern struct { pub fn reallocate( self: RocList, - alignment: usize, + alignment: u32, new_length: usize, element_width: usize, ) RocList { @@ -98,7 +98,7 @@ pub const RocList = extern struct { /// reallocate by explicitly making a new allocation and copying elements over fn reallocateFresh( self: RocList, - alignment: usize, + alignment: u32, new_length: usize, element_width: usize, ) RocList { @@ -106,7 +106,7 @@ pub const RocList = extern struct { const delta_length = new_length - old_length; const data_bytes = new_length * element_width; - const first_slot = utils.allocateWithRefcount(alignment, data_bytes); + const first_slot = utils.allocateWithRefcount(data_bytes, alignment); // transfer the memory @@ -133,7 +133,7 @@ const Caller1 = fn (?[*]u8, ?[*]u8, ?[*]u8) callconv(.C) void; const Caller2 = fn (?[*]u8, ?[*]u8, ?[*]u8, ?[*]u8) callconv(.C) void; const Caller3 = fn (?[*]u8, ?[*]u8, ?[*]u8, ?[*]u8, ?[*]u8) callconv(.C) void; -pub fn listReverse(list: RocList, alignment: usize, element_width: usize) callconv(.C) RocList { +pub fn listReverse(list: RocList, alignment: u32, element_width: usize) callconv(.C) RocList { if (list.bytes) |source_ptr| { const size = list.len(); @@ -177,7 +177,7 @@ pub fn listMap( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, old_element_width: usize, new_element_width: usize, ) callconv(.C) RocList { @@ -207,7 +207,7 @@ pub fn listMapWithIndex( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, old_element_width: usize, new_element_width: usize, ) callconv(.C) RocList { @@ -248,7 +248,7 @@ pub fn listMap2( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, a_width: usize, b_width: usize, c_width: usize, @@ -296,7 +296,7 @@ pub fn listMap3( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, a_width: usize, b_width: usize, c_width: usize, @@ -350,7 +350,7 @@ pub fn listKeepIf( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, element_width: usize, inc: Inc, dec: Dec, @@ -401,7 +401,7 @@ pub fn listKeepOks( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, before_width: usize, result_width: usize, after_width: usize, @@ -428,7 +428,7 @@ pub fn listKeepErrs( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, before_width: usize, result_width: usize, after_width: usize, @@ -456,7 +456,7 @@ pub fn listKeepResult( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, before_width: usize, result_width: usize, after_width: usize, @@ -468,7 +468,7 @@ pub fn listKeepResult( var output = RocList.allocate(alignment, list.len(), list.len() * after_width); const target_ptr = output.bytes orelse unreachable; - var temporary = @ptrCast([*]u8, utils.alloc(alignment, result_width)); + var temporary = @ptrCast([*]u8, utils.alloc(result_width, alignment)); if (data_is_owned) { inc_n_data(data, size); @@ -490,7 +490,7 @@ pub fn listKeepResult( } } - utils.dealloc(alignment, temporary); + utils.dealloc(temporary, alignment); if (kept == 0) { utils.decref(alignment, output.bytes, size * after_width); @@ -511,7 +511,7 @@ pub fn listWalk( inc_n_data: IncN, data_is_owned: bool, accum: Opaque, - alignment: usize, + alignment: u32, element_width: usize, accum_width: usize, output: Opaque, @@ -529,7 +529,7 @@ pub fn listWalk( inc_n_data(data, list.len()); } - const bytes_ptr: [*]u8 = utils.alloc(alignment, accum_width); + const bytes_ptr: [*]u8 = utils.alloc(accum_width, alignment); var b1 = output orelse unreachable; var b2 = bytes_ptr; @@ -549,7 +549,7 @@ pub fn listWalk( } @memcpy(output orelse unreachable, b2, accum_width); - utils.dealloc(alignment, bytes_ptr); + utils.dealloc(bytes_ptr, alignment); } pub fn listWalkBackwards( @@ -559,7 +559,7 @@ pub fn listWalkBackwards( inc_n_data: IncN, data_is_owned: bool, accum: Opaque, - alignment: usize, + alignment: u32, element_width: usize, accum_width: usize, output: Opaque, @@ -577,7 +577,7 @@ pub fn listWalkBackwards( inc_n_data(data, list.len()); } - const bytes_ptr: [*]u8 = utils.alloc(alignment, accum_width); + const bytes_ptr: [*]u8 = utils.alloc(accum_width, alignment); var b1 = output orelse unreachable; var b2 = bytes_ptr; @@ -598,7 +598,7 @@ pub fn listWalkBackwards( } @memcpy(output orelse unreachable, b2, accum_width); - utils.dealloc(alignment, bytes_ptr); + utils.dealloc(bytes_ptr, alignment); } pub fn listWalkUntil( @@ -608,7 +608,7 @@ pub fn listWalkUntil( inc_n_data: IncN, data_is_owned: bool, accum: Opaque, - alignment: usize, + alignment: u32, element_width: usize, accum_width: usize, dec: Dec, @@ -626,7 +626,7 @@ pub fn listWalkUntil( return; } - const bytes_ptr: [*]u8 = utils.alloc(alignment, TAG_WIDTH + accum_width); + const bytes_ptr: [*]u8 = utils.alloc(TAG_WIDTH + accum_width, alignment); @memcpy(bytes_ptr + TAG_WIDTH, accum orelse unreachable, accum_width); @@ -655,7 +655,7 @@ pub fn listWalkUntil( } @memcpy(output orelse unreachable, bytes_ptr + TAG_WIDTH, accum_width); - utils.dealloc(alignment, bytes_ptr); + utils.dealloc(bytes_ptr, alignment); } // List.contains : List k, k -> Bool @@ -674,7 +674,7 @@ pub fn listContains(list: RocList, key: Opaque, key_width: usize, is_eq: EqFn) c return false; } -pub fn listRepeat(count: usize, alignment: usize, element: Opaque, element_width: usize, inc_n_element: IncN) callconv(.C) RocList { +pub fn listRepeat(count: usize, alignment: u32, element: Opaque, element_width: usize, inc_n_element: IncN) callconv(.C) RocList { if (count == 0) { return RocList.empty(); } @@ -697,7 +697,7 @@ pub fn listRepeat(count: usize, alignment: usize, element: Opaque, element_width } } -pub fn listSingle(alignment: usize, element: Opaque, element_width: usize) callconv(.C) RocList { +pub fn listSingle(alignment: u32, element: Opaque, element_width: usize) callconv(.C) RocList { var output = RocList.allocate(alignment, 1, element_width); if (output.bytes) |target| { @@ -709,7 +709,7 @@ pub fn listSingle(alignment: usize, element: Opaque, element_width: usize) callc return output; } -pub fn listAppend(list: RocList, alignment: usize, element: Opaque, element_width: usize) callconv(.C) RocList { +pub fn listAppend(list: RocList, alignment: u32, element: Opaque, element_width: usize) callconv(.C) RocList { const old_length = list.len(); var output = list.reallocate(alignment, old_length + 1, element_width); @@ -724,7 +724,7 @@ pub fn listAppend(list: RocList, alignment: usize, element: Opaque, element_widt pub fn listDrop( list: RocList, - alignment: usize, + alignment: u32, element_width: usize, drop_count: usize, dec: Dec, @@ -883,7 +883,7 @@ pub fn listSortWith( data: Opaque, inc_n_data: IncN, data_is_owned: bool, - alignment: usize, + alignment: u32, element_width: usize, ) callconv(.C) RocList { var list = input.makeUnique(alignment, element_width); @@ -947,7 +947,7 @@ fn swapElements(source_ptr: [*]u8, element_width: usize, index_1: usize, index_2 return swap(element_width, element_at_i, element_at_j); } -pub fn listJoin(list_of_lists: RocList, alignment: usize, element_width: usize) callconv(.C) RocList { +pub fn listJoin(list_of_lists: RocList, alignment: u32, element_width: usize) callconv(.C) RocList { var total_length: usize = 0; const slice_of_lists = @ptrCast([*]RocList, @alignCast(@alignOf(RocList), list_of_lists.bytes)); @@ -975,7 +975,7 @@ pub fn listJoin(list_of_lists: RocList, alignment: usize, element_width: usize) return output; } -pub fn listConcat(list_a: RocList, list_b: RocList, alignment: usize, element_width: usize) callconv(.C) RocList { +pub fn listConcat(list_a: RocList, list_b: RocList, alignment: u32, element_width: usize) callconv(.C) RocList { if (list_a.isEmpty()) { return list_b; } else if (list_b.isEmpty()) { diff --git a/compiler/builtins/bitcode/src/str.zig b/compiler/builtins/bitcode/src/str.zig index 5d990d819d..f8016dcc0a 100644 --- a/compiler/builtins/bitcode/src/str.zig +++ b/compiler/builtins/bitcode/src/str.zig @@ -53,7 +53,7 @@ pub const RocStr = extern struct { } pub fn initBig(in_place: InPlace, number_of_chars: u64) RocStr { - const first_element = utils.allocateWithRefcount(@sizeOf(usize), number_of_chars); + const first_element = utils.allocateWithRefcount(number_of_chars, @sizeOf(usize)); return RocStr{ .str_bytes = first_element, @@ -99,7 +99,7 @@ pub const RocStr = extern struct { if (length < roc_str_size) { return RocStr.empty(); } else { - var new_bytes: []T = utils.alloc(RocStr.alignment, length) catch unreachable; + var new_bytes: []T = utils.alloc(length, RocStr.alignment) catch unreachable; var new_bytes_ptr: [*]u8 = @ptrCast([*]u8, &new_bytes); @@ -1070,7 +1070,7 @@ fn strToBytes(arg: RocStr) RocList { return RocList.empty(); } else if (arg.isSmallStr()) { const length = arg.len(); - const ptr = utils.allocateWithRefcount(RocStr.alignment, length); + const ptr = utils.allocateWithRefcount(length, RocStr.alignment); @memcpy(ptr, arg.asU8ptr(), length); diff --git a/compiler/builtins/bitcode/src/utils.zig b/compiler/builtins/bitcode/src/utils.zig index 7bc4150ec3..23fd894947 100644 --- a/compiler/builtins/bitcode/src/utils.zig +++ b/compiler/builtins/bitcode/src/utils.zig @@ -2,14 +2,14 @@ const std = @import("std"); const always_inline = std.builtin.CallOptions.Modifier.always_inline; // If allocation fails, this must cxa_throw - it must not return a null pointer! -extern fn roc_alloc(alignment: usize, size: usize) callconv(.C) *c_void; +extern fn roc_alloc(size: usize, alignment: u32) callconv(.C) *c_void; // This should never be passed a null pointer. // If allocation fails, this must cxa_throw - it must not return a null pointer! -extern fn roc_realloc(alignment: usize, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) *c_void; +extern fn roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) *c_void; // This should never be passed a null pointer. -extern fn roc_dealloc(alignment: usize, c_ptr: *c_void) callconv(.C) void; +extern fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void; comptime { // During tetsts, use the testing allocators to satisfy these functions. @@ -20,33 +20,33 @@ comptime { } } -fn testing_roc_alloc(alignment: usize, size: usize) callconv(.C) *c_void { +fn testing_roc_alloc(size: usize, alignment: u32) callconv(.C) *c_void { return @ptrCast(*c_void, std.testing.allocator.alloc(u8, size) catch unreachable); } -fn testing_roc_realloc(alignment: usize, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) *c_void { +fn testing_roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) *c_void { const ptr = @ptrCast([*]u8, @alignCast(16, c_ptr)); const slice = ptr[0..old_size]; return @ptrCast(*c_void, std.testing.allocator.realloc(slice, new_size) catch unreachable); } -fn testing_roc_dealloc(alignment: usize, c_ptr: *c_void) callconv(.C) void { +fn testing_roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { const ptr = @ptrCast([*]u8, @alignCast(16, c_ptr)); std.testing.allocator.destroy(ptr); } -pub fn alloc(alignment: usize, size: usize) [*]u8 { - return @ptrCast([*]u8, @call(.{ .modifier = always_inline }, roc_alloc, .{ alignment, size })); +pub fn alloc(size: usize, alignment: u32) [*]u8 { + return @ptrCast([*]u8, @call(.{ .modifier = always_inline }, roc_alloc, .{ size, alignment })); } -pub fn realloc(alignment: usize, c_ptr: [*]u8, old_size: usize, new_size: usize) [*]u8 { - return @ptrCast([*]u8, @call(.{ .modifier = always_inline }, roc_realloc, .{ alignment, c_ptr, old_size, new_size })); +pub fn realloc(c_ptr: [*]u8, new_size: usize, old_size: usize, alignment: u32) [*]u8 { + return @ptrCast([*]u8, @call(.{ .modifier = always_inline }, roc_realloc, .{ c_ptr, new_size, old_size, alignment })); } -pub fn dealloc(alignment: usize, c_ptr: [*]u8) void { - return @call(.{ .modifier = always_inline }, roc_dealloc, .{ alignment, c_ptr }); +pub fn dealloc(c_ptr: [*]u8, alignment: u32) void { + return @call(.{ .modifier = always_inline }, roc_dealloc, .{ c_ptr, alignment }); } pub const Inc = fn (?[*]u8) callconv(.C) void; @@ -110,7 +110,7 @@ pub fn intWidth(width: IntWidth) anytype { } pub fn decref( - alignment: usize, + alignment: u32, bytes_or_null: ?[*]u8, data_bytes: usize, ) void { @@ -128,7 +128,7 @@ pub fn decref( switch (alignment) { 16 => { if (refcount == REFCOUNT_ONE_ISIZE) { - dealloc(alignment, bytes - 16); + dealloc(bytes - 16, alignment); } else if (refcount_isize < 0) { (isizes - 1)[0] = refcount - 1; } @@ -136,7 +136,7 @@ pub fn decref( else => { // NOTE enums can currently have an alignment of < 8 if (refcount == REFCOUNT_ONE_ISIZE) { - dealloc(alignment, bytes - 8); + dealloc(bytes - 8, alignment); } else if (refcount_isize < 0) { (isizes - 1)[0] = refcount - 1; } @@ -145,8 +145,8 @@ pub fn decref( } pub fn allocateWithRefcount( - alignment: usize, data_bytes: usize, + alignment: u32, ) [*]u8 { comptime const result_in_place = false; @@ -154,7 +154,7 @@ pub fn allocateWithRefcount( 16 => { const length = 2 * @sizeOf(usize) + data_bytes; - var new_bytes: [*]align(16) u8 = @alignCast(16, alloc(alignment, length)); + var new_bytes: [*]align(16) u8 = @alignCast(16, alloc(length, alignment)); var as_usize_array = @ptrCast([*]usize, new_bytes); if (result_in_place) { @@ -173,7 +173,7 @@ pub fn allocateWithRefcount( else => { const length = @sizeOf(usize) + data_bytes; - var new_bytes: [*]align(8) u8 = @alignCast(8, alloc(alignment, length)); + var new_bytes: [*]align(8) u8 = @alignCast(8, alloc(length, alignment)); var as_isize_array = @ptrCast([*]isize, new_bytes); if (result_in_place) { @@ -192,7 +192,7 @@ pub fn allocateWithRefcount( pub fn unsafeReallocate( source_ptr: [*]u8, - alignment: usize, + alignment: u32, old_length: usize, new_length: usize, element_width: usize, @@ -211,7 +211,7 @@ pub fn unsafeReallocate( // TODO handle out of memory // NOTE realloc will dealloc the original allocation const old_allocation = source_ptr - align_width; - const new_allocation = realloc(alignment, old_allocation, old_width, new_width); + const new_allocation = realloc(old_allocation, new_width, old_width, alignment); const new_source = @ptrCast([*]u8, new_allocation) + align_width; return new_source; diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index e232d9bb1d..581349f7d3 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -223,16 +223,31 @@ impl<'a, 'ctx, 'env> Env<'a, 'ctx, 'env> { }) } + pub fn alignment_type(&self) -> IntType<'ctx> { + self.context.i32_type() + } + + pub fn alignment_const(&self, alignment: u32) -> IntValue<'ctx> { + self.alignment_type().const_int(alignment as u64, false) + } + + pub fn alignment_intvalue(&self, element_layout: &Layout<'a>) -> BasicValueEnum<'ctx> { + let alignment = element_layout.alignment_bytes(self.ptr_bytes); + let alignment_iv = self.alignment_const(alignment); + + alignment_iv.into() + } + pub fn call_alloc( &self, - alignment: u32, number_of_bytes: IntValue<'ctx>, + alignment: u32, ) -> PointerValue<'ctx> { let function = self.module.get_function("roc_alloc").unwrap(); - let alignment = self.ptr_int().const_int(alignment as u64, false); + let alignment = self.alignment_const(alignment); let call = self.builder.build_call( function, - &[alignment.into(), number_of_bytes.into()], + &[number_of_bytes.into(), alignment.into()], "roc_alloc", ); @@ -245,12 +260,12 @@ impl<'a, 'ctx, 'env> Env<'a, 'ctx, 'env> { // TODO check if alloc returned null; if so, runtime error for OOM! } - pub fn call_dealloc(&self, alignment: u32, ptr: PointerValue<'ctx>) -> InstructionValue<'ctx> { + pub fn call_dealloc(&self, ptr: PointerValue<'ctx>, alignment: u32) -> InstructionValue<'ctx> { let function = self.module.get_function("roc_dealloc").unwrap(); - let alignment = self.ptr_int().const_int(alignment as u64, false); + let alignment = self.alignment_const(alignment); let call = self.builder - .build_call(function, &[alignment.into(), ptr.into()], "roc_dealloc"); + .build_call(function, &[ptr.into(), alignment.into()], "roc_dealloc"); call.set_call_convention(C_CALL_CONV); @@ -1698,7 +1713,7 @@ pub fn allocate_with_refcount_help<'a, 'ctx, 'env>( "add_extra_bytes", ); - env.call_alloc(layout.alignment_bytes(env.ptr_bytes), number_of_bytes) + env.call_alloc(number_of_bytes, layout.alignment_bytes(env.ptr_bytes)) }; // We must return a pointer to the first element: diff --git a/compiler/gen/src/llvm/build_list.rs b/compiler/gen/src/llvm/build_list.rs index 90cbbc43b5..1cc8d5ec51 100644 --- a/compiler/gen/src/llvm/build_list.rs +++ b/compiler/gen/src/llvm/build_list.rs @@ -18,16 +18,6 @@ use inkwell::{AddressSpace, IntPredicate}; use roc_builtins::bitcode; use roc_mono::layout::{Builtin, Layout, LayoutIds, MemoryMode}; -fn alignment_intvalue<'a, 'ctx, 'env>( - env: &Env<'a, 'ctx, 'env>, - element_layout: &Layout<'a>, -) -> BasicValueEnum<'ctx> { - let alignment = element_layout.alignment_bytes(env.ptr_bytes); - let alignment_iv = env.ptr_int().const_int(alignment as u64, false); - - alignment_iv.into() -} - fn list_returned_from_zig<'a, 'ctx, 'env>( env: &Env<'a, 'ctx, 'env>, output: BasicValueEnum<'ctx>, @@ -102,7 +92,7 @@ pub fn list_single<'a, 'ctx, 'env>( call_bitcode_fn_returns_list( env, &[ - alignment_intvalue(env, element_layout), + env.alignment_intvalue(element_layout), pass_element_as_opaque(env, element), layout_width(env, element_layout), ], @@ -124,7 +114,7 @@ pub fn list_repeat<'a, 'ctx, 'env>( env, &[ list_len.into(), - alignment_intvalue(env, element_layout), + env.alignment_intvalue(element_layout), pass_element_as_opaque(env, element), layout_width(env, element_layout), inc_element_fn.as_global_value().as_pointer_value().into(), @@ -218,7 +208,7 @@ pub fn list_join<'a, 'ctx, 'env>( env, &[ pass_list_as_i128(env, outer_list), - alignment_intvalue(env, element_layout), + env.alignment_intvalue(element_layout), layout_width(env, element_layout), ], &bitcode::LIST_JOIN, @@ -258,7 +248,7 @@ pub fn list_reverse<'a, 'ctx, 'env>( env, &[ pass_list_as_i128(env, list), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, &element_layout), ], &bitcode::LIST_REVERSE, @@ -314,7 +304,7 @@ pub fn list_append<'a, 'ctx, 'env>( env, &[ pass_list_as_i128(env, original_wrapper.into()), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), pass_element_as_opaque(env, element), layout_width(env, element_layout), ], @@ -335,7 +325,7 @@ pub fn list_drop<'a, 'ctx, 'env>( env, &[ pass_list_as_i128(env, original_wrapper.into()), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, &element_layout), count.into(), dec_element_fn.as_global_value().as_pointer_value().into(), @@ -344,7 +334,7 @@ pub fn list_drop<'a, 'ctx, 'env>( ) } -/// List.set : List elem, Int, elem -> List elem +/// List.set : List elem, Nat, elem -> List elem pub fn list_set<'a, 'ctx, 'env>( parent: FunctionValue<'ctx>, args: &[(BasicValueEnum<'ctx>, &'a Layout<'a>)], @@ -501,7 +491,7 @@ pub fn list_walk_generic<'a, 'ctx, 'env>( roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), pass_as_opaque(env, default_ptr), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, element_layout), layout_width(env, default_layout), pass_as_opaque(env, result_ptr), @@ -520,7 +510,7 @@ pub fn list_walk_generic<'a, 'ctx, 'env>( roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), pass_as_opaque(env, default_ptr), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, element_layout), layout_width(env, default_layout), dec_element_fn.as_global_value().as_pointer_value().into(), @@ -642,7 +632,7 @@ pub fn list_keep_if<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, element_layout), inc_element_fn.as_global_value().as_pointer_value().into(), dec_element_fn.as_global_value().as_pointer_value().into(), @@ -678,7 +668,7 @@ pub fn list_keep_oks<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, &before_layout), + env.alignment_intvalue(&before_layout), layout_width(env, before_layout), layout_width(env, result_layout), layout_width(env, after_layout), @@ -715,7 +705,7 @@ pub fn list_keep_errs<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, &before_layout), + env.alignment_intvalue(&before_layout), layout_width(env, before_layout), layout_width(env, result_layout), layout_width(env, after_layout), @@ -762,7 +752,7 @@ pub fn list_keep_result<'a, 'ctx, 'env>( pass_list_as_i128(env, list), pass_as_opaque(env, closure_data_ptr), stepper_caller.into(), - alignment_intvalue(env, &before_layout), + env.alignment_intvalue(&before_layout), layout_width(env, before_layout), layout_width(env, after_layout), layout_width(env, result_layout), @@ -789,7 +779,7 @@ pub fn list_sort_with<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, element_layout), ], bitcode::LIST_SORT_WITH, @@ -812,7 +802,7 @@ pub fn list_map_with_index<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, element_layout), layout_width(env, return_layout), ], @@ -836,7 +826,7 @@ pub fn list_map<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, &element_layout), + env.alignment_intvalue(&element_layout), layout_width(env, element_layout), layout_width(env, return_layout), ], @@ -866,7 +856,7 @@ pub fn list_map2<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, return_layout), + env.alignment_intvalue(return_layout), layout_width(env, element1_layout), layout_width(env, element2_layout), layout_width(env, return_layout), @@ -903,7 +893,7 @@ pub fn list_map3<'a, 'ctx, 'env>( pass_as_opaque(env, roc_function_call.data), roc_function_call.inc_n_data.into(), roc_function_call.data_is_owned.into(), - alignment_intvalue(env, result_layout), + env.alignment_intvalue(result_layout), layout_width(env, element1_layout), layout_width(env, element2_layout), layout_width(env, element3_layout), @@ -936,7 +926,7 @@ pub fn list_concat<'a, 'ctx, 'env>( &[ pass_list_as_i128(env, first_list), pass_list_as_i128(env, second_list), - alignment_intvalue(env, elem_layout), + env.alignment_intvalue(elem_layout), layout_width(env, elem_layout), ], &bitcode::LIST_CONCAT, diff --git a/compiler/gen/src/llvm/externs.rs b/compiler/gen/src/llvm/externs.rs index 732c18c98f..2bb2a01c6e 100644 --- a/compiler/gen/src/llvm/externs.rs +++ b/compiler/gen/src/llvm/externs.rs @@ -22,8 +22,8 @@ pub fn add_default_roc_externs<'ctx>( // already been defined by the builtins, which rely on it. let fn_val = module.get_function("roc_alloc").unwrap(); let mut params = fn_val.get_param_iter(); - let _alignment_arg = params.next().unwrap(); let size_arg = params.next().unwrap(); + let _alignment_arg = params.next().unwrap(); debug_assert!(params.next().is_none()); @@ -83,10 +83,10 @@ pub fn add_default_roc_externs<'ctx>( // already been defined by the builtins, which rely on it. let fn_val = module.get_function("roc_realloc").unwrap(); let mut params = fn_val.get_param_iter(); - let _alignment_arg = params.next().unwrap(); let ptr_arg = params.next().unwrap(); - let _old_size_arg = params.next().unwrap(); let new_size_arg = params.next().unwrap(); + let _old_size_arg = params.next().unwrap(); + let _alignment_arg = params.next().unwrap(); debug_assert!(params.next().is_none()); @@ -119,8 +119,8 @@ pub fn add_default_roc_externs<'ctx>( // already been defined by the builtins, which rely on it. let fn_val = module.get_function("roc_dealloc").unwrap(); let mut params = fn_val.get_param_iter(); - let _alignment_arg = params.next().unwrap(); let ptr_arg = params.next().unwrap(); + let _alignment_arg = params.next().unwrap(); debug_assert!(params.next().is_none()); diff --git a/compiler/gen/src/llvm/refcounting.rs b/compiler/gen/src/llvm/refcounting.rs index e924d4e4bd..cf0a254163 100644 --- a/compiler/gen/src/llvm/refcounting.rs +++ b/compiler/gen/src/llvm/refcounting.rs @@ -279,12 +279,12 @@ impl<'ctx> PointerToRefcount<'ctx> { match alignment { n if env.ptr_bytes == n => { // the refcount ptr is also the ptr to the allocated region - env.call_dealloc(alignment, ptr); + env.call_dealloc(ptr, alignment); } n if 2 * env.ptr_bytes == n => { // we need to step back another ptr_bytes to get the allocated ptr let allocated = Self::from_ptr_to_data(env, ptr); - env.call_dealloc(alignment, allocated.value); + env.call_dealloc(allocated.value, alignment); } n => unreachable!("invalid extra_bytes {:?}", n), } diff --git a/compiler/test_gen/src/gen_list.rs b/compiler/test_gen/src/gen_list.rs index b8b10fe553..98ccb25d32 100644 --- a/compiler/test_gen/src/gen_list.rs +++ b/compiler/test_gen/src/gen_list.rs @@ -8,22 +8,22 @@ use indoc::indoc; use roc_std::{RocList, RocStr}; #[no_mangle] -pub unsafe fn roc_alloc(_alignment: u32, size: usize) -> *mut c_void { +pub unsafe fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void { libc::malloc(size) } #[no_mangle] pub unsafe fn roc_realloc( - _alignment: u32, c_ptr: *mut c_void, - _old_size: usize, new_size: usize, + _old_size: usize, + _alignment: u32, ) -> *mut c_void { libc::realloc(c_ptr, new_size) } #[no_mangle] -pub unsafe fn roc_dealloc(_alignment: u32, c_ptr: *mut c_void) { +pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) { libc::free(c_ptr) } diff --git a/examples/balance/platform/src/lib.rs b/examples/balance/platform/src/lib.rs index cc8a7dc37b..5bff358b10 100644 --- a/examples/balance/platform/src/lib.rs +++ b/examples/balance/platform/src/lib.rs @@ -26,22 +26,22 @@ extern "C" { } #[no_mangle] -pub unsafe fn roc_alloc(_alignment: u32, size: usize) -> *mut c_void { +pub unsafe fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void { return malloc(size); } #[no_mangle] pub unsafe fn roc_realloc( - _alignment: u32, c_ptr: *mut c_void, - _old_size: usize, new_size: usize, + _old_size: usize, + _alignment: u32, ) -> *mut c_void { return realloc(c_ptr, new_size); } #[no_mangle] -pub unsafe fn roc_dealloc(_alignment: u32, c_ptr: *mut c_void) { +pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) { return free(c_ptr); } diff --git a/examples/benchmarks/platform/host.zig b/examples/benchmarks/platform/host.zig index b5c97ce283..56461d93e6 100644 --- a/examples/benchmarks/platform/host.zig +++ b/examples/benchmarks/platform/host.zig @@ -33,15 +33,15 @@ extern fn malloc(size: usize) callconv(.C) ?*c_void; extern fn realloc(c_ptr: [*]align(@alignOf(u128)) u8, size: usize) callconv(.C) ?*c_void; extern fn free(c_ptr: [*]align(@alignOf(u128)) u8) callconv(.C) void; -export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { +export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void { return malloc(size); } -export fn roc_realloc(alignment: u32, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) ?*c_void { +export fn roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) ?*c_void { return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size); } -export fn roc_dealloc(alignment: u32, c_ptr: *c_void) callconv(.C) void { +export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { free(@alignCast(16, @ptrCast([*]u8, c_ptr))); } diff --git a/examples/cli/platform/src/lib.rs b/examples/cli/platform/src/lib.rs index 2f96584924..daf2b25ef0 100644 --- a/examples/cli/platform/src/lib.rs +++ b/examples/cli/platform/src/lib.rs @@ -32,22 +32,22 @@ extern "C" { } #[no_mangle] -pub unsafe fn roc_alloc(_alignment: u32, size: usize) -> *mut c_void { +pub unsafe fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void { return malloc(size); } #[no_mangle] pub unsafe fn roc_realloc( - _alignment: u32, c_ptr: *mut c_void, - _old_size: usize, new_size: usize, + _old_size: usize, + _alignment: u32, ) -> *mut c_void { return realloc(c_ptr, new_size); } #[no_mangle] -pub unsafe fn roc_dealloc(_alignment: u32, c_ptr: *mut c_void) { +pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) { return free(c_ptr); } diff --git a/examples/custom-malloc/platform/host.zig b/examples/custom-malloc/platform/host.zig index 2ce6354671..bf7bcb30cc 100644 --- a/examples/custom-malloc/platform/host.zig +++ b/examples/custom-malloc/platform/host.zig @@ -64,7 +64,7 @@ pub export fn main() u8 { return 0; } -export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { +export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void { const stdout = std.io.getStdOut().writer(); const allocator = testing.allocator; @@ -80,11 +80,11 @@ export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { return ptr; } -export fn roc_realloc(alignment: u32, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) ?*c_void { +export fn roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) ?*c_void { return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size); } -export fn roc_dealloc(alignment: u32, c_ptr: *c_void) callconv(.C) void { +export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { const stdout = std.io.getStdOut().writer(); const allocator = testing.allocator; diff --git a/examples/effect/thing/platform-dir/src/lib.rs b/examples/effect/thing/platform-dir/src/lib.rs index a9a6633753..2fe8147386 100644 --- a/examples/effect/thing/platform-dir/src/lib.rs +++ b/examples/effect/thing/platform-dir/src/lib.rs @@ -34,22 +34,22 @@ extern "C" { } #[no_mangle] -pub unsafe fn roc_alloc(_alignment: u32, size: usize) -> *mut c_void { +pub unsafe fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void { return malloc(size); } #[no_mangle] pub unsafe fn roc_realloc( - _alignment: u32, c_ptr: *mut c_void, - _old_size: usize, new_size: usize, + _old_size: usize, + _alignment: u32, ) -> *mut c_void { return realloc(c_ptr, new_size); } #[no_mangle] -pub unsafe fn roc_dealloc(_alignment: u32, c_ptr: *mut c_void) { +pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) { return free(c_ptr); } diff --git a/examples/hello-world/platform/host.zig b/examples/hello-world/platform/host.zig index c10cfdd426..1419ddd15e 100644 --- a/examples/hello-world/platform/host.zig +++ b/examples/hello-world/platform/host.zig @@ -23,15 +23,15 @@ extern fn malloc(size: usize) callconv(.C) ?*c_void; extern fn realloc(c_ptr: [*]align(@alignOf(u128)) u8, size: usize) callconv(.C) ?*c_void; extern fn free(c_ptr: [*]align(@alignOf(u128)) u8) callconv(.C) void; -export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { +export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void { return malloc(size); } -export fn roc_realloc(alignment: u32, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) ?*c_void { +export fn roc_realloc(c_ptr: *c_void, old_size: usize, new_size: usize, alignment: u32) callconv(.C) ?*c_void { return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size); } -export fn roc_dealloc(alignment: u32, c_ptr: *c_void) callconv(.C) void { +export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { free(@alignCast(16, @ptrCast([*]u8, c_ptr))); } diff --git a/examples/quicksort/platform/host.zig b/examples/quicksort/platform/host.zig index 026253378c..6eaf3d1b91 100644 --- a/examples/quicksort/platform/host.zig +++ b/examples/quicksort/platform/host.zig @@ -26,15 +26,15 @@ extern fn malloc(size: usize) callconv(.C) ?*c_void; extern fn realloc(c_ptr: [*]align(@alignOf(u128)) u8, size: usize) callconv(.C) ?*c_void; extern fn free(c_ptr: [*]align(@alignOf(u128)) u8) callconv(.C) void; -export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { +export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void { return malloc(size); } -export fn roc_realloc(alignment: u32, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) ?*c_void { +export fn roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) ?*c_void { return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size); } -export fn roc_dealloc(alignment: u32, c_ptr: *c_void) callconv(.C) void { +export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { free(@alignCast(16, @ptrCast([*]u8, c_ptr))); } diff --git a/examples/shared-quicksort/platform/src/lib.rs b/examples/shared-quicksort/platform/src/lib.rs index b7529b1f56..a6d1eb4d9f 100644 --- a/examples/shared-quicksort/platform/src/lib.rs +++ b/examples/shared-quicksort/platform/src/lib.rs @@ -15,22 +15,22 @@ extern "C" { } #[no_mangle] -pub unsafe fn roc_alloc(_alignment: u32, size: usize) -> *mut c_void { +pub unsafe fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void { return malloc(size); } #[no_mangle] pub unsafe fn roc_realloc( - _alignment: u32, c_ptr: *mut c_void, - _old_size: usize, new_size: usize, + _old_size: usize, + _alignment: u32, ) -> *mut c_void { return realloc(c_ptr, new_size); } #[no_mangle] -pub unsafe fn roc_dealloc(_alignment: u32, c_ptr: *mut c_void) { +pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) { return free(c_ptr); } diff --git a/examples/task/platform/host.zig b/examples/task/platform/host.zig index f573ac4b0e..3bef60306c 100644 --- a/examples/task/platform/host.zig +++ b/examples/task/platform/host.zig @@ -32,15 +32,15 @@ extern fn malloc(size: usize) callconv(.C) ?*c_void; extern fn realloc(c_ptr: [*]align(@alignOf(u128)) u8, size: usize) callconv(.C) ?*c_void; extern fn free(c_ptr: [*]align(@alignOf(u128)) u8) callconv(.C) void; -export fn roc_alloc(alignment: u32, size: usize) callconv(.C) ?*c_void { +export fn roc_alloc(size: usize, alignment: u32) callconv(.C) ?*c_void { return malloc(size); } -export fn roc_realloc(alignment: u32, c_ptr: *c_void, old_size: usize, new_size: usize) callconv(.C) ?*c_void { +export fn roc_realloc(c_ptr: *c_void, new_size: usize, old_size: usize, alignment: u32) callconv(.C) ?*c_void { return realloc(@alignCast(16, @ptrCast([*]u8, c_ptr)), new_size); } -export fn roc_dealloc(alignment: u32, c_ptr: *c_void) callconv(.C) void { +export fn roc_dealloc(c_ptr: *c_void, alignment: u32) callconv(.C) void { free(@alignCast(16, @ptrCast([*]u8, c_ptr))); } diff --git a/examples/tea/platform/src/lib.rs b/examples/tea/platform/src/lib.rs index 1a8f6b9446..3ed1a9e7cb 100644 --- a/examples/tea/platform/src/lib.rs +++ b/examples/tea/platform/src/lib.rs @@ -67,22 +67,22 @@ extern "C" { } #[no_mangle] -pub unsafe fn roc_alloc(_alignment: u32, size: usize) -> *mut c_void { +pub unsafe fn roc_alloc(size: usize, _alignment: u32) -> *mut c_void { return malloc(size); } #[no_mangle] pub unsafe fn roc_realloc( - _alignment: u32, c_ptr: *mut c_void, - _old_size: usize, new_size: usize, + _old_size: usize, + _alignment: u32, ) -> *mut c_void { return realloc(c_ptr, new_size); } #[no_mangle] -pub unsafe fn roc_dealloc(_alignment: u32, c_ptr: *mut c_void) { +pub unsafe fn roc_dealloc(c_ptr: *mut c_void, _alignment: u32) { return free(c_ptr); } diff --git a/roc_std/src/lib.rs b/roc_std/src/lib.rs index 0296d12ea3..ada0cd8a55 100644 --- a/roc_std/src/lib.rs +++ b/roc_std/src/lib.rs @@ -9,14 +9,14 @@ pub mod alloca; extern "C" { pub fn printf(format: *const u8, ...) -> i32; - pub fn roc_alloc(alignment: usize, size: usize) -> *mut c_void; + pub fn roc_alloc(size: usize, alignment: u32) -> *mut c_void; pub fn roc_realloc( - alignment: usize, ptr: *mut c_void, - old_size: usize, new_size: usize, + old_size: usize, + alignment: u32, ) -> *mut c_void; - pub fn roc_dealloc(alignment: usize, ptr: *mut c_void); + pub fn roc_dealloc(ptr: *mut c_void, alignment: u32); } const REFCOUNT_1: usize = isize::MIN as usize; @@ -148,7 +148,7 @@ impl RocList { let num_bytes = core::mem::size_of::() + padding + element_bytes; let elements = unsafe { - let raw_ptr = roc_alloc(core::mem::size_of::(), num_bytes) as *mut u8; + let raw_ptr = roc_alloc(num_bytes, core::mem::size_of::() as u32) as *mut u8; // pointer to the first element let raw_ptr = Self::get_element_ptr(raw_ptr as *mut T) as *mut T; @@ -379,7 +379,7 @@ impl RocStr { let num_bytes = core::mem::size_of::() + element_bytes; let elements = unsafe { - let raw_ptr = roc_alloc(core::mem::size_of::(), num_bytes) as *mut u8; + let raw_ptr = roc_alloc(num_bytes, core::mem::size_of::() as u32) as *mut u8; // write the capacity let capacity_ptr = raw_ptr as *mut usize; *capacity_ptr = capacity; @@ -461,7 +461,7 @@ impl Clone for RocStr { let capacity_size = core::mem::size_of::(); let copy_length = self.length + capacity_size; let elements = unsafe { - let raw = roc_alloc(core::mem::size_of::(), copy_length); + let raw = roc_alloc(copy_length, core::mem::size_of::() as u32); libc::memcpy( raw,