1 use crate::std::alloc::System;
2 use crate::std::cmp;
3 use crate::std::ptr;
4 use core::alloc::{GlobalAlloc, Layout};
5
6 // The minimum alignment guaranteed by the architecture. This value is used to
7 // add fast paths for low alignment values.
8 #[cfg(any(
9 target_arch = "x86",
10 target_arch = "arm",
11 target_arch = "m68k",
12 target_arch = "csky",
13 target_arch = "mips",
14 target_arch = "mips32r6",
15 target_arch = "powerpc",
16 target_arch = "powerpc64",
17 target_arch = "sparc",
18 target_arch = "asmjs",
19 target_arch = "wasm32",
20 target_arch = "hexagon",
21 all(target_arch = "riscv32", not(target_os = "espidf")),
22 all(target_arch = "xtensa", not(target_os = "espidf")),
23 ))]
24 pub const MIN_ALIGN: usize = 8;
25 #[cfg(any(
26 target_arch = "x86_64",
27 target_arch = "aarch64",
28 target_arch = "loongarch64",
29 target_arch = "mips64",
30 target_arch = "mips64r6",
31 target_arch = "s390x",
32 target_arch = "sparc64",
33 target_arch = "riscv64",
34 target_arch = "wasm64",
35 ))]
36 pub const MIN_ALIGN: usize = 16;
37 // The allocator on the esp-idf platform guarantees 4 byte alignment.
38 #[cfg(any(
39 all(target_arch = "riscv32", target_os = "espidf"),
40 all(target_arch = "xtensa", target_os = "espidf"),
41 ))]
42 pub const MIN_ALIGN: usize = 4;
43
realloc_fallback( alloc: &System, ptr: *mut u8, old_layout: Layout, new_size: usize, ) -> *mut u844 pub unsafe fn realloc_fallback(
45 alloc: &System,
46 ptr: *mut u8,
47 old_layout: Layout,
48 new_size: usize,
49 ) -> *mut u8 {
50 // Docs for GlobalAlloc::realloc require this to be valid:
51 let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
52
53 let new_ptr = GlobalAlloc::alloc(alloc, new_layout);
54 if !new_ptr.is_null() {
55 let size = cmp::min(old_layout.size(), new_size);
56 ptr::copy_nonoverlapping(ptr, new_ptr, size);
57 GlobalAlloc::dealloc(alloc, ptr, old_layout);
58 }
59 new_ptr
60 }
61