std: move allocators to sys
· patricklam/verify-rust-std@4dc5b67 (original) (raw)
``
1
`+
use super::{realloc_fallback, MIN_ALIGN};
`
1
2
`use crate::alloc::{GlobalAlloc, Layout, System};
`
2
3
`use crate::ptr;
`
3
``
`-
use crate::sys::common::alloc::{realloc_fallback, MIN_ALIGN};
`
4
4
``
5
5
`#[stable(feature = "alloc_system_type", since = "1.28.0")]
`
6
6
`unsafe impl GlobalAlloc for System {
`
`@@ -11,7 +11,7 @@ unsafe impl GlobalAlloc for System {
`
11
11
`// Also see https://github.com/rust-lang/rust/issues/45955 and
`
12
12
`// https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914.
`
13
13
`if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
`
14
``
`-
libc::malloc(layout.size()) as *mut u8
`
``
14
`+
unsafe { libc::malloc(layout.size()) as *mut u8 }
`
15
15
`} else {
`
16
16
`` // posix_memalign
returns a non-aligned value if supplied a very
``
17
17
`// large alignment on older versions of Apple's platforms (unknown
`
`@@ -25,35 +25,35 @@ unsafe impl GlobalAlloc for System {
`
25
25
`return ptr::null_mut();
`
26
26
`}
`
27
27
`}
`
28
``
`-
aligned_malloc(&layout)
`
``
28
`+
unsafe { aligned_malloc(&layout) }
`
29
29
`}
`
30
30
`}
`
31
31
``
32
32
`#[inline]
`
33
33
`unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
`
34
34
`` // See the comment above in alloc
for why this check looks the way it does.
``
35
35
`if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
`
36
``
`-
libc::calloc(layout.size(), 1) as *mut u8
`
``
36
`+
unsafe { libc::calloc(layout.size(), 1) as *mut u8 }
`
37
37
`} else {
`
38
``
`-
let ptr = self.alloc(layout);
`
``
38
`+
let ptr = unsafe { self.alloc(layout) };
`
39
39
`if !ptr.is_null() {
`
40
``
`-
ptr::write_bytes(ptr, 0, layout.size());
`
``
40
`+
unsafe { ptr::write_bytes(ptr, 0, layout.size()) };
`
41
41
`}
`
42
42
` ptr
`
43
43
`}
`
44
44
`}
`
45
45
``
46
46
`#[inline]
`
47
47
`unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
`
48
``
`-
libc::free(ptr as *mut libc::c_void)
`
``
48
`+
unsafe { libc::free(ptr as *mut libc::c_void) }
`
49
49
`}
`
50
50
``
51
51
`#[inline]
`
52
52
`unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
`
53
53
`if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
`
54
``
`-
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
`
``
54
`+
unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
`
55
55
`} else {
`
56
``
`-
realloc_fallback(self, ptr, layout, new_size)
`
``
56
`+
unsafe { realloc_fallback(self, ptr, layout, new_size) }
`
57
57
`}
`
58
58
`}
`
59
59
`}
`
`@@ -81,7 +81,7 @@ cfg_if::cfg_if! {
`
81
81
`// posix_memalign only has one, clear requirement: that the alignment be a multiple of
`
82
82
`` // sizeof(void*)
. Since these are all powers of 2, we can just use max.
``
83
83
`let align = layout.align().max(crate::mem::size_of::());
`
84
``
`-
let ret = libc::posix_memalign(&mut out, align, layout.size());
`
``
84
`+
let ret = unsafe { libc::posix_memalign(&mut out, align, layout.size()) };
`
85
85
`if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
`
86
86
`}
`
87
87
`}
`