Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use std::marker::PhantomData;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::os::raw::{c_int, c_ulong, c_void};
use std::ptr;

enum Class { }

Expand Down Expand Up @@ -236,7 +235,7 @@ concrete_block_impl!(concrete_block_invoke_args12, a: A, b: B, c: C, d: D, e: E,
#[repr(C)]
pub struct ConcreteBlock<A, R, F> {
base: BlockBase<A, R>,
descriptor: Box<BlockDescriptor<ConcreteBlock<A, R, F>>>,
descriptor: &'static BlockDescriptor,
closure: F,
}

Expand All @@ -256,15 +255,16 @@ impl<A, R, F> ConcreteBlock<A, R, F> {
/// correct arguments.
unsafe fn with_invoke(invoke: unsafe extern fn(*mut Self, ...) -> R,
closure: F) -> Self {
let descriptor = &BlockDescriptorImpl::<ConcreteBlock<A, R, F>>::DESCRIPTOR;
const BLOCK_HAS_COPY_DISPOSE: c_int = 1 << 25;
ConcreteBlock {
base: BlockBase {
isa: &_NSConcreteStackBlock,
// 1 << 25 = BLOCK_HAS_COPY_DISPOSE
flags: 1 << 25,
flags: if descriptor.dispose_helper.is_some() { BLOCK_HAS_COPY_DISPOSE } else { 0 },
_reserved: 0,
invoke: mem::transmute(invoke),
},
descriptor: Box::new(BlockDescriptor::new()),
descriptor: descriptor,
closure: closure,
}
}
Expand Down Expand Up @@ -308,32 +308,32 @@ impl<A, R, F> DerefMut for ConcreteBlock<A, R, F> {
}
}

unsafe extern fn block_context_dispose<B>(block: &mut B) {
unsafe extern fn block_context_dispose<B>(block: *mut c_void) {
// Read the block onto the stack and let it drop
ptr::read(block);
block.cast::<B>().read();
}

unsafe extern fn block_context_copy<B>(_dst: &mut B, _src: &B) {
unsafe extern fn block_context_copy(_dst: *mut c_void, _src: *const c_void) {
// The runtime memmoves the src block into the dst block, nothing to do
}

#[repr(C)]
struct BlockDescriptor<B> {
struct BlockDescriptor {
_reserved: c_ulong,
block_size: c_ulong,
copy_helper: unsafe extern fn(&mut B, &B),
dispose_helper: unsafe extern fn(&mut B),
copy_helper: Option<unsafe extern fn(*mut c_void, *const c_void)>,
dispose_helper: Option<unsafe extern fn(*mut c_void)>,
}

impl<B> BlockDescriptor<B> {
fn new() -> BlockDescriptor<B> {
BlockDescriptor {
_reserved: 0,
block_size: mem::size_of::<B>() as c_ulong,
copy_helper: block_context_copy::<B>,
dispose_helper: block_context_dispose::<B>,
}
}
struct BlockDescriptorImpl<B>(PhantomData<B>);

impl<B> BlockDescriptorImpl<B> {
const DESCRIPTOR: BlockDescriptor = BlockDescriptor {
_reserved: 0,
block_size: mem::size_of::<B>() as c_ulong,
copy_helper: if std::mem::needs_drop::<B>() { Some(block_context_copy) } else { None },
dispose_helper: if std::mem::needs_drop::<B>() { Some(block_context_dispose::<B>) } else { None },
};
}

#[cfg(test)]
Expand Down