Skip to content

Commit

Permalink
Implement custom Drop for IRVersionedCSInfo
Browse files Browse the repository at this point in the history
Every `IRVersioned*Info` structure has an accompanying
`IRShaderReflectionRelease*Info()` function to clean up allocated
pointers inside, mostly strings.  Even though `IRVersionedCSInfo`
doesn't contain any pointers, it is a versioned structure that could
be extended in the future with allocated objects, so we should call the
existing (currently no-op) `Release` function on it prematurely.
  • Loading branch information
MarijnS95 committed Sep 26, 2024
1 parent 7241263 commit dda00cc
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
ffi::{c_char, CStr, OsStr},
fmt,
mem::MaybeUninit,
ops::Deref,
ptr::NonNull,
sync::Arc,
};
Expand Down Expand Up @@ -121,10 +122,7 @@ impl IRShaderReflection {
}

#[doc(alias = "IRShaderReflectionCopyComputeInfo")]
pub fn compute_info(
&self,
version: ffi::IRReflectionVersion,
) -> Option<ffi::IRVersionedCSInfo> {
pub fn compute_info(&self, version: ffi::IRReflectionVersion) -> Option<IRVersionedCSInfo> {
let mut info = MaybeUninit::uninit();
if unsafe {
self.funcs.IRShaderReflectionCopyComputeInfo(
Expand All @@ -133,13 +131,38 @@ impl IRShaderReflection {
info.as_mut_ptr(),
)
} {
Some(unsafe { info.assume_init() })
Some(IRVersionedCSInfo {
me: unsafe { info.assume_init() },
funcs: self.funcs.clone(),
})
} else {
None
}
}
}

pub struct IRVersionedCSInfo {
me: ffi::IRVersionedCSInfo,
funcs: Arc<bindings::metal_irconverter>,
}

impl Deref for IRVersionedCSInfo {
type Target = ffi::IRVersionedCSInfo;

fn deref(&self) -> &Self::Target {
&self.me
}
}

impl Drop for IRVersionedCSInfo {
fn drop(&mut self) {
assert!(unsafe {
self.funcs
.IRShaderReflectionReleaseComputeInfo(&mut self.me)
})
}
}

pub struct IRObject {
me: NonNull<bindings::IRObject>,
funcs: Arc<bindings::metal_irconverter>,
Expand Down

0 comments on commit dda00cc

Please sign in to comment.