Skip to main content

singe_cuda/types/
pointer.rs

1use std::ptr;
2
3use singe_cuda_sys::driver;
4
5use crate::view::{AnyBitPatternDeviceRepr, DeviceRepr, NoUninitDeviceRepr, ZeroableDeviceRepr};
6
7#[derive(Debug, Clone, Copy)]
8#[repr(transparent)]
9/// CUDA host callback function pointer.
10///
11/// This is a borrowed function pointer value, not an owned CUDA resource.
12pub struct HostFunction(driver::CUhostFn);
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15#[repr(transparent)]
16/// CUDA device function handle.
17///
18/// CUDA owns the underlying function as part of a module, library, or runtime registration.
19/// This wrapper is a copyable handle value and does not unload or destroy the function.
20pub struct DeviceFunction(driver::CUfunction);
21
22/// Unowned CUDA device address.
23/// Copying this value retains no allocation and proves no bounds, lifetime, context association, alignment, or access permissions.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
25#[repr(transparent)]
26pub struct DevicePtr(*mut ());
27
28// SAFETY: `DevicePtr` is a transparent pointer-sized address with no padding, and copying it never dereferences the address.
29unsafe impl DeviceRepr for DevicePtr {}
30
31// SAFETY: a pointer value has no padding, so every byte of a valid `DevicePtr` is initialized.
32unsafe impl NoUninitDeviceRepr for DevicePtr {}
33
34// SAFETY: the all-zero pattern is the null address, which `DevicePtr::null` also produces.
35unsafe impl ZeroableDeviceRepr for DevicePtr {}
36
37// SAFETY: `DevicePtr` is an opaque address that is never dereferenced through this type, so every bit pattern is a valid value.
38unsafe impl AnyBitPatternDeviceRepr for DevicePtr {}
39
40impl HostFunction {
41    /// Wraps a raw CUDA host callback function pointer.
42    ///
43    /// # Safety
44    ///
45    /// `raw` must be a callback function pointer with the ABI and lifetime required by CUDA for every operation that uses the returned handle.
46    #[must_use]
47    pub const unsafe fn new(raw: driver::CUhostFn) -> Self {
48        Self(raw)
49    }
50
51    /// Wraps a raw CUDA host callback function pointer.
52    ///
53    /// # Safety
54    ///
55    /// `raw` must be a callback function pointer with the ABI and lifetime required by CUDA for every operation that uses the returned handle.
56    #[must_use]
57    pub const unsafe fn from_raw(raw: driver::CUhostFn) -> Self {
58        // SAFETY: `new` has the same requirement on `raw` as this function, which the caller guarantees.
59        unsafe { Self::new(raw) }
60    }
61
62    /// Returns the stored native CUDA host callback pointer.
63    #[must_use]
64    pub const fn as_raw(self) -> driver::CUhostFn {
65        self.0
66    }
67}
68
69impl DeviceFunction {
70    /// Wraps a raw CUDA device function handle.
71    ///
72    /// # Safety
73    ///
74    /// `raw` must be a valid CUDA function handle whose owning module, library, or runtime registration remains loaded for every operation that uses the returned handle.
75    pub const unsafe fn new(raw: driver::CUfunction) -> Self {
76        Self(raw)
77    }
78
79    /// Wraps a raw CUDA device function handle.
80    ///
81    /// # Safety
82    ///
83    /// `raw` must be a valid CUDA function handle whose owning module, library, or runtime registration remains loaded for every operation that uses the returned handle.
84    pub const unsafe fn from_raw(raw: driver::CUfunction) -> Self {
85        // SAFETY: `new` has the same requirement on `raw` as this function, which the caller guarantees.
86        unsafe { Self::new(raw) }
87    }
88
89    /// Returns the stored native CUDA device function handle without transferring ownership.
90    #[must_use]
91    pub const fn as_raw(self) -> driver::CUfunction {
92        self.0
93    }
94
95    /// Returns whether the stored native function handle is null.
96    #[must_use]
97    pub const fn is_null(self) -> bool {
98        self.0.is_null()
99    }
100}
101
102impl DevicePtr {
103    /// Creates a null device address without allocating memory.
104    #[must_use]
105    pub const fn null() -> Self {
106        Self(ptr::null_mut())
107    }
108
109    /// Wraps a raw CUDA device pointer value.
110    ///
111    /// # Safety
112    ///
113    /// `raw` must be either null or a pointer value that is valid for the CUDA operation it is passed to.
114    /// This wrapper does not prove allocation ownership, size, lifetime, context association, or access permissions.
115    pub const unsafe fn new(raw: *mut ()) -> Self {
116        Self(raw)
117    }
118
119    /// Wraps a raw CUDA device pointer value.
120    ///
121    /// # Safety
122    ///
123    /// `raw` must be either null or a pointer value that is valid for the CUDA operation it is passed to.
124    /// This wrapper does not prove allocation ownership, size, lifetime, context association, or access permissions.
125    pub const unsafe fn from_raw(raw: *mut ()) -> Self {
126        // SAFETY: `new` has the same requirement on `raw` as this function, which the caller guarantees.
127        unsafe { Self::new(raw.cast()) }
128    }
129
130    /// Returns the untyped device pointer without retaining or validating its allocation.
131    #[must_use]
132    pub const fn as_raw(self) -> *mut () {
133        self.0.cast()
134    }
135
136    /// Returns the stored mutable device pointer without dereferencing it.
137    #[must_use]
138    pub const fn as_ptr(self) -> *mut () {
139        self.0
140    }
141
142    /// Returns the stored device address as a const pointer without dereferencing it.
143    #[must_use]
144    pub const fn as_const_ptr(self) -> *const () {
145        self.0.cast_const()
146    }
147
148    /// Returns whether the device address is null.
149    #[must_use]
150    pub const fn is_null(self) -> bool {
151        self.0.is_null()
152    }
153
154    /// The numeric device address.
155    #[must_use]
156    pub fn addr(self) -> usize {
157        self.0.addr()
158    }
159
160    /// Whether the address is a multiple of `alignment`, which must be nonzero.
161    #[must_use]
162    pub fn is_aligned_to(self, alignment: usize) -> bool {
163        self.addr().is_multiple_of(alignment)
164    }
165
166    /// Offsets the address by `bytes`, returning `None` when the address overflows.
167    /// The result is an address computation only and is not checked against any allocation.
168    #[must_use]
169    pub fn checked_byte_add(self, bytes: usize) -> Option<Self> {
170        self.addr()
171            .checked_add(bytes)
172            .map(|_| Self(self.0.wrapping_byte_add(bytes)))
173    }
174
175    /// Casts the device address to a pointer to `T` without checking alignment, bounds, or the allocation's element representation.
176    #[must_use]
177    pub const fn cast<T>(self) -> *mut T {
178        self.0.cast()
179    }
180}
181
182impl From<HostFunction> for driver::CUhostFn {
183    fn from(value: HostFunction) -> Self {
184        value.as_raw()
185    }
186}
187
188impl From<DeviceFunction> for driver::CUfunction {
189    fn from(value: DeviceFunction) -> Self {
190        value.as_raw()
191    }
192}
193
194impl From<DevicePtr> for *mut () {
195    fn from(value: DevicePtr) -> Self {
196        value.as_raw()
197    }
198}