Skip to content

Commit e1a427c

Browse files
committed
Replace impl GlobalAlloc for Global with a set of free functions
1 parent db7bf5c commit e1a427c

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/liballoc/alloc.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -49,37 +49,39 @@ pub type Heap = Global;
4949
#[allow(non_upper_case_globals)]
5050
pub const Heap: Global = Global;
5151

52-
unsafe impl GlobalAlloc for Global {
53-
#[inline]
54-
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
55-
__rust_alloc(layout.size(), layout.align())
56-
}
52+
#[unstable(feature = "allocator_api", issue = "32838")]
53+
#[inline]
54+
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
55+
__rust_alloc(layout.size(), layout.align())
56+
}
5757

58-
#[inline]
59-
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
60-
__rust_dealloc(ptr, layout.size(), layout.align())
61-
}
58+
#[unstable(feature = "allocator_api", issue = "32838")]
59+
#[inline]
60+
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
61+
__rust_dealloc(ptr, layout.size(), layout.align())
62+
}
6263

63-
#[inline]
64-
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
65-
__rust_realloc(ptr, layout.size(), layout.align(), new_size)
66-
}
64+
#[unstable(feature = "allocator_api", issue = "32838")]
65+
#[inline]
66+
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
67+
__rust_realloc(ptr, layout.size(), layout.align(), new_size)
68+
}
6769

68-
#[inline]
69-
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
70-
__rust_alloc_zeroed(layout.size(), layout.align())
71-
}
70+
#[unstable(feature = "allocator_api", issue = "32838")]
71+
#[inline]
72+
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
73+
__rust_alloc_zeroed(layout.size(), layout.align())
7274
}
7375

7476
unsafe impl Alloc for Global {
7577
#[inline]
7678
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
77-
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
79+
NonNull::new(alloc(layout)).ok_or(AllocErr)
7880
}
7981

8082
#[inline]
8183
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
82-
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
84+
dealloc(ptr.as_ptr(), layout)
8385
}
8486

8587
#[inline]
@@ -89,12 +91,12 @@ unsafe impl Alloc for Global {
8991
new_size: usize)
9092
-> Result<NonNull<u8>, AllocErr>
9193
{
92-
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
94+
NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
9395
}
9496

9597
#[inline]
9698
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
97-
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
99+
NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
98100
}
99101
}
100102

@@ -108,7 +110,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
108110
align as *mut u8
109111
} else {
110112
let layout = Layout::from_size_align_unchecked(size, align);
111-
let ptr = Global.alloc(layout);
113+
let ptr = alloc(layout);
112114
if !ptr.is_null() {
113115
ptr
114116
} else {
@@ -126,7 +128,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
126128
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
127129
if size != 0 {
128130
let layout = Layout::from_size_align_unchecked(size, align);
129-
Global.dealloc(ptr as *mut u8, layout);
131+
dealloc(ptr as *mut u8, layout);
130132
}
131133
}
132134

src/libstd/alloc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#[doc(inline)] #[allow(deprecated)] pub use alloc_crate::alloc::Heap;
1616
#[doc(inline)] pub use alloc_crate::alloc::{Global, Layout, oom};
17+
#[doc(inline)] pub use alloc_crate::alloc::{alloc, alloc_zeroed, dealloc, realloc};
1718
#[doc(inline)] pub use alloc_system::System;
1819
#[doc(inline)] pub use core::alloc::*;
1920

src/test/run-pass/allocator/xcrate-use2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern crate custom;
1919
extern crate custom_as_global;
2020
extern crate helper;
2121

22-
use std::alloc::{Global, Alloc, GlobalAlloc, System, Layout};
22+
use std::alloc::{alloc, dealloc, GlobalAlloc, System, Layout};
2323
use std::sync::atomic::{Ordering, ATOMIC_USIZE_INIT};
2424

2525
static GLOBAL: custom::A = custom::A(ATOMIC_USIZE_INIT);
@@ -30,10 +30,10 @@ fn main() {
3030
let layout = Layout::from_size_align(4, 2).unwrap();
3131

3232
// Global allocator routes to the `custom_as_global` global
33-
let ptr = Global.alloc(layout.clone());
33+
let ptr = alloc(layout.clone());
3434
helper::work_with(&ptr);
3535
assert_eq!(custom_as_global::get(), n + 1);
36-
Global.dealloc(ptr, layout.clone());
36+
dealloc(ptr, layout.clone());
3737
assert_eq!(custom_as_global::get(), n + 2);
3838

3939
// Usage of the system allocator avoids all globals

0 commit comments

Comments
 (0)