1 //! Memory allocation APIs
3 #![stable(feature = "alloc_module", since = "1.28.0")]
5 use core::intrinsics::{min_align_of_val, size_of_val};
6 use core::ptr::{NonNull, Unique};
9 #[stable(feature = "alloc_module", since = "1.28.0")]
11 pub use core::alloc::*;
14 // These are the magic symbols to call the global allocator. rustc generates
15 // them from the `#[global_allocator]` attribute if there is one, or uses the
16 // default implementations in libstd (`__rdl_alloc` etc in `src/libstd/alloc.rs`)
19 #[rustc_allocator_nounwind]
20 fn __rust_alloc(size: usize, align: usize) -> *mut u8;
21 #[rustc_allocator_nounwind]
22 fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
23 #[rustc_allocator_nounwind]
24 fn __rust_realloc(ptr: *mut u8,
27 new_size: usize) -> *mut u8;
28 #[rustc_allocator_nounwind]
29 fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
32 /// The global memory allocator.
34 /// This type implements the [`Alloc`] trait by forwarding calls
35 /// to the allocator registered with the `#[global_allocator]` attribute
36 /// if there is one, or the `std` crate’s default.
37 #[unstable(feature = "allocator_api", issue = "32838")]
38 #[derive(Copy, Clone, Default, Debug)]
41 /// Allocate memory with the global allocator.
43 /// This function forwards calls to the [`GlobalAlloc::alloc`] method
44 /// of the allocator registered with the `#[global_allocator]` attribute
45 /// if there is one, or the `std` crate’s default.
47 /// This function is expected to be deprecated in favor of the `alloc` method
48 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
52 /// See [`GlobalAlloc::alloc`].
57 /// use std::alloc::{alloc, dealloc, Layout};
60 /// let layout = Layout::new::<u16>();
61 /// let ptr = alloc(layout);
63 /// *(ptr as *mut u16) = 42;
64 /// assert_eq!(*(ptr as *mut u16), 42);
66 /// dealloc(ptr, layout);
69 #[stable(feature = "global_alloc", since = "1.28.0")]
71 pub unsafe fn alloc(layout: Layout) -> *mut u8 {
72 __rust_alloc(layout.size(), layout.align())
75 /// Deallocate memory with the global allocator.
77 /// This function forwards calls to the [`GlobalAlloc::dealloc`] method
78 /// of the allocator registered with the `#[global_allocator]` attribute
79 /// if there is one, or the `std` crate’s default.
81 /// This function is expected to be deprecated in favor of the `dealloc` method
82 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
86 /// See [`GlobalAlloc::dealloc`].
87 #[stable(feature = "global_alloc", since = "1.28.0")]
89 pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
90 __rust_dealloc(ptr, layout.size(), layout.align())
93 /// Reallocate memory with the global allocator.
95 /// This function forwards calls to the [`GlobalAlloc::realloc`] method
96 /// of the allocator registered with the `#[global_allocator]` attribute
97 /// if there is one, or the `std` crate’s default.
99 /// This function is expected to be deprecated in favor of the `realloc` method
100 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
104 /// See [`GlobalAlloc::realloc`].
105 #[stable(feature = "global_alloc", since = "1.28.0")]
107 pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
108 __rust_realloc(ptr, layout.size(), layout.align(), new_size)
111 /// Allocate zero-initialized memory with the global allocator.
113 /// This function forwards calls to the [`GlobalAlloc::alloc_zeroed`] method
114 /// of the allocator registered with the `#[global_allocator]` attribute
115 /// if there is one, or the `std` crate’s default.
117 /// This function is expected to be deprecated in favor of the `alloc_zeroed` method
118 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
122 /// See [`GlobalAlloc::alloc_zeroed`].
127 /// use std::alloc::{alloc_zeroed, dealloc, Layout};
130 /// let layout = Layout::new::<u16>();
131 /// let ptr = alloc_zeroed(layout);
133 /// assert_eq!(*(ptr as *mut u16), 0);
135 /// dealloc(ptr, layout);
138 #[stable(feature = "global_alloc", since = "1.28.0")]
140 pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
141 __rust_alloc_zeroed(layout.size(), layout.align())
144 #[unstable(feature = "allocator_api", issue = "32838")]
145 unsafe impl Alloc for Global {
147 unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
148 NonNull::new(alloc(layout)).ok_or(AllocErr)
152 unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
153 dealloc(ptr.as_ptr(), layout)
157 unsafe fn realloc(&mut self,
161 -> Result<NonNull<u8>, AllocErr>
163 NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
167 unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
168 NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
172 /// The allocator for unique pointers.
173 // This function must not unwind. If it does, MIR codegen will fail.
175 #[lang = "exchange_malloc"]
177 unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
181 let layout = Layout::from_size_align_unchecked(size, align);
182 let ptr = alloc(layout);
186 handle_alloc_error(layout)
191 #[cfg_attr(not(test), lang = "box_free")]
193 pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
194 let ptr = ptr.as_ptr();
195 let size = size_of_val(&*ptr);
196 let align = min_align_of_val(&*ptr);
197 // We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
199 let layout = Layout::from_size_align_unchecked(size, align);
200 dealloc(ptr as *mut u8, layout);
204 /// Abort on memory allocation error or failure.
206 /// Callers of memory allocation APIs wishing to abort computation
207 /// in response to an allocation error are encouraged to call this function,
208 /// rather than directly invoking `panic!` or similar.
210 /// The default behavior of this function is to print a message to standard error
211 /// and abort the process.
212 /// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
214 /// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
215 /// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
216 #[stable(feature = "global_alloc", since = "1.28.0")]
217 #[rustc_allocator_nounwind]
218 pub fn handle_alloc_error(layout: Layout) -> ! {
219 #[allow(improper_ctypes)]
222 fn oom_impl(layout: Layout) -> !;
224 unsafe { oom_impl(layout) }
230 use self::test::Bencher;
232 use alloc::{Global, Alloc, Layout, handle_alloc_error};
235 fn allocate_zeroed() {
237 let layout = Layout::from_size_align(1024, 1).unwrap();
238 let ptr = Global.alloc_zeroed(layout.clone())
239 .unwrap_or_else(|_| handle_alloc_error(layout));
241 let mut i = ptr.cast::<u8>().as_ptr();
242 let end = i.add(layout.size());
247 Global.dealloc(ptr, layout);
252 fn alloc_owned_small(b: &mut Bencher) {
254 let _: Box<_> = box 10;