Skip to content

Commit c7cffc5

Browse files
committed
Deprecate heap::EMPTY in favour of Unique::empty or otherwise.
1 parent 4ff583b commit c7cffc5

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

src/liballoc/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn make_place<T>() -> IntermediateBox<T> {
156156
let align = mem::align_of::<T>();
157157

158158
let p = if size == 0 {
159-
heap::EMPTY as *mut u8
159+
mem::align_of::<T>() as *mut u8
160160
} else {
161161
let p = unsafe { heap::allocate(size, align) };
162162
if p.is_null() {

src/liballoc/heap.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ pub fn usable_size(size: usize, align: usize) -> usize {
138138
///
139139
/// This preserves the non-null invariant for types like `Box<T>`. The address
140140
/// may overlap with non-zero-size memory allocations.
141-
pub const EMPTY: *mut () = 0x1 as *mut ();
141+
#[rustc_deprecated(since = "1.19", reason = "Use Unique/Shared::empty() instead")]
142+
#[unstable(feature = "heap_api", issue = "27700")]
143+
pub const EMPTY: *mut () = 1 as *mut ();
142144

143145
/// The allocator for unique pointers.
144146
// This function must not unwind. If it does, MIR trans will fail.
@@ -147,7 +149,7 @@ pub const EMPTY: *mut () = 0x1 as *mut ();
147149
#[inline]
148150
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
149151
if size == 0 {
150-
EMPTY as *mut u8
152+
align as *mut u8
151153
} else {
152154
let ptr = allocate(size, align);
153155
if ptr.is_null() {

src/liballoc/raw_vec.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ use core::cmp;
2222
/// involved. This type is excellent for building your own data structures like Vec and VecDeque.
2323
/// In particular:
2424
///
25-
/// * Produces heap::EMPTY on zero-sized types
26-
/// * Produces heap::EMPTY on zero-length allocations
25+
/// * Produces Unique::empty() on zero-sized types
26+
/// * Produces Unique::empty() on zero-length allocations
2727
/// * Catches all overflows in capacity computations (promotes them to "capacity overflow" panics)
2828
/// * Guards against 32-bit systems allocating more than isize::MAX bytes
2929
/// * Guards against overflowing your length
3030
/// * Aborts on OOM
31-
/// * Avoids freeing heap::EMPTY
31+
/// * Avoids freeing Unique::empty()
3232
/// * Contains a ptr::Unique and thus endows the user with all related benefits
3333
///
3434
/// This type does not in anyway inspect the memory that it manages. When dropped it *will*
@@ -55,15 +55,13 @@ impl<T> RawVec<T> {
5555
/// it makes a RawVec with capacity `usize::MAX`. Useful for implementing
5656
/// delayed allocation.
5757
pub fn new() -> Self {
58-
unsafe {
59-
// !0 is usize::MAX. This branch should be stripped at compile time.
60-
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
58+
// !0 is usize::MAX. This branch should be stripped at compile time.
59+
let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
6160

62-
// heap::EMPTY doubles as "unallocated" and "zero-sized allocation"
63-
RawVec {
64-
ptr: Unique::new(heap::EMPTY as *mut T),
65-
cap: cap,
66-
}
61+
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
62+
RawVec {
63+
ptr: Unique::empty(),
64+
cap: cap,
6765
}
6866
}
6967

@@ -101,7 +99,7 @@ impl<T> RawVec<T> {
10199

102100
// handles ZSTs and `cap = 0` alike
103101
let ptr = if alloc_size == 0 {
104-
heap::EMPTY as *mut u8
102+
mem::align_of::<T>() as *mut u8
105103
} else {
106104
let align = mem::align_of::<T>();
107105
let ptr = if zeroed {
@@ -148,10 +146,10 @@ impl<T> RawVec<T> {
148146

149147
impl<T> RawVec<T> {
150148
/// Gets a raw pointer to the start of the allocation. Note that this is
151-
/// heap::EMPTY if `cap = 0` or T is zero-sized. In the former case, you must
149+
/// Unique::empty() if `cap = 0` or T is zero-sized. In the former case, you must
152150
/// be careful.
153151
pub fn ptr(&self) -> *mut T {
154-
self.ptr.ptr()
152+
self.ptr.as_ptr()
155153
}
156154

157155
/// Gets the capacity of the allocation.

src/libarena/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#![feature(alloc)]
3232
#![feature(core_intrinsics)]
3333
#![feature(dropck_eyepatch)]
34-
#![feature(heap_api)]
3534
#![feature(generic_param_attrs)]
3635
#![feature(staged_api)]
3736
#![cfg_attr(test, feature(test))]
@@ -48,7 +47,6 @@ use std::mem;
4847
use std::ptr;
4948
use std::slice;
5049

51-
use alloc::heap;
5250
use alloc::raw_vec::RawVec;
5351

5452
/// An arena that can hold objects of only one type.
@@ -140,7 +138,7 @@ impl<T> TypedArena<T> {
140138
unsafe {
141139
if mem::size_of::<T>() == 0 {
142140
self.ptr.set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1) as *mut T);
143-
let ptr = heap::EMPTY as *mut T;
141+
let ptr = mem::align_of::<T>() as *mut T;
144142
// Don't drop the object. This `write` is equivalent to `forget`.
145143
ptr::write(ptr, object);
146144
&mut *ptr

src/libcollections/vec.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
#![stable(feature = "rust1", since = "1.0.0")]
6868

6969
use alloc::boxed::Box;
70-
use alloc::heap::EMPTY;
7170
use alloc::raw_vec::RawVec;
7271
use borrow::ToOwned;
7372
use borrow::Cow;
@@ -2192,7 +2191,8 @@ impl<T> Iterator for IntoIter<T> {
21922191
self.ptr = arith_offset(self.ptr as *const i8, 1) as *mut T;
21932192

21942193
// Use a non-null pointer value
2195-
Some(ptr::read(EMPTY as *mut T))
2194+
// (self.ptr might be null because of wrapping)
2195+
Some(ptr::read(1 as *mut T))
21962196
} else {
21972197
let old = self.ptr;
21982198
self.ptr = self.ptr.offset(1);
@@ -2231,7 +2231,8 @@ impl<T> DoubleEndedIterator for IntoIter<T> {
22312231
self.end = arith_offset(self.end as *const i8, -1) as *mut T;
22322232

22332233
// Use a non-null pointer value
2234-
Some(ptr::read(EMPTY as *mut T))
2234+
// (self.end might be null because of wrapping)
2235+
Some(ptr::read(1 as *mut T))
22352236
} else {
22362237
self.end = self.end.offset(-1);
22372238

src/libstd/collections/hash/table.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use alloc::heap::{EMPTY, allocate, deallocate};
11+
use alloc::heap::{allocate, deallocate};
1212

1313
use cmp;
1414
use hash::{BuildHasher, Hash, Hasher};
@@ -33,6 +33,7 @@ use self::BucketState::*;
3333
type HashUint = usize;
3434

3535
const EMPTY_BUCKET: HashUint = 0;
36+
const EMPTY: usize = 1;
3637

3738
/// Special `Unique<HashUint>` that uses the lower bit of the pointer
3839
/// to expose a boolean tag.

0 commit comments

Comments
 (0)