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::*;
24 // These are the magic symbols to call the global allocator. rustc generates
25 // them from the `#[global_allocator]` attribute if there is one, or uses the
26 // default implementations in libstd (`__rdl_alloc` etc in `src/libstd/alloc.rs`)
29 #[rustc_allocator_nounwind]
30 fn __rust_alloc(size: usize, align: usize) -> *mut u8;
31 #[rustc_allocator_nounwind]
32 fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
33 #[rustc_allocator_nounwind]
34 fn __rust_realloc(ptr: *mut u8,
37 new_size: usize) -> *mut u8;
38 #[rustc_allocator_nounwind]
39 fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
42 /// The global memory allocator.
44 /// This type implements the [`Alloc`] trait by forwarding calls
45 /// to the allocator registered with the `#[global_allocator]` attribute
46 /// if there is one, or the `std` crate’s default.
47 #[unstable(feature = "allocator_api", issue = "32838")]
48 #[derive(Copy, Clone, Default, Debug)]
51 /// Allocate memory with the global allocator.
53 /// This function forwards calls to the [`GlobalAlloc::alloc`] method
54 /// of the allocator registered with the `#[global_allocator]` attribute
55 /// if there is one, or the `std` crate’s default.
57 /// This function is expected to be deprecated in favor of the `alloc` method
58 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
62 /// See [`GlobalAlloc::alloc`].
67 /// use std::alloc::{alloc, dealloc, Layout};
70 /// let layout = Layout::new::<u16>();
71 /// let ptr = alloc(layout);
73 /// *(ptr as *mut u16) = 42;
74 /// assert_eq!(*(ptr as *mut u16), 42);
76 /// dealloc(ptr, layout);
79 #[stable(feature = "global_alloc", since = "1.28.0")]
81 pub unsafe fn alloc(layout: Layout) -> *mut u8 {
82 __rust_alloc(layout.size(), layout.align())
85 /// Deallocate memory with the global allocator.
87 /// This function forwards calls to the [`GlobalAlloc::dealloc`] method
88 /// of the allocator registered with the `#[global_allocator]` attribute
89 /// if there is one, or the `std` crate’s default.
91 /// This function is expected to be deprecated in favor of the `dealloc` method
92 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
96 /// See [`GlobalAlloc::dealloc`].
97 #[stable(feature = "global_alloc", since = "1.28.0")]
99 pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
100 __rust_dealloc(ptr, layout.size(), layout.align())
103 /// Reallocate memory with the global allocator.
105 /// This function forwards calls to the [`GlobalAlloc::realloc`] method
106 /// of the allocator registered with the `#[global_allocator]` attribute
107 /// if there is one, or the `std` crate’s default.
109 /// This function is expected to be deprecated in favor of the `realloc` method
110 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
114 /// See [`GlobalAlloc::realloc`].
115 #[stable(feature = "global_alloc", since = "1.28.0")]
117 pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
118 __rust_realloc(ptr, layout.size(), layout.align(), new_size)
121 /// Allocate zero-initialized memory with the global allocator.
123 /// This function forwards calls to the [`GlobalAlloc::alloc_zeroed`] method
124 /// of the allocator registered with the `#[global_allocator]` attribute
125 /// if there is one, or the `std` crate’s default.
127 /// This function is expected to be deprecated in favor of the `alloc_zeroed` method
128 /// of the [`Global`] type when it and the [`Alloc`] trait become stable.
132 /// See [`GlobalAlloc::alloc_zeroed`].
137 /// use std::alloc::{alloc_zeroed, dealloc, Layout};
140 /// let layout = Layout::new::<u16>();
141 /// let ptr = alloc_zeroed(layout);
143 /// assert_eq!(*(ptr as *mut u16), 0);
145 /// dealloc(ptr, layout);
148 #[stable(feature = "global_alloc", since = "1.28.0")]
150 pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
151 __rust_alloc_zeroed(layout.size(), layout.align())
154 #[unstable(feature = "allocator_api", issue = "32838")]
155 unsafe impl Alloc for Global {
157 unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
158 NonNull::new(alloc(layout)).ok_or(AllocErr)
162 unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
163 dealloc(ptr.as_ptr(), layout)
167 unsafe fn realloc(&mut self,
171 -> Result<NonNull<u8>, AllocErr>
173 NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
177 unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
178 NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
182 /// The allocator for unique pointers.
183 // This function must not unwind. If it does, MIR codegen will fail.
185 #[lang = "exchange_malloc"]
187 unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
191 let layout = Layout::from_size_align_unchecked(size, align);
192 let ptr = alloc(layout);
196 handle_alloc_error(layout)
201 #[cfg_attr(not(test), lang = "box_free")]
203 pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
204 let ptr = ptr.as_ptr();
205 let size = size_of_val(&*ptr);
206 let align = min_align_of_val(&*ptr);
207 // We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
209 let layout = Layout::from_size_align_unchecked(size, align);
210 dealloc(ptr as *mut u8, layout);
214 /// Abort on memory allocation error or failure.
216 /// Callers of memory allocation APIs wishing to abort computation
217 /// in response to an allocation error are encouraged to call this function,
218 /// rather than directly invoking `panic!` or similar.
220 /// The default behavior of this function is to print a message to standard error
221 /// and abort the process.
222 /// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
224 /// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
225 /// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
226 #[stable(feature = "global_alloc", since = "1.28.0")]
227 #[rustc_allocator_nounwind]
228 pub fn handle_alloc_error(layout: Layout) -> ! {
229 #[allow(improper_ctypes)]
232 fn oom_impl(layout: Layout) -> !;
234 unsafe { oom_impl(layout) }
240 use self::test::Bencher;
242 use alloc::{Global, Alloc, Layout, handle_alloc_error};
245 fn allocate_zeroed() {
247 let layout = Layout::from_size_align(1024, 1).unwrap();
248 let ptr = Global.alloc_zeroed(layout.clone())
249 .unwrap_or_else(|_| handle_alloc_error(layout));
251 let mut i = ptr.cast::<u8>().as_ptr();
252 let end = i.add(layout.size());
257 Global.dealloc(ptr, layout);
262 fn alloc_owned_small(b: &mut Bencher) {
264 let _: Box<_> = box 10;