]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/alloc.rs
Rollup merge of #67686 - ssomers:keys_start_slasher, r=Mark-Simulacrum
[rust.git] / src / libstd / sys_common / alloc.rs
1 #![allow(dead_code)]
2
3 use crate::alloc::{GlobalAlloc, Layout, System};
4 use crate::cmp;
5 use crate::ptr;
6
7 // The minimum alignment guaranteed by the architecture. This value is used to
8 // add fast paths for low alignment values.
9 #[cfg(all(any(
10     target_arch = "x86",
11     target_arch = "arm",
12     target_arch = "mips",
13     target_arch = "powerpc",
14     target_arch = "powerpc64",
15     target_arch = "asmjs",
16     target_arch = "wasm32",
17     target_arch = "hexagon"
18 )))]
19 pub const MIN_ALIGN: usize = 8;
20 #[cfg(all(any(
21     target_arch = "x86_64",
22     target_arch = "aarch64",
23     target_arch = "mips64",
24     target_arch = "s390x",
25     target_arch = "sparc64",
26     target_arch = "riscv64"
27 )))]
28 pub const MIN_ALIGN: usize = 16;
29
30 pub unsafe fn realloc_fallback(
31     alloc: &System,
32     ptr: *mut u8,
33     old_layout: Layout,
34     new_size: usize,
35 ) -> *mut u8 {
36     // Docs for GlobalAlloc::realloc require this to be valid:
37     let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
38
39     let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
40     if !new_ptr.is_null() {
41         let size = cmp::min(old_layout.size(), new_size);
42         ptr::copy_nonoverlapping(ptr, new_ptr, size);
43         GlobalAlloc::dealloc(alloc, ptr, old_layout);
44     }
45     new_ptr
46 }