add refcount constants

This commit is contained in:
Brendan Hansknecht 2024-12-28 15:01:00 -08:00
parent ad41b509cf
commit a7e4a55e1e
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
3 changed files with 11 additions and 6 deletions

View file

@ -582,6 +582,9 @@ impl Hash for U128 {
}
}
pub const ROC_REFCOUNT_CONSTANT: usize = 0;
pub const ROC_REFCOUNT_ONE: usize = isize::MIN as usize;
/// All Roc types that are refcounted must implement this trait.
///
/// For aggregate types, this must recurse down the structure.

View file

@ -14,7 +14,9 @@ use core::{
};
use std::{cmp::max, ops::Range};
use crate::{roc_alloc, roc_dealloc, roc_realloc, storage::Storage, RocRefcounted};
use crate::{
roc_alloc, roc_dealloc, roc_realloc, storage::Storage, RocRefcounted, ROC_REFCOUNT_CONSTANT,
};
#[cfg(feature = "serde")]
use core::marker::PhantomData;
@ -680,7 +682,7 @@ where
unsafe {
let value = std::ptr::read(ptr);
// Only safe to write to the pointer if it is not constant (0)
if value != 0 {
if value != ROC_REFCOUNT_CONSTANT {
std::ptr::write(ptr, (value as isize + 1) as usize);
}
}

View file

@ -21,7 +21,7 @@ use core::{
use std::ffi::{CStr, CString};
use std::{ops::Range, ptr::NonNull};
use crate::{roc_realloc, RocList, RocRefcounted};
use crate::{roc_realloc, RocList, RocRefcounted, ROC_REFCOUNT_CONSTANT};
#[repr(transparent)]
pub struct RocStr(RocStrInner);
@ -944,8 +944,8 @@ impl BigString {
let ptr = self.ptr_to_refcount();
// Only safe to write to the pointer if it is not constant (0)
if unsafe { std::ptr::read(ptr) } != 0 {
unsafe { std::ptr::write(ptr, 0) }
if unsafe { std::ptr::read(ptr) } != ROC_REFCOUNT_CONSTANT {
unsafe { std::ptr::write(ptr, ROC_REFCOUNT_CONSTANT) }
}
}
@ -954,7 +954,7 @@ impl BigString {
unsafe {
let value = std::ptr::read(ptr);
// Only safe to write to the pointer if it is not constant (0)
if value != 0 {
if value != ROC_REFCOUNT_CONSTANT {
std::ptr::write(ptr, (value as isize + 1) as usize);
}
}