]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys_common/alloc.rs
New upstream version 1.38.0+dfsg1
[rustc.git] / src / libstd / sys_common / alloc.rs
CommitLineData
a1dfa0c6
XL
1#![allow(dead_code)]
2
532ac7d7
XL
3use crate::alloc::{GlobalAlloc, Layout, System};
4use crate::cmp;
5use crate::ptr;
a1dfa0c6
XL
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(target_arch = "x86",
10 target_arch = "arm",
11 target_arch = "mips",
12 target_arch = "powerpc",
13 target_arch = "powerpc64",
14 target_arch = "asmjs",
416331ca
XL
15 target_arch = "wasm32",
16 target_arch = "hexagon")))]
a1dfa0c6
XL
17pub const MIN_ALIGN: usize = 8;
18#[cfg(all(any(target_arch = "x86_64",
19 target_arch = "aarch64",
20 target_arch = "mips64",
21 target_arch = "s390x",
22 target_arch = "sparc64")))]
23pub const MIN_ALIGN: usize = 16;
24
25pub unsafe fn realloc_fallback(
26 alloc: &System,
27 ptr: *mut u8,
28 old_layout: Layout,
29 new_size: usize,
30) -> *mut u8 {
31 // Docs for GlobalAlloc::realloc require this to be valid:
32 let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
33
34 let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
35 if !new_ptr.is_null() {
36 let size = cmp::min(old_layout.size(), new_size);
37 ptr::copy_nonoverlapping(ptr, new_ptr, size);
38 GlobalAlloc::dealloc(alloc, ptr, old_layout);
39 }
40 new_ptr
41}