From dccc0f68da5d28150fc0e96a268642dc4117a649 Mon Sep 17 00:00:00 2001 From: Dan Knutson <19314778+Giesch@users.noreply.github.com> Date: Sat, 30 Nov 2024 10:56:58 -0600 Subject: [PATCH 1/4] add RoxBox::as_ptr --- crates/roc_std/src/roc_box.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/roc_std/src/roc_box.rs b/crates/roc_std/src/roc_box.rs index 688525afca7..d72c6fa3b8e 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,15 @@ where } fn storage(&self) -> &Cell { - let alignment = Self::alloc_alignment(); + unsafe { &*self.as_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 + pub unsafe fn as_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 } } From 1036695b773ddea5ba717508c179d1aee8bbe41f Mon Sep 17 00:00:00 2001 From: Dan Gieschen Knutson <19314778+Giesch@users.noreply.github.com> Date: Sat, 30 Nov 2024 13:04:41 -0600 Subject: [PATCH 2/4] Update crates/roc_std/src/roc_box.rs --- crates/roc_std/src/roc_box.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/roc_std/src/roc_box.rs b/crates/roc_std/src/roc_box.rs index d72c6fa3b8e..91609bdbceb 100644 --- a/crates/roc_std/src/roc_box.rs +++ b/crates/roc_std/src/roc_box.rs @@ -76,7 +76,7 @@ where /// The raw pointer to a roc box, including the leading refcount /// Intended for use by platforms in roc_dealloc - pub unsafe fn as_ptr(&self) -> *mut c_void { + 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 From 346014d55c5f13a8821aa754a5908a539204e649 Mon Sep 17 00:00:00 2001 From: Dan Knutson <19314778+Giesch@users.noreply.github.com> Date: Sat, 30 Nov 2024 13:33:37 -0600 Subject: [PATCH 3/4] fix rename --- crates/roc_std/src/roc_box.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/roc_std/src/roc_box.rs b/crates/roc_std/src/roc_box.rs index 91609bdbceb..31338e89b2f 100644 --- a/crates/roc_std/src/roc_box.rs +++ b/crates/roc_std/src/roc_box.rs @@ -71,7 +71,7 @@ where } fn storage(&self) -> &Cell { - unsafe { &*self.as_ptr().cast::>() } + unsafe { &*self.as_refcount_ptr().cast::>() } } /// The raw pointer to a roc box, including the leading refcount From 89ce3d61d8b964989e181ed18d9f63128b45e71b Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Mon, 2 Dec 2024 14:31:19 +1100 Subject: [PATCH 4/4] add Safety section to roc_box as_refcount_ptr() --- crates/roc_std/src/roc_box.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/roc_std/src/roc_box.rs b/crates/roc_std/src/roc_box.rs index 31338e89b2f..a6995f3902f 100644 --- a/crates/roc_std/src/roc_box.rs +++ b/crates/roc_std/src/roc_box.rs @@ -76,6 +76,10 @@ where /// 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) };