Skip to content

Commit

Permalink
Use ptr::addr_of! to avoid creating references to block ISAs
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jan 7, 2024
1 parent 498904a commit b733009
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crates/block2/src/concrete_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl<A, R, F> ConcreteBlock<A, R, F> {
/// correct arguments.
unsafe fn with_invoke(invoke: unsafe extern "C" fn(), closure: F) -> Self {
let layout = abi::Block_layout {
isa: unsafe { &ffi::_NSConcreteStackBlock },
isa: unsafe { ptr::addr_of!(ffi::_NSConcreteStackBlock) },
flags: Self::FLAGS,
reserved: 0,
invoke: Some(invoke),
Expand Down
13 changes: 8 additions & 5 deletions crates/block2/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ struct Isa(*const ffi::Class);

impl Isa {
fn is_global(self) -> bool {
ptr::eq(unsafe { &ffi::_NSConcreteGlobalBlock }, self.0)
ptr::eq(
unsafe { ptr::addr_of!(ffi::_NSConcreteGlobalBlock) },
self.0,
)
}

fn is_stack(self) -> bool {
ptr::eq(unsafe { &ffi::_NSConcreteStackBlock }, self.0)
ptr::eq(unsafe { ptr::addr_of!(ffi::_NSConcreteStackBlock) }, self.0)
}
}

Expand Down Expand Up @@ -216,13 +219,13 @@ mod tests {

#[test]
fn test_isa() {
let isa = Isa(unsafe { &ffi::_NSConcreteGlobalBlock });
let isa = Isa(unsafe { ptr::addr_of!(ffi::_NSConcreteGlobalBlock) });
assert!(isa.is_global());
assert!(!isa.is_stack());
let isa = Isa(unsafe { &ffi::_NSConcreteStackBlock });
let isa = Isa(unsafe { ptr::addr_of!(ffi::_NSConcreteStackBlock) });
assert!(!isa.is_global());
assert!(isa.is_stack());
let isa = Isa(unsafe { &ffi::private::_NSConcreteMallocBlock });
let isa = Isa(unsafe { ptr::addr_of!(ffi::private::_NSConcreteMallocBlock) });
assert!(!isa.is_global());
assert!(!isa.is_stack());
let isa = Isa(ptr::null());
Expand Down
8 changes: 5 additions & 3 deletions crates/block2/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ mod tests {

#[test]
fn test_linkable() {
println!("{:p}", unsafe { &_NSConcreteGlobalBlock });
println!("{:p}", unsafe { &_NSConcreteStackBlock });
println!("{:p}", unsafe { &private::_NSConcreteMallocBlock });
println!("{:?}", unsafe { ptr::addr_of!(_NSConcreteGlobalBlock) });
println!("{:?}", unsafe { ptr::addr_of!(_NSConcreteStackBlock) });
println!("{:?}", unsafe {
ptr::addr_of!(private::_NSConcreteMallocBlock)
});
println!("{:p}", _Block_copy as unsafe extern "C" fn(_) -> _);
println!(
"{:p}",
Expand Down
2 changes: 1 addition & 1 deletion crates/block2/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ macro_rules! global_block {
#[allow(unused_unsafe)]
$vis static $name: $crate::GlobalBlock<($($t,)*) $(, $r)?> = unsafe {
let mut layout = $crate::GlobalBlock::<($($t,)*) $(, $r)?>::__DEFAULT_LAYOUT;
layout.isa = &$crate::ffi::_NSConcreteGlobalBlock;
layout.isa = ::core::ptr::addr_of!($crate::ffi::_NSConcreteGlobalBlock);
layout.invoke = ::core::option::Option::Some({
unsafe extern "C" fn inner(_: *mut $crate::__Block_layout, $($a: $t),*) $(-> $r)? {
$body
Expand Down

0 comments on commit b733009

Please sign in to comment.