1 // Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Memory allocation APIs
13 #![stable(feature = "alloc_module", since = "1.28.0")]
15 use core::intrinsics::{min_align_of_val, size_of_val};
16 use core::ptr::{NonNull, Unique};
19 #[stable(feature = "alloc_module", since = "1.28.0")]
21 pub use core::alloc::*;
25 #[rustc_allocator_nounwind]
26 fn __rust_alloc(size: usize, align: usize) -> *mut u8;
27 #[rustc_allocator_nounwind]
28 fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
29 #[rustc_allocator_nounwind]
30 fn __rust_realloc(ptr: *mut u8,
33 new_size: usize) -> *mut u8;
34 #[rustc_allocator_nounwind]
35 fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
38 /// The global memory allocator.
40 /// This type implements the [`Alloc`] trait by forwarding calls
41 /// to the allocator registered with the `#[global_allocator]` attribute
42 /// if there is one, or the `std` crate’s default.
43 #[unstable(feature = "allocator_api", issue = "32838")]
44 #[derive(Copy, Clone, Default, Debug)]
47 /// Allocate memory with the global allocator.
49 /// This function forwards calls to the [`GlobalAlloc::alloc`] method
50 /// of the allocator registered with the `#[global_allocator]` attribute
51 /// if there is one, or the `std` crate’s default.
53 /// This function is expected to be deprecated in favor of the `alloc` method
54 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
58 /// See [`GlobalAlloc::alloc`].
63 /// use std::alloc::{alloc, dealloc, Layout};
66 /// let layout = Layout::new::<u16>();
67 /// let ptr = alloc(layout);
69 /// *(ptr as *mut u16) = 42;
70 /// assert_eq!(*(ptr as *mut u16), 42);
72 /// dealloc(ptr, layout);
75 #[stable(feature = "global_alloc", since = "1.28.0")]
77 pub unsafe fn alloc(layout: Layout) -> *mut u8 {
78 __rust_alloc(layout.size(), layout.align())
81 /// Deallocate memory with the global allocator.
83 /// This function forwards calls to the [`GlobalAlloc::dealloc`] method
84 /// of the allocator registered with the `#[global_allocator]` attribute
85 /// if there is one, or the `std` crate’s default.
87 /// This function is expected to be deprecated in favor of the `dealloc` method
88 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
92 /// See [`GlobalAlloc::dealloc`].
93 #[stable(feature = "global_alloc", since = "1.28.0")]
95 pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
96 __rust_dealloc(ptr, layout.size(), layout.align())
99 /// Reallocate memory with the global allocator.
101 /// This function forwards calls to the [`GlobalAlloc::realloc`] method
102 /// of the allocator registered with the `#[global_allocator]` attribute
103 /// if there is one, or the `std` crate’s default.
105 /// This function is expected to be deprecated in favor of the `realloc` method
106 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
110 /// See [`GlobalAlloc::realloc`].
111 #[stable(feature = "global_alloc", since = "1.28.0")]
113 pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
114 __rust_realloc(ptr, layout.size(), layout.align(), new_size)
117 /// Allocate zero-initialized memory with the global allocator.
119 /// This function forwards calls to the [`GlobalAlloc::alloc_zeroed`] method
120 /// of the allocator registered with the `#[global_allocator]` attribute
121 /// if there is one, or the `std` crate’s default.
123 /// This function is expected to be deprecated in favor of the `alloc_zeroed` method
124 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
128 /// See [`GlobalAlloc::alloc_zeroed`].
133 /// use std::alloc::{alloc_zeroed, dealloc, Layout};
136 /// let layout = Layout::new::<u16>();
137 /// let ptr = alloc_zeroed(layout);
139 /// assert_eq!(*(ptr as *mut u16), 0);
141 /// dealloc(ptr, layout);
144 #[stable(feature = "global_alloc", since = "1.28.0")]
146 pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
147 __rust_alloc_zeroed(layout.size(), layout.align())
150 #[unstable(feature = "allocator_api", issue = "32838")]
151 unsafe impl Alloc for Global {
153 unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
154 NonNull::new(alloc(layout)).ok_or(AllocErr)
158 unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
159 dealloc(ptr.as_ptr(), layout)
163 unsafe fn realloc(&mut self,
167 -> Result<NonNull<u8>, AllocErr>
169 NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
173 unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
174 NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
178 /// The allocator for unique pointers.
179 // This function must not unwind. If it does, MIR codegen will fail.
181 #[lang = "exchange_malloc"]
183 unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
187 let layout = Layout::from_size_align_unchecked(size, align);
188 let ptr = alloc(layout);
192 handle_alloc_error(layout)
197 #[cfg_attr(not(test), lang = "box_free")]
199 pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
200 let ptr = ptr.as_ptr();
201 let size = size_of_val(&*ptr);
202 let align = min_align_of_val(&*ptr);
203 // We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
205 let layout = Layout::from_size_align_unchecked(size, align);
206 dealloc(ptr as *mut u8, layout);
210 /// Abort on memory allocation error or failure.
212 /// Callers of memory allocation APIs wishing to abort computation
213 /// in response to an allocation error are encouraged to call this function,
214 /// rather than directly invoking `panic!` or similar.
216 /// The default behavior of this function is to print a message to standard error
217 /// and abort the process.
218 /// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
220 /// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
221 /// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
222 #[stable(feature = "global_alloc", since = "1.28.0")]
223 #[rustc_allocator_nounwind]
224 pub fn handle_alloc_error(layout: Layout) -> ! {
225 #[allow(improper_ctypes)]
228 fn oom_impl(layout: Layout) -> !;
230 unsafe { oom_impl(layout) }
236 use self::test::Bencher;
238 use alloc::{Global, Alloc, Layout, handle_alloc_error};
241 fn allocate_zeroed() {
243 let layout = Layout::from_size_align(1024, 1).unwrap();
244 let ptr = Global.alloc_zeroed(layout.clone())
245 .unwrap_or_else(|_| handle_alloc_error(layout));
247 let mut i = ptr.cast::<u8>().as_ptr();
248 let end = i.add(layout.size());
253 Global.dealloc(ptr, layout);
258 fn alloc_owned_small(b: &mut Bencher) {
260 let _: Box<_> = box 10;