|
| 1 | +use core::alloc::{Layout, LayoutError}; |
| 2 | +use core::mem::SizedTypeProperties; |
| 3 | +use core::ptr::NonNull; |
| 4 | + |
| 5 | +use crate::raw_rc::RefCounts; |
| 6 | + |
| 7 | +/// A `Layout` that describes a reference-counted allocation. |
| 8 | +#[derive(Clone, Copy)] |
| 9 | +pub(crate) struct RcLayout(Layout); |
| 10 | + |
| 11 | +impl RcLayout { |
| 12 | + /// Tries to create an `RcLayout` to store a value with layout `value_layout`. Returns `Err` if |
| 13 | + /// `value_layout` is too big to store in a reference-counted allocation. |
| 14 | + #[inline] |
| 15 | + pub(crate) const fn try_from_value_layout(value_layout: Layout) -> Result<Self, LayoutError> { |
| 16 | + match RefCounts::LAYOUT.extend(value_layout) { |
| 17 | + Ok((rc_layout, _)) => Ok(Self(rc_layout)), |
| 18 | + Err(error) => Err(error), |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + /// Creates an `RcLayout` to store a value with layout `value_layout`. Panics if `value_layout` |
| 23 | + /// is too big to store in a reference-counted allocation. |
| 24 | + #[cfg(not(no_global_oom_handling))] |
| 25 | + #[inline] |
| 26 | + pub(crate) fn from_value_layout(value_layout: Layout) -> Self { |
| 27 | + Self::try_from_value_layout(value_layout).unwrap() |
| 28 | + } |
| 29 | + |
| 30 | + /// Creates an `RcLayout` to store a value with layout `value_layout`. |
| 31 | + /// |
| 32 | + /// # Safety |
| 33 | + /// |
| 34 | + /// `RcLayout::try_from_value_layout(value_layout)` must return `Ok`. |
| 35 | + #[inline] |
| 36 | + pub(crate) unsafe fn from_value_layout_unchecked(value_layout: Layout) -> Self { |
| 37 | + unsafe { Self::try_from_value_layout(value_layout).unwrap_unchecked() } |
| 38 | + } |
| 39 | + |
| 40 | + /// Creates an `RcLayout` to store an array of `length` elements of type `T`. Panics if the array |
| 41 | + /// is too big to store in a reference-counted allocation. |
| 42 | + #[cfg(not(no_global_oom_handling))] |
| 43 | + pub(crate) fn new_array<T>(length: usize) -> Self { |
| 44 | + #[inline] |
| 45 | + fn inner(value_layout: Layout, length: usize) -> RcLayout { |
| 46 | + // We can use `repeat_packed` here because the outer function passes `T::LAYOUT` as the |
| 47 | + // `value_layout`, which is already padded to a multiple of its alignment. |
| 48 | + value_layout.repeat_packed(length).and_then(RcLayout::try_from_value_layout).unwrap() |
| 49 | + } |
| 50 | + |
| 51 | + inner(T::LAYOUT, length) |
| 52 | + } |
| 53 | + |
| 54 | + /// Returns an `Layout` object that describes the reference-counted allocation. |
| 55 | + pub(crate) fn get(&self) -> Layout { |
| 56 | + self.0 |
| 57 | + } |
| 58 | + |
| 59 | + /// Returns the byte offset of the value stored in a reference-counted allocation that is |
| 60 | + /// described by `self`. |
| 61 | + #[inline] |
| 62 | + pub(crate) fn value_offset(&self) -> usize { |
| 63 | + // SAFETY: |
| 64 | + // |
| 65 | + // This essentially calculates `size_of::<RefCounts>().next_multiple_of(self.align())`. |
| 66 | + // |
| 67 | + // See comments in `Layout::size_rounded_up_to_custom_align` for detailed explanation. |
| 68 | + unsafe { |
| 69 | + let align_m1 = self.0.align().unchecked_sub(1); |
| 70 | + |
| 71 | + size_of::<RefCounts>().unchecked_add(align_m1) & !align_m1 |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns the byte size of the value stored in a reference-counted allocation that is |
| 76 | + /// described by `self`. |
| 77 | + #[cfg(not(no_global_oom_handling))] |
| 78 | + #[inline] |
| 79 | + pub(crate) fn value_size(&self) -> usize { |
| 80 | + unsafe { self.0.size().unchecked_sub(self.value_offset()) } |
| 81 | + } |
| 82 | + |
| 83 | + /// Creates an `RcLayout` for storing a value that is pointed to by `value_ptr`. |
| 84 | + /// |
| 85 | + /// # Safety |
| 86 | + /// |
| 87 | + /// `value_ptr` has correct metadata of `T`. |
| 88 | + #[cfg(not(no_global_oom_handling))] |
| 89 | + pub(crate) unsafe fn from_value_ptr<T>(value_ptr: NonNull<T>) -> Self |
| 90 | + where |
| 91 | + T: ?Sized, |
| 92 | + { |
| 93 | + /// A helper trait for computing `RcLayout` to store a `Self` object. If `Self` is |
| 94 | + /// `Sized`, the `RcLayout` value is computed at compile time. |
| 95 | + trait SpecRcLayout { |
| 96 | + unsafe fn spec_rc_layout(value_ptr: NonNull<Self>) -> RcLayout; |
| 97 | + } |
| 98 | + |
| 99 | + impl<T> SpecRcLayout for T |
| 100 | + where |
| 101 | + T: ?Sized, |
| 102 | + { |
| 103 | + #[inline] |
| 104 | + default unsafe fn spec_rc_layout(value_ptr: NonNull<Self>) -> RcLayout { |
| 105 | + RcLayout::from_value_layout(unsafe { Layout::for_value_raw(value_ptr.as_ptr()) }) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + impl<T> SpecRcLayout for T { |
| 110 | + #[inline] |
| 111 | + unsafe fn spec_rc_layout(_: NonNull<Self>) -> RcLayout { |
| 112 | + Self::RC_LAYOUT |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + unsafe { T::spec_rc_layout(value_ptr) } |
| 117 | + } |
| 118 | + |
| 119 | + /// Creates an `RcLayout` for storing a value that is pointed to by `value_ptr`, assuming the |
| 120 | + /// value is small enough to fit inside a reference-counted allocation. |
| 121 | + /// |
| 122 | + /// # Safety |
| 123 | + /// |
| 124 | + /// - `value_ptr` has correct metadata for a `T` object. |
| 125 | + /// - It is known that the memory layout described by `value_ptr` can be used to create an |
| 126 | + /// `RcLayout` successfully. |
| 127 | + pub(crate) unsafe fn from_value_ptr_unchecked<T>(value_ptr: NonNull<T>) -> Self |
| 128 | + where |
| 129 | + T: ?Sized, |
| 130 | + { |
| 131 | + /// A helper trait for computing `RcLayout` to store a `Self` object. If `Self` is |
| 132 | + /// `Sized`, the `RcLayout` value is computed at compile time. |
| 133 | + trait SpecRcLayoutUnchecked { |
| 134 | + unsafe fn spec_rc_layout_unchecked(value_ptr: NonNull<Self>) -> RcLayout; |
| 135 | + } |
| 136 | + |
| 137 | + impl<T> SpecRcLayoutUnchecked for T |
| 138 | + where |
| 139 | + T: ?Sized, |
| 140 | + { |
| 141 | + #[inline] |
| 142 | + default unsafe fn spec_rc_layout_unchecked(value_ptr: NonNull<Self>) -> RcLayout { |
| 143 | + unsafe { |
| 144 | + RcLayout::from_value_layout_unchecked(Layout::for_value_raw(value_ptr.as_ptr())) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + impl<T> SpecRcLayoutUnchecked for T { |
| 150 | + #[inline] |
| 151 | + unsafe fn spec_rc_layout_unchecked(_: NonNull<Self>) -> RcLayout { |
| 152 | + Self::RC_LAYOUT |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + unsafe { T::spec_rc_layout_unchecked(value_ptr) } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +pub(crate) trait RcLayoutExt { |
| 161 | + /// Computes `RcLayout` at compile time if `Self` is `Sized`. |
| 162 | + const RC_LAYOUT: RcLayout; |
| 163 | +} |
| 164 | + |
| 165 | +impl<T> RcLayoutExt for T { |
| 166 | + const RC_LAYOUT: RcLayout = match RcLayout::try_from_value_layout(T::LAYOUT) { |
| 167 | + Ok(rc_layout) => rc_layout, |
| 168 | + Err(_) => panic!("value is too big to store in a reference-counted allocation"), |
| 169 | + }; |
| 170 | +} |
0 commit comments