Skip to content

Commit f1ce693

Browse files
committed
alter exchange_free for sized deallocation
The support for sized deallocation is nearly complete. The only known missing pieces are `Box<str>`, `Box<[T]>` and `proc`.
1 parent 9450198 commit f1ce693

File tree

5 files changed

+53
-24
lines changed

5 files changed

+53
-24
lines changed

src/liballoc/heap.rs

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

1111
// FIXME: #13994: port to the sized deallocation API when available
12-
// FIXME: #13996: need a way to mark the `allocate` and `reallocate` return values as `noalias`
12+
// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias` and `nonnull`
1313

1414
use core::intrinsics::{abort, cttz32};
1515
use core::option::{None, Option};
@@ -133,14 +133,20 @@ unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
133133
}
134134
}
135135

136-
#[cfg(not(test))]
136+
#[cfg(not(test), stage0)]
137137
#[lang="exchange_free"]
138138
#[inline]
139-
// FIXME: #13994 (rustc should pass align and size here)
140139
unsafe fn exchange_free(ptr: *mut u8) {
141140
deallocate(ptr, 0, 8);
142141
}
143142

143+
#[cfg(not(test), not(stage0))]
144+
#[lang="exchange_free"]
145+
#[inline]
146+
unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
147+
deallocate(ptr, size, align);
148+
}
149+
144150
// FIXME: #7496
145151
#[cfg(not(test))]
146152
#[lang="closure_exchange_malloc"]

src/librustc/middle/trans/cleanup.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,14 @@ impl<'a> CleanupMethods<'a> for FunctionContext<'a> {
278278
fn schedule_free_value(&self,
279279
cleanup_scope: ScopeId,
280280
val: ValueRef,
281-
heap: Heap) {
281+
heap: Heap,
282+
content_ty: ty::t) {
282283
/*!
283284
* Schedules a call to `free(val)`. Note that this is a shallow
284285
* operation.
285286
*/
286287

287-
let drop = box FreeValue { ptr: val, heap: heap };
288+
let drop = box FreeValue { ptr: val, heap: heap, content_ty: content_ty };
288289

289290
debug!("schedule_free_value({:?}, val={}, heap={:?})",
290291
cleanup_scope,
@@ -847,6 +848,7 @@ pub enum Heap {
847848
pub struct FreeValue {
848849
ptr: ValueRef,
849850
heap: Heap,
851+
content_ty: ty::t
850852
}
851853

852854
impl Cleanup for FreeValue {
@@ -860,7 +862,7 @@ impl Cleanup for FreeValue {
860862
glue::trans_free(bcx, self.ptr)
861863
}
862864
HeapExchange => {
863-
glue::trans_exchange_free(bcx, self.ptr)
865+
glue::trans_exchange_free_ty(bcx, self.ptr, self.content_ty)
864866
}
865867
}
866868
}
@@ -931,7 +933,8 @@ pub trait CleanupMethods<'a> {
931933
fn schedule_free_value(&self,
932934
cleanup_scope: ScopeId,
933935
val: ValueRef,
934-
heap: Heap);
936+
heap: Heap,
937+
content_ty: ty::t);
935938
fn schedule_clean(&self,
936939
cleanup_scope: ScopeId,
937940
cleanup: Box<Cleanup>);

src/librustc/middle/trans/expr.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ fn trans_uniq_expr<'a>(bcx: &'a Block<'a>,
11831183
} else {
11841184
let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
11851185
fcx.schedule_free_value(cleanup::CustomScope(custom_cleanup_scope),
1186-
val, cleanup::HeapExchange);
1186+
val, cleanup::HeapExchange, contents_ty);
11871187
let bcx = trans_into(bcx, contents, SaveIn(val));
11881188
fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
11891189
bcx
@@ -1205,7 +1205,7 @@ fn trans_managed_expr<'a>(bcx: &'a Block<'a>,
12051205

12061206
let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
12071207
fcx.schedule_free_value(cleanup::CustomScope(custom_cleanup_scope),
1208-
bx, cleanup::HeapManaged);
1208+
bx, cleanup::HeapManaged, contents_ty);
12091209
let bcx = trans_into(bcx, contents, SaveIn(body));
12101210
fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
12111211
immediate_rvalue_bcx(bcx, bx, box_ty).to_expr_datumblock()
@@ -1789,13 +1789,14 @@ fn deref_once<'a>(bcx: &'a Block<'a>,
17891789
let scope = cleanup::temporary_scope(bcx.tcx(), expr.id);
17901790
let ptr = Load(bcx, datum.val);
17911791
if !type_is_zero_size(bcx.ccx(), content_ty) {
1792-
bcx.fcx.schedule_free_value(scope, ptr, cleanup::HeapExchange);
1792+
bcx.fcx.schedule_free_value(scope, ptr, cleanup::HeapExchange, content_ty);
17931793
}
17941794
}
17951795
RvalueExpr(Rvalue { mode: ByValue }) => {
17961796
let scope = cleanup::temporary_scope(bcx.tcx(), expr.id);
17971797
if !type_is_zero_size(bcx.ccx(), content_ty) {
1798-
bcx.fcx.schedule_free_value(scope, datum.val, cleanup::HeapExchange);
1798+
bcx.fcx.schedule_free_value(scope, datum.val, cleanup::HeapExchange,
1799+
content_ty);
17991800
}
18001801
}
18011802
LvalueExpr => { }

src/librustc/middle/trans/glue.rs

+28-12
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,30 @@ pub fn trans_free<'a>(cx: &'a Block<'a>, v: ValueRef) -> &'a Block<'a> {
5050
Some(expr::Ignore)).bcx
5151
}
5252

53-
pub fn trans_exchange_free<'a>(cx: &'a Block<'a>, v: ValueRef)
54-
-> &'a Block<'a> {
53+
fn trans_exchange_free<'a>(cx: &'a Block<'a>, v: ValueRef, size: u64,
54+
align: u64) -> &'a Block<'a> {
5555
let _icx = push_ctxt("trans_exchange_free");
56+
let ccx = cx.ccx();
5657
callee::trans_lang_call(cx,
5758
langcall(cx, None, "", ExchangeFreeFnLangItem),
58-
[PointerCast(cx, v, Type::i8p(cx.ccx()))],
59+
[PointerCast(cx, v, Type::i8p(ccx)), C_uint(ccx, size as uint), C_uint(ccx, align as uint)],
5960
Some(expr::Ignore)).bcx
6061
}
6162

63+
pub fn trans_exchange_free_ty<'a>(bcx: &'a Block<'a>, ptr: ValueRef,
64+
content_ty: ty::t) -> &'a Block<'a> {
65+
let sizing_type = sizing_type_of(bcx.ccx(), content_ty);
66+
let content_size = llsize_of_alloc(bcx.ccx(), sizing_type);
67+
68+
// `Box<ZeroSizeType>` does not allocate.
69+
if content_size != 0 {
70+
let content_align = llalign_of_min(bcx.ccx(), sizing_type);
71+
trans_exchange_free(bcx, ptr, content_size, content_align)
72+
} else {
73+
bcx
74+
}
75+
}
76+
6277
pub fn take_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
6378
-> &'a Block<'a> {
6479
// NB: v is an *alias* of type t here, not a direct value.
@@ -87,17 +102,15 @@ fn get_drop_glue_type(ccx: &CrateContext, t: ty::t) -> ty::t {
87102
ty::ty_vec(_, None) | ty::ty_str => t,
88103
_ => {
89104
let llty = sizing_type_of(ccx, typ);
90-
// Unique boxes do not allocate for zero-size types. The standard
91-
// library may assume that `free` is never called on the pointer
92-
// returned for `Box<ZeroSizeType>`.
105+
// `Box<ZeroSizeType>` does not allocate.
93106
if llsize_of_alloc(ccx, llty) == 0 {
94107
ty::mk_i8()
95108
} else {
96109
ty::mk_uniq(tcx, ty::mk_i8())
97110
}
98-
}
99-
}
100111
}
112+
}
113+
}
101114
_ => t
102115
}
103116
}
@@ -285,20 +298,22 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
285298
ty::ty_vec(mt, None) => {
286299
with_cond(bcx, not_null, |bcx| {
287300
let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, mt.ty);
288-
trans_exchange_free(bcx, llbox)
301+
// FIXME: #13994: the old `Box<[T]>` will not support sized deallocation
302+
trans_exchange_free(bcx, llbox, 0, 8)
289303
})
290304
}
291305
ty::ty_str => {
292306
with_cond(bcx, not_null, |bcx| {
293307
let unit_ty = ty::sequence_element_type(bcx.tcx(), t);
294308
let bcx = tvec::make_drop_glue_unboxed(bcx, llbox, unit_ty);
295-
trans_exchange_free(bcx, llbox)
309+
// FIXME: #13994: the old `Box<str>` will not support sized deallocation
310+
trans_exchange_free(bcx, llbox, 0, 8)
296311
})
297312
}
298313
_ => {
299314
with_cond(bcx, not_null, |bcx| {
300315
let bcx = drop_ty(bcx, llbox, content_ty);
301-
trans_exchange_free(bcx, llbox)
316+
trans_exchange_free_ty(bcx, llbox, content_ty)
302317
})
303318
}
304319
}
@@ -340,7 +355,8 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
340355
Call(bcx, dtor, [PointerCast(bcx, cdata, Type::i8p(bcx.ccx()))], []);
341356

342357
// Free the environment itself
343-
trans_exchange_free(bcx, env)
358+
// FIXME: #13994: pass align and size here
359+
trans_exchange_free(bcx, env, 0, 8)
344360
})
345361
}
346362
_ => {

src/librustc/middle/trans/tvec.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,11 @@ pub fn trans_uniq_vstore<'a>(bcx: &'a Block<'a>,
287287
// Create a temporary scope lest execution should fail while
288288
// constructing the vector.
289289
let temp_scope = fcx.push_custom_cleanup_scope();
290+
291+
// FIXME: #13994: the old `Box<[T]> will not support sized deallocation, this is a placeholder
292+
let content_ty = vt.unit_ty;
290293
fcx.schedule_free_value(cleanup::CustomScope(temp_scope),
291-
val, cleanup::HeapExchange);
294+
val, cleanup::HeapExchange, content_ty);
292295

293296
let dataptr = get_dataptr(bcx, val);
294297

0 commit comments

Comments
 (0)