Simplify Refcounting

Instead of -max_size to -1 for regular refcounts, use 1 to max_size.
0 still means constant refcount.
The highest bit is used to signify atomic refcounting required.
This does not turn on any sort of atomic refcounting.
This commit is contained in:
Brendan Hansknecht 2024-12-31 17:35:46 -08:00
parent 8001de5468
commit 4b8693537a
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
15 changed files with 57 additions and 84 deletions

View file

@ -179,23 +179,19 @@ pub const RocList = extern struct {
}
pub fn isUnique(self: RocList) bool {
return self.refcountMachine() == utils.REFCOUNT_ONE;
return utils.rcUnique(@bitCast(self.refcount()));
}
fn refcountMachine(self: RocList) usize {
fn refcount(self: RocList) usize {
if (self.getCapacity() == 0 and !self.isSeamlessSlice()) {
// the zero-capacity is Clone, copying it will not leak memory
return utils.REFCOUNT_ONE;
return 1;
}
const ptr: [*]usize = @as([*]usize, @ptrCast(@alignCast(self.getAllocationDataPtr())));
return (ptr - 1)[0];
}
fn refcountHuman(self: RocList) usize {
return self.refcountMachine() - utils.REFCOUNT_ONE + 1;
}
pub fn makeUniqueExtra(self: RocList, alignment: u32, element_width: usize, elements_refcounted: bool, dec: Dec, update_mode: UpdateMode) RocList {
if (update_mode == .InPlace) {
return self;