]> git.proxmox.com Git - rustc.git/blame - library/alloc/src/alloc.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / library / alloc / src / alloc.rs
CommitLineData
94b46f34
XL
1//! Memory allocation APIs
2
3#![stable(feature = "alloc_module", since = "1.28.0")]
83c7162d 4
29967ef6
XL
5#[cfg(not(test))]
6use core::intrinsics;
7use core::intrinsics::{min_align_of_val, size_of_val};
8
9use core::ptr::Unique;
10#[cfg(not(test))]
11use core::ptr::{self, NonNull};
83c7162d 12
94b46f34 13#[stable(feature = "alloc_module", since = "1.28.0")]
83c7162d
XL
14#[doc(inline)]
15pub use core::alloc::*;
16
5e7ed085
FG
17use core::marker::Destruct;
18
416331ca
XL
19#[cfg(test)]
20mod tests;
21
83c7162d 22extern "Rust" {
a1dfa0c6 23 // These are the magic symbols to call the global allocator. rustc generates
29967ef6
XL
24 // them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute
25 // (the code expanding that attribute macro generates those functions), or to call
26 // the default implementations in libstd (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)
a1dfa0c6 27 // otherwise.
fc512014
XL
28 // The rustc fork of LLVM also special-cases these function names to be able to optimize them
29 // like `malloc`, `realloc`, and `free`, respectively.
416331ca 30 #[rustc_allocator]
83c7162d
XL
31 #[rustc_allocator_nounwind]
32 fn __rust_alloc(size: usize, align: usize) -> *mut u8;
33 #[rustc_allocator_nounwind]
34 fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
35 #[rustc_allocator_nounwind]
dfeec247 36 fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
83c7162d
XL
37 #[rustc_allocator_nounwind]
38 fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
39}
40
94b46f34
XL
41/// The global memory allocator.
42///
fc512014 43/// This type implements the [`Allocator`] trait by forwarding calls
94b46f34
XL
44/// to the allocator registered with the `#[global_allocator]` attribute
45/// if there is one, or the `std` crate’s default.
9fa01778
XL
46///
47/// Note: while this type is unstable, the functionality it provides can be
29967ef6 48/// accessed through the [free functions in `alloc`](self#functions).
94b46f34 49#[unstable(feature = "allocator_api", issue = "32838")]
83c7162d 50#[derive(Copy, Clone, Default, Debug)]
29967ef6 51#[cfg(not(test))]
83c7162d
XL
52pub struct Global;
53
29967ef6
XL
54#[cfg(test)]
55pub use std::alloc::Global;
56
94b46f34
XL
57/// Allocate memory with the global allocator.
58///
59/// This function forwards calls to the [`GlobalAlloc::alloc`] method
60/// of the allocator registered with the `#[global_allocator]` attribute
61/// if there is one, or the `std` crate’s default.
62///
63/// This function is expected to be deprecated in favor of the `alloc` method
fc512014 64/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
94b46f34
XL
65///
66/// # Safety
67///
68/// See [`GlobalAlloc::alloc`].
b7449926
XL
69///
70/// # Examples
71///
72/// ```
73/// use std::alloc::{alloc, dealloc, Layout};
74///
75/// unsafe {
76/// let layout = Layout::new::<u16>();
77/// let ptr = alloc(layout);
78///
79/// *(ptr as *mut u16) = 42;
80/// assert_eq!(*(ptr as *mut u16), 42);
81///
82/// dealloc(ptr, layout);
83/// }
84/// ```
94b46f34 85#[stable(feature = "global_alloc", since = "1.28.0")]
3c0e092e 86#[must_use = "losing the pointer will leak memory"]
94b46f34
XL
87#[inline]
88pub unsafe fn alloc(layout: Layout) -> *mut u8 {
f035d41b 89 unsafe { __rust_alloc(layout.size(), layout.align()) }
94b46f34 90}
83c7162d 91
94b46f34
XL
92/// Deallocate memory with the global allocator.
93///
94/// This function forwards calls to the [`GlobalAlloc::dealloc`] method
95/// of the allocator registered with the `#[global_allocator]` attribute
96/// if there is one, or the `std` crate’s default.
97///
98/// This function is expected to be deprecated in favor of the `dealloc` method
fc512014 99/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
94b46f34
XL
100///
101/// # Safety
102///
103/// See [`GlobalAlloc::dealloc`].
104#[stable(feature = "global_alloc", since = "1.28.0")]
105#[inline]
106pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
f035d41b 107 unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
94b46f34 108}
83c7162d 109
94b46f34
XL
110/// Reallocate memory with the global allocator.
111///
112/// This function forwards calls to the [`GlobalAlloc::realloc`] method
113/// of the allocator registered with the `#[global_allocator]` attribute
114/// if there is one, or the `std` crate’s default.
115///
116/// This function is expected to be deprecated in favor of the `realloc` method
fc512014 117/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
94b46f34
XL
118///
119/// # Safety
120///
121/// See [`GlobalAlloc::realloc`].
122#[stable(feature = "global_alloc", since = "1.28.0")]
3c0e092e 123#[must_use = "losing the pointer will leak memory"]
94b46f34
XL
124#[inline]
125pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
f035d41b 126 unsafe { __rust_realloc(ptr, layout.size(), layout.align(), new_size) }
94b46f34 127}
83c7162d 128
94b46f34
XL
129/// Allocate zero-initialized memory with the global allocator.
130///
131/// This function forwards calls to the [`GlobalAlloc::alloc_zeroed`] method
132/// of the allocator registered with the `#[global_allocator]` attribute
133/// if there is one, or the `std` crate’s default.
134///
135/// This function is expected to be deprecated in favor of the `alloc_zeroed` method
fc512014 136/// of the [`Global`] type when it and the [`Allocator`] trait become stable.
94b46f34
XL
137///
138/// # Safety
139///
140/// See [`GlobalAlloc::alloc_zeroed`].
b7449926
XL
141///
142/// # Examples
143///
144/// ```
145/// use std::alloc::{alloc_zeroed, dealloc, Layout};
146///
147/// unsafe {
148/// let layout = Layout::new::<u16>();
149/// let ptr = alloc_zeroed(layout);
150///
151/// assert_eq!(*(ptr as *mut u16), 0);
152///
153/// dealloc(ptr, layout);
154/// }
155/// ```
94b46f34 156#[stable(feature = "global_alloc", since = "1.28.0")]
3c0e092e 157#[must_use = "losing the pointer will leak memory"]
94b46f34
XL
158#[inline]
159pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
f035d41b 160 unsafe { __rust_alloc_zeroed(layout.size(), layout.align()) }
83c7162d
XL
161}
162
29967ef6 163#[cfg(not(test))]
3dfed10e 164impl Global {
83c7162d 165 #[inline]
1b1a35ee 166 fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
3dfed10e
XL
167 match layout.size() {
168 0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
169 // SAFETY: `layout` is non-zero in size,
170 size => unsafe {
171 let raw_ptr = if zeroed { alloc_zeroed(layout) } else { alloc(layout) };
1b1a35ee 172 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
3dfed10e
XL
173 Ok(NonNull::slice_from_raw_parts(ptr, size))
174 },
74b04a01 175 }
83c7162d
XL
176 }
177
fc512014 178 // SAFETY: Same as `Allocator::grow`
3dfed10e
XL
179 #[inline]
180 unsafe fn grow_impl(
1b1a35ee 181 &self,
3dfed10e 182 ptr: NonNull<u8>,
1b1a35ee
XL
183 old_layout: Layout,
184 new_layout: Layout,
3dfed10e 185 zeroed: bool,
1b1a35ee 186 ) -> Result<NonNull<[u8]>, AllocError> {
3dfed10e 187 debug_assert!(
1b1a35ee
XL
188 new_layout.size() >= old_layout.size(),
189 "`new_layout.size()` must be greater than or equal to `old_layout.size()`"
3dfed10e
XL
190 );
191
1b1a35ee
XL
192 match old_layout.size() {
193 0 => self.alloc_impl(new_layout, zeroed),
3dfed10e
XL
194
195 // SAFETY: `new_size` is non-zero as `old_size` is greater than or equal to `new_size`
196 // as required by safety conditions. Other conditions must be upheld by the caller
1b1a35ee
XL
197 old_size if old_layout.align() == new_layout.align() => unsafe {
198 let new_size = new_layout.size();
199
200 // `realloc` probably checks for `new_size >= old_layout.size()` or something similar.
201 intrinsics::assume(new_size >= old_layout.size());
3dfed10e 202
1b1a35ee
XL
203 let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
204 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
3dfed10e
XL
205 if zeroed {
206 raw_ptr.add(old_size).write_bytes(0, new_size - old_size);
207 }
208 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
209 },
1b1a35ee
XL
210
211 // SAFETY: because `new_layout.size()` must be greater than or equal to `old_size`,
212 // both the old and new memory allocation are valid for reads and writes for `old_size`
213 // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
214 // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
215 // for `dealloc` must be upheld by the caller.
216 old_size => unsafe {
217 let new_ptr = self.alloc_impl(new_layout, zeroed)?;
218 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), old_size);
fc512014 219 self.deallocate(ptr, old_layout);
1b1a35ee
XL
220 Ok(new_ptr)
221 },
3dfed10e
XL
222 }
223 }
224}
225
226#[unstable(feature = "allocator_api", issue = "32838")]
29967ef6 227#[cfg(not(test))]
fc512014 228unsafe impl Allocator for Global {
3dfed10e 229 #[inline]
fc512014 230 fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
3dfed10e
XL
231 self.alloc_impl(layout, false)
232 }
233
234 #[inline]
fc512014 235 fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
3dfed10e
XL
236 self.alloc_impl(layout, true)
237 }
238
83c7162d 239 #[inline]
fc512014 240 unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
74b04a01 241 if layout.size() != 0 {
3dfed10e
XL
242 // SAFETY: `layout` is non-zero in size,
243 // other conditions must be upheld by the caller
f035d41b 244 unsafe { dealloc(ptr.as_ptr(), layout) }
74b04a01 245 }
83c7162d
XL
246 }
247
248 #[inline]
ba9703b0 249 unsafe fn grow(
1b1a35ee 250 &self,
dfeec247 251 ptr: NonNull<u8>,
1b1a35ee
XL
252 old_layout: Layout,
253 new_layout: Layout,
254 ) -> Result<NonNull<[u8]>, AllocError> {
3dfed10e 255 // SAFETY: all conditions must be upheld by the caller
1b1a35ee 256 unsafe { self.grow_impl(ptr, old_layout, new_layout, false) }
3dfed10e 257 }
ba9703b0 258
3dfed10e
XL
259 #[inline]
260 unsafe fn grow_zeroed(
1b1a35ee 261 &self,
3dfed10e 262 ptr: NonNull<u8>,
1b1a35ee
XL
263 old_layout: Layout,
264 new_layout: Layout,
265 ) -> Result<NonNull<[u8]>, AllocError> {
3dfed10e 266 // SAFETY: all conditions must be upheld by the caller
1b1a35ee 267 unsafe { self.grow_impl(ptr, old_layout, new_layout, true) }
83c7162d
XL
268 }
269
270 #[inline]
ba9703b0 271 unsafe fn shrink(
1b1a35ee 272 &self,
ba9703b0 273 ptr: NonNull<u8>,
1b1a35ee
XL
274 old_layout: Layout,
275 new_layout: Layout,
276 ) -> Result<NonNull<[u8]>, AllocError> {
ba9703b0 277 debug_assert!(
1b1a35ee
XL
278 new_layout.size() <= old_layout.size(),
279 "`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
ba9703b0
XL
280 );
281
1b1a35ee 282 match new_layout.size() {
3dfed10e
XL
283 // SAFETY: conditions must be upheld by the caller
284 0 => unsafe {
fc512014 285 self.deallocate(ptr, old_layout);
1b1a35ee 286 Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
3dfed10e 287 },
ba9703b0 288
3dfed10e 289 // SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller
1b1a35ee
XL
290 new_size if old_layout.align() == new_layout.align() => unsafe {
291 // `realloc` probably checks for `new_size <= old_layout.size()` or something similar.
292 intrinsics::assume(new_size <= old_layout.size());
3dfed10e 293
1b1a35ee
XL
294 let raw_ptr = realloc(ptr.as_ptr(), old_layout, new_size);
295 let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
3dfed10e
XL
296 Ok(NonNull::slice_from_raw_parts(ptr, new_size))
297 },
1b1a35ee
XL
298
299 // SAFETY: because `new_size` must be smaller than or equal to `old_layout.size()`,
300 // both the old and new memory allocation are valid for reads and writes for `new_size`
301 // bytes. Also, because the old allocation wasn't yet deallocated, it cannot overlap
302 // `new_ptr`. Thus, the call to `copy_nonoverlapping` is safe. The safety contract
303 // for `dealloc` must be upheld by the caller.
304 new_size => unsafe {
fc512014 305 let new_ptr = self.allocate(new_layout)?;
1b1a35ee 306 ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_mut_ptr(), new_size);
fc512014 307 self.deallocate(ptr, old_layout);
1b1a35ee
XL
308 Ok(new_ptr)
309 },
74b04a01 310 }
83c7162d
XL
311 }
312}
313
314/// The allocator for unique pointers.
17df50a5 315#[cfg(all(not(no_global_oom_handling), not(test)))]
83c7162d
XL
316#[lang = "exchange_malloc"]
317#[inline]
318unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
f035d41b 319 let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
fc512014 320 match Global.allocate(layout) {
1b1a35ee 321 Ok(ptr) => ptr.as_mut_ptr(),
ba9703b0 322 Err(_) => handle_alloc_error(layout),
83c7162d
XL
323 }
324}
325
94b46f34 326#[cfg_attr(not(test), lang = "box_free")]
83c7162d 327#[inline]
a2a8927a 328#[rustc_const_unstable(feature = "const_box", issue = "92521")]
74b04a01 329// This signature has to be the same as `Box`, otherwise an ICE will happen.
fc512014 330// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
74b04a01 331// well.
fc512014
XL
332// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
333// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
04454e1e 334pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Destruct>(
a2a8927a
XL
335 ptr: Unique<T>,
336 alloc: A,
337) {
f035d41b
XL
338 unsafe {
339 let size = size_of_val(ptr.as_ref());
340 let align = min_align_of_val(ptr.as_ref());
341 let layout = Layout::from_size_align_unchecked(size, align);
a2a8927a 342 alloc.deallocate(From::from(ptr.cast()), layout)
f035d41b 343 }
83c7162d
XL
344}
345
29967ef6
XL
346// # Allocation error handler
347
17df50a5 348#[cfg(not(no_global_oom_handling))]
29967ef6
XL
349extern "Rust" {
350 // This is the magic symbol to call the global alloc error handler. rustc generates
351 // it to call `__rg_oom` if there is a `#[alloc_error_handler]`, or to call the
352 // default implementations below (`__rdl_oom`) otherwise.
29967ef6
XL
353 fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
354}
355
94b46f34
XL
356/// Abort on memory allocation error or failure.
357///
358/// Callers of memory allocation APIs wishing to abort computation
359/// in response to an allocation error are encouraged to call this function,
360/// rather than directly invoking `panic!` or similar.
361///
362/// The default behavior of this function is to print a message to standard error
363/// and abort the process.
364/// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
365///
366/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
367/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
368#[stable(feature = "global_alloc", since = "1.28.0")]
a2a8927a 369#[rustc_const_unstable(feature = "const_alloc_error", issue = "92523")]
17df50a5 370#[cfg(all(not(no_global_oom_handling), not(test)))]
fc512014 371#[cold]
a2a8927a
XL
372pub const fn handle_alloc_error(layout: Layout) -> ! {
373 const fn ct_error(_: Layout) -> ! {
374 panic!("allocation failed");
29967ef6 375 }
a2a8927a
XL
376
377 fn rt_error(layout: Layout) -> ! {
378 unsafe {
379 __rust_alloc_error_handler(layout.size(), layout.align());
380 }
381 }
382
383 unsafe { core::intrinsics::const_eval_select((layout,), ct_error, rt_error) }
29967ef6
XL
384}
385
386// For alloc test `std::alloc::handle_alloc_error` can be used directly.
17df50a5 387#[cfg(all(not(no_global_oom_handling), test))]
29967ef6
XL
388pub use std::alloc::handle_alloc_error;
389
04454e1e 390#[cfg(all(not(no_global_oom_handling), not(test)))]
29967ef6
XL
391#[doc(hidden)]
392#[allow(unused_attributes)]
393#[unstable(feature = "alloc_internals", issue = "none")]
394pub mod __alloc_error_handler {
395 use crate::alloc::Layout;
396
397 // called via generated `__rust_alloc_error_handler`
398
399 // if there is no `#[alloc_error_handler]`
400 #[rustc_std_internal_symbol]
923072b8 401 pub unsafe fn __rdl_oom(size: usize, _align: usize) -> ! {
5e7ed085 402 panic!("memory allocation of {size} bytes failed")
29967ef6
XL
403 }
404
94222f64 405 // if there is an `#[alloc_error_handler]`
29967ef6 406 #[rustc_std_internal_symbol]
923072b8 407 pub unsafe fn __rg_oom(size: usize, align: usize) -> ! {
29967ef6
XL
408 let layout = unsafe { Layout::from_size_align_unchecked(size, align) };
409 extern "Rust" {
410 #[lang = "oom"]
411 fn oom_impl(layout: Layout) -> !;
412 }
413 unsafe { oom_impl(layout) }
414 }
415}
5869c6ff
XL
416
417/// Specialize clones into pre-allocated, uninitialized memory.
418/// Used by `Box::clone` and `Rc`/`Arc::make_mut`.
419pub(crate) trait WriteCloneIntoRaw: Sized {
420 unsafe fn write_clone_into_raw(&self, target: *mut Self);
421}
422
423impl<T: Clone> WriteCloneIntoRaw for T {
424 #[inline]
425 default unsafe fn write_clone_into_raw(&self, target: *mut Self) {
426 // Having allocated *first* may allow the optimizer to create
427 // the cloned value in-place, skipping the local and move.
428 unsafe { target.write(self.clone()) };
429 }
430}
431
432impl<T: Copy> WriteCloneIntoRaw for T {
433 #[inline]
434 unsafe fn write_clone_into_raw(&self, target: *mut Self) {
435 // We can always copy in-place, without ever involving a local value.
436 unsafe { target.copy_from_nonoverlapping(self, 1) };
437 }
438}