Skip to content

Commit

Permalink
Merge pull request #7280 from Giesch/main
Browse files Browse the repository at this point in the history
add RoxBox::as_refcount_ptr
  • Loading branch information
lukewilliamboswell authored Dec 2, 2024
2 parents dea9101 + 89ce3d6 commit f2d6111
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions crates/roc_std/src/roc_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use core::{
ptr::{self, NonNull},
};

use std::os::raw::c_void;

#[repr(C)]
pub struct RocBox<T>
where
Expand Down Expand Up @@ -69,16 +71,19 @@ where
}

fn storage(&self) -> &Cell<Storage> {
let alignment = Self::alloc_alignment();
unsafe { &*self.as_refcount_ptr().cast::<Cell<Storage>>() }
}

unsafe {
&*self
.contents
.as_ptr()
.cast::<u8>()
.sub(alignment)
.cast::<Cell<Storage>>()
}
/// 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::<u8>().sub(alignment) };
with_offset as *mut c_void
}
}

Expand Down

0 comments on commit f2d6111

Please sign in to comment.