Skip to content

Commit fd242ee

Browse files
committed
impl GlobalAlloc for Global
1 parent eae0d46 commit fd242ee

File tree

1 file changed

+50
-35
lines changed

1 file changed

+50
-35
lines changed

src/liballoc/alloc.rs

+50-35
Original file line numberDiff line numberDiff line change
@@ -73,62 +73,42 @@ pub type Heap = Global;
7373
#[allow(non_upper_case_globals)]
7474
pub const Heap: Global = Global;
7575

76-
unsafe impl Alloc for Global {
76+
unsafe impl GlobalAlloc for Global {
7777
#[inline]
78-
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
78+
unsafe fn alloc(&self, layout: Layout) -> *mut Void {
7979
#[cfg(not(stage0))]
8080
let ptr = __rust_alloc(layout.size(), layout.align());
8181
#[cfg(stage0)]
8282
let ptr = __rust_alloc(layout.size(), layout.align(), &mut 0);
83-
84-
if !ptr.is_null() {
85-
Ok(ptr)
86-
} else {
87-
Err(AllocErr)
88-
}
83+
ptr as *mut Void
8984
}
9085

9186
#[inline]
92-
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
93-
__rust_dealloc(ptr, layout.size(), layout.align())
87+
unsafe fn dealloc(&self, ptr: *mut Void, layout: Layout) {
88+
__rust_dealloc(ptr as *mut u8, layout.size(), layout.align())
9489
}
9590

9691
#[inline]
97-
unsafe fn realloc(&mut self,
98-
ptr: *mut u8,
99-
layout: Layout,
100-
new_size: usize)
101-
-> Result<*mut u8, AllocErr>
102-
{
92+
unsafe fn realloc(&self, ptr: *mut Void, layout: Layout, new_size: usize) -> *mut Void {
10393
#[cfg(not(stage0))]
104-
let ptr = __rust_realloc(ptr, layout.size(), layout.align(), new_size);
94+
let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(), new_size);
10595
#[cfg(stage0)]
106-
let ptr = __rust_realloc(ptr, layout.size(), layout.align(),
96+
let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(),
10797
new_size, layout.align(), &mut 0);
108-
109-
if !ptr.is_null() {
110-
Ok(ptr)
111-
} else {
112-
Err(AllocErr)
113-
}
98+
ptr as *mut Void
11499
}
115100

116101
#[inline]
117-
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
102+
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Void {
118103
#[cfg(not(stage0))]
119104
let ptr = __rust_alloc_zeroed(layout.size(), layout.align());
120105
#[cfg(stage0)]
121106
let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
122-
123-
if !ptr.is_null() {
124-
Ok(ptr)
125-
} else {
126-
Err(AllocErr)
127-
}
107+
ptr as *mut Void
128108
}
129109

130110
#[inline]
131-
fn oom(&mut self) -> ! {
111+
fn oom(&self) -> ! {
132112
unsafe {
133113
#[cfg(not(stage0))]
134114
__rust_oom();
@@ -138,6 +118,38 @@ unsafe impl Alloc for Global {
138118
}
139119
}
140120

121+
unsafe impl Alloc for Global {
122+
#[inline]
123+
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
124+
GlobalAlloc::alloc(self, layout).into()
125+
}
126+
127+
#[inline]
128+
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
129+
GlobalAlloc::dealloc(self, ptr as *mut Void, layout)
130+
}
131+
132+
#[inline]
133+
unsafe fn realloc(&mut self,
134+
ptr: *mut u8,
135+
layout: Layout,
136+
new_size: usize)
137+
-> Result<*mut u8, AllocErr>
138+
{
139+
GlobalAlloc::realloc(self, ptr as *mut Void, layout, new_size).into()
140+
}
141+
142+
#[inline]
143+
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
144+
GlobalAlloc::alloc_zeroed(self, layout).into()
145+
}
146+
147+
#[inline]
148+
fn oom(&mut self) -> ! {
149+
GlobalAlloc::oom(self)
150+
}
151+
}
152+
141153
/// The allocator for unique pointers.
142154
// This function must not unwind. If it does, MIR trans will fail.
143155
#[cfg(not(test))]
@@ -148,9 +160,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
148160
align as *mut u8
149161
} else {
150162
let layout = Layout::from_size_align_unchecked(size, align);
151-
Global.alloc(layout).unwrap_or_else(|_| {
163+
let ptr = Global.alloc(layout);
164+
if !ptr.is_null() {
165+
ptr as *mut u8
166+
} else {
152167
Global.oom()
153-
})
168+
}
154169
}
155170
}
156171

@@ -162,7 +177,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
162177
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
163178
if size != 0 {
164179
let layout = Layout::from_size_align_unchecked(size, align);
165-
Global.dealloc(ptr as *mut u8, layout);
180+
Global.dealloc(ptr as *mut Void, layout);
166181
}
167182
}
168183

0 commit comments

Comments
 (0)