| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | #[allow(unused_imports)] |
| 6 | use super::allocator::{
|
| 7 | KVmalloc,
|
| 8 | Kmalloc,
|
| 9 | Vmalloc,
|
| 10 | VmallocPageIter, |
| 11 | };
|
| 12 |
|
| 13 | use super::{
|
| 14 | AllocError,
|
| 15 | Allocator,
|
| 16 | Flags,
|
| 17 | NumaNode, |
| 18 | };
|
| 19 |
|
| 20 | use crate::{
|
| 21 | fmt,
|
| 22 | page::AsPageIter,
|
| 23 | prelude::*,
|
| 24 | types::ForeignOwnable, |
| 25 | };
|
| 26 |
|
| 27 | use core::{
|
| 28 | alloc::Layout,
|
| 29 | borrow::{
|
| 30 | Borrow,
|
| 31 | BorrowMut, |
| 32 | },
|
| 33 | marker::PhantomData,
|
| 34 | mem::{
|
| 35 | ManuallyDrop,
|
| 36 | MaybeUninit, |
| 37 | },
|
| 38 | ops::{
|
| 39 | Deref,
|
| 40 | DerefMut, |
| 41 | },
|
| 42 | ptr::NonNull,
|
| 43 | result::Result, |
| 44 | };
|
| 45 |
|
| 46 | use pin_init::ZeroableOption;
|
| 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 | #[repr(transparent)]
|
| 103 | #[derive(core::marker::CoercePointee)]
|
| 104 | pub struct Box<#[pointee] T: ?Sized, A: Allocator>(NonNull<T>, PhantomData<A>);
|
| 105 |
|
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | pub type KBox<T> = Box<T, super::allocator::Kmalloc>;
|
| 117 |
|
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | pub type VBox<T> = Box<T, super::allocator::Vmalloc>;
|
| 129 |
|
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | pub type KVBox<T> = Box<T, super::allocator::KVmalloc>;
|
| 141 |
|
| 142 | |
| 143 | |
| 144 | unsafe impl<T, A: Allocator> ZeroableOption for Box<T, A> {}
|
| 145 |
|
| 146 | |
| 147 | unsafe impl<T, A> Send for Box<T, A>
|
| 148 | where
|
| 149 | T: Send + ?Sized,
|
| 150 | A: Allocator,
|
| 151 | {
|
| 152 | }
|
| 153 |
|
| 154 | |
| 155 | unsafe impl<T, A> Sync for Box<T, A>
|
| 156 | where
|
| 157 | T: Sync + ?Sized,
|
| 158 | A: Allocator,
|
| 159 | {
|
| 160 | }
|
| 161 |
|
| 162 | impl<T, A> Box<T, A>
|
| 163 | where
|
| 164 | T: ?Sized,
|
| 165 | A: Allocator,
|
| 166 | {
|
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | #[inline]
|
| 177 | pub const unsafe fn from_raw(raw: *mut T) -> Self {
|
| 178 | |
| 179 | |
| 180 | Self(unsafe { NonNull::new_unchecked(raw) }, PhantomData)
|
| 181 | }
|
| 182 |
|
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | #[inline]
|
| 201 | pub fn into_raw(b: Self) -> *mut T {
|
| 202 | ManuallyDrop::new(b).0.as_ptr()
|
| 203 | }
|
| 204 |
|
| 205 | |
| 206 | |
| 207 | |
| 208 | #[inline]
|
| 209 | pub fn leak<'a>(b: Self) -> &'a mut T {
|
| 210 | |
| 211 | |
| 212 | unsafe { &mut *Box::into_raw(b) }
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | impl<T, A> Box<MaybeUninit<T>, A>
|
| 217 | where
|
| 218 | A: Allocator,
|
| 219 | {
|
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | pub unsafe fn assume_init(self) -> Box<T, A> {
|
| 229 | let raw = Self::into_raw(self);
|
| 230 |
|
| 231 | |
| 232 | |
| 233 | |
| 234 | unsafe { Box::from_raw(raw.cast()) }
|
| 235 | }
|
| 236 |
|
| 237 | |
| 238 | pub fn write(mut self, value: T) -> Box<T, A> {
|
| 239 | (*self).write(value);
|
| 240 |
|
| 241 | |
| 242 | unsafe { self.assume_init() }
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | impl<T, A> Box<T, A>
|
| 247 | where
|
| 248 | A: Allocator,
|
| 249 | {
|
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | pub fn new(x: T, flags: Flags) -> Result<Self, AllocError> {
|
| 255 | let b = Self::new_uninit(flags)?;
|
| 256 | Ok(Box::write(b, x))
|
| 257 | }
|
| 258 |
|
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | pub fn new_uninit(flags: Flags) -> Result<Box<MaybeUninit<T>, A>, AllocError> {
|
| 274 | let layout = Layout::new::<MaybeUninit<T>>();
|
| 275 | let ptr = A::alloc(layout, flags, NumaNode::NO_NODE)?;
|
| 276 |
|
| 277 | |
| 278 | |
| 279 | Ok(Box(ptr.cast(), PhantomData))
|
| 280 | }
|
| 281 |
|
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | pub fn zeroed(flags: Flags) -> Result<Self, AllocError>
|
| 295 | where
|
| 296 | T: Zeroable,
|
| 297 | {
|
| 298 | |
| 299 | |
| 300 | Ok(unsafe { Self::new_uninit(flags | __GFP_ZERO)?.assume_init() })
|
| 301 | }
|
| 302 |
|
| 303 | |
| 304 | |
| 305 | #[inline]
|
| 306 | pub fn pin(x: T, flags: Flags) -> Result<Pin<Box<T, A>>, AllocError>
|
| 307 | where
|
| 308 | A: 'static,
|
| 309 | {
|
| 310 | Ok(Self::new(x, flags)?.into())
|
| 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 | pub fn pin_slice<Func, Item, E>(
|
| 359 | mut init: Func,
|
| 360 | len: usize,
|
| 361 | flags: Flags,
|
| 362 | ) -> Result<Pin<Box<[T], A>>, E>
|
| 363 | where
|
| 364 | Func: FnMut(usize) -> Item,
|
| 365 | Item: PinInit<T, E>,
|
| 366 | E: From<AllocError>,
|
| 367 | {
|
| 368 | let mut buffer = super::Vec::<T, A>::with_capacity(len, flags)?;
|
| 369 | for i in 0..len {
|
| 370 | let ptr = buffer.spare_capacity_mut().as_mut_ptr().cast();
|
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | unsafe { pin_init::raw_try_init(ptr, init(i))? };
|
| 376 |
|
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | unsafe { buffer.inc_len(1) };
|
| 383 | }
|
| 384 |
|
| 385 | let (ptr, _, _) = buffer.into_raw_parts();
|
| 386 | let slice = core::ptr::slice_from_raw_parts_mut(ptr, len);
|
| 387 |
|
| 388 | |
| 389 | |
| 390 | Ok(Pin::from(unsafe { Box::from_raw(slice) }))
|
| 391 | }
|
| 392 |
|
| 393 | |
| 394 | |
| 395 | pub fn into_pin(this: Self) -> Pin<Self> {
|
| 396 | this.into()
|
| 397 | }
|
| 398 |
|
| 399 | |
| 400 | fn forget_contents(this: Self) -> Box<MaybeUninit<T>, A> {
|
| 401 | let ptr = Self::into_raw(this);
|
| 402 |
|
| 403 | |
| 404 | unsafe { Box::from_raw(ptr.cast()) }
|
| 405 | }
|
| 406 |
|
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | pub fn drop_contents(this: Self) -> Box<MaybeUninit<T>, A> {
|
| 421 | let ptr = this.0.as_ptr();
|
| 422 |
|
| 423 | |
| 424 | |
| 425 | unsafe { core::ptr::drop_in_place(ptr) };
|
| 426 |
|
| 427 | Self::forget_contents(this)
|
| 428 | }
|
| 429 |
|
| 430 | |
| 431 | pub fn into_inner(b: Self) -> T {
|
| 432 | |
| 433 | let value = unsafe { core::ptr::read(&*b) };
|
| 434 | let _ = Self::forget_contents(b);
|
| 435 | value
|
| 436 | }
|
| 437 | }
|
| 438 |
|
| 439 | impl<T, A> From<Box<T, A>> for Pin<Box<T, A>>
|
| 440 | where
|
| 441 | T: ?Sized,
|
| 442 | A: Allocator,
|
| 443 | {
|
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | fn from(b: Box<T, A>) -> Self {
|
| 449 | |
| 450 | |
| 451 | unsafe { Pin::new_unchecked(b) }
|
| 452 | }
|
| 453 | }
|
| 454 |
|
| 455 | impl<T, A> InPlaceWrite<T> for Box<MaybeUninit<T>, A>
|
| 456 | where
|
| 457 | A: Allocator + 'static,
|
| 458 | {
|
| 459 | type Initialized = Box<T, A>;
|
| 460 |
|
| 461 | #[inline]
|
| 462 | fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
|
| 463 | let slot = self.as_mut_ptr();
|
| 464 | |
| 465 | |
| 466 | unsafe { pin_init::raw_try_init(slot, init)? };
|
| 467 | |
| 468 | Ok(unsafe { Box::assume_init(self) })
|
| 469 | }
|
| 470 |
|
| 471 | #[inline]
|
| 472 | fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
|
| 473 | let slot = self.as_mut_ptr();
|
| 474 | |
| 475 | |
| 476 | unsafe { pin_init::raw_try_init(slot, init)? };
|
| 477 | |
| 478 | Ok(unsafe { Box::assume_init(self) }.into())
|
| 479 | }
|
| 480 | }
|
| 481 |
|
| 482 | impl<T, A> InPlaceInit<T> for Box<T, A>
|
| 483 | where
|
| 484 | A: Allocator + 'static,
|
| 485 | {
|
| 486 | type PinnedSelf = Pin<Self>;
|
| 487 |
|
| 488 | #[inline]
|
| 489 | fn try_pin_init<E>(init: impl PinInit<T, E>, flags: Flags) -> Result<Pin<Self>, E>
|
| 490 | where
|
| 491 | E: From<AllocError>,
|
| 492 | {
|
| 493 | Box::<_, A>::new_uninit(flags)?.write_pin_init(init)
|
| 494 | }
|
| 495 |
|
| 496 | #[inline]
|
| 497 | fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
|
| 498 | where
|
| 499 | E: From<AllocError>,
|
| 500 | {
|
| 501 | Box::<_, A>::new_uninit(flags)?.write_init(init)
|
| 502 | }
|
| 503 | }
|
| 504 |
|
| 505 | |
| 506 | |
| 507 | unsafe impl<T, A> ForeignOwnable for Box<T, A>
|
| 508 | where
|
| 509 | A: Allocator,
|
| 510 | {
|
| 511 | const FOREIGN_ALIGN: usize = if core::mem::align_of::<T>() < A::MIN_ALIGN {
|
| 512 | A::MIN_ALIGN
|
| 513 | } else {
|
| 514 | core::mem::align_of::<T>()
|
| 515 | };
|
| 516 |
|
| 517 | type Borrowed<'a>
|
| 518 | = &'a T
|
| 519 | where
|
| 520 | Self: 'a;
|
| 521 | type BorrowedMut<'a>
|
| 522 | = &'a mut T
|
| 523 | where
|
| 524 | Self: 'a;
|
| 525 |
|
| 526 | fn into_foreign(self) -> *mut c_void {
|
| 527 | Box::into_raw(self).cast()
|
| 528 | }
|
| 529 |
|
| 530 | unsafe fn from_foreign(ptr: *mut c_void) -> Self {
|
| 531 | |
| 532 | |
| 533 | unsafe { Box::from_raw(ptr.cast()) }
|
| 534 | }
|
| 535 |
|
| 536 | unsafe fn borrow<'a>(ptr: *mut c_void) -> &'a T {
|
| 537 | |
| 538 | |
| 539 | unsafe { &*ptr.cast() }
|
| 540 | }
|
| 541 |
|
| 542 | unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> &'a mut T {
|
| 543 | let ptr = ptr.cast();
|
| 544 | |
| 545 | |
| 546 | unsafe { &mut *ptr }
|
| 547 | }
|
| 548 | }
|
| 549 |
|
| 550 | |
| 551 | |
| 552 | unsafe impl<T, A> ForeignOwnable for Pin<Box<T, A>>
|
| 553 | where
|
| 554 | A: Allocator,
|
| 555 | {
|
| 556 | const FOREIGN_ALIGN: usize = <Box<T, A> as ForeignOwnable>::FOREIGN_ALIGN;
|
| 557 | type Borrowed<'a>
|
| 558 | = Pin<&'a T>
|
| 559 | where
|
| 560 | Self: 'a;
|
| 561 | type BorrowedMut<'a>
|
| 562 | = Pin<&'a mut T>
|
| 563 | where
|
| 564 | Self: 'a;
|
| 565 |
|
| 566 | fn into_foreign(self) -> *mut c_void {
|
| 567 | |
| 568 | Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }).cast()
|
| 569 | }
|
| 570 |
|
| 571 | unsafe fn from_foreign(ptr: *mut c_void) -> Self {
|
| 572 | |
| 573 | |
| 574 | unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast())) }
|
| 575 | }
|
| 576 |
|
| 577 | unsafe fn borrow<'a>(ptr: *mut c_void) -> Pin<&'a T> {
|
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | let r = unsafe { &*ptr.cast() };
|
| 583 |
|
| 584 | |
| 585 | unsafe { Pin::new_unchecked(r) }
|
| 586 | }
|
| 587 |
|
| 588 | unsafe fn borrow_mut<'a>(ptr: *mut c_void) -> Pin<&'a mut T> {
|
| 589 | let ptr = ptr.cast();
|
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | let r = unsafe { &mut *ptr };
|
| 595 |
|
| 596 | |
| 597 | unsafe { Pin::new_unchecked(r) }
|
| 598 | }
|
| 599 | }
|
| 600 |
|
| 601 | impl<T, A> Deref for Box<T, A>
|
| 602 | where
|
| 603 | T: ?Sized,
|
| 604 | A: Allocator,
|
| 605 | {
|
| 606 | type Target = T;
|
| 607 |
|
| 608 | fn deref(&self) -> &T {
|
| 609 | |
| 610 | |
| 611 | unsafe { self.0.as_ref() }
|
| 612 | }
|
| 613 | }
|
| 614 |
|
| 615 | impl<T, A> DerefMut for Box<T, A>
|
| 616 | where
|
| 617 | T: ?Sized,
|
| 618 | A: Allocator,
|
| 619 | {
|
| 620 | fn deref_mut(&mut self) -> &mut T {
|
| 621 | |
| 622 | |
| 623 | unsafe { self.0.as_mut() }
|
| 624 | }
|
| 625 | }
|
| 626 |
|
| 627 | |
| 628 | |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | impl<T, A> Borrow<T> for Box<T, A>
|
| 645 | where
|
| 646 | T: ?Sized,
|
| 647 | A: Allocator,
|
| 648 | {
|
| 649 | fn borrow(&self) -> &T {
|
| 650 | self.deref()
|
| 651 | }
|
| 652 | }
|
| 653 |
|
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | impl<T, A> BorrowMut<T> for Box<T, A>
|
| 672 | where
|
| 673 | T: ?Sized,
|
| 674 | A: Allocator,
|
| 675 | {
|
| 676 | fn borrow_mut(&mut self) -> &mut T {
|
| 677 | self.deref_mut()
|
| 678 | }
|
| 679 | }
|
| 680 |
|
| 681 | impl<T, A> fmt::Display for Box<T, A>
|
| 682 | where
|
| 683 | T: ?Sized + fmt::Display,
|
| 684 | A: Allocator,
|
| 685 | {
|
| 686 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 687 | <T as fmt::Display>::fmt(&**self, f)
|
| 688 | }
|
| 689 | }
|
| 690 |
|
| 691 | impl<T, A> fmt::Debug for Box<T, A>
|
| 692 | where
|
| 693 | T: ?Sized + fmt::Debug,
|
| 694 | A: Allocator,
|
| 695 | {
|
| 696 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 697 | <T as fmt::Debug>::fmt(&**self, f)
|
| 698 | }
|
| 699 | }
|
| 700 |
|
| 701 | impl<T, A> Drop for Box<T, A>
|
| 702 | where
|
| 703 | T: ?Sized,
|
| 704 | A: Allocator,
|
| 705 | {
|
| 706 | fn drop(&mut self) {
|
| 707 | let layout = Layout::for_value::<T>(self);
|
| 708 |
|
| 709 | |
| 710 | unsafe { core::ptr::drop_in_place::<T>(self.deref_mut()) };
|
| 711 |
|
| 712 | |
| 713 | |
| 714 | |
| 715 | unsafe { A::free(self.0.cast(), layout) };
|
| 716 | }
|
| 717 | }
|
| 718 |
|
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | impl<T> AsPageIter for VBox<T> {
|
| 743 | type Iter<'a>
|
| 744 | = VmallocPageIter<'a>
|
| 745 | where
|
| 746 | T: 'a;
|
| 747 |
|
| 748 | fn page_iter(&mut self) -> Self::Iter<'_> {
|
| 749 | let ptr = self.0.cast();
|
| 750 | let size = core::mem::size_of::<T>();
|
| 751 |
|
| 752 | |
| 753 | |
| 754 | |
| 755 | |
| 756 | unsafe { VmallocPageIter::new(ptr, size) }
|
| 757 | }
|
| 758 | }
|