]> git.proxmox.com Git - rustc.git/blame - library/std/src/sys/common/alloc.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / library / std / src / sys / common / alloc.rs
CommitLineData
532ac7d7
XL
1use crate::alloc::{GlobalAlloc, Layout, System};
2use crate::cmp;
3use crate::ptr;
a1dfa0c6
XL
4
5// The minimum alignment guaranteed by the architecture. This value is used to
6// add fast paths for low alignment values.
60c5eb7d
XL
7#[cfg(all(any(
8 target_arch = "x86",
9 target_arch = "arm",
10 target_arch = "mips",
11 target_arch = "powerpc",
12 target_arch = "powerpc64",
1b1a35ee 13 target_arch = "sparc",
60c5eb7d
XL
14 target_arch = "asmjs",
15 target_arch = "wasm32",
1b1a35ee 16 target_arch = "hexagon",
94222f64
XL
17 target_arch = "riscv32",
18 target_arch = "xtensa"
60c5eb7d 19)))]
a1dfa0c6 20pub const MIN_ALIGN: usize = 8;
60c5eb7d
XL
21#[cfg(all(any(
22 target_arch = "x86_64",
23 target_arch = "aarch64",
24 target_arch = "mips64",
25 target_arch = "s390x",
dfeec247
XL
26 target_arch = "sparc64",
27 target_arch = "riscv64"
60c5eb7d 28)))]
a1dfa0c6
XL
29pub const MIN_ALIGN: usize = 16;
30
31pub unsafe fn realloc_fallback(
32 alloc: &System,
33 ptr: *mut u8,
34 old_layout: Layout,
35 new_size: usize,
36) -> *mut u8 {
37 // Docs for GlobalAlloc::realloc require this to be valid:
38 let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
39
40 let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
41 if !new_ptr.is_null() {
42 let size = cmp::min(old_layout.size(), new_size);
43 ptr::copy_nonoverlapping(ptr, new_ptr, size);
44 GlobalAlloc::dealloc(alloc, ptr, old_layout);
45 }
46 new_ptr
47}