Skip to content

Commit 5e5a0c2

Browse files
committed
Separate alloc::heap::Alloc trait for stage0 #[global_allocator]
1 parent e521b8b commit 5e5a0c2

File tree

8 files changed

+109
-14
lines changed

8 files changed

+109
-14
lines changed

src/Cargo.lock

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

src/liballoc/alloc.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ use core::usize;
2222
#[doc(inline)]
2323
pub use core::alloc::*;
2424

25-
#[doc(hidden)]
26-
pub mod __core {
27-
pub use core::*;
28-
}
29-
3025
extern "Rust" {
3126
#[allocator]
3227
#[rustc_allocator_nounwind]

src/liballoc/heap.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub use alloc::{Excess, Layout, AllocErr, CannotReallocInPlace};
12+
use core::alloc::Alloc as CoreAlloc;
13+
14+
#[doc(hidden)]
15+
pub mod __core {
16+
pub use core::*;
17+
}
18+
19+
/// Compatibility with older versions of #[global_allocator] during bootstrap
20+
pub unsafe trait Alloc {
21+
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;
22+
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout);
23+
fn oom(&mut self, err: AllocErr) -> !;
24+
fn usable_size(&self, layout: &Layout) -> (usize, usize);
25+
unsafe fn realloc(&mut self,
26+
ptr: *mut u8,
27+
layout: Layout,
28+
new_layout: Layout) -> Result<*mut u8, AllocErr>;
29+
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;
30+
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr>;
31+
unsafe fn realloc_excess(&mut self,
32+
ptr: *mut u8,
33+
layout: Layout,
34+
new_layout: Layout) -> Result<Excess, AllocErr>;
35+
unsafe fn grow_in_place(&mut self,
36+
ptr: *mut u8,
37+
layout: Layout,
38+
new_layout: Layout) -> Result<(), CannotReallocInPlace>;
39+
unsafe fn shrink_in_place(&mut self,
40+
ptr: *mut u8,
41+
layout: Layout,
42+
new_layout: Layout) -> Result<(), CannotReallocInPlace>;
43+
}
44+
45+
#[allow(deprecated)]
46+
unsafe impl<T> Alloc for T where T: CoreAlloc {
47+
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
48+
CoreAlloc::alloc(self, layout)
49+
}
50+
51+
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
52+
CoreAlloc::dealloc(self, ptr, layout)
53+
}
54+
55+
fn oom(&mut self, err: AllocErr) -> ! {
56+
CoreAlloc::oom(self, err)
57+
}
58+
59+
fn usable_size(&self, layout: &Layout) -> (usize, usize) {
60+
CoreAlloc::usable_size(self, layout)
61+
}
62+
63+
unsafe fn realloc(&mut self,
64+
ptr: *mut u8,
65+
layout: Layout,
66+
new_layout: Layout) -> Result<*mut u8, AllocErr> {
67+
CoreAlloc::realloc(self, ptr, layout, new_layout)
68+
}
69+
70+
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
71+
CoreAlloc::alloc_zeroed(self, layout)
72+
}
73+
74+
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
75+
CoreAlloc::alloc_excess(self, layout)
76+
}
77+
78+
unsafe fn realloc_excess(&mut self,
79+
ptr: *mut u8,
80+
layout: Layout,
81+
new_layout: Layout) -> Result<Excess, AllocErr> {
82+
CoreAlloc::realloc_excess(self, ptr, layout, new_layout)
83+
}
84+
85+
unsafe fn grow_in_place(&mut self,
86+
ptr: *mut u8,
87+
layout: Layout,
88+
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
89+
CoreAlloc::grow_in_place(self, ptr, layout, new_layout)
90+
}
91+
92+
unsafe fn shrink_in_place(&mut self,
93+
ptr: *mut u8,
94+
layout: Layout,
95+
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
96+
CoreAlloc::shrink_in_place(self, ptr, layout, new_layout)
97+
}
98+
}

src/liballoc/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,16 @@ pub mod alloc;
153153
#[unstable(feature = "allocator_api", issue = "32838")]
154154
#[rustc_deprecated(since = "1.27.0", reason = "module renamed to `alloc`")]
155155
/// Use the `alloc` module instead.
156+
#[cfg(not(stage0))]
156157
pub mod heap {
157158
pub use alloc::*;
158159
}
159160

161+
#[unstable(feature = "allocator_api", issue = "32838")]
162+
#[rustc_deprecated(since = "1.27.0", reason = "module renamed to `alloc`")]
163+
#[cfg(stage0)]
164+
pub mod heap;
165+
160166
// Primitive types using the heaps above
161167

162168
// Need to conditionally define the mod from `boxed.rs` to avoid

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 = { path = "../liballoc" }
1615
alloc_system = { path = "../liballoc_system" }
1716
core = { path = "../libcore" }
1817
libc = { path = "../rustc/libc_shim" }

src/liballoc_jemalloc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub use contents::*;
3232
mod contents {
3333
use core::ptr;
3434

35-
use core::heap::{Alloc, AllocErr, Layout};
35+
use core::alloc::{Alloc, AllocErr, Layout};
3636
use alloc_system::System;
3737
use libc::{c_int, c_void, size_t};
3838

src/liballoc_system/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ test = false
1010
doc = false
1111

1212
[dependencies]
13-
alloc = { path = "../liballoc" }
1413
core = { path = "../libcore" }
1514
libc = { path = "../rustc/libc_shim" }
1615
compiler_builtins = { path = "../rustc/compiler_builtins_shim" }

src/liballoc_system/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const MIN_ALIGN: usize = 8;
4141
#[allow(dead_code)]
4242
const MIN_ALIGN: usize = 16;
4343

44-
use core::heap::{Alloc, AllocErr, Layout, Excess, CannotReallocInPlace};
44+
use core::alloc::{Alloc, AllocErr, Layout, Excess, CannotReallocInPlace};
4545

4646
#[unstable(feature = "allocator_api", issue = "32838")]
4747
pub struct System;
@@ -121,7 +121,7 @@ mod platform {
121121

122122
use MIN_ALIGN;
123123
use System;
124-
use core::heap::{Alloc, AllocErr, Layout};
124+
use core::alloc::{Alloc, AllocErr, Layout};
125125

126126
#[unstable(feature = "allocator_api", issue = "32838")]
127127
unsafe impl<'a> Alloc for &'a System {
@@ -283,7 +283,7 @@ mod platform {
283283

284284
use MIN_ALIGN;
285285
use System;
286-
use core::heap::{Alloc, AllocErr, Layout, CannotReallocInPlace};
286+
use core::alloc::{Alloc, AllocErr, Layout, CannotReallocInPlace};
287287

288288
type LPVOID = *mut u8;
289289
type HANDLE = LPVOID;
@@ -495,7 +495,7 @@ mod platform {
495495
mod platform {
496496
extern crate dlmalloc;
497497

498-
use core::heap::{Alloc, AllocErr, Layout, Excess, CannotReallocInPlace};
498+
use core::alloc::{Alloc, AllocErr, Layout, Excess, CannotReallocInPlace};
499499
use System;
500500
use self::dlmalloc::GlobalDlmalloc;
501501

0 commit comments

Comments
 (0)