mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 14:54:47 +00:00
simplify allocate_with_refcount to be closer to the old llvm version
This commit is contained in:
parent
eea8eeb1b6
commit
a1c469232f
1 changed files with 7 additions and 47 deletions
|
@ -226,57 +226,17 @@ pub fn allocateWithRefcount(
|
||||||
data_bytes: usize,
|
data_bytes: usize,
|
||||||
element_alignment: u32,
|
element_alignment: u32,
|
||||||
) [*]u8 {
|
) [*]u8 {
|
||||||
const alignment = std.math.max(@sizeOf(usize), element_alignment);
|
const ptr_width = @sizeOf(usize);
|
||||||
const first_slot_offset = std.math.max(@sizeOf(usize), element_alignment);
|
const alignment = std.math.max(ptr_width, element_alignment);
|
||||||
const length = alignment + data_bytes;
|
const length = alignment + data_bytes;
|
||||||
|
|
||||||
switch (alignment) {
|
var new_bytes: [*]u8 = alloc(length, alignment) orelse unreachable;
|
||||||
16 => {
|
|
||||||
// TODO handle alloc failing!
|
|
||||||
var new_bytes: [*]align(16) u8 = @alignCast(16, alloc(length, alignment) orelse unreachable);
|
|
||||||
|
|
||||||
var as_usize_array = @ptrCast([*]usize, new_bytes);
|
const data_ptr = new_bytes + alignment;
|
||||||
as_usize_array[0] = 0;
|
const refcount_ptr = @ptrCast([*]usize, @alignCast(ptr_width, data_ptr) - ptr_width);
|
||||||
as_usize_array[1] = if (RC_TYPE == Refcount.none) REFCOUNT_MAX_ISIZE else REFCOUNT_ONE;
|
refcount_ptr[0] = if (RC_TYPE == Refcount.none) REFCOUNT_MAX_ISIZE else REFCOUNT_ONE;
|
||||||
|
|
||||||
var as_u8_array = @ptrCast([*]u8, new_bytes);
|
return data_ptr;
|
||||||
const first_slot = as_u8_array + first_slot_offset;
|
|
||||||
|
|
||||||
return first_slot;
|
|
||||||
},
|
|
||||||
8 => {
|
|
||||||
// TODO handle alloc failing!
|
|
||||||
var raw = alloc(length, alignment) orelse unreachable;
|
|
||||||
var new_bytes: [*]align(8) u8 = @alignCast(8, raw);
|
|
||||||
|
|
||||||
var as_isize_array = @ptrCast([*]isize, new_bytes);
|
|
||||||
as_isize_array[0] = if (RC_TYPE == Refcount.none) REFCOUNT_MAX_ISIZE else REFCOUNT_ONE_ISIZE;
|
|
||||||
|
|
||||||
var as_u8_array = @ptrCast([*]u8, new_bytes);
|
|
||||||
const first_slot = as_u8_array + first_slot_offset;
|
|
||||||
|
|
||||||
return first_slot;
|
|
||||||
},
|
|
||||||
4 => {
|
|
||||||
// TODO handle alloc failing!
|
|
||||||
var raw = alloc(length, alignment) orelse unreachable;
|
|
||||||
var new_bytes: [*]align(@alignOf(isize)) u8 = @alignCast(@alignOf(isize), raw);
|
|
||||||
|
|
||||||
var as_isize_array = @ptrCast([*]isize, new_bytes);
|
|
||||||
as_isize_array[0] = if (RC_TYPE == Refcount.none) REFCOUNT_MAX_ISIZE else REFCOUNT_ONE_ISIZE;
|
|
||||||
|
|
||||||
var as_u8_array = @ptrCast([*]u8, new_bytes);
|
|
||||||
const first_slot = as_u8_array + first_slot_offset;
|
|
||||||
|
|
||||||
return first_slot;
|
|
||||||
},
|
|
||||||
else => {
|
|
||||||
// const stdout = std.io.getStdOut().writer();
|
|
||||||
// stdout.print("alignment: {d}", .{alignment}) catch unreachable;
|
|
||||||
// @panic("allocateWithRefcount with invalid alignment");
|
|
||||||
unreachable;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const CSlice = extern struct {
|
pub const CSlice = extern struct {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue