Skip to content

Commit f00c29f

Browse files
committed
Docs: fix doctest and add missing safety sections
1 parent 6ab550c commit f00c29f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

crates/cust/src/memory/device/device_box.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ impl<T: DeviceCopy + bytemuck::Zeroable> DeviceBox<T> {
169169
///
170170
/// This doesn't actually allocate if `T` is zero-sized.
171171
///
172+
/// # Safety
173+
///
174+
/// This method enqueues two operations on the stream: An async allocation
175+
/// and an async memset. Because of this, you must ensure that:
176+
/// - The memory is not used in any way before it is actually allocated on the stream. You
177+
/// can ensure this happens by synchronizing the stream explicitly or using events.
178+
///
172179
/// # Examples
173180
///
174181
/// ```
@@ -180,7 +187,7 @@ impl<T: DeviceCopy + bytemuck::Zeroable> DeviceBox<T> {
180187
/// unsafe {
181188
/// let mut zero = DeviceBox::zeroed_async(&stream)?;
182189
/// zero.async_copy_to(&mut value, &stream)?;
183-
/// zero.free_async(&stream)?;
190+
/// zero.drop_async(&stream)?;
184191
/// }
185192
/// stream.synchronize()?;
186193
/// assert_eq!(value, 0);

crates/cust/src/memory/malloc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ pub unsafe fn cuda_malloc<T>(count: usize) -> CudaResult<DevicePointer<T>> {
5555
/// Retains all of the unsafe semantics of [`cuda_malloc`] with the extra requirement that the memory
5656
/// must not be used until it is allocated on the stream. Therefore, proper stream ordering semantics must be
5757
/// respected.
58+
///
59+
/// # Safety
60+
///
61+
/// The memory behind the returned pointer must not be used in any way until the
62+
/// allocation actually takes place in the stream.
5863
pub unsafe fn cuda_malloc_async<T>(stream: &Stream, count: usize) -> CudaResult<DevicePointer<T>> {
5964
let size = count.checked_mul(mem::size_of::<T>()).unwrap_or(0);
6065
if size == 0 {
@@ -76,6 +81,10 @@ pub unsafe fn cuda_malloc_async<T>(stream: &Stream, count: usize) -> CudaResult<
7681
/// Retains all of the unsafe semantics of [`cuda_free`] with the extra requirement that the memory
7782
/// must not be used after it is dropped. Therefore, proper stream ordering semantics must be
7883
/// respected.
84+
///
85+
/// # Safety
86+
///
87+
/// The pointer must be valid.
7988
pub unsafe fn cuda_free_async<T>(stream: &Stream, mut p: DevicePointer<T>) -> CudaResult<()> {
8089
if mem::size_of::<T>() == 0 {
8190
return Err(CudaError::InvalidMemoryAllocation);

0 commit comments

Comments
 (0)