Skip to content

Commit

Permalink
Manually drop file descriptor if not UNIX
Browse files Browse the repository at this point in the history
  • Loading branch information
ViliamVadocz committed May 18, 2023
1 parent 3104db8 commit 7e6852f
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/driver/safe/external_memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::marker::PhantomData;
use core::{marker::PhantomData, mem::ManuallyDrop};
use std::fs::File;
use std::ops::Range;
use std::sync::Arc;
Expand Down Expand Up @@ -31,7 +31,7 @@ impl CudaDevice {
external_memory,
size,
_device: self.clone(),
_file: file,
_file: ManuallyDrop::new(file),
})
}
}
Expand All @@ -40,7 +40,7 @@ pub struct ExternalMemory {
external_memory: sys::CUexternalMemory,
size: u64,
_device: Arc<CudaDevice>,
_file: std::fs::File,
_file: ManuallyDrop<File>,
}

impl Drop for ExternalMemory {
Expand All @@ -49,8 +49,10 @@ impl Drop for ExternalMemory {
// From CUDA docs, when successfully importing UNIX file descriptor:
// Ownership of the file descriptor is transferred to the CUDA driver when the handle is imported successfully.
// Performing any operations on the file descriptor after it is imported results in undefined behavior.
#[cfg(unix)]
core::mem::forget(self._file);
#[cfg(not(unix))]
unsafe {
ManuallyDrop::<File>::drop(&mut self._file)
};
}
}

Expand Down

0 comments on commit 7e6852f

Please sign in to comment.