Skip to content

Commit 5aa2da0

Browse files
committed
fix sized deallocation for proc
1 parent 395e453 commit 5aa2da0

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

src/liballoc/heap.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use core::ptr::RawPtr;
1616
#[cfg(not(test))] use core::raw;
17-
#[cfg(not(test))] use util;
17+
#[cfg(stage0, not(test))] use util;
1818

1919
/// Returns a pointer to `size` bytes of memory.
2020
///
@@ -119,7 +119,7 @@ unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
119119
}
120120

121121
// FIXME: #7496
122-
#[cfg(not(test))]
122+
#[cfg(stage0, not(test))]
123123
#[lang="closure_exchange_malloc"]
124124
#[inline]
125125
#[allow(deprecated)]
@@ -134,6 +134,21 @@ unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
134134
alloc as *mut u8
135135
}
136136

137+
// FIXME: #7496
138+
#[cfg(not(stage0), not(test))]
139+
#[lang="closure_exchange_malloc"]
140+
#[inline]
141+
#[allow(deprecated)]
142+
unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
143+
align: uint) -> *mut u8 {
144+
let p = allocate(size, align);
145+
146+
let alloc = p as *mut raw::Box<()>;
147+
(*alloc).drop_glue = drop_glue;
148+
149+
alloc as *mut u8
150+
}
151+
137152
#[cfg(jemalloc)]
138153
mod imp {
139154
use core::option::{None, Option};

src/librustc/middle/trans/base.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use middle::trans::glue;
6666
use middle::trans::inline;
6767
use middle::trans::intrinsic;
6868
use middle::trans::machine;
69-
use middle::trans::machine::{llsize_of, llsize_of_real};
69+
use middle::trans::machine::{llsize_of, llsize_of_real, llalign_of_min};
7070
use middle::trans::meth;
7171
use middle::trans::monomorphize;
7272
use middle::trans::tvec;
@@ -382,13 +382,44 @@ pub fn malloc_raw_dyn<'a>(bcx: &'a Block<'a>,
382382
Result::new(r.bcx, PointerCast(r.bcx, r.val, llty_ptr))
383383
}
384384

385+
pub fn malloc_raw_dyn_proc<'a>(
386+
bcx: &'a Block<'a>,
387+
t: ty::t, alloc_fn: LangItem) -> Result<'a> {
388+
let _icx = push_ctxt("malloc_raw_dyn_proc");
389+
let ccx = bcx.ccx();
390+
391+
let langcall = require_alloc_fn(bcx, t, alloc_fn);
392+
393+
// Grab the TypeRef type of ptr_ty.
394+
let ptr_ty = ty::mk_uniq(bcx.tcx(), t);
395+
let ptr_llty = type_of(ccx, ptr_ty);
396+
397+
let llty = type_of(bcx.ccx(), t);
398+
let size = llsize_of(bcx.ccx(), llty);
399+
let llalign = C_uint(ccx, llalign_of_min(bcx.ccx(), llty) as uint);
400+
401+
// Allocate space:
402+
let drop_glue = glue::get_drop_glue(ccx, ty::mk_uniq(bcx.tcx(), t));
403+
let r = callee::trans_lang_call(
404+
bcx,
405+
langcall,
406+
[
407+
PointerCast(bcx, drop_glue, Type::glue_fn(ccx, Type::i8p(ccx)).ptr_to()),
408+
size,
409+
llalign
410+
],
411+
None);
412+
Result::new(r.bcx, PointerCast(r.bcx, r.val, ptr_llty))
413+
}
414+
415+
385416
pub fn malloc_raw_dyn_managed<'a>(
386417
bcx: &'a Block<'a>,
387418
t: ty::t,
388419
alloc_fn: LangItem,
389420
size: ValueRef)
390421
-> Result<'a> {
391-
let _icx = push_ctxt("malloc_raw_managed");
422+
let _icx = push_ctxt("malloc_raw_dyn_managed");
392423
let ccx = bcx.ccx();
393424

394425
let langcall = require_alloc_fn(bcx, t, alloc_fn);

src/librustc/middle/trans/closure.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use middle::trans::common::*;
2424
use middle::trans::datum::{Datum, DatumBlock, Expr, Lvalue, rvalue_scratch_datum};
2525
use middle::trans::debuginfo;
2626
use middle::trans::expr;
27-
use middle::trans::machine::llsize_of;
2827
use middle::trans::type_of::*;
2928
use middle::trans::type_::Type;
3029
use middle::ty;
@@ -144,15 +143,12 @@ fn allocate_cbox<'a>(bcx: &'a Block<'a>,
144143
let tcx = bcx.tcx();
145144

146145
// Allocate and initialize the box:
146+
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
147147
match store {
148148
ty::UniqTraitStore => {
149-
let ty = type_of(bcx.ccx(), cdata_ty);
150-
let size = llsize_of(bcx.ccx(), ty);
151-
// we treat proc as @ here, which isn't ideal
152-
malloc_raw_dyn_managed(bcx, cdata_ty, ClosureExchangeMallocFnLangItem, size)
149+
malloc_raw_dyn_proc(bcx, cbox_ty, ClosureExchangeMallocFnLangItem)
153150
}
154151
ty::RegionTraitStore(..) => {
155-
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
156152
let llbox = alloc_ty(bcx, cbox_ty, "__closure");
157153
Result::new(bcx, llbox)
158154
}

src/librustc/middle/trans/glue.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,8 @@ fn make_drop_glue<'a>(bcx: &'a Block<'a>, v0: ValueRef, t: ty::t) -> &'a Block<'
520520
with_cond(bcx, IsNotNull(bcx, env), |bcx| {
521521
let dtor_ptr = GEPi(bcx, env, [0u, abi::box_field_tydesc]);
522522
let dtor = Load(bcx, dtor_ptr);
523-
let cdata = GEPi(bcx, env, [0u, abi::box_field_body]);
524-
Call(bcx, dtor, [PointerCast(bcx, cdata, Type::i8p(bcx.ccx()))], None);
525-
526-
// Free the environment itself
527-
// FIXME: #13994: pass align and size here
528-
trans_exchange_free(bcx, env, 0, 8)
523+
Call(bcx, dtor, [PointerCast(bcx, box_cell_v, Type::i8p(bcx.ccx()))], None);
524+
bcx
529525
})
530526
}
531527
ty::ty_trait(..) => {

0 commit comments

Comments
 (0)