core/alloc/
mod.rs

1//! Memory allocation APIs
2
3#![stable(feature = "alloc_module", since = "1.28.0")]
4
5#[cfg(not(feature = "ferrocene_certified"))]
6mod global;
7mod layout;
8
9#[stable(feature = "global_alloc", since = "1.28.0")]
10#[cfg(not(feature = "ferrocene_certified"))]
11pub use self::global::GlobalAlloc;
12#[stable(feature = "alloc_layout", since = "1.28.0")]
13pub use self::layout::Layout;
14#[stable(feature = "alloc_layout", since = "1.28.0")]
15#[deprecated(
16    since = "1.52.0",
17    note = "Name does not follow std convention, use LayoutError",
18    suggestion = "LayoutError"
19)]
20#[allow(deprecated, deprecated_in_future)]
21#[cfg(not(feature = "ferrocene_certified"))]
22pub use self::layout::LayoutErr;
23#[stable(feature = "alloc_layout_error", since = "1.50.0")]
24#[cfg(not(feature = "ferrocene_certified"))]
25pub use self::layout::LayoutError;
26#[cfg(not(feature = "ferrocene_certified"))]
27use crate::error::Error;
28#[cfg(not(feature = "ferrocene_certified"))]
29use crate::fmt;
30#[cfg(not(feature = "ferrocene_certified"))]
31use crate::ptr::{self, NonNull};
32
33/// The `AllocError` error indicates an allocation failure
34/// that may be due to resource exhaustion or to
35/// something wrong when combining the given input arguments with this
36/// allocator.
37#[unstable(feature = "allocator_api", issue = "32838")]
38#[derive(Copy, Clone, PartialEq, Eq, Debug)]
39#[cfg(not(feature = "ferrocene_certified"))]
40pub struct AllocError;
41
42#[unstable(
43    feature = "allocator_api",
44    reason = "the precise API and guarantees it provides may be tweaked.",
45    issue = "32838"
46)]
47#[cfg(not(feature = "ferrocene_certified"))]
48impl Error for AllocError {}
49
50// (we need this for downstream impl of trait Error)
51#[unstable(feature = "allocator_api", issue = "32838")]
52#[cfg(not(feature = "ferrocene_certified"))]
53impl fmt::Display for AllocError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        f.write_str("memory allocation failed")
56    }
57}
58
59/// An implementation of `Allocator` can allocate, grow, shrink, and deallocate arbitrary blocks of
60/// data described via [`Layout`][].
61///
62/// `Allocator` is designed to be implemented on ZSTs, references, or smart pointers.
63/// An allocator for `MyAlloc([u8; N])` cannot be moved, without updating the pointers to the
64/// allocated memory.
65///
66/// In contrast to [`GlobalAlloc`][], `Allocator` allows zero-sized allocations. If an underlying
67/// allocator does not support this (like jemalloc) or responds by returning a null pointer
68/// (such as `libc::malloc`), this must be caught by the implementation.
69///
70/// ### Currently allocated memory
71///
72/// Some of the methods require that a memory block is *currently allocated* by an allocator.
73/// This means that:
74///  * the starting address for that memory block was previously
75///    returned by [`allocate`], [`grow`], or [`shrink`], and
76///  * the memory block has not subsequently been deallocated.
77///
78/// A memory block is deallocated by a call to [`deallocate`],
79/// or by a call to [`grow`] or [`shrink`] that returns `Ok`.
80/// A call to `grow` or `shrink` that returns `Err`,
81/// does not deallocate the memory block passed to it.
82///
83/// [`allocate`]: Allocator::allocate
84/// [`grow`]: Allocator::grow
85/// [`shrink`]: Allocator::shrink
86/// [`deallocate`]: Allocator::deallocate
87///
88/// ### Memory fitting
89///
90/// Some of the methods require that a `layout` *fit* a memory block or vice versa. This means that the
91/// following conditions must hold:
92///  * the memory block must be *currently allocated* with alignment of [`layout.align()`], and
93///  * [`layout.size()`] must fall in the range `min ..= max`, where:
94///    - `min` is the size of the layout used to allocate the block, and
95///    - `max` is the actual size returned from [`allocate`], [`grow`], or [`shrink`].
96///
97/// [`layout.align()`]: Layout::align
98/// [`layout.size()`]: Layout::size
99///
100/// # Safety
101///
102/// Memory blocks that are [*currently allocated*] by an allocator,
103/// must point to valid memory, and retain their validity until either:
104///  - the memory block is deallocated, or
105///  - the allocator is dropped.
106///
107/// Copying, cloning, or moving the allocator must not invalidate memory blocks returned from it.
108/// A copied or cloned allocator must behave like the original allocator.
109///
110/// A memory block which is [*currently allocated*] may be passed to
111/// any method of the allocator that accepts such an argument.
112///
113/// [*currently allocated*]: #currently-allocated-memory
114#[unstable(feature = "allocator_api", issue = "32838")]
115#[cfg(not(feature = "ferrocene_certified"))]
116pub unsafe trait Allocator {
117    /// Attempts to allocate a block of memory.
118    ///
119    /// On success, returns a [`NonNull<[u8]>`][NonNull] meeting the size and alignment guarantees of `layout`.
120    ///
121    /// The returned block may have a larger size than specified by `layout.size()`, and may or may
122    /// not have its contents initialized.
123    ///
124    /// The returned block of memory remains valid as long as it is [*currently allocated*] and the shorter of:
125    ///   - the borrow-checker lifetime of the allocator type itself.
126    ///   - as long as the allocator and all its clones have not been dropped.
127    ///
128    /// [*currently allocated*]: #currently-allocated-memory
129    ///
130    /// # Errors
131    ///
132    /// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
133    /// allocator's size or alignment constraints.
134    ///
135    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
136    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
137    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
138    ///
139    /// Clients wishing to abort computation in response to an allocation error are encouraged to
140    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
141    ///
142    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
143    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
144
145    /// Behaves like `allocate`, but also ensures that the returned memory is zero-initialized.
146    ///
147    /// # Errors
148    ///
149    /// Returning `Err` indicates that either memory is exhausted or `layout` does not meet
150    /// allocator's size or alignment constraints.
151    ///
152    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
153    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
154    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
155    ///
156    /// Clients wishing to abort computation in response to an allocation error are encouraged to
157    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
158    ///
159    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
160    fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
161        let ptr = self.allocate(layout)?;
162        // SAFETY: `alloc` returns a valid memory block
163        unsafe { ptr.as_non_null_ptr().as_ptr().write_bytes(0, ptr.len()) }
164        Ok(ptr)
165    }
166
167    /// Deallocates the memory referenced by `ptr`.
168    ///
169    /// # Safety
170    ///
171    /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator, and
172    /// * `layout` must [*fit*] that block of memory.
173    ///
174    /// [*currently allocated*]: #currently-allocated-memory
175    /// [*fit*]: #memory-fitting
176    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
177
178    /// Attempts to extend the memory block.
179    ///
180    /// Returns a new [`NonNull<[u8]>`][NonNull] containing a pointer and the actual size of the allocated
181    /// memory. The pointer is suitable for holding data described by `new_layout`. To accomplish
182    /// this, the allocator may extend the allocation referenced by `ptr` to fit the new layout.
183    ///
184    /// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been
185    /// transferred to this allocator. Any access to the old `ptr` is Undefined Behavior, even if the
186    /// allocation was grown in-place. The newly returned pointer is the only valid pointer
187    /// for accessing this memory now.
188    ///
189    /// If this method returns `Err`, then ownership of the memory block has not been transferred to
190    /// this allocator, and the contents of the memory block are unaltered.
191    ///
192    /// # Safety
193    ///
194    /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator.
195    /// * `old_layout` must [*fit*] that block of memory (The `new_layout` argument need not fit it.).
196    /// * `new_layout.size()` must be greater than or equal to `old_layout.size()`.
197    ///
198    /// Note that `new_layout.align()` need not be the same as `old_layout.align()`.
199    ///
200    /// [*currently allocated*]: #currently-allocated-memory
201    /// [*fit*]: #memory-fitting
202    ///
203    /// # Errors
204    ///
205    /// Returns `Err` if the new layout does not meet the allocator's size and alignment
206    /// constraints of the allocator, or if growing otherwise fails.
207    ///
208    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
209    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
210    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
211    ///
212    /// Clients wishing to abort computation in response to an allocation error are encouraged to
213    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
214    ///
215    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
216    unsafe fn grow(
217        &self,
218        ptr: NonNull<u8>,
219        old_layout: Layout,
220        new_layout: Layout,
221    ) -> Result<NonNull<[u8]>, AllocError> {
222        debug_assert!(
223            new_layout.size() >= old_layout.size(),
224            "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
225        );
226
227        let new_ptr = self.allocate(new_layout)?;
228
229        // SAFETY: because `new_layout.size()` must be greater than or equal to
230        // `old_layout.size()`, both the old and new memory allocation are valid for reads and
231        // writes for `old_layout.size()` bytes. Also, because the old allocation wasn't yet
232        // deallocated, it cannot overlap `new_ptr`. Thus, the call to `copy_nonoverlapping` is
233        // safe. The safety contract for `dealloc` must be upheld by the caller.
234        unsafe {
235            ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_layout.size());
236            self.deallocate(ptr, old_layout);
237        }
238
239        Ok(new_ptr)
240    }
241
242    /// Behaves like `grow`, but also ensures that the new contents are set to zero before being
243    /// returned.
244    ///
245    /// The memory block will contain the following contents after a successful call to
246    /// `grow_zeroed`:
247    ///   * Bytes `0..old_layout.size()` are preserved from the original allocation.
248    ///   * Bytes `old_layout.size()..old_size` will either be preserved or zeroed, depending on
249    ///     the allocator implementation. `old_size` refers to the size of the memory block prior
250    ///     to the `grow_zeroed` call, which may be larger than the size that was originally
251    ///     requested when it was allocated.
252    ///   * Bytes `old_size..new_size` are zeroed. `new_size` refers to the size of the memory
253    ///     block returned by the `grow_zeroed` call.
254    ///
255    /// # Safety
256    ///
257    /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator.
258    /// * `old_layout` must [*fit*] that block of memory (The `new_layout` argument need not fit it.).
259    /// * `new_layout.size()` must be greater than or equal to `old_layout.size()`.
260    ///
261    /// Note that `new_layout.align()` need not be the same as `old_layout.align()`.
262    ///
263    /// [*currently allocated*]: #currently-allocated-memory
264    /// [*fit*]: #memory-fitting
265    ///
266    /// # Errors
267    ///
268    /// Returns `Err` if the new layout does not meet the allocator's size and alignment
269    /// constraints of the allocator, or if growing otherwise fails.
270    ///
271    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
272    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
273    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
274    ///
275    /// Clients wishing to abort computation in response to an allocation error are encouraged to
276    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
277    ///
278    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
279    unsafe fn grow_zeroed(
280        &self,
281        ptr: NonNull<u8>,
282        old_layout: Layout,
283        new_layout: Layout,
284    ) -> Result<NonNull<[u8]>, AllocError> {
285        debug_assert!(
286            new_layout.size() >= old_layout.size(),
287            "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
288        );
289
290        let new_ptr = self.allocate_zeroed(new_layout)?;
291
292        // SAFETY: because `new_layout.size()` must be greater than or equal to
293        // `old_layout.size()`, both the old and new memory allocation are valid for reads and
294        // writes for `old_layout.size()` bytes. Also, because the old allocation wasn't yet
295        // deallocated, it cannot overlap `new_ptr`. Thus, the call to `copy_nonoverlapping` is
296        // safe. The safety contract for `dealloc` must be upheld by the caller.
297        unsafe {
298            ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_layout.size());
299            self.deallocate(ptr, old_layout);
300        }
301
302        Ok(new_ptr)
303    }
304
305    /// Attempts to shrink the memory block.
306    ///
307    /// Returns a new [`NonNull<[u8]>`][NonNull] containing a pointer and the actual size of the allocated
308    /// memory. The pointer is suitable for holding data described by `new_layout`. To accomplish
309    /// this, the allocator may shrink the allocation referenced by `ptr` to fit the new layout.
310    ///
311    /// If this returns `Ok`, then ownership of the memory block referenced by `ptr` has been
312    /// transferred to this allocator. Any access to the old `ptr` is Undefined Behavior, even if the
313    /// allocation was shrunk in-place. The newly returned pointer is the only valid pointer
314    /// for accessing this memory now.
315    ///
316    /// If this method returns `Err`, then ownership of the memory block has not been transferred to
317    /// this allocator, and the contents of the memory block are unaltered.
318    ///
319    /// # Safety
320    ///
321    /// * `ptr` must denote a block of memory [*currently allocated*] via this allocator.
322    /// * `old_layout` must [*fit*] that block of memory (The `new_layout` argument need not fit it.).
323    /// * `new_layout.size()` must be smaller than or equal to `old_layout.size()`.
324    ///
325    /// Note that `new_layout.align()` need not be the same as `old_layout.align()`.
326    ///
327    /// [*currently allocated*]: #currently-allocated-memory
328    /// [*fit*]: #memory-fitting
329    ///
330    /// # Errors
331    ///
332    /// Returns `Err` if the new layout does not meet the allocator's size and alignment
333    /// constraints of the allocator, or if shrinking otherwise fails.
334    ///
335    /// Implementations are encouraged to return `Err` on memory exhaustion rather than panicking or
336    /// aborting, but this is not a strict requirement. (Specifically: it is *legal* to implement
337    /// this trait atop an underlying native allocation library that aborts on memory exhaustion.)
338    ///
339    /// Clients wishing to abort computation in response to an allocation error are encouraged to
340    /// call the [`handle_alloc_error`] function, rather than directly invoking `panic!` or similar.
341    ///
342    /// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
343    unsafe fn shrink(
344        &self,
345        ptr: NonNull<u8>,
346        old_layout: Layout,
347        new_layout: Layout,
348    ) -> Result<NonNull<[u8]>, AllocError> {
349        debug_assert!(
350            new_layout.size() <= old_layout.size(),
351            "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
352        );
353
354        let new_ptr = self.allocate(new_layout)?;
355
356        // SAFETY: because `new_layout.size()` must be lower than or equal to
357        // `old_layout.size()`, both the old and new memory allocation are valid for reads and
358        // writes for `new_layout.size()` bytes. Also, because the old allocation wasn't yet
359        // deallocated, it cannot overlap `new_ptr`. Thus, the call to `copy_nonoverlapping` is
360        // safe. The safety contract for `dealloc` must be upheld by the caller.
361        unsafe {
362            ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_layout.size());
363            self.deallocate(ptr, old_layout);
364        }
365
366        Ok(new_ptr)
367    }
368
369    /// Creates a "by reference" adapter for this instance of `Allocator`.
370    ///
371    /// The returned adapter also implements `Allocator` and will simply borrow this.
372    #[inline(always)]
373    fn by_ref(&self) -> &Self
374    where
375        Self: Sized,
376    {
377        self
378    }
379}
380
381#[unstable(feature = "allocator_api", issue = "32838")]
382#[cfg(not(feature = "ferrocene_certified"))]
383unsafe impl<A> Allocator for &A
384where
385    A: Allocator + ?Sized,
386{
387    #[inline]
388    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
389        (**self).allocate(layout)
390    }
391
392    #[inline]
393    fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
394        (**self).allocate_zeroed(layout)
395    }
396
397    #[inline]
398    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
399        // SAFETY: the safety contract must be upheld by the caller
400        unsafe { (**self).deallocate(ptr, layout) }
401    }
402
403    #[inline]
404    unsafe fn grow(
405        &self,
406        ptr: NonNull<u8>,
407        old_layout: Layout,
408        new_layout: Layout,
409    ) -> Result<NonNull<[u8]>, AllocError> {
410        // SAFETY: the safety contract must be upheld by the caller
411        unsafe { (**self).grow(ptr, old_layout, new_layout) }
412    }
413
414    #[inline]
415    unsafe fn grow_zeroed(
416        &self,
417        ptr: NonNull<u8>,
418        old_layout: Layout,
419        new_layout: Layout,
420    ) -> Result<NonNull<[u8]>, AllocError> {
421        // SAFETY: the safety contract must be upheld by the caller
422        unsafe { (**self).grow_zeroed(ptr, old_layout, new_layout) }
423    }
424
425    #[inline]
426    unsafe fn shrink(
427        &self,
428        ptr: NonNull<u8>,
429        old_layout: Layout,
430        new_layout: Layout,
431    ) -> Result<NonNull<[u8]>, AllocError> {
432        // SAFETY: the safety contract must be upheld by the caller
433        unsafe { (**self).shrink(ptr, old_layout, new_layout) }
434    }
435}