Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dispatch2: Add fn data_create() which copies a slice #710

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions crates/dispatch2/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Dispatch object definition.

use alloc::boxed::Box;
use core::{ops::Deref, ptr::NonNull};

use super::{ffi::*, queue::Queue, utils::function_wrapper, QualityOfServiceClass};

// TODO: Autogenerate with https://github.com/madsmtm/objc2/issues/609
const DISPATCH_DATA_DESTRUCTOR_DEFAULT: dispatch_block_t = std::ptr::null_mut();

/// Error returned by [DispatchObject::set_target_queue].
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
Expand Down Expand Up @@ -60,6 +64,77 @@ impl<T> DispatchObject<T> {
result
}

/// Creates a dispatch data object with a copy of the given contiguous buffer of memory.
// TODO: Would it be safe for users to replace the finalizer?
pub fn data_create_copy(data: &[u8], queue: &Queue) -> Self {
// SAFETY: Buffer pointer is valid for the given number of bytes. Queue handle is valid,
// and the destructor is a NULL value which indicates the buffer should be copied.
let object = unsafe {
dispatch_data_create(
NonNull::new_unchecked(data.as_ptr().cast_mut()).cast(),
data.len(),
queue.as_raw(),
DISPATCH_DATA_DESTRUCTOR_DEFAULT,
)
};

Self {
object: object.cast(),
is_activated: false,
}
}

/// Creates a dispatch data object with a reference to the given contiguous buffer of memory.
pub fn data_create_static(data: &'static [u8], queue: &Queue) -> Self {
block2::global_block! {
static NOOP_BLOCK = || {}
}
// SAFETY: Buffer pointer is valid for the given number of bytes. Queue handle is valid,
// and the destructor is a NULL value which indicates the buffer should be copied.
let object = unsafe {
dispatch_data_create(
NonNull::new_unchecked(data.as_ptr().cast_mut()).cast(),
data.len(),
queue.as_raw(),
<*const _>::cast_mut(NOOP_BLOCK.deref()),
)
};

Self {
object: object.cast(),
is_activated: false,
}
}

/// Creates a dispatch data object with ownership of the given contiguous buffer of memory.
// TODO: Would it be safe for users to replace the finalizer?
pub fn data_create(data: Box<[u8]>, queue: &Queue) -> Self {
let data_len = data.len();
let raw = Box::into_raw(data);
let delete_box = block2::RcBlock::new(move || {
// SAFETY: The fat pointer (plus size) was retrieved from Box::into_raw(), and its
// ownership was *not* consumed by dispatch_data_create().
let _ = unsafe { Box::<[u8]>::from_raw(raw) };
});

// SAFETY: Buffer pointer is valid for the given number of bytes. Queue handle is valid,
// and the destructor is a NULL value which indicates the buffer should be copied.
// let t = Box::into_raw(data);
let object = unsafe {
dispatch_data_create(
NonNull::new_unchecked(raw).cast(),
data_len,
queue.as_raw(),
<*const _>::cast_mut(delete_box.deref()),
)
};

Self {
object: object.cast(),
is_activated: false,
}
}

/// Set the finalizer function for the object.
pub fn set_finalizer<F>(&mut self, destructor: F)
where
Expand Down
4 changes: 2 additions & 2 deletions crates/dispatch2/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub struct Queue {
}

impl Queue {
/// Create a new [Queue].
/// Create a new [`Queue`].
pub fn new(label: &str, queue_attribute: QueueAttribute) -> Self {
let label = CString::new(label).expect("Invalid label!");

Expand All @@ -147,7 +147,7 @@ impl Queue {
}
}

/// Create a new [Queue] with a given target [Queue].
/// Create a new [`Queue`] with a given target [`Queue`].
pub fn new_with_target(label: &str, queue_attribute: QueueAttribute, target: &Queue) -> Self {
let label = CString::new(label).expect("Invalid label!");

Expand Down
6 changes: 6 additions & 0 deletions framework-crates/objc2-metal/Cargo.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions framework-crates/objc2-metal/translation-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ class.MTLRasterizationRateLayerDescriptor.methods.sampleCount.skipped = true
# Swift also skips this.
class.MTLRasterizationRateMapDescriptor.methods."rasterizationRateMapDescriptorWithScreenSize:layerCount:layers:".skipped = true

# Needs dispatch
class.MTLSharedEventListener.methods."initWithDispatchQueue:".skipped = true
class.MTLSharedEventListener.methods.dispatchQueue.skipped = true
protocol.MTLDevice.methods."newLibraryWithData:error:".skipped = true

# Needs mach / kernel types
protocol.MTLResource.methods."setOwnerWithIdentity:".skipped = true

Expand Down
Loading