Skip to content

Commit

Permalink
Replace impl GlobalAlloc for Global with a set of free functions
Browse files Browse the repository at this point in the history
  • Loading branch information
glandium committed May 31, 2018
1 parent db7bf5c commit e1a427c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
48 changes: 25 additions & 23 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
}
}

Expand All @@ -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 {
Expand All @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/libstd/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
6 changes: 3 additions & 3 deletions src/test/run-pass/allocator/xcrate-use2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down

0 comments on commit e1a427c

Please sign in to comment.