diff --git a/crates/roc_std/src/roc_box.rs b/crates/roc_std/src/roc_box.rs index 688525afca7..a6995f3902f 100644 --- a/crates/roc_std/src/roc_box.rs +++ b/crates/roc_std/src/roc_box.rs @@ -10,6 +10,8 @@ use core::{ ptr::{self, NonNull}, }; +use std::os::raw::c_void; + #[repr(C)] pub struct RocBox where @@ -69,16 +71,19 @@ where } fn storage(&self) -> &Cell { - let alignment = Self::alloc_alignment(); + unsafe { &*self.as_refcount_ptr().cast::>() } + } - unsafe { - &*self - .contents - .as_ptr() - .cast::() - .sub(alignment) - .cast::>() - } + /// The raw pointer to a roc box, including the leading refcount + /// Intended for use by platforms in roc_dealloc + /// + /// # Safety + /// + /// Returns a raw pointer to the roc box + pub unsafe fn as_refcount_ptr(&self) -> *mut c_void { + let alignment = Self::alloc_alignment(); + let with_offset = unsafe { self.contents.as_ptr().cast::().sub(alignment) }; + with_offset as *mut c_void } }