alloc.rs (2612e3bbc0386368a850140a6c9b990cd496a5ec) alloc.rs (89eed1ab1161e7d60595917e3b982e03dfcc0f8d)
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3//! Memory allocation APIs
4
5#![stable(feature = "alloc_module", since = "1.28.0")]
6
7#[cfg(not(test))]
8use core::intrinsics;
9use core::intrinsics::{min_align_of_val, size_of_val};
10
11use core::ptr::Unique;
12#[cfg(not(test))]
13use core::ptr::{self, NonNull};
14
15#[stable(feature = "alloc_module", since = "1.28.0")]
16#[doc(inline)]
17pub use core::alloc::*;
18
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3//! Memory allocation APIs
4
5#![stable(feature = "alloc_module", since = "1.28.0")]
6
7#[cfg(not(test))]
8use core::intrinsics;
9use core::intrinsics::{min_align_of_val, size_of_val};
10
11use core::ptr::Unique;
12#[cfg(not(test))]
13use core::ptr::{self, NonNull};
14
15#[stable(feature = "alloc_module", since = "1.28.0")]
16#[doc(inline)]
17pub use core::alloc::*;
18
19use core::marker::Destruct;
20
21#[cfg(test)]
22mod tests;
23
24extern "Rust" {
25 // These are the magic symbols to call the global allocator. rustc generates
26 // them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute
27 // (the code expanding that attribute macro generates those functions), or to call
28 // the default implementations in std (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)

--- 7 unchanged lines hidden (view full) ---

36 #[rustc_nounwind]
37 fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
38 #[rustc_reallocator]
39 #[rustc_nounwind]
40 fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
41 #[rustc_allocator_zeroed]
42 #[rustc_nounwind]
43 fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
19#[cfg(test)]
20mod tests;
21
22extern "Rust" {
23 // These are the magic symbols to call the global allocator. rustc generates
24 // them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute
25 // (the code expanding that attribute macro generates those functions), or to call
26 // the default implementations in std (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)

--- 7 unchanged lines hidden (view full) ---

34 #[rustc_nounwind]
35 fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
36 #[rustc_reallocator]
37 #[rustc_nounwind]
38 fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
39 #[rustc_allocator_zeroed]
40 #[rustc_nounwind]
41 fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
42
43 #[cfg(not(bootstrap))]
44 static __rust_no_alloc_shim_is_unstable: u8;
44}
45
46/// The global memory allocator.
47///
48/// This type implements the [`Allocator`] trait by forwarding calls
49/// to the allocator registered with the `#[global_allocator]` attribute
50/// if there is one, or the `std` crate’s default.
51///

--- 37 unchanged lines hidden (view full) ---

89///
90/// dealloc(ptr, layout);
91/// }
92/// ```
93#[stable(feature = "global_alloc", since = "1.28.0")]
94#[must_use = "losing the pointer will leak memory"]
95#[inline]
96pub unsafe fn alloc(layout: Layout) -> *mut u8 {
45}
46
47/// The global memory allocator.
48///
49/// This type implements the [`Allocator`] trait by forwarding calls
50/// to the allocator registered with the `#[global_allocator]` attribute
51/// if there is one, or the `std` crate’s default.
52///

--- 37 unchanged lines hidden (view full) ---

90///
91/// dealloc(ptr, layout);
92/// }
93/// ```
94#[stable(feature = "global_alloc", since = "1.28.0")]
95#[must_use = "losing the pointer will leak memory"]
96#[inline]
97pub unsafe fn alloc(layout: Layout) -> *mut u8 {
97 unsafe { __rust_alloc(layout.size(), layout.align()) }
98 unsafe {
99 // Make sure we don't accidentally allow omitting the allocator shim in
100 // stable code until it is actually stabilized.
101 #[cfg(not(bootstrap))]
102 core::ptr::read_volatile(&__rust_no_alloc_shim_is_unstable);
103
104 __rust_alloc(layout.size(), layout.align())
105 }
98}
99
100/// Deallocate memory with the global allocator.
101///
102/// This function forwards calls to the [`GlobalAlloc::dealloc`] method
103/// of the allocator registered with the `#[global_allocator]` attribute
104/// if there is one, or the `std` crate’s default.
105///

--- 222 unchanged lines hidden (view full) ---

328 match Global.allocate(layout) {
329 Ok(ptr) => ptr.as_mut_ptr(),
330 Err(_) => handle_alloc_error(layout),
331 }
332}
333
334#[cfg_attr(not(test), lang = "box_free")]
335#[inline]
106}
107
108/// Deallocate memory with the global allocator.
109///
110/// This function forwards calls to the [`GlobalAlloc::dealloc`] method
111/// of the allocator registered with the `#[global_allocator]` attribute
112/// if there is one, or the `std` crate’s default.
113///

--- 222 unchanged lines hidden (view full) ---

336 match Global.allocate(layout) {
337 Ok(ptr) => ptr.as_mut_ptr(),
338 Err(_) => handle_alloc_error(layout),
339 }
340}
341
342#[cfg_attr(not(test), lang = "box_free")]
343#[inline]
336#[rustc_const_unstable(feature = "const_box", issue = "92521")]
337// This signature has to be the same as `Box`, otherwise an ICE will happen.
338// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
339// well.
340// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
341// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
344// This signature has to be the same as `Box`, otherwise an ICE will happen.
345// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
346// well.
347// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
348// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
342pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Destruct>(
343 ptr: Unique<T>,
344 alloc: A,
345) {
349pub(crate) unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: Unique<T>, alloc: A) {
346 unsafe {
347 let size = size_of_val(ptr.as_ref());
348 let align = min_align_of_val(ptr.as_ref());
349 let layout = Layout::from_size_align_unchecked(size, align);
350 alloc.deallocate(From::from(ptr.cast()), layout)
351 }
352}
353

--- 92 unchanged lines hidden ---
350 unsafe {
351 let size = size_of_val(ptr.as_ref());
352 let align = min_align_of_val(ptr.as_ref());
353 let layout = Layout::from_size_align_unchecked(size, align);
354 alloc.deallocate(From::from(ptr.cast()), layout)
355 }
356}
357

--- 92 unchanged lines hidden ---