1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
use core::fmt;
use core::ptr;
use memory::DeviceCopy;
/// A pointer to device memory.
///
/// `DevicePointer` cannot be dereferenced by the CPU, as it is a pointer to a memory allocation in
/// the device. It can be safely copied to the device (eg. as part of a kernel launch) and either
/// unwrapped or transmuted to an appropriate pointer.
///
/// `DevicePointer` is guaranteed to have an equivalent internal representation to a raw pointer.
/// Thus, it can be safely reinterpreted or transmuted to `*mut T`. It is safe to pass a
/// `DevicePointer` through an FFI boundary to C code expecting a `*mut T`, so long as the code on
/// the other side of that boundary does not attempt to dereference the pointer on the CPU. It is
/// thus possible to pass a `DevicePointer` to a CUDA kernel written in C.
#[repr(transparent)]
#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct DevicePointer<T>(*mut T);
unsafe impl<T> DeviceCopy for DevicePointer<T> {}
impl<T> DevicePointer<T> {
/// Returns a null device pointer.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// let ptr : DevicePointer<u64> = DevicePointer::null();
/// assert!(ptr.is_null());
/// ```
pub fn null() -> Self {
unsafe { Self::wrap(ptr::null_mut()) }
}
/// Wrap the given raw pointer in a DevicePointer. The given pointer is assumed to be a valid,
/// device pointer or null.
///
/// # Safety
///
/// The given pointer must have been allocated with [`cuda_malloc`](fn.cuda_malloc.html) or
/// be null.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// use std::ptr;
/// unsafe {
/// let null : *mut u64 = ptr::null_mut();
/// assert!(DevicePointer::wrap(null).is_null());
/// }
/// ```
pub unsafe fn wrap(ptr: *mut T) -> Self {
DevicePointer(ptr)
}
/// Returns the contained pointer as a raw pointer. The returned pointer is not valid on the CPU
/// and must not be dereferenced.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let dev_ptr = cuda_malloc::<u64>(1).unwrap();
/// let ptr: *const u64 = dev_ptr.as_raw();
/// cuda_free(dev_ptr);
/// }
/// ```
pub fn as_raw(self) -> *const T {
self.0
}
/// Returns the contained pointer as a mutable raw pointer. The returned pointer is not valid on the CPU
/// and must not be dereferenced.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut dev_ptr = cuda_malloc::<u64>(1).unwrap();
/// let ptr: *mut u64 = dev_ptr.as_raw_mut();
/// cuda_free(dev_ptr);
/// }
/// ```
pub fn as_raw_mut(&mut self) -> *mut T {
self.0
}
/// Returns true if the pointer is null.
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// use std::ptr;
/// unsafe {
/// let null : *mut u64 = ptr::null_mut();
/// assert!(DevicePointer::wrap(null).is_null());
/// }
/// ```
pub fn is_null(self) -> bool {
self.0.is_null()
}
/// Calculates the offset from a device pointer.
///
/// `count` is in units of T; eg. a `count` of 3 represents a pointer offset of
/// `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined
/// Behavior:
///
/// * Both the starting and resulting pointer must be either in bounds or one
/// byte past the end of *the same* allocated object.
///
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
///
/// * The offset being in bounds cannot rely on "wrapping around" the address
/// space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
///
/// Consider using `wrapping_offset` instead if these constraints are
/// difficult to satisfy. The only advantage of this method is that it
/// enables more aggressive compiler optimizations.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
/// let offset = dev_ptr.offset(1); // Points to the 2nd u64 in the buffer
/// cuda_free(dev_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub unsafe fn offset(self, count: isize) -> Self {
Self::wrap(self.0.offset(count))
}
/// Calculates the offset from a device pointer using wrapping arithmetic.
///
/// `count` is in units of T; eg. a `count` of 3 represents a pointer offset of
/// `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// The resulting pointer does not need to be in bounds, but it is
/// potentially hazardous to dereference (which requires `unsafe`).
/// In particular, the resulting pointer may *not* be used to access a
/// different allocated object than the one `self` points to. In other
/// words, `x.wrapping_offset(y.wrapping_offset_from(x))` is
/// *not* the same as `y`, and dereferencing it is undefined behavior
/// unless `x` and `y` point into the same allocated object.
///
/// Always use `.offset(count)` instead when possible, because `offset`
/// allows the compiler to optimize better. If you need to cross object
/// boundaries, cast the pointer to an integer and do the arithmetic there.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
/// let offset = dev_ptr.wrapping_offset(1); // Points to the 2nd u64 in the buffer
/// cuda_free(dev_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub fn wrapping_offset(self, count: isize) -> Self {
unsafe { Self::wrap(self.0.wrapping_offset(count)) }
}
/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined
/// Behavior:
///
/// * Both the starting and resulting pointer must be either in bounds or one
/// byte past the end of an allocated object.
///
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
///
/// * The offset being in bounds cannot rely on "wrapping around" the address
/// space. That is, the infinite-precision sum must fit in a `usize`.
///
/// Consider using `wrapping_offset` instead if these constraints are
/// difficult to satisfy. The only advantage of this method is that it
/// enables more aggressive compiler optimizations.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
/// let offset = dev_ptr.add(1); // Points to the 2nd u64 in the buffer
/// cuda_free(dev_ptr); // Must free the buffer using the original pointer
/// }
/// ```
#[allow(should_implement_trait)]
pub unsafe fn add(self, count: usize) -> Self
where
T: Sized,
{
self.offset(count as isize)
}
/// Calculates the offset from a pointer (convenience for
/// `.offset((count as isize).wrapping_neg())`).
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined
/// Behavior:
///
/// * Both the starting and resulting pointer must be either in bounds or one
/// byte past the end of an allocated object.
///
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
///
/// * The offset being in bounds cannot rely on "wrapping around" the address
/// space. That is, the infinite-precision sum must fit in a `usize`.
///
/// Consider using `wrapping_offset` instead if these constraints are
/// difficult to satisfy. The only advantage of this method is that it
/// enables more aggressive compiler optimizations.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
/// let offset = dev_ptr.add(4).sub(3); // Points to the 2nd u64 in the buffer
/// cuda_free(dev_ptr); // Must free the buffer using the original pointer
/// }
#[allow(should_implement_trait)]
pub unsafe fn sub(self, count: usize) -> Self
where
T: Sized,
{
self.offset((count as isize).wrapping_neg())
}
/// Calculates the offset from a pointer using wrapping arithmetic.
/// (convenience for `.wrapping_offset(count as isize)`)
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// The resulting pointer does not need to be in bounds, but it is
/// potentially hazardous to dereference.
///
/// Always use `.add(count)` instead when possible, because `add`
/// allows the compiler to optimize better.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
/// let offset = dev_ptr.wrapping_add(1); // Points to the 2nd u64 in the buffer
/// cuda_free(dev_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub fn wrapping_add(self, count: usize) -> Self
where
T: Sized,
{
self.wrapping_offset(count as isize)
}
/// Calculates the offset from a pointer using wrapping arithmetic.
/// (convenience for `.wrapping_offset((count as isize).wrapping_sub())`)
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// The resulting pointer does not need to be in bounds, but it is
/// potentially hazardous to dereference (which requires `unsafe`).
///
/// Always use `.sub(count)` instead when possible, because `sub`
/// allows the compiler to optimize better.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut dev_ptr = cuda_malloc::<u64>(5).unwrap();
/// let offset = dev_ptr.wrapping_add(4).wrapping_sub(3); // Points to the 2nd u64 in the buffer
/// cuda_free(dev_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub fn wrapping_sub(self, count: usize) -> Self
where
T: Sized,
{
self.wrapping_offset((count as isize).wrapping_neg())
}
}
impl<T> fmt::Pointer for DevicePointer<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&self.0, f)
}
}
impl<T> Clone for DevicePointer<T> {
fn clone(&self) -> Self {
DevicePointer(self.0)
}
}
impl<T> Copy for DevicePointer<T> {}
/// A pointer to unified memory.
///
/// `UnifiedPointer` can be safely dereferenced by the CPU, as the memory allocation it points to is
/// shared between the CPU and the GPU. It can also be safely copied to the device (eg. as part of
/// a kernel launch).
///
/// `UnifiedPointer` is guaranteed to have an equivalent internal representation to a raw pointer.
/// Thus, it can be safely reinterpreted or transmuted to `*mut T`. It is also safe to pass a
/// `UnifiedPointer` through an FFI boundary to C code expecting a `*mut T`. It is
/// thus possible to pass a `UnifiedPointer` to a CUDA kernel written in C.
#[repr(transparent)]
#[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct UnifiedPointer<T: DeviceCopy>(*mut T);
unsafe impl<T: DeviceCopy> DeviceCopy for UnifiedPointer<T> {}
impl<T: DeviceCopy> UnifiedPointer<T> {
/// Returns a null unified pointer.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// let ptr : UnifiedPointer<u64> = UnifiedPointer::null();
/// assert!(ptr.is_null());
/// ```
pub fn null() -> Self {
unsafe { Self::wrap(ptr::null_mut()) }
}
/// Wrap the given raw pointer in a UnifiedPointer. The given pointer is assumed to be a valid,
/// unified-memory pointer or null.
///
/// # Safety
///
/// The given pointer must have been allocated with
/// [`cuda_malloc_unified`](fn.cuda_malloc_unified.html) or be null.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// use std::ptr;
/// unsafe {
/// let null : *mut u64 = ptr::null_mut();
/// assert!(UnifiedPointer::wrap(null).is_null());
/// }
/// ```
pub unsafe fn wrap(ptr: *mut T) -> Self {
UnifiedPointer(ptr)
}
/// Returns the contained pointer as a raw pointer.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let unified_ptr = cuda_malloc_unified::<u64>(1).unwrap();
/// let ptr: *const u64 = unified_ptr.as_raw();
/// cuda_free_unified(unified_ptr);
/// }
/// ```
pub fn as_raw(self) -> *const T {
self.0
}
/// Returns the contained pointer as a mutable raw pointer.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut unified_ptr = cuda_malloc_unified::<u64>(1).unwrap();
/// let ptr: *mut u64 = unified_ptr.as_raw_mut();
/// *ptr = 5u64;
/// cuda_free_unified(unified_ptr);
/// }
/// ```
pub fn as_raw_mut(&mut self) -> *mut T {
self.0
}
/// Returns true if the pointer is null.
///
/// # Examples:
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// use std::ptr;
/// unsafe {
/// let null : *mut u64 = ptr::null_mut();
/// assert!(UnifiedPointer::wrap(null).is_null());
/// }
/// ```
pub fn is_null(self) -> bool {
self.0.is_null()
}
/// Calculates the offset from a unified pointer.
///
/// `count` is in units of T; eg. a `count` of 3 represents a pointer offset of
/// `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined
/// Behavior:
///
/// * Both the starting and resulting pointer must be either in bounds or one
/// byte past the end of *the same* allocated object.
///
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
///
/// * The offset being in bounds cannot rely on "wrapping around" the address
/// space. That is, the infinite-precision sum, **in bytes** must fit in a usize.
///
/// Consider using `wrapping_offset` instead if these constraints are
/// difficult to satisfy. The only advantage of this method is that it
/// enables more aggressive compiler optimizations.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut unified_ptr = cuda_malloc_unified::<u64>(5).unwrap();
/// let offset = unified_ptr.offset(1); // Points to the 2nd u64 in the buffer
/// cuda_free_unified(unified_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub unsafe fn offset(self, count: isize) -> Self {
Self::wrap(self.0.offset(count))
}
/// Calculates the offset from a unified pointer using wrapping arithmetic.
///
/// `count` is in units of T; eg. a `count` of 3 represents a pointer offset of
/// `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// The resulting pointer does not need to be in bounds, but it is
/// potentially hazardous to dereference (which requires `unsafe`).
/// In particular, the resulting pointer may *not* be used to access a
/// different allocated object than the one `self` points to. In other
/// words, `x.wrapping_offset(y.wrapping_offset_from(x))` is
/// *not* the same as `y`, and dereferencing it is undefined behavior
/// unless `x` and `y` point into the same allocated object.
///
/// Always use `.offset(count)` instead when possible, because `offset`
/// allows the compiler to optimize better. If you need to cross object
/// boundaries, cast the pointer to an integer and do the arithmetic there.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut unified_ptr = cuda_malloc_unified::<u64>(5).unwrap();
/// let offset = unified_ptr.wrapping_offset(1); // Points to the 2nd u64 in the buffer
/// cuda_free_unified(unified_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub fn wrapping_offset(self, count: isize) -> Self {
unsafe { Self::wrap(self.0.wrapping_offset(count)) }
}
/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`).
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined
/// Behavior:
///
/// * Both the starting and resulting pointer must be either in bounds or one
/// byte past the end of an allocated object.
///
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
///
/// * The offset being in bounds cannot rely on "wrapping around" the address
/// space. That is, the infinite-precision sum must fit in a `usize`.
///
/// Consider using `wrapping_offset` instead if these constraints are
/// difficult to satisfy. The only advantage of this method is that it
/// enables more aggressive compiler optimizations.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut unified_ptr = cuda_malloc_unified::<u64>(5).unwrap();
/// let offset = unified_ptr.add(1); // Points to the 2nd u64 in the buffer
/// cuda_free_unified(unified_ptr); // Must free the buffer using the original pointer
/// }
/// ```
#[allow(should_implement_trait)]
pub unsafe fn add(self, count: usize) -> Self
where
T: Sized,
{
self.offset(count as isize)
}
/// Calculates the offset from a pointer (convenience for
/// `.offset((count as isize).wrapping_neg())`).
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// If any of the following conditions are violated, the result is Undefined
/// Behavior:
///
/// * Both the starting and resulting pointer must be either in bounds or one
/// byte past the end of an allocated object.
///
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
///
/// * The offset being in bounds cannot rely on "wrapping around" the address
/// space. That is, the infinite-precision sum must fit in a `usize`.
///
/// Consider using `wrapping_offset` instead if these constraints are
/// difficult to satisfy. The only advantage of this method is that it
/// enables more aggressive compiler optimizations.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut unified_ptr = cuda_malloc_unified::<u64>(5).unwrap();
/// let offset = unified_ptr.add(4).sub(3); // Points to the 2nd u64 in the buffer
/// cuda_free_unified(unified_ptr); // Must free the buffer using the original pointer
/// }
#[allow(should_implement_trait)]
pub unsafe fn sub(self, count: usize) -> Self
where
T: Sized,
{
self.offset((count as isize).wrapping_neg())
}
/// Calculates the offset from a pointer using wrapping arithmetic.
/// (convenience for `.wrapping_offset(count as isize)`)
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// The resulting pointer does not need to be in bounds, but it is
/// potentially hazardous to dereference.
///
/// Always use `.add(count)` instead when possible, because `add`
/// allows the compiler to optimize better.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut unified_ptr = cuda_malloc_unified::<u64>(5).unwrap();
/// let offset = unified_ptr.wrapping_add(1); // Points to the 2nd u64 in the buffer
/// cuda_free_unified(unified_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub fn wrapping_add(self, count: usize) -> Self
where
T: Sized,
{
self.wrapping_offset(count as isize)
}
/// Calculates the offset from a pointer using wrapping arithmetic.
/// (convenience for `.wrapping_offset((count as isize).wrapping_sub())`)
///
/// `count` is in units of T; e.g. a `count` of 3 represents a pointer
/// offset of `3 * size_of::<T>()` bytes.
///
/// # Safety
///
/// The resulting pointer does not need to be in bounds, but it is
/// potentially hazardous to dereference (which requires `unsafe`).
///
/// Always use `.sub(count)` instead when possible, because `sub`
/// allows the compiler to optimize better.
///
/// # Examples
///
/// ```
/// # let _context = rustacuda::quick_init().unwrap();
/// use rustacuda::memory::*;
/// unsafe {
/// let mut unified_ptr = cuda_malloc_unified::<u64>(5).unwrap();
/// let offset = unified_ptr.wrapping_add(4).wrapping_sub(3); // Points to the 2nd u64 in the buffer
/// cuda_free_unified(unified_ptr); // Must free the buffer using the original pointer
/// }
/// ```
pub fn wrapping_sub(self, count: usize) -> Self
where
T: Sized,
{
self.wrapping_offset((count as isize).wrapping_neg())
}
}
impl<T: DeviceCopy> fmt::Pointer for UnifiedPointer<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&self.0, f)
}
}
impl<T: DeviceCopy> Clone for UnifiedPointer<T> {
fn clone(&self) -> Self {
UnifiedPointer(self.0)
}
}
impl<T: DeviceCopy> Copy for UnifiedPointer<T> {}