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 //! extern crate jemallocator;
47 //! use jemallocator::Jemalloc;
49 //! #[global_allocator]
50 //! static GLOBAL: Jemalloc = Jemalloc;
55 //! The `#[global_allocator]` can only be used once in a crate
56 //! or its recursive dependencies.
58 #![deny(unsafe_op_in_unsafe_fn)]
59 #![stable(feature = "alloc_module", since = "1.28.0")]
62 use core
::ptr
::NonNull
;
63 use core
::sync
::atomic
::{AtomicPtr, Ordering}
;
66 use crate::sys_common
::util
::dumb_print
;
68 #[stable(feature = "alloc_module", since = "1.28.0")]
70 pub use alloc_crate
::alloc
::*;
72 /// The default memory allocator provided by the operating system.
74 /// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows,
75 /// plus related functions.
77 /// This type implements the `GlobalAlloc` trait and Rust programs by default
78 /// work as if they had this definition:
81 /// use std::alloc::System;
83 /// #[global_allocator]
84 /// static A: System = System;
87 /// let a = Box::new(4); // Allocates from the system allocator.
88 /// println!("{}", a);
92 /// You can also define your own wrapper around `System` if you'd like, such as
93 /// keeping track of the number of all bytes allocated:
96 /// use std::alloc::{System, GlobalAlloc, Layout};
97 /// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
101 /// static ALLOCATED: AtomicUsize = AtomicUsize::new(0);
103 /// unsafe impl GlobalAlloc for Counter {
104 /// unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
105 /// let ret = System.alloc(layout);
106 /// if !ret.is_null() {
107 /// ALLOCATED.fetch_add(layout.size(), SeqCst);
112 /// unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
113 /// System.dealloc(ptr, layout);
114 /// ALLOCATED.fetch_sub(layout.size(), SeqCst);
118 /// #[global_allocator]
119 /// static A: Counter = Counter;
122 /// println!("allocated bytes before main: {}", ALLOCATED.load(SeqCst));
126 /// It can also be used directly to allocate memory independently of whatever
127 /// global allocator has been selected for a Rust program. For example if a Rust
128 /// program opts in to using jemalloc as the global allocator, `System` will
129 /// still allocate memory using `malloc` and `HeapAlloc`.
130 #[stable(feature = "alloc_system_type", since = "1.28.0")]
131 #[derive(Debug, Default, Copy, Clone)]
136 fn alloc_impl(&mut self, layout
: Layout
, zeroed
: bool
) -> Result
<NonNull
<[u8]>, AllocErr
> {
137 match layout
.size() {
138 0 => Ok(NonNull
::slice_from_raw_parts(layout
.dangling(), 0)),
139 // SAFETY: `layout` is non-zero in size,
141 let raw_ptr
= if zeroed
{
142 GlobalAlloc
::alloc_zeroed(self, layout
)
144 GlobalAlloc
::alloc(self, layout
)
146 let ptr
= NonNull
::new(raw_ptr
).ok_or(AllocErr
)?
;
147 Ok(NonNull
::slice_from_raw_parts(ptr
, size
))
152 // Safety: Same as `AllocRef::grow`
160 ) -> Result
<NonNull
<[u8]>, AllocErr
> {
162 new_size
>= layout
.size(),
163 "`new_size` must be greater than or equal to `layout.size()`"
166 match layout
.size() {
167 // SAFETY: the caller must ensure that the `new_size` does not overflow.
168 // `layout.align()` comes from a `Layout` and is thus guaranteed to be valid for a Layout.
170 let new_layout
= Layout
::from_size_align_unchecked(new_size
, layout
.align());
171 self.alloc_impl(new_layout
, zeroed
)
174 // SAFETY: `new_size` is non-zero as `old_size` is greater than or equal to `new_size`
175 // as required by safety conditions. Other conditions must be upheld by the caller
177 // `realloc` probably checks for `new_size >= size` or something similar.
178 intrinsics
::assume(new_size
>= layout
.size());
180 let raw_ptr
= GlobalAlloc
::realloc(self, ptr
.as_ptr(), layout
, new_size
);
181 let ptr
= NonNull
::new(raw_ptr
).ok_or(AllocErr
)?
;
183 raw_ptr
.add(old_size
).write_bytes(0, new_size
- old_size
);
185 Ok(NonNull
::slice_from_raw_parts(ptr
, new_size
))
191 // The AllocRef impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl,
192 // which is in `std::sys::*::alloc`.
193 #[unstable(feature = "allocator_api", issue = "32838")]
194 unsafe impl AllocRef
for System
{
196 fn alloc(&mut self, layout
: Layout
) -> Result
<NonNull
<[u8]>, AllocErr
> {
197 self.alloc_impl(layout
, false)
201 fn alloc_zeroed(&mut self, layout
: Layout
) -> Result
<NonNull
<[u8]>, AllocErr
> {
202 self.alloc_impl(layout
, true)
206 unsafe fn dealloc(&mut self, ptr
: NonNull
<u8>, layout
: Layout
) {
207 if layout
.size() != 0 {
208 // SAFETY: `layout` is non-zero in size,
209 // other conditions must be upheld by the caller
210 unsafe { GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) }
220 ) -> Result
<NonNull
<[u8]>, AllocErr
> {
221 // SAFETY: all conditions must be upheld by the caller
222 unsafe { self.grow_impl(ptr, layout, new_size, false) }
226 unsafe fn grow_zeroed(
231 ) -> Result
<NonNull
<[u8]>, AllocErr
> {
232 // SAFETY: all conditions must be upheld by the caller
233 unsafe { self.grow_impl(ptr, layout, new_size, true) }
242 ) -> Result
<NonNull
<[u8]>, AllocErr
> {
244 new_size
<= layout
.size(),
245 "`new_size` must be smaller than or equal to `layout.size()`"
249 // SAFETY: conditions must be upheld by the caller
251 self.dealloc(ptr
, layout
);
252 Ok(NonNull
::slice_from_raw_parts(layout
.dangling(), 0))
255 // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
257 // `realloc` probably checks for `new_size <= size` or something similar.
258 intrinsics
::assume(new_size
<= layout
.size());
260 let raw_ptr
= GlobalAlloc
::realloc(self, ptr
.as_ptr(), layout
, new_size
);
261 let ptr
= NonNull
::new(raw_ptr
).ok_or(AllocErr
)?
;
262 Ok(NonNull
::slice_from_raw_parts(ptr
, new_size
))
268 static HOOK
: AtomicPtr
<()> = AtomicPtr
::new(ptr
::null_mut());
270 /// Registers a custom allocation error hook, replacing any that was previously registered.
272 /// The allocation error hook is invoked when an infallible memory allocation fails, before
273 /// the runtime aborts. The default hook prints a message to standard error,
274 /// but this behavior can be customized with the [`set_alloc_error_hook`] and
275 /// [`take_alloc_error_hook`] functions.
277 /// The hook is provided with a `Layout` struct which contains information
278 /// about the allocation that failed.
280 /// The allocation error hook is a global resource.
281 #[unstable(feature = "alloc_error_hook", issue = "51245")]
282 pub fn set_alloc_error_hook(hook
: fn(Layout
)) {
283 HOOK
.store(hook
as *mut (), Ordering
::SeqCst
);
286 /// Unregisters the current allocation error hook, returning it.
288 /// *See also the function [`set_alloc_error_hook`].*
290 /// If no custom hook is registered, the default hook will be returned.
291 #[unstable(feature = "alloc_error_hook", issue = "51245")]
292 pub fn take_alloc_error_hook() -> fn(Layout
) {
293 let hook
= HOOK
.swap(ptr
::null_mut(), Ordering
::SeqCst
);
294 if hook
.is_null() { default_alloc_error_hook }
else { unsafe { mem::transmute(hook) }
}
297 fn default_alloc_error_hook(layout
: Layout
) {
298 dumb_print(format_args
!("memory allocation of {} bytes failed", layout
.size()));
303 #[alloc_error_handler]
304 #[unstable(feature = "alloc_internals", issue = "none")]
305 pub fn rust_oom(layout
: Layout
) -> ! {
306 let hook
= HOOK
.load(Ordering
::SeqCst
);
307 let hook
: fn(Layout
) =
308 if hook
.is_null() { default_alloc_error_hook }
else { unsafe { mem::transmute(hook) }
};
310 crate::process
::abort()
315 #[allow(unused_attributes)]
316 #[unstable(feature = "alloc_internals", issue = "none")]
317 pub mod __default_lib_allocator
{
318 use super::{GlobalAlloc, Layout, System}
;
319 // These magic symbol names are used as a fallback for implementing the
320 // `__rust_alloc` etc symbols (see `src/liballoc/alloc.rs`) when there is
321 // no `#[global_allocator]` attribute.
323 // for symbol names src/librustc_ast/expand/allocator.rs
324 // for signatures src/librustc_allocator/lib.rs
326 // linkage directives are provided as part of the current compiler allocator
329 #[rustc_std_internal_symbol]
330 pub unsafe extern "C" fn __rdl_alloc(size
: usize, align
: usize) -> *mut u8 {
331 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
332 // `GlobalAlloc::alloc`.
334 let layout
= Layout
::from_size_align_unchecked(size
, align
);
339 #[rustc_std_internal_symbol]
340 pub unsafe extern "C" fn __rdl_dealloc(ptr
: *mut u8, size
: usize, align
: usize) {
341 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
342 // `GlobalAlloc::dealloc`.
343 unsafe { System.dealloc(ptr, Layout::from_size_align_unchecked(size, align)) }
346 #[rustc_std_internal_symbol]
347 pub unsafe extern "C" fn __rdl_realloc(
353 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
354 // `GlobalAlloc::realloc`.
356 let old_layout
= Layout
::from_size_align_unchecked(old_size
, align
);
357 System
.realloc(ptr
, old_layout
, new_size
)
361 #[rustc_std_internal_symbol]
362 pub unsafe extern "C" fn __rdl_alloc_zeroed(size
: usize, align
: usize) -> *mut u8 {
363 // SAFETY: see the guarantees expected by `Layout::from_size_align` and
364 // `GlobalAlloc::alloc_zeroed`.
366 let layout
= Layout
::from_size_align_unchecked(size
, align
);
367 System
.alloc_zeroed(layout
)