Skip to content

Commit

Permalink
Fix memory leak with new family methods. Refs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed May 6, 2023
1 parent d4bb1d3 commit 2b01042
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 180 deletions.
3 changes: 3 additions & 0 deletions cidre-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ fn gen_msg_send(
};

let fn_name = fn_name.to_string();
if extern_name.starts_with("new") && fn_name.ends_with("_ar") {
panic!("can't use _ar functions with methods started with `new`. See #3");
}
let mut generics = Vec::new();

let args = loop {
Expand Down
8 changes: 0 additions & 8 deletions cidre/src/mtl/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ impl Buf {
pub fn did_modify_range(&self, range: ns::Range);

#[objc::msg_send(newTextureWithDescriptor:offset:bytesPerRow:)]
pub fn new_texture_with_descriptor_ar(
&self,
descritptor: &mtl::TextureDescriptor,
offset: usize,
bytes_per_row: usize,
) -> Option<&'ar mtl::Texture>;

#[objc::rar_retain()]
pub fn new_texture_with_descriptor(
&self,
descritptor: &mtl::TextureDescriptor,
Expand Down
87 changes: 3 additions & 84 deletions cidre/src/mtl/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ impl Device {
/// queue.as_type_ref().show();
///
#[objc::msg_send(newCommandQueue)]
pub fn new_cmd_queue_ar(&self) -> Option<&'ar CmdQueue>;

#[objc::rar_retain()]
pub fn new_cmd_queue(&self) -> Option<arc::R<CmdQueue>>;

/// ```
Expand All @@ -123,32 +120,15 @@ impl Device {
/// queue.as_type_ref().show();
///
#[objc::msg_send(newCommandQueueWithMaxCommandBufferCount:)]
pub fn new_cmd_queue_max_cmd_buf_count_ar(
&self,
max_cmd_buf_count: usize,
) -> Option<&'ar CmdQueue>;

#[objc::rar_retain()]
pub fn new_cmd_queue_max_cmd_buf_count(
&self,
max_cmd_buf_count: usize,
) -> Option<arc::R<CmdQueue>>;

#[objc::msg_send(newTextureWithDescriptor:)]
pub fn new_texture_ar(&self, descriptor: &mtl::TextureDescriptor) -> Option<&'ar mtl::Texture>;

#[objc::rar_retain()]
pub fn new_texture(&self, descriptor: &mtl::TextureDescriptor) -> Option<arc::R<mtl::Texture>>;

#[objc::msg_send(newTextureWithDescriptor:iosurface:plane:)]
pub fn new_texture_with_surface_ar(
&self,
descriptor: &mtl::TextureDescriptor,
surface: &io::Surface,
plane: usize,
) -> Option<&'ar mtl::Texture>;

#[objc::rar_retain()]
pub fn new_texture_with_surface(
&self,
descriptor: &mtl::TextureDescriptor,
Expand All @@ -157,20 +137,9 @@ impl Device {
) -> Option<arc::R<mtl::Texture>>;

#[objc::msg_send(newDefaultLibrary)]
pub fn new_default_lib_ar(&self) -> Option<&'ar Lib>;

#[objc::rar_retain()]
pub fn new_default_lib(&self) -> Option<arc::R<Lib>>;

#[objc::msg_send(newLibraryWithSource:options:error:)]
pub fn new_lib_with_src_err_ar(
&self,
source: &ns::String,
options: Option<&mtl::CompileOptions>,
error: *mut Option<&ns::Error>,
) -> Option<&'ar Lib>;

#[objc::rar_retain()]
pub fn new_lib_with_src_err(
&self,
source: &ns::String,
Expand Down Expand Up @@ -212,27 +181,13 @@ impl Device {
F: FnOnce(Option<&'ar mtl::library::Lib>, Option<&'ar ns::Error>) + 'static;

#[objc::msg_send(newComputePipelineStateWithFunction:error:)]
pub fn new_compute_ps_with_fn_err_ar<'a>(
&self,
function: &mtl::Fn,
error: &mut Option<&'a ns::Error>,
) -> Option<&'ar mtl::ComputePipelineState>;

#[objc::rar_retain()]
pub fn new_compute_ps_with_fn_err<'a>(
&self,
function: &mtl::Fn,
error: &mut Option<&'a ns::Error>,
) -> Option<arc::R<mtl::ComputePipelineState>>;

#[objc::msg_send(newRenderPipelineStateWithDescriptor:error:)]
pub unsafe fn new_render_ps_err_ar(
&self,
descriptor: &mtl::RenderPipelineDescriptor,
error: &mut Option<&ns::Error>,
) -> Option<&'ar mtl::RenderPipelineState>;

#[objc::rar_retain]
pub unsafe fn new_render_ps_err(
&self,
descriptor: &mtl::RenderPipelineDescriptor,
Expand Down Expand Up @@ -271,22 +226,10 @@ impl Device {
}

#[objc::msg_send(newBufferWithLength:options:)]
pub fn new_buf_ar(&self, length: usize, options: mtl::ResourceOptions)
-> Option<&'ar mtl::Buf>;

#[objc::rar_retain()]
pub fn new_buf(&self, length: usize, options: mtl::ResourceOptions)
-> Option<arc::R<mtl::Buf>>;

#[objc::msg_send(newBufferWithBytes:length:options:)]
pub fn new_buf_with_bytes_ar(
&self,
bytes: *const c_void,
length: usize,
options: mtl::ResourceOptions,
) -> Option<&'ar Buf>;

#[objc::rar_retain()]
pub fn new_buf_with_bytes(
&self,
bytes: *const c_void,
Expand All @@ -295,43 +238,25 @@ impl Device {
) -> Option<arc::R<Buf>>;

#[inline]
pub fn new_buf_with_slice_ar<'ar, T: Sized>(
pub fn new_buf_with_slice<T: Sized>(
&self,
slice: &[T],
options: mtl::ResourceOptions,
) -> Option<&'ar mtl::Buf> {
self.new_buf_with_bytes_ar(
) -> Option<arc::R<mtl::Buf>> {
self.new_buf_with_bytes(
slice.as_ptr() as _,
std::mem::size_of::<T>() * slice.len(),
options,
)
}

#[inline]
pub fn new_buf_with_slice<T: Sized>(
&self,
slice: &[T],
options: mtl::ResourceOptions,
) -> Option<arc::R<mtl::Buf>> {
arc::rar_retain_option(self.new_buf_with_slice_ar(slice, options))
}

#[objc::msg_send(newFence)]
pub fn new_fence_ar(&self) -> Option<&'ar Fence>;

#[objc::rar_retain()]
pub fn new_fence(&self) -> Option<arc::R<Fence>>;

#[objc::msg_send(newEvent)]
pub fn new_event_ar(&self) -> Option<&'ar Event>;

#[objc::rar_retain()]
pub fn new_event(&self) -> Option<arc::R<Event>>;

#[objc::msg_send(newSharedEvent)]
pub fn new_shared_event_ar(&self) -> Option<&'ar SharedEvent>;

#[objc::rar_retain()]
pub fn new_shared_event(&self) -> Option<arc::R<SharedEvent>>;

#[objc::msg_send(maxBufferLength)]
Expand All @@ -350,9 +275,6 @@ impl Device {
) -> SizeAndAlign;

#[objc::msg_send(newHeapWithDescriptor:)]
pub fn new_heap_desc_ar(&self, descriptor: &mtl::HeapDescriptor) -> Option<&'ar mtl::Heap>;

#[objc::rar_retain()]
pub fn new_heap_desc(&self, descriptor: &mtl::HeapDescriptor) -> Option<arc::R<mtl::Heap>>;

/// The maximum threadgroup memory available to a compute kernel, in bytes.
Expand Down Expand Up @@ -438,7 +360,6 @@ mod tests {
let tier = device.argument_buffers_support();
assert_ne!(tier, mtl::ArgumentBuffersTier::_1);

assert!(device.new_default_lib_ar().is_none());
assert!(device.new_default_lib().is_none());

let td = mtl::TextureDescriptor::new_2d_with_pixel_format(
Expand All @@ -449,11 +370,9 @@ mod tests {
);

let _t = device.new_texture(&td).unwrap();
let _t = device.new_texture_ar(&td).unwrap();

assert!(device.max_buffer_length() > 10);

let _queue = device.new_cmd_queue_ar().unwrap();
let _queue = device.new_cmd_queue().unwrap();

let source = ns::String::with_str("void function_a() {}");
Expand Down
57 changes: 0 additions & 57 deletions cidre/src/mtl/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,16 @@ impl Heap {

/// Create a new buffer backed by heap memory.
#[objc::msg_send(newBufferWithLength:options:)]
pub fn new_buf_ar(&self, length: usize, options: mtl::ResourceOptions)
-> Option<&'ar mtl::Buf>;

#[objc::rar_retain()]
pub fn new_buf(&self, length: usize, options: mtl::ResourceOptions)
-> Option<arc::R<mtl::Buf>>;

#[objc::msg_send(newTextureWithDescriptor:)]
pub fn new_texture_ar(&self, descriptor: &mtl::TextureDescriptor) -> Option<&'ar mtl::Texture>;

#[objc::rar_retain()]
pub fn new_texture(&self, descriptor: &mtl::TextureDescriptor) -> Option<arc::R<mtl::Texture>>;

#[objc::msg_send(setPurgeableState:)]
pub fn set_purgeable_state(&mut self, state: mtl::PurgableState);

#[objc::msg_send(newBufferWithLength:options:offset:)]
pub fn new_buf_with_offset_ar(
&self,
length: usize,
options: mtl::ResouceOptions,
offset: usize,
) -> Option<&'ar mtl::Buf>;

#[objc::rar_retain]
pub fn new_buf_with_offset(
&self,
length: usize,
Expand All @@ -126,79 +111,39 @@ impl Heap {
) -> Option<arc::R<mtl::Buf>>;

#[objc::msg_send(newTextureWithDescriptor:offset:)]
pub fn new_texture_with_offset_ar(
&self,
descriptor: &mtl::TextureDescriptor,
offset: usize,
) -> Option<&'ar mtl::Texture>;

#[objc::rar_retain]
pub fn new_texture_with_offset(
&self,
descriptor: &mtl::TextureDescriptor,
offset: usize,
) -> Option<arc::R<mtl::Texture>>;

#[objc::msg_send(newTextureWithDescriptor:offset:)]
pub fn new_texture_with_desc_offset_ar(
&self,
descriptor: &mtl::TextureDescriptor,
offset: usize,
) -> Option<&'ar mtl::Texture>;

#[objc::rar_retain]
pub fn new_texture_with_desc_offset(
&self,
descriptor: &mtl::TextureDescriptor,
offset: usize,
) -> Option<arc::R<mtl::Texture>>;

#[objc::msg_send(newAccelerationStructureWithSize:)]
pub fn new_acceleration_structure_size_ar(
&self,
size: usize,
) -> Option<&'ar mtl::AccelerationStructure>;

#[objc::rar_retain]
pub fn new_acceleration_structure_size(
&self,
size: usize,
) -> Option<arc::R<mtl::AccelerationStructure>>;

#[objc::msg_send(newAccelerationStructureWithSize:offset:)]
pub fn new_acceleration_structure_size_offset_ar(
&self,
size: usize,
offset: usize,
) -> Option<&'ar mtl::AccelerationStructure>;

#[objc::rar_retain]
pub fn new_acceleration_structure_size_offset(
&self,
size: usize,
offset: usize,
) -> Option<arc::R<mtl::AccelerationStructure>>;

#[objc::msg_send(newAccelerationStructureWithDescriptor:)]
pub fn new_acceleration_structure_with_desc_ar(
&self,
descriptor: &mtl::AccelerationStructureDescriptor,
) -> Option<&'ar mtl::AccelerationStructure>;

#[objc::rar_retain]
pub fn new_acceleration_structure_with_desc(
&self,
descriptor: &mtl::AccelerationStructureDescriptor,
) -> Option<arc::R<mtl::AccelerationStructure>>;

#[objc::msg_send(newAccelerationStructureWithDescriptor:offset:)]
pub fn new_acceleration_structure_with_desc_offset_ar(
&self,
descriptor: &mtl::AccelerationStructureDescriptor,
offset: usize,
) -> Option<&'ar mtl::AccelerationStructure>;

#[objc::rar_retain]
pub fn new_acceleration_structure_with_desc_offset(
&self,
descriptor: &mtl::AccelerationStructureDescriptor,
Expand Down Expand Up @@ -228,7 +173,5 @@ mod tests {
let device = mtl::Device::default().unwrap();
let heap = device.new_heap_desc(&desc).unwrap();
assert!(heap.size() >= 1024);
let heap = device.new_heap_desc_ar(&desc).unwrap();
assert!(heap.size() >= 1024);
}
}
25 changes: 0 additions & 25 deletions cidre/src/mtl/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ impl Fn {
pub fn name(&self) -> &ns::String;

#[objc::msg_send(newArgumentEncoderWithBufferIndex:)]
pub fn new_argument_enc_with_buf_index_ar(
&self,
index: ns::UInteger,
) -> &'ar mtl::ArgumentEncoder;

#[objc::rar_retain()]
pub fn new_argument_enc_with_buf_index(
&self,
index: ns::UInteger,
Expand Down Expand Up @@ -174,22 +168,11 @@ impl Lib {
pub fn fn_names(&self) -> &ns::Array<ns::String>;

#[objc::msg_send(newFunctionWithName:)]
pub fn new_fn_ar(&self, name: &ns::String) -> Option<&'ar Fn>;

#[objc::rar_retain()]
pub fn new_fn(&self, name: &ns::String) -> Option<arc::R<Fn>>;

/// # Safety
/// Use new_fn_const_values
#[objc::msg_send(newFunctionWithName:constantValues:error:)]
pub unsafe fn new_fn_with_consts_err_ar(
&self,
name: &ns::String,
constant_values: &mtl::FnConstValues,
error: *mut Option<&'ar ns::Error>,
) -> Option<&'ar Fn>;

#[objc::rar_retain()]
pub unsafe fn new_fn_with_consts_err<'ar>(
&self,
name: &ns::String,
Expand All @@ -215,14 +198,6 @@ impl Lib {
}

#[objc::msg_send(newFunctionWithName:descriptor:error:)]
pub unsafe fn new_fn_with_desc_err_ar(
&self,
name: &ns::String,
descriptor: &mtl::FnDescriptor,
error: &mut Option<&'ar ns::Error>,
) -> Option<&'ar Fn>;

#[objc::rar_retain()]
pub unsafe fn new_fn_with_desc_err<'ar>(
&self,
name: &ns::String,
Expand Down
Loading

0 comments on commit 2b01042

Please sign in to comment.