]> git.lizzy.rs Git - rust.git/blob - library/std/src/alloc.rs
Rollup merge of #98368 - sunfishcode:sunfishcode/std-os-fd, r=joshtriplett
[rust.git] / library / std / src / alloc.rs
1 //! Memory allocation APIs.
2 //!
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>`.
5 //!
6 //! Currently the default global allocator is unspecified. Libraries, however,
7 //! like `cdylib`s and `staticlib`s are guaranteed to use the [`System`] by
8 //! default.
9 //!
10 //! # The `#[global_allocator]` attribute
11 //!
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.
15 //!
16 //! ```rust
17 //! use std::alloc::{GlobalAlloc, System, Layout};
18 //!
19 //! struct MyAllocator;
20 //!
21 //! unsafe impl GlobalAlloc for MyAllocator {
22 //!     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
23 //!         System.alloc(layout)
24 //!     }
25 //!
26 //!     unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
27 //!         System.dealloc(ptr, layout)
28 //!     }
29 //! }
30 //!
31 //! #[global_allocator]
32 //! static GLOBAL: MyAllocator = MyAllocator;
33 //!
34 //! fn main() {
35 //!     // This `Vec` will allocate memory through `GLOBAL` above
36 //!     let mut v = Vec::new();
37 //!     v.push(1);
38 //! }
39 //! ```
40 //!
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:
43 //!
44 //! ```rust,ignore (demonstrates crates.io usage)
45 //! use jemallocator::Jemalloc;
46 //!
47 //! #[global_allocator]
48 //! static GLOBAL: Jemalloc = Jemalloc;
49 //!
50 //! fn main() {}
51 //! ```
52 //!
53 //! The `#[global_allocator]` can only be used once in a crate
54 //! or its recursive dependencies.
55
56 #![deny(unsafe_op_in_unsafe_fn)]
57 #![stable(feature = "alloc_module", since = "1.28.0")]
58
59 use core::intrinsics;
60 use core::ptr::NonNull;
61 use core::sync::atomic::{AtomicPtr, Ordering};
62 use core::{mem, ptr};
63
64 #[stable(feature = "alloc_module", since = "1.28.0")]
65 #[doc(inline)]
66 pub use alloc_crate::alloc::*;
67
68 /// The default memory allocator provided by the operating system.
69 ///
70 /// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows,
71 /// plus related functions. However, it is not valid to mix use of the backing
72 /// system allocator with `System`, as this implementation may include extra
73 /// work, such as to serve alignment requests greater than the alignment
74 /// provided directly by the backing system allocator.
75 ///
76 /// This type implements the `GlobalAlloc` trait and Rust programs by default
77 /// work as if they had this definition:
78 ///
79 /// ```rust
80 /// use std::alloc::System;
81 ///
82 /// #[global_allocator]
83 /// static A: System = System;
84 ///
85 /// fn main() {
86 ///     let a = Box::new(4); // Allocates from the system allocator.
87 ///     println!("{a}");
88 /// }
89 /// ```
90 ///
91 /// You can also define your own wrapper around `System` if you'd like, such as
92 /// keeping track of the number of all bytes allocated:
93 ///
94 /// ```rust
95 /// use std::alloc::{System, GlobalAlloc, Layout};
96 /// use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
97 ///
98 /// struct Counter;
99 ///
100 /// static ALLOCATED: AtomicUsize = AtomicUsize::new(0);
101 ///
102 /// unsafe impl GlobalAlloc for Counter {
103 ///     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
104 ///         let ret = System.alloc(layout);
105 ///         if !ret.is_null() {
106 ///             ALLOCATED.fetch_add(layout.size(), SeqCst);
107 ///         }
108 ///         ret
109 ///     }
110 ///
111 ///     unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
112 ///         System.dealloc(ptr, layout);
113 ///         ALLOCATED.fetch_sub(layout.size(), SeqCst);
114 ///     }
115 /// }
116 ///
117 /// #[global_allocator]
118 /// static A: Counter = Counter;
119 ///
120 /// fn main() {
121 ///     println!("allocated bytes before main: {}", ALLOCATED.load(SeqCst));
122 /// }
123 /// ```
124 ///
125 /// It can also be used directly to allocate memory independently of whatever
126 /// global allocator has been selected for a Rust program. For example if a Rust
127 /// program opts in to using jemalloc as the global allocator, `System` will
128 /// still allocate memory using `malloc` and `HeapAlloc`.
129 #[stable(feature = "alloc_system_type", since = "1.28.0")]
130 #[derive(Debug, Default, Copy, Clone)]
131 pub struct System;
132
133 impl System {
134     #[inline]
135     fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
136         match layout.size() {
137             0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
138             // SAFETY: `layout` is non-zero in size,
139             size => unsafe {
140                 let raw_ptr = if zeroed {
141                     GlobalAlloc::alloc_zeroed(self, layout)
142                 } else {
143                     GlobalAlloc::alloc(self, layout)
144                 };
145                 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
146                 Ok(NonNull::slice_from_raw_parts(ptr, size))
147             },
148         }
149     }
150
151     // SAFETY: Same as `Allocator::grow`
152     #[inline]
153     unsafe fn grow_impl(
154         &self,
155         ptr: NonNull<u8>,
156         old_layout: Layout,
157         new_layout: Layout,
158         zeroed: bool,
159     ) -> Result<NonNull<[u8]>, AllocError> {
160         debug_assert!(
161             new_layout.size() >= old_layout.size(),
162             "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
163         );
164
165         match old_layout.size() {
166             0 => self.alloc_impl(new_layout, zeroed),
167
168             // SAFETY: `new_size` is non-zero as `new_size` is greater than or equal to `old_size`
169             // as required by safety conditions and the `old_size == 0` case was handled in the
170             // previous match arm. Other conditions must be upheld by the caller
171             old_size if old_layout.align() == new_layout.align() => unsafe {
172                 let new_size = new_layout.size();
173
174                 // `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
175                 intrinsics::assume(new_size >= old_layout.size());
176
177                 let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
178                 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
179                 if zeroed {
180                     raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
181                 }
182                 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
183             },
184
185             // SAFETY: because `new_layout.size()` must be greater than or equal to `old_size`,
186             // both the old and new memory allocation are valid for reads and writes for `old_size`
187             // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
188             // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
189             // for `dealloc` must be upheld by the caller.
190             old_size => unsafe {
191                 let new_ptr = self.alloc_impl(new_layout, zeroed)?;
192                 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
193                 Allocator::deallocate(self, ptr, old_layout);
194                 Ok(new_ptr)
195             },
196         }
197     }
198 }
199
200 // The Allocator impl checks the layout size to be non-zero and forwards to the GlobalAlloc impl,
201 // which is in `std::sys::*::alloc`.
202 #[unstable(feature = "allocator_api", issue = "32838")]
203 unsafe impl Allocator for System {
204     #[inline]
205     fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
206         self.alloc_impl(layout, false)
207     }
208
209     #[inline]
210     fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
211         self.alloc_impl(layout, true)
212     }
213
214     #[inline]
215     unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
216         if layout.size() != 0 {
217             // SAFETY: `layout` is non-zero in size,
218             // other conditions must be upheld by the caller
219             unsafe { GlobalAlloc::dealloc(self, ptr.as_ptr(), layout) }
220         }
221     }
222
223     #[inline]
224     unsafe fn grow(
225         &self,
226         ptr: NonNull<u8>,
227         old_layout: Layout,
228         new_layout: Layout,
229     ) -> Result<NonNull<[u8]>, AllocError> {
230         // SAFETY: all conditions must be upheld by the caller
231         unsafe { self.grow_impl(ptr, old_layout, new_layout, false) }
232     }
233
234     #[inline]
235     unsafe fn grow_zeroed(
236         &self,
237         ptr: NonNull<u8>,
238         old_layout: Layout,
239         new_layout: Layout,
240     ) -> Result<NonNull<[u8]>, AllocError> {
241         // SAFETY: all conditions must be upheld by the caller
242         unsafe { self.grow_impl(ptr, old_layout, new_layout, true) }
243     }
244
245     #[inline]
246     unsafe fn shrink(
247         &self,
248         ptr: NonNull<u8>,
249         old_layout: Layout,
250         new_layout: Layout,
251     ) -> Result<NonNull<[u8]>, AllocError> {
252         debug_assert!(
253             new_layout.size() <= old_layout.size(),
254             "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
255         );
256
257         match new_layout.size() {
258             // SAFETY: conditions must be upheld by the caller
259             0 => unsafe {
260                 Allocator::deallocate(self, ptr, old_layout);
261                 Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
262             },
263
264             // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
265             new_size if old_layout.align() == new_layout.align() => unsafe {
266                 // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
267                 intrinsics::assume(new_size <= old_layout.size());
268
269                 let raw_ptr = GlobalAlloc::realloc(self, ptr.as_ptr(), old_layout, new_size);
270                 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
271                 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
272             },
273
274             // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
275             // both the old and new memory allocation are valid for reads and writes for `new_size`
276             // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
277             // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
278             // for `dealloc` must be upheld by the caller.
279             new_size => unsafe {
280                 let new_ptr = Allocator::allocate(self, new_layout)?;
281                 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
282                 Allocator::deallocate(self, ptr, old_layout);
283                 Ok(new_ptr)
284             },
285         }
286     }
287 }
288
289 static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
290
291 /// Registers a custom allocation error hook, replacing any that was previously registered.
292 ///
293 /// The allocation error hook is invoked when an infallible memory allocation fails, before
294 /// the runtime aborts. The default hook prints a message to standard error,
295 /// but this behavior can be customized with the [`set_alloc_error_hook`] and
296 /// [`take_alloc_error_hook`] functions.
297 ///
298 /// The hook is provided with a `Layout` struct which contains information
299 /// about the allocation that failed.
300 ///
301 /// The allocation error hook is a global resource.
302 ///
303 /// # Examples
304 ///
305 /// ```
306 /// #![feature(alloc_error_hook)]
307 ///
308 /// use std::alloc::{Layout, set_alloc_error_hook};
309 ///
310 /// fn custom_alloc_error_hook(layout: Layout) {
311 ///    panic!("memory allocation of {} bytes failed", layout.size());
312 /// }
313 ///
314 /// set_alloc_error_hook(custom_alloc_error_hook);
315 /// ```
316 #[unstable(feature = "alloc_error_hook", issue = "51245")]
317 pub fn set_alloc_error_hook(hook: fn(Layout)) {
318     HOOK.store(hook as *mut (), Ordering::SeqCst);
319 }
320
321 /// Unregisters the current allocation error hook, returning it.
322 ///
323 /// *See also the function [`set_alloc_error_hook`].*
324 ///
325 /// If no custom hook is registered, the default hook will be returned.
326 #[unstable(feature = "alloc_error_hook", issue = "51245")]
327 pub fn take_alloc_error_hook() -> fn(Layout) {
328     let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
329     if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } }
330 }
331
332 fn default_alloc_error_hook(layout: Layout) {
333     extern "Rust" {
334         // This symbol is emitted by rustc next to __rust_alloc_error_handler.
335         // Its value depends on the -Zoom={panic,abort} compiler option.
336         static __rust_alloc_error_handler_should_panic: u8;
337     }
338
339     #[allow(unused_unsafe)]
340     if unsafe { __rust_alloc_error_handler_should_panic != 0 } {
341         panic!("memory allocation of {} bytes failed\n", layout.size());
342     } else {
343         rtprintpanic!("memory allocation of {} bytes failed\n", layout.size());
344     }
345 }
346
347 #[cfg(not(test))]
348 #[doc(hidden)]
349 #[alloc_error_handler]
350 #[unstable(feature = "alloc_internals", issue = "none")]
351 pub fn rust_oom(layout: Layout) -> ! {
352     let hook = HOOK.load(Ordering::SeqCst);
353     let hook: fn(Layout) =
354         if hook.is_null() { default_alloc_error_hook } else { unsafe { mem::transmute(hook) } };
355     hook(layout);
356     crate::process::abort()
357 }
358
359 #[cfg(not(test))]
360 #[doc(hidden)]
361 #[allow(unused_attributes)]
362 #[unstable(feature = "alloc_internals", issue = "none")]
363 pub mod __default_lib_allocator {
364     use super::{GlobalAlloc, Layout, System};
365     // These magic symbol names are used as a fallback for implementing the
366     // `__rust_alloc` etc symbols (see `src/liballoc/alloc.rs`) when there is
367     // no `#[global_allocator]` attribute.
368
369     // for symbol names src/librustc_ast/expand/allocator.rs
370     // for signatures src/librustc_allocator/lib.rs
371
372     // linkage directives are provided as part of the current compiler allocator
373     // ABI
374
375     #[rustc_std_internal_symbol]
376     pub unsafe extern "C" fn __rdl_alloc(size: usize, align: usize) -> *mut u8 {
377         // SAFETY: see the guarantees expected by `Layout::from_size_align` and
378         // `GlobalAlloc::alloc`.
379         unsafe {
380             let layout = Layout::from_size_align_unchecked(size, align);
381             System.alloc(layout)
382         }
383     }
384
385     #[rustc_std_internal_symbol]
386     pub unsafe extern "C" fn __rdl_dealloc(ptr: *mut u8, size: usize, align: usize) {
387         // SAFETY: see the guarantees expected by `Layout::from_size_align` and
388         // `GlobalAlloc::dealloc`.
389         unsafe { System.dealloc(ptr, Layout::from_size_align_unchecked(size, align)) }
390     }
391
392     #[rustc_std_internal_symbol]
393     pub unsafe extern "C" fn __rdl_realloc(
394         ptr: *mut u8,
395         old_size: usize,
396         align: usize,
397         new_size: usize,
398     ) -> *mut u8 {
399         // SAFETY: see the guarantees expected by `Layout::from_size_align` and
400         // `GlobalAlloc::realloc`.
401         unsafe {
402             let old_layout = Layout::from_size_align_unchecked(old_size, align);
403             System.realloc(ptr, old_layout, new_size)
404         }
405     }
406
407     #[rustc_std_internal_symbol]
408     pub unsafe extern "C" fn __rdl_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
409         // SAFETY: see the guarantees expected by `Layout::from_size_align` and
410         // `GlobalAlloc::alloc_zeroed`.
411         unsafe {
412             let layout = Layout::from_size_align_unchecked(size, align);
413             System.alloc_zeroed(layout)
414         }
415     }
416 }