|
| 1 | +use crate::{ |
| 2 | + alloc::{AllocError, Allocator, Layout}, |
| 3 | + fmt, |
| 4 | + marker::PhantomData, |
| 5 | + ptr::NonNull, |
| 6 | +}; |
| 7 | + |
| 8 | +/// An allocator that requests some extra memory from the parent allocator for storing a prefix and/or a suffix. |
| 9 | +/// |
| 10 | +/// The alignment of the memory block is the maximum of the alignment of `Prefix` and the requested |
| 11 | +/// alignment. This may introduce an unused padding between `Prefix` and the returned memory. |
| 12 | +/// |
| 13 | +/// To get a pointer to the prefix, [`prefix()`] may be called. |
| 14 | +/// |
| 15 | +/// [`prefix()`]: Self::prefix |
| 16 | +/// |
| 17 | +/// Consider |
| 18 | +/// |
| 19 | +/// ```rust,ignore (not real code) |
| 20 | +/// #[repr(C)] |
| 21 | +/// struct Struct { |
| 22 | +/// t: T, |
| 23 | +/// data: Data, |
| 24 | +/// } |
| 25 | +/// ``` |
| 26 | +/// |
| 27 | +/// where `Data` is a type with layout `layout`. |
| 28 | +/// |
| 29 | +/// When this allocator creates an allocation for layout `layout`, the pointer can be |
| 30 | +/// offset by `-offsetof(Struct, data)` and the resulting pointer points is an allocation |
| 31 | +/// of `A` for `Layout::new::<Struct>()`. |
| 32 | +#[unstable(feature = "allocator_api_internals", issue = "none")] |
| 33 | +pub struct PrefixAllocator<Alloc, Prefix = ()> { |
| 34 | + /// The parent allocator to be used as backend |
| 35 | + pub parent: Alloc, |
| 36 | + _prefix: PhantomData<*const Prefix>, |
| 37 | +} |
| 38 | + |
| 39 | +impl<Alloc: fmt::Debug, Prefix> fmt::Debug for PrefixAllocator<Alloc, Prefix> { |
| 40 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 41 | + f.debug_struct("Affix").field("parent", &self.parent).finish() |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<Alloc: Default, Prefix> Default for PrefixAllocator<Alloc, Prefix> { |
| 46 | + fn default() -> Self { |
| 47 | + Self::new(Alloc::default()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<Alloc: Clone, Prefix> Clone for PrefixAllocator<Alloc, Prefix> { |
| 52 | + fn clone(&self) -> Self { |
| 53 | + Self::new(self.parent.clone()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<Alloc: Copy, Prefix> Copy for PrefixAllocator<Alloc, Prefix> {} |
| 58 | + |
| 59 | +impl<Alloc: PartialEq, Prefix> PartialEq for PrefixAllocator<Alloc, Prefix> { |
| 60 | + fn eq(&self, other: &Self) -> bool { |
| 61 | + self.parent.eq(&other.parent) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<Alloc: Eq, Prefix> Eq for PrefixAllocator<Alloc, Prefix> {} |
| 66 | + |
| 67 | +unsafe impl<Alloc: Send, Prefix> Send for PrefixAllocator<Alloc, Prefix> {} |
| 68 | +unsafe impl<Alloc: Sync, Prefix> Sync for PrefixAllocator<Alloc, Prefix> {} |
| 69 | +impl<Alloc: Unpin, Prefix> Unpin for PrefixAllocator<Alloc, Prefix> {} |
| 70 | + |
| 71 | +impl<Alloc, Prefix> PrefixAllocator<Alloc, Prefix> { |
| 72 | + pub const fn new(parent: Alloc) -> Self { |
| 73 | + Self { parent, _prefix: PhantomData } |
| 74 | + } |
| 75 | + |
| 76 | + /// Returns the offset between the `Prefix` and the stored data. |
| 77 | + #[inline] |
| 78 | + pub fn prefix_offset(layout: Layout) -> usize { |
| 79 | + let prefix_layout = Layout::new::<Prefix>(); |
| 80 | + prefix_layout.size() + prefix_layout.padding_needed_for(layout.align()) |
| 81 | + } |
| 82 | + |
| 83 | + /// Returns a pointer to the prefix. |
| 84 | + /// |
| 85 | + /// # Safety |
| 86 | + /// |
| 87 | + /// * `ptr` must denote a block of memory *[currently allocated]* via this allocator, and |
| 88 | + /// * `layout` must *[fit]* that block of memory. |
| 89 | + /// |
| 90 | + /// [currently allocated]: https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html#currently-allocated-memory |
| 91 | + /// [fit]: https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html#memory-fitting |
| 92 | + #[inline] |
| 93 | + pub unsafe fn prefix(ptr: NonNull<u8>, layout: Layout) -> NonNull<Prefix> { |
| 94 | + let prefix_offset = Self::prefix_offset(layout); |
| 95 | + // SAFETY: `prefix_offset` is smaller (and not equal to) `ptr` as the same function for calculating `prefix_offset` is used when allocating. |
| 96 | + unsafe { NonNull::new_unchecked(ptr.as_ptr().sub(prefix_offset)).cast() } |
| 97 | + } |
| 98 | + |
| 99 | + fn create_ptr(ptr: NonNull<[u8]>, offset_prefix: usize) -> NonNull<[u8]> { |
| 100 | + let len = ptr.len() - offset_prefix; |
| 101 | + |
| 102 | + // SAFETY: `prefix_offset` is smaller (and not equal to) `ptr` as the same function for calculating `prefix_offset` is used when allocating. |
| 103 | + let ptr = unsafe { NonNull::new_unchecked(ptr.as_mut_ptr().add(offset_prefix)) }; |
| 104 | + |
| 105 | + NonNull::slice_from_raw_parts(ptr, len) |
| 106 | + } |
| 107 | + |
| 108 | + #[inline] |
| 109 | + fn alloc_impl( |
| 110 | + layout: Layout, |
| 111 | + alloc: impl FnOnce(Layout) -> Result<NonNull<[u8]>, AllocError>, |
| 112 | + ) -> Result<NonNull<[u8]>, AllocError> { |
| 113 | + let (layout, offset_prefix) = |
| 114 | + Layout::new::<Prefix>().extend(layout).map_err(|_| AllocError)?; |
| 115 | + |
| 116 | + Ok(Self::create_ptr(alloc(layout)?, offset_prefix)) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +unsafe impl<Alloc, Prefix> Allocator for PrefixAllocator<Alloc, Prefix> |
| 121 | +where |
| 122 | + Alloc: Allocator, |
| 123 | +{ |
| 124 | + fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { |
| 125 | + Self::alloc_impl(layout, |l| self.parent.allocate(l)) |
| 126 | + } |
| 127 | + |
| 128 | + fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { |
| 129 | + Self::alloc_impl(layout, |l| self.parent.allocate_zeroed(l)) |
| 130 | + } |
| 131 | + |
| 132 | + unsafe fn grow( |
| 133 | + &self, |
| 134 | + _ptr: NonNull<u8>, |
| 135 | + _old_layout: Layout, |
| 136 | + _new_layout: Layout, |
| 137 | + ) -> Result<NonNull<[u8]>, AllocError> { |
| 138 | + // For (A)Rc it's not needed. When implementing please take care, if the alignment changes. |
| 139 | + unimplemented!("PrefixAllocator currently does not implement growing."); |
| 140 | + } |
| 141 | + |
| 142 | + unsafe fn grow_zeroed( |
| 143 | + &self, |
| 144 | + _ptr: NonNull<u8>, |
| 145 | + _old_layout: Layout, |
| 146 | + _new_layout: Layout, |
| 147 | + ) -> Result<NonNull<[u8]>, AllocError> { |
| 148 | + // For (A)Rc it's not needed. When implementing please take care, if the alignment changes. |
| 149 | + unimplemented!("PrefixAllocator currently does not implement growing."); |
| 150 | + } |
| 151 | + |
| 152 | + unsafe fn shrink( |
| 153 | + &self, |
| 154 | + _ptr: NonNull<u8>, |
| 155 | + _old_layout: Layout, |
| 156 | + _new_layout: Layout, |
| 157 | + ) -> Result<NonNull<[u8]>, AllocError> { |
| 158 | + // For (A)Rc it's not needed. When implementing please take care, if the alignment changes. |
| 159 | + unimplemented!("PrefixAllocator currently does not implement shrinking."); |
| 160 | + } |
| 161 | + |
| 162 | + unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { |
| 163 | + let (layout, prefix_offset) = Layout::new::<Prefix>().extend(layout).unwrap(); |
| 164 | + // SAFETY: `prefix_offset` is smaller (and not equal to) `ptr` as the same function for calculating `prefix_offset` is used when allocating. |
| 165 | + unsafe { |
| 166 | + let base_ptr = NonNull::new_unchecked(ptr.as_ptr().sub(prefix_offset)); |
| 167 | + self.parent.deallocate(base_ptr, layout) |
| 168 | + }; |
| 169 | + } |
| 170 | +} |
0 commit comments