Skip to content

Commit

Permalink
expose free/release/destroy functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gerwin3 committed Aug 14, 2023
1 parent 3f456a9 commit d27c95d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 21 deletions.
21 changes: 17 additions & 4 deletions src/ffi/memory/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,18 @@ impl<T: Copy> DeviceBuffer<T> {
&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<T: Copy> Drop for DeviceBuffer<T> {
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;
}
Expand All @@ -191,6 +194,16 @@ impl<T: Copy> Drop for DeviceBuffer<T> {
}
}

impl<T: Copy> Drop for DeviceBuffer<T> {
#[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::*;
Expand Down
21 changes: 17 additions & 4 deletions src/ffi/memory/device2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,18 @@ impl<T: Copy> DeviceBuffer2D<T> {
&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<T: Copy> Drop for DeviceBuffer2D<T> {
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;
}
Expand All @@ -216,6 +219,16 @@ impl<T: Copy> Drop for DeviceBuffer2D<T> {
}
}

impl<T: Copy> Drop for DeviceBuffer2D<T> {
#[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::*;
Expand Down
21 changes: 17 additions & 4 deletions src/ffi/memory/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,18 @@ impl<T: Copy> HostBuffer<T> {
&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<T: Copy> Drop for HostBuffer<T> {
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;
}
Expand All @@ -169,6 +172,16 @@ impl<T: Copy> Drop for HostBuffer<T> {
}
}

impl<T: Copy> Drop for HostBuffer<T> {
#[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::*;
Expand Down
2 changes: 0 additions & 2 deletions src/ffi/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -84,7 +83,6 @@ impl DevicePtr {
/// // Propertly deallocate the pointer here and do *NOT* use
/// // use `self` for anything!
/// }
///
/// }
/// ```
#[inline]
Expand Down
21 changes: 17 additions & 4 deletions src/ffi/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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.
Expand Down
25 changes: 22 additions & 3 deletions src/npp/ffi/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit d27c95d

Please sign in to comment.