Fix dec ref for empty list

This commit is contained in:
tarjei 2021-05-22 00:54:03 +02:00
parent 85e5b0ff82
commit e062bdaad8

View file

@ -665,24 +665,32 @@ pub fn listAppend(list: RocList, alignment: usize, element: Opaque, element_widt
return output; return output;
} }
pub fn listDrop(list: RocList, alignment: usize, element_width: usize, drop_count: usize, dec: Dec) callconv(.C) RocList { pub fn listDrop(
const size = list.len(); list: RocList,
const keep_count = size - drop_count; alignment: usize,
if (size <= drop_count) { element_width: usize,
return RocList.empty(); drop_count: usize,
} dec: Dec,
) callconv(.C) RocList {
if (list.bytes) |source_ptr| { if (list.bytes) |source_ptr| {
const size = list.len();
const keep_count = size - drop_count;
var i: usize = 0;
while (i < std.math.min(drop_count, size)) : (i += 1) {
const element = source_ptr + i * element_width;
dec(element);
}
if (drop_count >= size) {
return RocList.empty();
}
const output = RocList.allocate(std.heap.c_allocator, alignment, keep_count, element_width); const output = RocList.allocate(std.heap.c_allocator, alignment, keep_count, element_width);
const target_ptr = output.bytes orelse unreachable; const target_ptr = output.bytes orelse unreachable;
@memcpy(target_ptr, source_ptr + drop_count * element_width, keep_count * element_width); @memcpy(target_ptr, source_ptr + drop_count * element_width, keep_count * element_width);
var i: usize = 0;
while (i < drop_count) : (i += 1) {
const element = source_ptr + i * element_width;
dec(element);
}
utils.decref(std.heap.c_allocator, alignment, list.bytes, size * element_width); utils.decref(std.heap.c_allocator, alignment, list.bytes, size * element_width);
return output; return output;