]>
Commit | Line | Data |
---|---|---|
a1dfa0c6 XL |
1 | #![allow(dead_code)] |
2 | ||
532ac7d7 XL |
3 | use crate::alloc::{GlobalAlloc, Layout, System}; |
4 | use crate::cmp; | |
5 | use 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. | |
60c5eb7d XL |
9 | #[cfg(all(any( |
10 | target_arch = "x86", | |
11 | target_arch = "arm", | |
12 | target_arch = "mips", | |
13 | target_arch = "powerpc", | |
14 | target_arch = "powerpc64", | |
1b1a35ee | 15 | target_arch = "sparc", |
60c5eb7d XL |
16 | target_arch = "asmjs", |
17 | target_arch = "wasm32", | |
1b1a35ee XL |
18 | target_arch = "hexagon", |
19 | target_arch = "riscv32" | |
60c5eb7d | 20 | )))] |
a1dfa0c6 | 21 | pub const MIN_ALIGN: usize = 8; |
60c5eb7d XL |
22 | #[cfg(all(any( |
23 | target_arch = "x86_64", | |
24 | target_arch = "aarch64", | |
25 | target_arch = "mips64", | |
26 | target_arch = "s390x", | |
dfeec247 XL |
27 | target_arch = "sparc64", |
28 | target_arch = "riscv64" | |
60c5eb7d | 29 | )))] |
a1dfa0c6 XL |
30 | pub const MIN_ALIGN: usize = 16; |
31 | ||
32 | pub unsafe fn realloc_fallback( | |
33 | alloc: &System, | |
34 | ptr: *mut u8, | |
35 | old_layout: Layout, | |
36 | new_size: usize, | |
37 | ) -> *mut u8 { | |
38 | // Docs for GlobalAlloc::realloc require this to be valid: | |
39 | let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align()); | |
40 | ||
41 | let new_ptr = GlobalAlloc::alloc(alloc, new_layout); | |
42 | if !new_ptr.is_null() { | |
43 | let size = cmp::min(old_layout.size(), new_size); | |
44 | ptr::copy_nonoverlapping(ptr, new_ptr, size); | |
45 | GlobalAlloc::dealloc(alloc, ptr, old_layout); | |
46 | } | |
47 | new_ptr | |
48 | } |