Replace impl GlobalAlloc for Global
with a set of free functions
This commit is contained in:
parent
f6ab74b8e7
commit
3373204ac4
3 changed files with 29 additions and 26 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue