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