cleanup constant refcount checking

This commit is contained in:
Brendan Hansknecht 2025-01-01 12:28:16 -08:00
parent e96affe992
commit c6d594a758
No known key found for this signature in database
GPG key ID: 0EA784685083E75B

View file

@ -210,7 +210,7 @@ pub fn increfRcPtrC(ptr_to_refcount: *isize, amount: isize) callconv(.C) void {
// Ensure that the refcount is not whole program lifetime.
const refcount: isize = ptr_to_refcount.*;
if (refcount != REFCOUNT_MAX_ISIZE) {
if (!rcConstant(refcount)) {
// Note: we assume that a refcount will never overflow.
// As such, we do not need to cap incrementing.
switch (RC_TYPE) {
@ -373,7 +373,7 @@ inline fn decref_ptr_to_refcount(
// Ensure that the refcount is not whole program lifetime.
const refcount: isize = refcount_ptr[0];
if (refcount != REFCOUNT_MAX_ISIZE) {
if (!rcConstant(refcount)) {
switch (RC_TYPE) {
.normal => {
const old = @as(usize, @bitCast(refcount));
@ -441,6 +441,20 @@ pub fn rcUnique(refcount: isize) bool {
}
}
pub fn rcConstant(refcount: isize) bool {
switch (RC_TYPE) {
.normal => {
return refcount == REFCOUNT_MAX_ISIZE;
},
.atomic => {
return refcount & REFCOUNT_VALUE_MASK == REFCOUNT_MAX_ISIZE & REFCOUNT_VALUE_MASK;
},
.none => {
return true;
},
}
}
// We follow roughly the [fbvector](https://github.com/facebook/folly/blob/main/folly/docs/FBVector.md) when it comes to growing a RocList.
// Here is [their growth strategy](https://github.com/facebook/folly/blob/3e0525988fd444201b19b76b390a5927c15cb697/folly/FBVector.h#L1128) for push_back:
//