1 //! Memory allocation APIs.
3 //! In a given program, the standard library has one “global” memory allocator
4 //! that is used for example by `Box<T>` and `Vec<T>`.
6 //! Currently the default global allocator is unspecified. Libraries, however,
7 //! like `cdylib`s and `staticlib`s are guaranteed to use the [`System`] by
10 //! # The `#[global_allocator]` attribute
12 //! This attribute allows configuring the choice of global allocator.
13 //! You can use this to implement a completely custom global allocator
14 //! to route all default allocation requests to a custom object.
17 //! use std::alloc::{GlobalAlloc, System, Layout};
19 //! struct MyAllocator;
21 //! unsafe impl GlobalAlloc for MyAllocator {
22 //! unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
23 //! System.alloc(layout)
26 //! unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
27 //! System.dealloc(ptr, layout)
31 //! #[global_allocator]
32 //! static GLOBAL: MyAllocator = MyAllocator;
35 //! // This `Vec` will allocate memory through `GLOBAL` above
36 //! let mut v = Vec::new();
41 //! The attribute is used on a `static` item whose type implements the
42 //! [`GlobalAlloc`] trait. This type can be provided by an external library:
44 //! ```rust,ignore (demonstrates crates.io usage)
45 //! use jemallocator::Jemalloc;
47 //! #[global_allocator]
48 //! static GLOBAL: Jemalloc = Jemalloc;
53 //! The `#[global_allocator]` can only be used once in a crate
54 //! or its recursive dependencies.
56 #![deny(unsafe_op_in_unsafe_fn)]
57 #![stable(feature = "alloc_module", since = "1.28.0")]
60 use core::ptr::NonNull;
61 use core::sync::atomic::{AtomicPtr, Ordering};
64 #[stable(feature = "alloc_module", since = "1.28.0")]
66 pub use alloc_crate::alloc::*;
68 /// The default memory allocator provided by the operating system.
70 /// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows,
71 /// plus related functions.
73 /// This type implements the `GlobalAlloc` trait and Rust programs by default
74 /// work as if they had this definition:
77 /// use std::alloc::System;
79 /// #[global_allocator]
80 /// static A: System = System;
83 /// let a = Box::new(4); // Allocates from the system allocator.
88 /// You can also define your own wrapper around `System` if you'd like, such as
89 /// keeping track of the number of all bytes allocated:
92 /// use std::alloc::{System, GlobalAlloc, Layout};
93 /// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
97 /// static ALLOCATED: AtomicUsize = AtomicUsize::new(0);
99 /// unsafe impl GlobalAlloc for Counter {
100 /// unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
101 /// let ret = System.alloc(layout);
102 /// if !ret.is_null() {
103 /// ALLOCATED.fetch_add(layout.size(), SeqCst);
108 /// unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
109 /// System.dealloc(ptr, layout);
110 /// ALLOCATED.fetch_sub(layout.size(), SeqCst);
114 /// #[global_allocator]
115 /// static A: Counter = Counter;
118 /// println!("allocated bytes before main: {}", ALLOCATED.load(SeqCst));
122 /// It can also be used directly to allocate memory independently of whatever
123 /// global allocator has been selected for a Rust program. For example if a Rust
124 /// program opts in to using jemalloc as the global allocator, `System` will
125 /// still allocate memory using `malloc` and `HeapAlloc`.
126 #[stable(feature = "alloc_system_type", since = "1.28.0")]
127 #[derive(Debug, Default, Copy, Clone)]
132 fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
133 match layout.size() {
134 0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
135 // SAFETY: `layout` is non-zero in size,
137 let raw_ptr = if zeroed {
138 GlobalAlloc::alloc_zeroed(self, layout)
140 GlobalAlloc::alloc(self, layout)
142 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
143 Ok(NonNull::slice_from_raw_parts(ptr, size))
148 // SAFETY: Same as `Allocator::grow`
156 ) -> Result<NonNull<[u8]>, AllocError> {
158 new_layout.size() >= old_layout.size(),
159 "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
162 match old_layout.size() {
163 0 => self.alloc_impl(new_layout, zeroed),
165 // SAFETY: `new_size` is non-zero as `new_size` is greater than or equal to `old_size`
166 // as required by safety conditions and the `old_size == 0` case was handled in the
167 // previous match arm. Other conditions must be upheld by the caller
168 old_size if old_layout.align() == new_layout.align() => unsafe {
169 let new_size = new_layout.size();
171 // `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
172 intrinsics::assume(new_size >= old_layout.size());
174 let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
175 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
177 raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
179 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
182 // SAFETY: because `new_layout.size()` must be greater than or equal to `old_size`,
183 // both the old and new memory allocation are valid for reads and writes for `old_size`
184 // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
185 // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
186 // for `dealloc` must be upheld by the caller.
188 let new_ptr = self.alloc_impl(new_layout, zeroed)?;
189 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
190 Allocator::deallocate(self, ptr, old_layout);
197 // The Allocator impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl,
198 // which is in `std::sys::*::alloc`.
199 #[unstable(feature = "allocator_api", issue = "32838")]
200 unsafe impl Allocator for System {
202 fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
203 self.alloc_impl(layout, false)
207 fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
208 self.alloc_impl(layout, true)
212 unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
213 if layout.size() != 0 {
214 // SAFETY: `layout` is non-zero in size,
215 // other conditions must be upheld by the caller
216 unsafe { GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) }
226 ) -> Result<NonNull<[u8]>, AllocError> {
227 // SAFETY: all conditions must be upheld by the caller
228 unsafe { self.grow_impl(ptr, old_layout, new_layout, false) }
232 unsafe fn grow_zeroed(
237 ) -> Result<NonNull<[u8]>, AllocError> {
238 // SAFETY: all conditions must be upheld by the caller
239 unsafe { self.grow_impl(ptr, old_layout, new_layout, true) }
248 ) -> Result<NonNull<[u8]>, AllocError> {
250 new_layout.size() <= old_layout.size(),
251 "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
254 match new_layout.size() {
255 // SAFETY: conditions must be upheld by the caller
257 Allocator::deallocate(self, ptr, old_layout);
258 Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
261 // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
262 new_size if old_layout.align() == new_layout.align() => unsafe {
263 // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
264 intrinsics::assume(new_size <= old_layout.size());
266 let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
267 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
268 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
271 // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
272 // both the old and new memory allocation are valid for reads and writes for `new_size`
273 // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
274 // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
275 // for `dealloc` must be upheld by the caller.
277 let new_ptr = Allocator::allocate(self, new_layout)?;
278 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
279 Allocator::deallocate(self, ptr, old_layout);
286 static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
288 /// Registers a custom allocation error hook, replacing any that was previously registered.
290 /// The allocation error hook is invoked when an infallible memory allocation fails, before
291 /// the runtime aborts. The default hook prints a message to standard error,
292 /// but this behavior can be customized with the [`set_alloc_error_hook`] and
293 /// [`take_alloc_error_hook`] functions.
295 /// The hook is provided with a `Layout` struct which contains information
296 /// about the allocation that failed.
298 /// The allocation error hook is a global resource.
303 /// #![feature(alloc_error_hook)]
305 /// use std::alloc::{Layout, set_alloc_error_hook};
307 /// fn custom_alloc_error_hook(layout: Layout) {
308 /// panic!("memory allocation of {} bytes failed", layout.size());
311 /// set_alloc_error_hook(custom_alloc_error_hook);
313 #[unstable(feature = "alloc_error_hook", issue = "51245")]
314 pub fn set_alloc_error_hook(hook: fn(Layout)) {
315 HOOK.store(hook as *mut (), Ordering::SeqCst);
318 /// Unregisters the current allocation error hook, returning it.
320 /// *See also the function [`set_alloc_error_hook`].*
322 /// If no custom hook is registered, the default hook will be returned.
323 #[unstable(feature = "alloc_error_hook", issue = "51245")]
324 pub fn take_alloc_error_hook() -> fn(Layout) {
325 let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
326 if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } }
329 fn default_alloc_error_hook(layout: Layout) {
331 // This symbol is emitted by rustc next to __rust_alloc_error_handler.
332 // Its value depends on the -Zoom={panic,abort} compiler option.
333 static __rust_alloc_error_handler_should_panic: u8;
336 #[allow(unused_unsafe)]
337 if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
338 panic!("memory allocation of {} bytes failed\n", layout.size());
340 rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
346 #[alloc_error_handler]
347 #[unstable(feature = "alloc_internals", issue = "none")]
348 pub fn rust_oom(layout: Layout) -> ! {
349 let hook = HOOK.load(Ordering::SeqCst);
350 let hook: fn(Layout) =
351 if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } };
353 crate::process::abort()
358 #[allow(unused_attributes)]
359 #[unstable(feature = "alloc_internals", issue = "none")]
360 pub mod __default_lib_allocator {
361 use super::{GlobalAlloc, Layout, System};
362 // These magic symbol names are used as a fallback for implementing the
363 // `__rust_alloc` etc symbols (see `src/liballoc/alloc.rs`) when there is
364 // no `#[global_allocator]` attribute.
366 // for symbol names src/librustc_ast/expand/allocator.rs
367 // for signatures src/librustc_allocator/lib.rs
369 // linkage directives are provided as part of the current compiler allocator
372 #[rustc_std_internal_symbol]
373 pub unsafe extern "C" fn __rdl_alloc(size: usize, align: usize) -> *mut u8 {
374 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
375 // `GlobalAlloc::alloc`.
377 let layout = Layout::from_size_align_unchecked(size, align);
382 #[rustc_std_internal_symbol]
383 pub unsafe extern "C" fn __rdl_dealloc(ptr: *mut u8, size: usize, align: usize) {
384 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
385 // `GlobalAlloc::dealloc`.
386 unsafe { System.dealloc(ptr, Layout::from_size_align_unchecked(size, align)) }
389 #[rustc_std_internal_symbol]
390 pub unsafe extern "C" fn __rdl_realloc(
396 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
397 // `GlobalAlloc::realloc`.
399 let old_layout = Layout::from_size_align_unchecked(old_size, align);
400 System.realloc(ptr, old_layout, new_size)
404 #[rustc_std_internal_symbol]
405 pub unsafe extern "C" fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
406 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
407 // `GlobalAlloc::alloc_zeroed`.
409 let layout = Layout::from_size_align_unchecked(size, align);
410 System.alloc_zeroed(layout)