From d27c95d69dbeded4f797a442611052298811cdf1 Mon Sep 17 00:00:00 2001 From: Gerwin van der Lugt Date: Mon, 14 Aug 2023 16:05:34 +0200 Subject: [PATCH] expose free/release/destroy functions --- src/ffi/memory/device.rs | 21 +++++++++++++++++---- src/ffi/memory/device2d.rs | 21 +++++++++++++++++---- src/ffi/memory/host.rs | 21 +++++++++++++++++---- src/ffi/ptr.rs | 2 -- src/ffi/stream.rs | 21 +++++++++++++++++---- src/npp/ffi/context.rs | 25 ++++++++++++++++++++++--- 6 files changed, 90 insertions(+), 21 deletions(-) diff --git a/src/ffi/memory/device.rs b/src/ffi/memory/device.rs index 9af6e67..bc82679 100644 --- a/src/ffi/memory/device.rs +++ b/src/ffi/memory/device.rs @@ -167,15 +167,18 @@ impl DeviceBuffer { &self.internal } - /// Get readonly reference to internal [`DevicePtr`]. + /// Get mutable reference to internal [`DevicePtr`]. #[inline(always)] pub fn as_mut_internal(&mut self) -> &mut DevicePtr { &mut self.internal } -} -impl Drop for DeviceBuffer { - fn drop(&mut self) { + /// Release the buffer memory. + /// + /// # Safety + /// + /// The buffer may not be used after this function is called, except for being dropped. + pub unsafe fn free(&mut self) { if self.internal.is_null() { return; } @@ -191,6 +194,16 @@ impl Drop for DeviceBuffer { } } +impl Drop for DeviceBuffer { + #[inline] + fn drop(&mut self) { + // SAFETY: This is safe since the buffer cannot be used after this. + unsafe { + self.free(); + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ffi/memory/device2d.rs b/src/ffi/memory/device2d.rs index 99bb0c2..5567bda 100644 --- a/src/ffi/memory/device2d.rs +++ b/src/ffi/memory/device2d.rs @@ -192,15 +192,18 @@ impl DeviceBuffer2D { &self.internal } - /// Get readonly reference to internal [`DevicePtr`]. + /// Get mutable reference to internal [`DevicePtr`]. #[inline(always)] pub fn as_mut_internal(&mut self) -> &mut DevicePtr { &mut self.internal } -} -impl Drop for DeviceBuffer2D { - fn drop(&mut self) { + /// Release the buffer memory. + /// + /// # Safety + /// + /// The buffer may not be used after this function is called, except for being dropped. + pub unsafe fn free(&mut self) { if self.internal.is_null() { return; } @@ -216,6 +219,16 @@ impl Drop for DeviceBuffer2D { } } +impl Drop for DeviceBuffer2D { + #[inline] + fn drop(&mut self) { + // SAFETY: This is safe since the buffer cannot be used after this. + unsafe { + self.free(); + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ffi/memory/host.rs b/src/ffi/memory/host.rs index 5f878e5..b8ebaee 100644 --- a/src/ffi/memory/host.rs +++ b/src/ffi/memory/host.rs @@ -145,15 +145,18 @@ impl HostBuffer { &self.internal } - /// Get readonly reference to internal [`DevicePtr`]. + /// Get mutable reference to internal [`DevicePtr`]. #[inline(always)] pub fn as_mut_internal(&mut self) -> &mut DevicePtr { &mut self.internal } -} -impl Drop for HostBuffer { - fn drop(&mut self) { + /// Release the buffer memory. + /// + /// # Safety + /// + /// The buffer may not be used after this function is called, except for being dropped. + pub unsafe fn free(&mut self) { if self.internal.is_null() { return; } @@ -169,6 +172,16 @@ impl Drop for HostBuffer { } } +impl Drop for HostBuffer { + #[inline] + fn drop(&mut self) { + // SAFETY: This is safe since the buffer cannot be used after this. + unsafe { + self.free(); + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ffi/ptr.rs b/src/ffi/ptr.rs index c73534f..a07523d 100644 --- a/src/ffi/ptr.rs +++ b/src/ffi/ptr.rs @@ -74,7 +74,6 @@ impl DevicePtr { /// } /// /// impl Drop for Object { - /// /// fn drop(&mut self) { /// // SAFETY: This is safe because `self` and `self.internal` /// // are not used beyond this unsafe block. @@ -84,7 +83,6 @@ impl DevicePtr { /// // Propertly deallocate the pointer here and do *NOT* use /// // use `self` for anything! /// } - /// /// } /// ``` #[inline] diff --git a/src/ffi/stream.rs b/src/ffi/stream.rs index 486c750..dfe4035 100644 --- a/src/ffi/stream.rs +++ b/src/ffi/stream.rs @@ -91,15 +91,18 @@ impl Stream { &self.internal } - /// Get readonly reference to internal [`DevicePtr`]. + /// Get mutable reference to internal [`DevicePtr`]. #[inline(always)] pub fn as_mut_internal(&mut self) -> &mut DevicePtr { &mut self.internal } -} -impl Drop for Stream { - fn drop(&mut self) { + /// Destroy stream. + /// + /// # Safety + /// + /// The object may not be used after this function is called, except for being dropped. + pub unsafe fn destroy(&mut self) { if self.internal.is_null() { return; } @@ -125,6 +128,16 @@ impl Drop for Stream { } } +impl Drop for Stream { + #[inline] + fn drop(&mut self) { + // SAFETY: This is safe since the object cannot be used after this. + unsafe { + self.destroy(); + } + } +} + cpp! {{ /// Holds the C++ code that makes up the native part required to get our CUDA callback to work /// over the FFI. diff --git a/src/npp/ffi/context.rs b/src/npp/ffi/context.rs index 343ff6e..b37c61f 100644 --- a/src/npp/ffi/context.rs +++ b/src/npp/ffi/context.rs @@ -107,17 +107,36 @@ impl Context { pub(crate) fn as_ptr(&self) -> *const std::ffi::c_void { self.raw } -} -impl Drop for Context { - fn drop(&mut self) { + /// Delete the context. + /// + /// # Safety + /// + /// The context may not be used after this function is called, except for being dropped. + pub unsafe fn delete(&mut self) { + if self.raw.is_null() { + return; + } + let raw = self.raw; + self.raw = std::ptr::null_mut(); + cpp!(unsafe [raw as "void*"] { delete ((NppStreamContext*) raw); }); } } +impl Drop for Context { + #[inline] + fn drop(&mut self) { + // SAFETY: This is safe since the buffer cannot be used after this. + unsafe { + self.delete(); + } + } +} + #[cfg(test)] mod tests { use super::*;