rune_alloc/alloc/
allocator.rs

1//! Types used to govern how allocations are performed.
2
3use core::alloc::Layout;
4
5use crate::alloc::AllocError;
6use crate::ptr::{self, NonNull};
7
8/// An implementation of `Allocator` can allocate, grow, shrink, and deallocate
9/// arbitrary blocks of data described via [`Layout`].
10///
11/// `Allocator` is designed to be implemented on ZSTs, references, or smart
12/// pointers because having an allocator like `MyAlloc([u8; N])` cannot be
13/// moved, without updating the pointers to the allocated memory.
14///
15/// Zero-sized allocations are allowed in `Allocator`. If an underlying
16/// allocator does not support this (like jemalloc) or return a null pointer
17/// (such as `libc::malloc`), this must be caught by the implementation.
18///
19/// ### Currently allocated memory
20///
21/// Some of the methods require that a memory block be *currently allocated* via
22/// an allocator. This means that:
23///
24/// * the starting address for that memory block was previously returned by
25///   [`allocate`], [`grow`], or [`shrink`], and
26///
27/// * the memory block has not been subsequently deallocated, where blocks are
28///   either deallocated directly by being passed to [`deallocate`] or were
29///   changed by being passed to [`grow`] or [`shrink`] that returns `Ok`. If
30///   `grow` or `shrink` have returned `Err`, the passed pointer remains valid.
31///
32/// [`allocate`]: Allocator::allocate
33/// [`grow`]: Allocator::grow
34/// [`shrink`]: Allocator::shrink
35/// [`deallocate`]: Allocator::deallocate
36///
37/// ### Memory fitting
38///
39/// Some of the methods require that a layout *fit* a memory block. What it
40/// means for a layout to "fit" a memory block means (or equivalently, for a
41/// memory block to "fit" a layout) is that the following conditions must hold:
42///
43/// * The block must be allocated with the same alignment as [`layout.align()`],
44///   and
45///
46/// * The provided [`layout.size()`] must fall in the range `min ..= max`,
47///   where:
48///   - `min` is the size of the layout most recently used to allocate the
49///     block, and
50///   - `max` is the latest actual size returned from [`allocate`], [`grow`], or
51///     [`shrink`].
52///
53/// [`layout.align()`]: Layout::align
54/// [`layout.size()`]: Layout::size
55///
56/// # Safety
57///
58/// * Memory blocks returned from an allocator that are [*currently allocated*]
59///   must point to valid memory and retain their validity while they are
60///   [*currently allocated*] and at least one of the instance and all of its
61///   clones has not been dropped.
62///
63/// * copying, cloning, or moving the allocator must not invalidate memory
64///   blocks returned from this allocator. A copied or cloned allocator must
65///   behave like the same allocator, and
66///
67/// * any pointer to a memory block which is [*currently allocated*] may be
68///   passed to any other method of the allocator.
69///
70/// [*currently allocated*]: #currently-allocated-memory
71pub unsafe trait Allocator {
72    /// Attempts to allocate a block of memory.
73    ///
74    /// On success, returns a [`NonNull<[u8]>`][NonNull] meeting the size and alignment guarantees of `layout`.
75    ///
76    /// The returned block may have a larger size than specified by `layout.size()`, and may or may
77    /// not have its contents initialized.
78    ///
79    /// # Errors
80    ///
81    /// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
82    /// allocator's size or alignment constraints.
83    ///
84    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
85    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
86    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
87    ///
88    /// Clients wishing to abort computation in response to an allocation error are encouraged to
89    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
90    ///
91    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
92    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
93
94    /// Behaves like `allocate`, but also ensures that the returned memory is zero-initialized.
95    ///
96    /// # Errors
97    ///
98    /// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
99    /// allocator's size or alignment constraints.
100    ///
101    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
102    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
103    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
104    ///
105    /// Clients wishing to abort computation in response to an allocation error are encouraged to
106    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
107    ///
108    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
109    fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
110        let ptr = self.allocate(layout)?;
111        // SAFETY: `alloc` returns a valid memory block
112        unsafe { ptr.as_ptr().cast::<u8>().write_bytes(0, ptr.len()) }
113        Ok(ptr)
114    }
115
116    /// Deallocates the memory referenced by `ptr`.
117    ///
118    /// # Safety
119    ///
120    /// * `ptr` must denote a block of memory [*currently allocated*] via this
121    ///   allocator, and
122    /// * `layout` must [*fit*] that block of memory.
123    ///
124    /// [*currently allocated*]: #currently-allocated-memory
125    /// [*fit*]: #memory-fitting
126    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
127
128    /// Attempts to extend the memory block.
129    ///
130    /// Returns a new [`NonNull<[u8]>`][NonNull] containing a pointer and the actual size of the allocated
131    /// memory. The pointer is suitable for holding data described by `new_layout`. To accomplish
132    /// this, the allocator may extend the allocation referenced by `ptr` to fit the new layout.
133    ///
134    /// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been
135    /// transferred to this allocator. Any access to the old `ptr` is Undefined Behavior, even if the
136    /// allocation was grown in-place. The newly returned pointer is the only valid pointer
137    /// for accessing this memory now.
138    ///
139    /// If this method returns `Err`, then ownership of the memory block has not been transferred to
140    /// this allocator, and the contents of the memory block are unaltered.
141    ///
142    /// # Safety
143    ///
144    /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator.
145    /// * `old_layout` must [*fit*] that block of memory (The `new_layout` argument need not fit it.).
146    /// * `new_layout.size()` must be greater than or equal to `old_layout.size()`.
147    ///
148    /// Note that `new_layout.align()` need not be the same as `old_layout.align()`.
149    ///
150    /// [*currently allocated*]: #currently-allocated-memory
151    /// [*fit*]: #memory-fitting
152    ///
153    /// # Errors
154    ///
155    /// Returns `Err` if the new layout does not meet the allocator's size and alignment
156    /// constraints of the allocator, or if growing otherwise fails.
157    ///
158    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
159    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
160    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
161    ///
162    /// Clients wishing to abort computation in response to an allocation error are encouraged to
163    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
164    ///
165    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
166    unsafe fn grow(
167        &self,
168        ptr: NonNull<u8>,
169        old_layout: Layout,
170        new_layout: Layout,
171    ) -> Result<NonNull<[u8]>, AllocError> {
172        debug_assert!(
173            new_layout.size() >= old_layout.size(),
174            "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
175        );
176
177        let new_ptr = self.allocate(new_layout)?;
178
179        // SAFETY: because `new_layout.size()` must be greater than or equal to
180        // `old_layout.size()`, both the old and new memory allocation are valid
181        // for reads and writes for `old_layout.size()` bytes. Also, because the
182        // old allocation wasn't yet deallocated, it cannot overlap `new_ptr`.
183        // Thus, the call to `copy_nonoverlapping` is safe. The safety contract
184        // for `dealloc` must be upheld by the caller.
185        unsafe {
186            ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr() as *mut u8, old_layout.size());
187            self.deallocate(ptr, old_layout);
188        }
189
190        Ok(new_ptr)
191    }
192
193    /// Behaves like `grow`, but also ensures that the new contents are set to
194    /// zero before being returned.
195    ///
196    /// The memory block will contain the following contents after a successful
197    /// call to `grow_zeroed`:
198    ///   * Bytes `0..old_layout.size()` are preserved from the original
199    ///     allocation.
200    ///   * Bytes `old_layout.size()..old_size` will either be preserved or
201    ///     zeroed, depending on the allocator implementation. `old_size` refers
202    ///     to the size of the memory block prior to the `grow_zeroed` call,
203    ///     which may be larger than the size that was originally requested when
204    ///     it was allocated.
205    ///   * Bytes `old_size..new_size` are zeroed. `new_size` refers to the size
206    ///     of the memory block returned by the `grow_zeroed` call.
207    ///
208    /// # Safety
209    ///
210    /// * `ptr` must denote a block of memory [*currently allocated*] via this
211    ///   allocator.
212    /// * `old_layout` must [*fit*] that block of memory (The `new_layout`
213    ///   argument need not fit it.).
214    /// * `new_layout.size()` must be greater than or equal to
215    ///   `old_layout.size()`.
216    ///
217    /// Note that `new_layout.align()` need not be the same as
218    /// `old_layout.align()`.
219    ///
220    /// [*currently allocated*]: #currently-allocated-memory
221    /// [*fit*]: #memory-fitting
222    ///
223    /// # Errors
224    ///
225    /// Returns `Err` if the new layout does not meet the allocator's size and
226    /// alignment constraints of the allocator, or if growing otherwise fails.
227    ///
228    /// Implementations are encouraged to return `Err` on memory exhaustion
229    /// rather than panicking or aborting, but this is not a strict requirement.
230    /// (Specifically: it is *legal* to implement this trait atop an underlying
231    /// native allocation library that aborts on memory exhaustion.)
232    ///
233    /// Clients wishing to abort computation in response to an allocation error
234    /// are encouraged to call the [`handle_alloc_error`] function, rather than
235    /// directly invoking `panic!` or similar.
236    ///
237    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
238    unsafe fn shrink(
239        &self,
240        ptr: NonNull<u8>,
241        old_layout: Layout,
242        new_layout: Layout,
243    ) -> Result<NonNull<[u8]>, AllocError> {
244        debug_assert!(
245            new_layout.size() <= old_layout.size(),
246            "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
247        );
248
249        let new_ptr = self.allocate(new_layout)?;
250
251        // SAFETY: because `new_layout.size()` must be lower than or equal to
252        // `old_layout.size()`, both the old and new memory allocation are valid for reads and
253        // writes for `new_layout.size()` bytes. Also, because the old allocation wasn't yet
254        // deallocated, it cannot overlap `new_ptr`. Thus, the call to `copy_nonoverlapping` is
255        // safe. The safety contract for `dealloc` must be upheld by the caller.
256        unsafe {
257            ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr() as *mut u8, new_layout.size());
258            self.deallocate(ptr, old_layout);
259        }
260
261        Ok(new_ptr)
262    }
263}
264
265unsafe impl<A> Allocator for &A
266where
267    A: Allocator + ?Sized,
268{
269    #[inline]
270    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
271        (**self).allocate(layout)
272    }
273
274    #[inline]
275    fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
276        (**self).allocate_zeroed(layout)
277    }
278
279    #[inline]
280    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
281        (**self).deallocate(ptr, layout)
282    }
283
284    #[inline]
285    unsafe fn grow(
286        &self,
287        ptr: NonNull<u8>,
288        old_layout: Layout,
289        new_layout: Layout,
290    ) -> Result<NonNull<[u8]>, AllocError> {
291        (**self).grow(ptr, old_layout, new_layout)
292    }
293
294    #[inline]
295    unsafe fn shrink(
296        &self,
297        ptr: NonNull<u8>,
298        old_layout: Layout,
299        new_layout: Layout,
300    ) -> Result<NonNull<[u8]>, AllocError> {
301        (**self).shrink(ptr, old_layout, new_layout)
302    }
303}