Skip to content

Commit 3212d70

Browse files
committed
auto merge of rust-lang#17280 : thestinger/rust/heap, r=pcwalton
2 parents f8426e2 + d206f05 commit 3212d70

File tree

8 files changed

+25
-55
lines changed

8 files changed

+25
-55
lines changed

src/liballoc/heap.rs

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

11-
// FIXME: #13994: port to the sized deallocation API when available
1211
// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
13-
// and `nonnull`
1412

15-
#[cfg(not(test))] use core::raw;
13+
#[cfg(stage0, not(test))] use core::raw;
1614
#[cfg(stage0, not(test))] use util;
1715

1816
/// Returns a pointer to `size` bytes of memory.
@@ -88,18 +86,19 @@ pub fn stats_print() {
8886
imp::stats_print();
8987
}
9088

91-
// The compiler never calls `exchange_free` on Box<ZeroSizeType>, so zero-size
92-
// allocations can point to this `static`. It would be incorrect to use a null
93-
// pointer, due to enums assuming types like unique pointers are never null.
94-
pub static mut EMPTY: uint = 12345;
89+
/// An arbitrary non-null address to represent zero-size allocations.
90+
///
91+
/// This preserves the non-null invariant for types like `Box<T>`. The address may overlap with
92+
/// non-zero-size memory allocations.
93+
pub static EMPTY: *mut () = 0x1 as *mut ();
9594

9695
/// The allocator for unique pointers.
9796
#[cfg(not(test))]
9897
#[lang="exchange_malloc"]
9998
#[inline]
10099
unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
101100
if size == 0 {
102-
&EMPTY as *const uint as *mut u8
101+
EMPTY as *mut u8
103102
} else {
104103
allocate(size, align)
105104
}
@@ -112,7 +111,6 @@ unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
112111
deallocate(ptr, size, align);
113112
}
114113

115-
// FIXME: #7496
116114
#[cfg(stage0, not(test))]
117115
#[lang="closure_exchange_malloc"]
118116
#[inline]
@@ -128,21 +126,6 @@ unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
128126
alloc as *mut u8
129127
}
130128

131-
// FIXME: #7496
132-
#[cfg(not(stage0), not(test))]
133-
#[lang="closure_exchange_malloc"]
134-
#[inline]
135-
#[allow(deprecated)]
136-
unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
137-
align: uint) -> *mut u8 {
138-
let p = allocate(size, align);
139-
140-
let alloc = p as *mut raw::Box<()>;
141-
(*alloc).drop_glue = drop_glue;
142-
143-
alloc as *mut u8
144-
}
145-
146129
// The minimum alignment guaranteed by the architecture. This value is used to
147130
// add fast paths for low alignment values. In practice, the alignment is a
148131
// constant at the call site and the branch will be optimized out.

src/libcollections/vec.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use core::prelude::*;
1616

17-
use alloc::heap::{allocate, reallocate, deallocate};
17+
use alloc::heap::{EMPTY, allocate, reallocate, deallocate};
1818
use core::cmp::max;
1919
use core::default::Default;
2020
use core::fmt;
@@ -28,10 +28,6 @@ use {Mutable, MutableSeq};
2828
use slice::{MutableOrdSlice, MutableSliceAllocating, CloneableVector};
2929
use slice::{Items, MutItems};
3030

31-
32-
#[doc(hidden)]
33-
pub static PTR_MARKER: u8 = 0;
34-
3531
/// An owned, growable vector.
3632
///
3733
/// # Examples
@@ -124,7 +120,7 @@ impl<T> Vec<T> {
124120
// non-null value which is fine since we never call deallocate on the ptr
125121
// if cap is 0. The reason for this is because the pointer of a slice
126122
// being NULL would break the null pointer optimization for enums.
127-
Vec { len: 0, cap: 0, ptr: &PTR_MARKER as *const _ as *mut T }
123+
Vec { len: 0, cap: 0, ptr: EMPTY as *mut T }
128124
}
129125

130126
/// Constructs a new, empty `Vec` with the specified capacity.
@@ -157,7 +153,7 @@ impl<T> Vec<T> {
157153
#[inline]
158154
pub fn with_capacity(capacity: uint) -> Vec<T> {
159155
if mem::size_of::<T>() == 0 {
160-
Vec { len: 0, cap: uint::MAX, ptr: &PTR_MARKER as *const _ as *mut T }
156+
Vec { len: 0, cap: uint::MAX, ptr: EMPTY as *mut T }
161157
} else if capacity == 0 {
162158
Vec::new()
163159
} else {

src/librustc/middle/lang_items.rs

-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ lets_do_this! {
265265
BeginUnwindLangItem, "begin_unwind", begin_unwind;
266266

267267
ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
268-
ClosureExchangeMallocFnLangItem, "closure_exchange_malloc", closure_exchange_malloc_fn;
269268
ExchangeFreeFnLangItem, "exchange_free", exchange_free_fn;
270269
MallocFnLangItem, "malloc", malloc_fn;
271270
FreeFnLangItem, "free", free_fn;

src/librustc/middle/trans/base.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,10 @@ pub fn malloc_raw_dyn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
383383
Result::new(r.bcx, PointerCast(r.bcx, r.val, llty_ptr))
384384
}
385385

386-
pub fn malloc_raw_dyn_proc<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
387-
t: ty::t, alloc_fn: LangItem)
388-
-> Result<'blk, 'tcx> {
386+
pub fn malloc_raw_dyn_proc<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: ty::t) -> Result<'blk, 'tcx> {
389387
let _icx = push_ctxt("malloc_raw_dyn_proc");
390388
let ccx = bcx.ccx();
391389

392-
let langcall = require_alloc_fn(bcx, t, alloc_fn);
393-
394390
// Grab the TypeRef type of ptr_ty.
395391
let ptr_ty = ty::mk_uniq(bcx.tcx(), t);
396392
let ptr_llty = type_of(ccx, ptr_ty);
@@ -399,18 +395,15 @@ pub fn malloc_raw_dyn_proc<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
399395
let size = llsize_of(bcx.ccx(), llty);
400396
let llalign = C_uint(ccx, llalign_of_min(bcx.ccx(), llty) as uint);
401397

402-
// Allocate space:
403-
let drop_glue = glue::get_drop_glue(ccx, ty::mk_uniq(bcx.tcx(), t));
404-
let r = callee::trans_lang_call(
405-
bcx,
406-
langcall,
407-
[
408-
PointerCast(bcx, drop_glue, Type::glue_fn(ccx, Type::i8p(ccx)).ptr_to()),
409-
size,
410-
llalign
411-
],
412-
None);
413-
Result::new(r.bcx, PointerCast(r.bcx, r.val, ptr_llty))
398+
// Allocate space and store the destructor pointer:
399+
let Result {bcx: bcx, val: llbox} = malloc_raw_dyn(bcx, ptr_llty, t, size, llalign);
400+
let dtor_ptr = GEPi(bcx, llbox, [0u, abi::box_field_drop_glue]);
401+
let drop_glue_field_ty = type_of(ccx, ty::mk_nil_ptr(bcx.tcx()));
402+
let drop_glue = PointerCast(bcx, glue::get_drop_glue(ccx, ty::mk_uniq(bcx.tcx(), t)),
403+
drop_glue_field_ty);
404+
Store(bcx, drop_glue, dtor_ptr);
405+
406+
Result::new(bcx, llbox)
414407
}
415408

416409

src/librustc/middle/trans/closure.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use driver::config::FullDebugInfo;
1515
use llvm::ValueRef;
1616
use middle::def;
1717
use middle::freevars;
18-
use middle::lang_items::ClosureExchangeMallocFnLangItem;
1918
use middle::trans::adt;
2019
use middle::trans::base::*;
2120
use middle::trans::build::*;
@@ -146,7 +145,7 @@ fn allocate_cbox<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
146145
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
147146
match store {
148147
ty::UniqTraitStore => {
149-
malloc_raw_dyn_proc(bcx, cbox_ty, ClosureExchangeMallocFnLangItem)
148+
malloc_raw_dyn_proc(bcx, cbox_ty)
150149
}
151150
ty::RegionTraitStore(..) => {
152151
let llbox = alloc_ty(bcx, cbox_ty, "__closure");

src/librustc/middle/trans/glue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, t: ty::t)
519519
let env_ptr_ty = Type::at_box(bcx.ccx(), Type::i8(bcx.ccx())).ptr_to();
520520
let env = PointerCast(bcx, env, env_ptr_ty);
521521
with_cond(bcx, IsNotNull(bcx, env), |bcx| {
522-
let dtor_ptr = GEPi(bcx, env, [0u, abi::box_field_tydesc]);
522+
let dtor_ptr = GEPi(bcx, env, [0u, abi::box_field_drop_glue]);
523523
let dtor = Load(bcx, dtor_ptr);
524524
Call(bcx, dtor, [PointerCast(bcx, box_cell_v, Type::i8p(bcx.ccx()))], None);
525525
bcx

src/librustc_back/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
pub static box_field_refcnt: uint = 0u;
12-
pub static box_field_tydesc: uint = 1u;
12+
pub static box_field_drop_glue: uint = 1u;
1313
pub static box_field_body: uint = 4u;
1414

1515
pub static tydesc_field_visit_glue: uint = 3u;

src/libsyntax/owned_slice.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::fmt;
1212
use std::default::Default;
1313
use std::hash;
1414
use std::{mem, raw, ptr, slice, vec};
15+
use std::rt::heap::EMPTY;
1516
use serialize::{Encodable, Decodable, Encoder, Decoder};
1617

1718
/// A non-growable owned slice. This would preferably become `~[T]`
@@ -81,10 +82,9 @@ impl<T> OwnedSlice<T> {
8182
}
8283

8384
pub fn as_slice<'a>(&'a self) -> &'a [T] {
84-
static PTR_MARKER: u8 = 0;
8585
let ptr = if self.data.is_null() {
8686
// length zero, i.e. this will never be read as a T.
87-
&PTR_MARKER as *const u8 as *const T
87+
EMPTY as *const T
8888
} else {
8989
self.data as *const T
9090
};

0 commit comments

Comments
 (0)