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