Skip to content

Commit 86753ce

Browse files
committed
Use the GlobalAlloc trait for #[global_allocator]
1 parent eb69593 commit 86753ce

File tree

17 files changed

+168
-707
lines changed

17 files changed

+168
-707
lines changed

src/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/doc/unstable-book/src/language-features/global-allocator.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ looks like:
2929
```rust
3030
#![feature(global_allocator, allocator_api, heap_api)]
3131

32-
use std::heap::{Alloc, System, Layout, AllocErr};
32+
use std::alloc::{GlobalAlloc, System, Layout, Void};
3333

3434
struct MyAllocator;
3535

36-
unsafe impl<'a> Alloc for &'a MyAllocator {
37-
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
36+
unsafe impl GlobalAlloc for MyAllocator {
37+
unsafe fn alloc(&self, layout: Layout) -> *mut Void {
3838
System.alloc(layout)
3939
}
4040

41-
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
41+
unsafe fn dealloc(&self, ptr: *mut Void, layout: Layout) {
4242
System.dealloc(ptr, layout)
4343
}
4444
}

src/liballoc/alloc.rs

Lines changed: 40 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,19 @@
1616
issue = "32838")]
1717

1818
use core::intrinsics::{min_align_of_val, size_of_val};
19-
use core::mem;
2019
use core::usize;
2120

2221
#[doc(inline)]
2322
pub use core::alloc::*;
2423

24+
#[cfg(stage0)]
2525
extern "Rust" {
2626
#[allocator]
2727
#[rustc_allocator_nounwind]
2828
fn __rust_alloc(size: usize, align: usize, err: *mut u8) -> *mut u8;
29-
#[cold]
30-
#[rustc_allocator_nounwind]
31-
fn __rust_oom(err: *const u8) -> !;
3229
#[rustc_allocator_nounwind]
3330
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
3431
#[rustc_allocator_nounwind]
35-
fn __rust_usable_size(layout: *const u8,
36-
min: *mut usize,
37-
max: *mut usize);
38-
#[rustc_allocator_nounwind]
3932
fn __rust_realloc(ptr: *mut u8,
4033
old_size: usize,
4134
old_align: usize,
@@ -44,31 +37,22 @@ extern "Rust" {
4437
err: *mut u8) -> *mut u8;
4538
#[rustc_allocator_nounwind]
4639
fn __rust_alloc_zeroed(size: usize, align: usize, err: *mut u8) -> *mut u8;
40+
}
41+
42+
#[cfg(not(stage0))]
43+
extern "Rust" {
44+
#[allocator]
4745
#[rustc_allocator_nounwind]
48-
fn __rust_alloc_excess(size: usize,
49-
align: usize,
50-
excess: *mut usize,
51-
err: *mut u8) -> *mut u8;
46+
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
5247
#[rustc_allocator_nounwind]
53-
fn __rust_realloc_excess(ptr: *mut u8,
54-
old_size: usize,
55-
old_align: usize,
56-
new_size: usize,
57-
new_align: usize,
58-
excess: *mut usize,
59-
err: *mut u8) -> *mut u8;
48+
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
6049
#[rustc_allocator_nounwind]
61-
fn __rust_grow_in_place(ptr: *mut u8,
62-
old_size: usize,
63-
old_align: usize,
64-
new_size: usize,
65-
new_align: usize) -> u8;
50+
fn __rust_realloc(ptr: *mut u8,
51+
old_size: usize,
52+
align: usize,
53+
new_size: usize) -> *mut u8;
6654
#[rustc_allocator_nounwind]
67-
fn __rust_shrink_in_place(ptr: *mut u8,
68-
old_size: usize,
69-
old_align: usize,
70-
new_size: usize,
71-
new_align: usize) -> u8;
55+
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
7256
}
7357

7458
#[derive(Copy, Clone, Default, Debug)]
@@ -86,22 +70,15 @@ pub const Heap: Global = Global;
8670
unsafe impl Alloc for Global {
8771
#[inline]
8872
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
89-
let mut err = AllocErr;
90-
let ptr = __rust_alloc(layout.size(),
91-
layout.align(),
92-
&mut err as *mut AllocErr as *mut u8);
93-
if ptr.is_null() {
94-
Err(AllocErr)
95-
} else {
96-
Ok(ptr)
97-
}
98-
}
73+
#[cfg(not(stage0))]
74+
let ptr = __rust_alloc(layout.size(), layout.align());
75+
#[cfg(stage0)]
76+
let ptr = __rust_alloc(layout.size(), layout.align(), &mut 0);
9977

100-
#[inline]
101-
#[cold]
102-
fn oom(&mut self, err: AllocErr) -> ! {
103-
unsafe {
104-
__rust_oom(&err as *const AllocErr as *const u8)
78+
if !ptr.is_null() {
79+
Ok(ptr)
80+
} else {
81+
Err(AllocErr)
10582
}
10683
}
10784

@@ -110,126 +87,41 @@ unsafe impl Alloc for Global {
11087
__rust_dealloc(ptr, layout.size(), layout.align())
11188
}
11289

113-
#[inline]
114-
fn usable_size(&self, layout: &Layout) -> (usize, usize) {
115-
let mut min = 0;
116-
let mut max = 0;
117-
unsafe {
118-
__rust_usable_size(layout as *const Layout as *const u8,
119-
&mut min,
120-
&mut max);
121-
}
122-
(min, max)
123-
}
124-
12590
#[inline]
12691
unsafe fn realloc(&mut self,
12792
ptr: *mut u8,
12893
layout: Layout,
12994
new_layout: Layout)
13095
-> Result<*mut u8, AllocErr>
13196
{
132-
let mut err = AllocErr;
133-
let ptr = __rust_realloc(ptr,
134-
layout.size(),
135-
layout.align(),
136-
new_layout.size(),
137-
new_layout.align(),
138-
&mut err as *mut AllocErr as *mut u8);
139-
if ptr.is_null() {
140-
Err(AllocErr)
97+
if layout.align() == new_layout.align() {
98+
#[cfg(not(stage0))]
99+
let ptr = __rust_realloc(ptr, layout.size(), layout.align(), new_layout.size());
100+
#[cfg(stage0)]
101+
let ptr = __rust_realloc(ptr, layout.size(), layout.align(),
102+
new_layout.size(), new_layout.align(), &mut 0);
103+
104+
if !ptr.is_null() {
105+
Ok(ptr)
106+
} else {
107+
Err(AllocErr)
108+
}
141109
} else {
142-
mem::forget(err);
143-
Ok(ptr)
110+
Err(AllocErr)
144111
}
145112
}
146113

147114
#[inline]
148115
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
149-
let mut err = AllocErr;
150-
let ptr = __rust_alloc_zeroed(layout.size(),
151-
layout.align(),
152-
&mut err as *mut AllocErr as *mut u8);
153-
if ptr.is_null() {
154-
Err(AllocErr)
155-
} else {
156-
Ok(ptr)
157-
}
158-
}
116+
#[cfg(not(stage0))]
117+
let ptr = __rust_alloc_zeroed(layout.size(), layout.align());
118+
#[cfg(stage0)]
119+
let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
159120

160-
#[inline]
161-
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
162-
let mut err = AllocErr;
163-
let mut size = 0;
164-
let ptr = __rust_alloc_excess(layout.size(),
165-
layout.align(),
166-
&mut size,
167-
&mut err as *mut AllocErr as *mut u8);
168-
if ptr.is_null() {
169-
Err(AllocErr)
121+
if !ptr.is_null() {
122+
Ok(ptr)
170123
} else {
171-
Ok(Excess(ptr, size))
172-
}
173-
}
174-
175-
#[inline]
176-
unsafe fn realloc_excess(&mut self,
177-
ptr: *mut u8,
178-
layout: Layout,
179-
new_layout: Layout) -> Result<Excess, AllocErr> {
180-
let mut err = AllocErr;
181-
let mut size = 0;
182-
let ptr = __rust_realloc_excess(ptr,
183-
layout.size(),
184-
layout.align(),
185-
new_layout.size(),
186-
new_layout.align(),
187-
&mut size,
188-
&mut err as *mut AllocErr as *mut u8);
189-
if ptr.is_null() {
190124
Err(AllocErr)
191-
} else {
192-
Ok(Excess(ptr, size))
193-
}
194-
}
195-
196-
#[inline]
197-
unsafe fn grow_in_place(&mut self,
198-
ptr: *mut u8,
199-
layout: Layout,
200-
new_layout: Layout)
201-
-> Result<(), CannotReallocInPlace>
202-
{
203-
debug_assert!(new_layout.size() >= layout.size());
204-
debug_assert!(new_layout.align() == layout.align());
205-
let ret = __rust_grow_in_place(ptr,
206-
layout.size(),
207-
layout.align(),
208-
new_layout.size(),
209-
new_layout.align());
210-
if ret != 0 {
211-
Ok(())
212-
} else {
213-
Err(CannotReallocInPlace)
214-
}
215-
}
216-
217-
#[inline]
218-
unsafe fn shrink_in_place(&mut self,
219-
ptr: *mut u8,
220-
layout: Layout,
221-
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
222-
debug_assert!(new_layout.size() <= layout.size());
223-
debug_assert!(new_layout.align() == layout.align());
224-
let ret = __rust_shrink_in_place(ptr,
225-
layout.size(),
226-
layout.align(),
227-
new_layout.size(),
228-
new_layout.align());
229-
if ret != 0 {
230-
Ok(())
231-
} else {
232-
Err(CannotReallocInPlace)
233125
}
234126
}
235127
}

src/liballoc_jemalloc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ test = false
1212
doc = false
1313

1414
[dependencies]
15-
alloc_system = { path = "../liballoc_system" }
1615
core = { path = "../libcore" }
1716
libc = { path = "../rustc/libc_shim" }
1817
compiler_builtins = { path = "../rustc/compiler_builtins_shim" }

0 commit comments

Comments
 (0)