Replace `impl GlobalAlloc for Global` with a set of free functions
This commit is contained in:
parent
f6ab74b8e7
commit
3373204ac4
|
@ -49,37 +49,39 @@ pub type Heap = Global;
|
|||
#[allow(non_upper_case_globals)]
|
||||
pub const Heap: Global = Global;
|
||||
|
||||
unsafe impl GlobalAlloc for Global {
|
||||
#[inline]
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
__rust_alloc(layout.size(), layout.align())
|
||||
}
|
||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[inline]
|
||||
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
|
||||
__rust_alloc(layout.size(), layout.align())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
__rust_dealloc(ptr, layout.size(), layout.align())
|
||||
}
|
||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[inline]
|
||||
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
|
||||
__rust_dealloc(ptr, layout.size(), layout.align())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||
__rust_realloc(ptr, layout.size(), layout.align(), new_size)
|
||||
}
|
||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[inline]
|
||||
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||
__rust_realloc(ptr, layout.size(), layout.align(), new_size)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||
__rust_alloc_zeroed(layout.size(), layout.align())
|
||||
}
|
||||
#[unstable(feature = "allocator_api", issue = "32838")]
|
||||
#[inline]
|
||||
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
||||
__rust_alloc_zeroed(layout.size(), layout.align())
|
||||
}
|
||||
|
||||
unsafe impl Alloc for Global {
|
||||
#[inline]
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
|
||||
NonNull::new(alloc(layout)).ok_or(AllocErr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
||||
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
|
||||
dealloc(ptr.as_ptr(), layout)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -89,12 +91,12 @@ unsafe impl Alloc for Global {
|
|||
new_size: usize)
|
||||
-> Result<NonNull<u8>, AllocErr>
|
||||
{
|
||||
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
|
||||
NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
|
||||
NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,7 +110,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
|
|||
align as *mut u8
|
||||
} else {
|
||||
let layout = Layout::from_size_align_unchecked(size, align);
|
||||
let ptr = Global.alloc(layout);
|
||||
let ptr = alloc(layout);
|
||||
if !ptr.is_null() {
|
||||
ptr
|
||||
} else {
|
||||
|
@ -126,7 +128,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
|
|||
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
|
||||
if size != 0 {
|
||||
let layout = Layout::from_size_align_unchecked(size, align);
|
||||
Global.dealloc(ptr as *mut u8, layout);
|
||||
dealloc(ptr as *mut u8, layout);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
#[doc(inline)] #[allow(deprecated)] pub use alloc_crate::alloc::Heap;
|
||||
#[doc(inline)] pub use alloc_crate::alloc::{Global, Layout, oom};
|
||||
#[doc(inline)] pub use alloc_crate::alloc::{alloc, alloc_zeroed, dealloc, realloc};
|
||||
#[doc(inline)] pub use alloc_system::System;
|
||||
#[doc(inline)] pub use core::alloc::*;
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ extern crate custom;
|
|||
extern crate custom_as_global;
|
||||
extern crate helper;
|
||||
|
||||
use std::alloc::{Global, Alloc, GlobalAlloc, System, Layout};
|
||||
use std::alloc::{alloc, dealloc, GlobalAlloc, System, Layout};
|
||||
use std::sync::atomic::{Ordering, ATOMIC_USIZE_INIT};
|
||||
|
||||
static GLOBAL: custom::A = custom::A(ATOMIC_USIZE_INIT);
|
||||
|
@ -30,10 +30,10 @@ fn main() {
|
|||
let layout = Layout::from_size_align(4, 2).unwrap();
|
||||
|
||||
// Global allocator routes to the `custom_as_global` global
|
||||
let ptr = Global.alloc(layout.clone());
|
||||
let ptr = alloc(layout.clone());
|
||||
helper::work_with(&ptr);
|
||||
assert_eq!(custom_as_global::get(), n + 1);
|
||||
Global.dealloc(ptr, layout.clone());
|
||||
dealloc(ptr, layout.clone());
|
||||
assert_eq!(custom_as_global::get(), n + 2);
|
||||
|
||||
// Usage of the system allocator avoids all globals
|
||||
|
|
Loading…
Reference in New Issue