smarter alignment code

This commit is contained in:
Folkert 2021-08-31 22:37:32 +02:00
parent 106f1311d2
commit a810c2c27b

View File

@ -161,7 +161,7 @@ pub fn allocateWithRefcount(
switch (alignment) {
16 => {
const length = 2 * @sizeOf(usize) + data_bytes;
const length = std.math.max(alignment, @sizeOf(usize)) + data_bytes;
var new_bytes: [*]align(16) u8 = @alignCast(16, alloc(length, alignment));
@ -179,10 +179,47 @@ pub fn allocateWithRefcount(
return first_slot;
},
8 => {
const length = std.math.max(alignment, @sizeOf(usize)) + data_bytes;
var raw = alloc(length, alignment);
var new_bytes: [*]align(8) u8 = @alignCast(8, raw);
var as_isize_array = @ptrCast([*]isize, new_bytes);
if (result_in_place) {
as_isize_array[0] = @intCast(isize, number_of_slots);
} else {
as_isize_array[0] = REFCOUNT_ONE_ISIZE;
}
var as_u8_array = @ptrCast([*]u8, new_bytes);
const first_slot = as_u8_array + alignment;
return first_slot;
},
4 => {
const length = std.math.max(alignment, @sizeOf(usize)) + data_bytes;
var raw = alloc(length, alignment);
var new_bytes: [*]align(4) u8 = @alignCast(4, raw);
var as_isize_array = @ptrCast([*]isize, new_bytes);
if (result_in_place) {
as_isize_array[0] = @intCast(isize, number_of_slots);
} else {
as_isize_array[0] = REFCOUNT_ONE_ISIZE;
}
var as_u8_array = @ptrCast([*]u8, new_bytes);
const first_slot = as_u8_array + alignment;
return first_slot;
},
else => {
const length = @sizeOf(usize) + data_bytes;
var new_bytes: [*]align(8) u8 = @alignCast(8, alloc(length, alignment));
var raw = alloc(length, alignment);
var new_bytes: [*]align(8) u8 = @alignCast(8, raw);
var as_isize_array = @ptrCast([*]isize, new_bytes);
if (result_in_place) {