mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 04:08:19 +00:00
Change list.deinit to take alignment directly
In doing so remove the many class directly to utils.decref
This commit is contained in:
parent
0a3b2e25a1
commit
440c0518f1
2 changed files with 38 additions and 41 deletions
|
@ -75,8 +75,8 @@ pub const RocList = extern struct {
|
|||
return list;
|
||||
}
|
||||
|
||||
pub fn deinit(self: RocList, comptime T: type) void {
|
||||
utils.decref(self.bytes, self.capacity, @alignOf(T));
|
||||
pub fn deinit(self: RocList, alignment: u32) void {
|
||||
utils.decref(self.bytes, self.capacity, alignment);
|
||||
}
|
||||
|
||||
pub fn elements(self: RocList, comptime T: type) ?[*]T {
|
||||
|
@ -127,9 +127,8 @@ pub const RocList = extern struct {
|
|||
const number_of_bytes = self.len() * element_width;
|
||||
@memcpy(new_bytes, old_bytes, number_of_bytes);
|
||||
|
||||
// NOTE we fuse an increment of all keys/values with a decrement of the input dict
|
||||
const data_bytes = self.capacity * element_width;
|
||||
utils.decref(self.bytes, data_bytes, alignment);
|
||||
// NOTE we fuse an increment of all keys/values with a decrement of the input list.
|
||||
self.deinit(alignment);
|
||||
|
||||
return new_list;
|
||||
}
|
||||
|
@ -193,7 +192,7 @@ pub const RocList = extern struct {
|
|||
@memset(dest_ptr + old_length * element_width, 0, delta_length * element_width);
|
||||
}
|
||||
|
||||
utils.decref(self.bytes, old_length * element_width, alignment);
|
||||
self.deinit(alignment);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -574,7 +573,7 @@ pub fn listSublist(
|
|||
|
||||
@memcpy(target_ptr, source_ptr + start * element_width, keep_len * element_width);
|
||||
|
||||
utils.decref(list.bytes, size * element_width, alignment);
|
||||
list.deinit(alignment);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -607,7 +606,7 @@ pub fn listDropAt(
|
|||
// because we rely on the pointer field being null if the list is empty
|
||||
// which also requires duplicating the utils.decref call to spend the RC token
|
||||
if (size < 2) {
|
||||
utils.decref(list.bytes, size * element_width, alignment);
|
||||
list.deinit(alignment);
|
||||
return RocList.empty();
|
||||
}
|
||||
|
||||
|
@ -637,7 +636,7 @@ pub fn listDropAt(
|
|||
const tail_size = (size - drop_index - 1) * element_width;
|
||||
@memcpy(tail_target, tail_source, tail_size);
|
||||
|
||||
utils.decref(list.bytes, size * element_width, alignment);
|
||||
list.deinit(alignment);
|
||||
|
||||
return output;
|
||||
} else {
|
||||
|
@ -751,7 +750,7 @@ pub fn listConcat(list_a: RocList, list_b: RocList, alignment: u32, element_widt
|
|||
return list_b;
|
||||
} else {
|
||||
// we must consume this list. Even though it has no elements, it could still have capacity
|
||||
list_b.deinit(usize);
|
||||
list_b.deinit(alignment);
|
||||
|
||||
return list_a;
|
||||
}
|
||||
|
@ -766,7 +765,7 @@ pub fn listConcat(list_a: RocList, list_b: RocList, alignment: u32, element_widt
|
|||
@memcpy(source_a + list_a.len() * element_width, source_b, list_b.len() * element_width);
|
||||
|
||||
// decrement list b.
|
||||
utils.decref(source_b, list_b.len(), alignment);
|
||||
list_b.deinit(alignment);
|
||||
|
||||
return resized_list_a;
|
||||
} else if (list_b.isUnique()) {
|
||||
|
@ -787,7 +786,7 @@ pub fn listConcat(list_a: RocList, list_b: RocList, alignment: u32, element_widt
|
|||
@memcpy(source_b, source_a, byte_count_a);
|
||||
|
||||
// decrement list a.
|
||||
utils.decref(source_a, list_a.capacity, alignment);
|
||||
list_a.deinit(alignment);
|
||||
|
||||
return resized_list_b;
|
||||
}
|
||||
|
@ -804,8 +803,8 @@ pub fn listConcat(list_a: RocList, list_b: RocList, alignment: u32, element_widt
|
|||
@memcpy(target + list_a.len() * element_width, source_b, list_b.len() * element_width);
|
||||
|
||||
// decrement list a and b.
|
||||
utils.decref(source_a, list_a.capacity, alignment);
|
||||
utils.decref(source_b, list_b.capacity, alignment);
|
||||
list_a.deinit(alignment);
|
||||
list_b.deinit(alignment);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -874,14 +873,14 @@ test "listConcat: non-unique with unique overlapping" {
|
|||
const ptr_width = @sizeOf(usize);
|
||||
const refcount_ptr = @ptrCast([*]isize, @alignCast(ptr_width, bytes) - ptr_width);
|
||||
utils.increfC(&refcount_ptr[0], 1);
|
||||
defer nonUnique.deinit(u8); // listConcat will dec the other refcount
|
||||
defer nonUnique.deinit(@sizeOf(u8)); // listConcat will dec the other refcount
|
||||
|
||||
var unique = RocList.fromSlice(u8, ([_]u8{ 2, 3, 4 })[0..]);
|
||||
defer unique.deinit(u8);
|
||||
defer unique.deinit(@sizeOf(u8));
|
||||
|
||||
var concatted = listConcat(nonUnique, unique, 1, 1);
|
||||
var wanted = RocList.fromSlice(u8, ([_]u8{ 1, 2, 3, 4 })[0..]);
|
||||
defer wanted.deinit(u8);
|
||||
defer wanted.deinit(@sizeOf(u8));
|
||||
|
||||
try expect(concatted.eql(wanted));
|
||||
}
|
||||
|
|
|
@ -623,7 +623,7 @@ test "strToScalars: empty string" {
|
|||
|
||||
const expected = RocList.empty();
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -634,10 +634,10 @@ test "strToScalars: One ASCII char" {
|
|||
|
||||
const expected_array = [_]u32{82};
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -648,10 +648,10 @@ test "strToScalars: Multiple ASCII chars" {
|
|||
|
||||
const expected_array = [_]u32{ 82, 111, 99, 33 };
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -662,10 +662,10 @@ test "strToScalars: One 2-byte UTF-8 character" {
|
|||
|
||||
const expected_array = [_]u32{233};
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -676,10 +676,10 @@ test "strToScalars: Multiple 2-byte UTF-8 characters" {
|
|||
|
||||
const expected_array = [_]u32{ 67, 228, 102, 233, 115 };
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -690,10 +690,10 @@ test "strToScalars: One 3-byte UTF-8 character" {
|
|||
|
||||
const expected_array = [_]u32{40527};
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -704,10 +704,10 @@ test "strToScalars: Multiple 3-byte UTF-8 characters" {
|
|||
|
||||
const expected_array = [_]u32{ 40527, 24456, 26377, 36259 };
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -719,10 +719,10 @@ test "strToScalars: One 4-byte UTF-8 character" {
|
|||
|
||||
const expected_array = [_]u32{73728};
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -734,10 +734,10 @@ test "strToScalars: Multiple 4-byte UTF-8 characters" {
|
|||
|
||||
const expected_array = [_]u32{ 73728, 73729 };
|
||||
const expected = RocList.fromSlice(u32, expected_array[0..expected_array.len]);
|
||||
defer RocList.deinit(expected, u32);
|
||||
defer expected.deinit(@sizeOf(u32));
|
||||
|
||||
const actual = strToScalars(str);
|
||||
defer RocList.deinit(actual, u32);
|
||||
defer actual.deinit(@sizeOf(u32));
|
||||
|
||||
try expect(RocList.eql(actual, expected));
|
||||
}
|
||||
|
@ -1377,7 +1377,7 @@ fn graphemesTest(input: []const u8, expected: []const []const u8) !void {
|
|||
try expectEqual(expected.len, count);
|
||||
|
||||
const graphemes = strGraphemes(rocstr);
|
||||
defer graphemes.deinit(u8);
|
||||
defer graphemes.deinit(@sizeOf(u8));
|
||||
if (input.len == 0) return; // empty string
|
||||
const elems = graphemes.elements(RocStr) orelse unreachable;
|
||||
for (expected) |g, i| {
|
||||
|
@ -1802,8 +1802,7 @@ inline fn fromUtf8(arg: RocList, update_mode: UpdateMode) FromUtf8Result {
|
|||
const string = RocStr.init(@ptrCast([*]u8, arg.bytes), arg.len());
|
||||
|
||||
// then decrement the input list
|
||||
const data_bytes = arg.len();
|
||||
utils.decref(arg.bytes, data_bytes, RocStr.alignment);
|
||||
arg.deinit(RocStr.alignment);
|
||||
|
||||
return FromUtf8Result{
|
||||
.is_ok = true,
|
||||
|
@ -1831,8 +1830,7 @@ inline fn fromUtf8(arg: RocList, update_mode: UpdateMode) FromUtf8Result {
|
|||
const temp = errorToProblem(@ptrCast([*]u8, arg.bytes), arg.length);
|
||||
|
||||
// consume the input list
|
||||
const data_bytes = arg.len();
|
||||
utils.decref(arg.bytes, data_bytes, RocStr.alignment);
|
||||
arg.deinit(RocStr.alignment);
|
||||
|
||||
return FromUtf8Result{
|
||||
.is_ok = false,
|
||||
|
@ -1879,7 +1877,7 @@ pub fn fromUtf8Range(arg: RocList, start: usize, count: usize, update_mode: Upda
|
|||
const string = RocStr.init(@ptrCast([*]const u8, bytes), count);
|
||||
|
||||
// decref the list
|
||||
utils.decref(arg.bytes, arg.len(), 1);
|
||||
arg.deinit(RocStr.alignment);
|
||||
|
||||
return FromUtf8Result{
|
||||
.is_ok = true,
|
||||
|
@ -1892,7 +1890,7 @@ pub fn fromUtf8Range(arg: RocList, start: usize, count: usize, update_mode: Upda
|
|||
const temp = errorToProblem(@ptrCast([*]u8, arg.bytes), arg.length);
|
||||
|
||||
// decref the list
|
||||
utils.decref(arg.bytes, arg.len(), 1);
|
||||
arg.deinit(RocStr.alignment);
|
||||
|
||||
return FromUtf8Result{
|
||||
.is_ok = false,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue