Skip to content

Commit d542e67

Browse files
committed
Do some cleanup of the allocation code.
1 parent 107442d commit d542e67

File tree

2 files changed

+42
-49
lines changed

2 files changed

+42
-49
lines changed

src/rustc/middle/trans/base.rs

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ enum dest {
6262
ignore,
6363
}
6464

65+
// Heap selectors. Indicate which heap something should go on.
66+
enum heap {
67+
heap_shared,
68+
heap_exchange,
69+
}
70+
6571
fn dest_str(ccx: @crate_ctxt, d: dest) -> str {
6672
alt d {
6773
by_val(v) { #fmt["by_val(%s)", val_str(ccx.tn, *v)] }
@@ -341,75 +347,61 @@ fn opaque_box_body(bcx: block,
341347
PointerCast(bcx, bodyptr, T_ptr(type_of(ccx, body_t)))
342348
}
343349

344-
// trans_malloc_boxed_raw: expects an unboxed type and returns a pointer to
350+
// malloc_raw: expects an unboxed type and returns a pointer to
345351
// enough space for a box of that type. This includes a rust_opaque_box
346352
// header.
347-
fn malloc_boxed_raw(bcx: block, t: ty::t,
348-
&static_ti: option<@tydesc_info>) -> ValueRef {
349-
let _icx = bcx.insn_ctxt("trans_malloc_boxed_raw");
353+
fn malloc_raw(bcx: block, t: ty::t, heap: heap) -> ValueRef {
354+
let _icx = bcx.insn_ctxt("malloc_raw");
350355
let ccx = bcx.ccx();
351356

352-
// Grab the TypeRef type of box_ptr, because that's what trans_raw_malloc
353-
// wants.
354-
let box_ptr = ty::mk_imm_box(ccx.tcx, t);
355-
let llty = type_of(ccx, box_ptr);
357+
let (mk_fn, upcall) = alt heap {
358+
heap_shared { (ty::mk_imm_box, ccx.upcalls.malloc) }
359+
heap_exchange {
360+
(ty::mk_imm_uniq, ccx.upcalls.exchange_malloc )
361+
}
362+
};
363+
364+
// Grab the TypeRef type of box_ptr_ty.
365+
let box_ptr_ty = mk_fn(bcx.tcx(), t);
366+
let llty = type_of(ccx, box_ptr_ty);
356367

357368
// Get the tydesc for the body:
369+
let mut static_ti = none;
358370
let lltydesc = get_tydesc(ccx, t, static_ti);
359371
lazily_emit_all_tydesc_glue(ccx, copy static_ti);
360372

361373
// Allocate space:
362-
let rval = Call(bcx, ccx.upcalls.malloc, [lltydesc]);
374+
let rval = Call(bcx, upcall, [lltydesc]);
363375
ret PointerCast(bcx, rval, llty);
364376
}
365377

366-
// trans_malloc_boxed: usefully wraps trans_malloc_box_raw; allocates a box,
367-
// initializes the reference count to 1, and pulls out the body and rc
368-
fn malloc_boxed(bcx: block, t: ty::t) -> {box: ValueRef, body: ValueRef} {
369-
let _icx = bcx.insn_ctxt("trans_malloc_boxed");
370-
let mut ti = none;
371-
let box = malloc_boxed_raw(bcx, t, ti);
372-
let box_no_addrspace = non_gc_box_cast(
373-
bcx, box, ty::mk_imm_box(bcx.tcx(), t));
374-
let body = GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
378+
// malloc_general: usefully wraps malloc_raw; allocates a box,
379+
// and pulls out the body
380+
fn malloc_general(bcx: block, t: ty::t, heap: heap) ->
381+
{box: ValueRef, body: ValueRef} {
382+
let _icx = bcx.insn_ctxt("malloc_general");
383+
let mk_ty = alt heap { heap_shared { ty::mk_imm_box }
384+
heap_exchange { ty::mk_imm_uniq } };
385+
let box = malloc_raw(bcx, t, heap);
386+
let non_gc_box = non_gc_box_cast(bcx, box, mk_ty(bcx.tcx(), t));
387+
let body = GEPi(bcx, non_gc_box, [0u, abi::box_field_body]);
375388
ret {box: box, body: body};
376389
}
377390

378-
fn malloc_unique_raw(bcx: block, t: ty::t) -> ValueRef {
379-
let _icx = bcx.insn_ctxt("malloc_unique_box_raw");
380-
let ccx = bcx.ccx();
381-
382-
// Grab the TypeRef type of box_ptr, because that's what trans_raw_malloc
383-
// wants.
384-
let box_ptr = ty::mk_imm_uniq(ccx.tcx, t);
385-
let llty = type_of(ccx, box_ptr);
386-
387-
// Get the tydesc for the body:
388-
let mut static_ti = none;
389-
let lltydesc = get_tydesc(ccx, t, static_ti);
390-
lazily_emit_all_tydesc_glue(ccx, static_ti);
391-
392-
// Allocate space:
393-
let rval = Call(bcx, ccx.upcalls.exchange_malloc, [lltydesc]);
394-
ret PointerCast(bcx, rval, llty);
391+
fn malloc_boxed(bcx: block, t: ty::t) -> {box: ValueRef, body: ValueRef} {
392+
malloc_general(bcx, t, heap_shared)
395393
}
396-
397394
fn malloc_unique(bcx: block, t: ty::t) -> {box: ValueRef, body: ValueRef} {
398-
let _icx = bcx.insn_ctxt("malloc_unique_box");
399-
let box = malloc_unique_raw(bcx, t);
400-
let non_gc_box = non_gc_box_cast(bcx, box, ty::mk_imm_uniq(bcx.tcx(), t));
401-
let body = GEPi(bcx, non_gc_box, [0u, abi::box_field_body]);
402-
ret {box: box, body: body};
395+
malloc_general(bcx, t, heap_exchange)
403396
}
404397

405398
fn malloc_unique_dyn_raw(bcx: block, t: ty::t, size: ValueRef) -> ValueRef {
406-
let _icx = bcx.insn_ctxt("malloc_unique_box_raw");
399+
let _icx = bcx.insn_ctxt("malloc_unique_dyn_raw");
407400
let ccx = bcx.ccx();
408401

409-
// Grab the TypeRef type of box_ptr, because that's what trans_raw_malloc
410-
// wants.
411-
let box_ptr = ty::mk_imm_uniq(ccx.tcx, t);
412-
let llty = type_of(ccx, box_ptr);
402+
// Grab the TypeRef type of box_ptr_ty.
403+
let box_ptr_ty = ty::mk_imm_uniq(ccx.tcx, t);
404+
let llty = type_of(ccx, box_ptr_ty);
413405

414406
// Get the tydesc for the body:
415407
let mut static_ti = none;
@@ -423,7 +415,7 @@ fn malloc_unique_dyn_raw(bcx: block, t: ty::t, size: ValueRef) -> ValueRef {
423415

424416
fn malloc_unique_dyn(bcx: block, t: ty::t, size: ValueRef
425417
) -> {box: ValueRef, body: ValueRef} {
426-
let _icx = bcx.insn_ctxt("malloc_unique_box");
418+
let _icx = bcx.insn_ctxt("malloc_unique_dyn");
427419
let box = malloc_unique_dyn_raw(bcx, t, size);
428420
let body = GEPi(bcx, box, [0u, abi::box_field_body]);
429421
ret {box: box, body: body};

src/rustc/middle/trans/closure.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,12 @@ fn allocate_cbox(bcx: block,
175175
let mut temp_cleanups = [];
176176
let (bcx, box) = alt ck {
177177
ty::ck_box {
178-
let box = malloc_boxed_raw(bcx, cdata_ty, ti);
178+
get_tydesc(ccx, cdata_ty, ti);
179+
let box = malloc_raw(bcx, cdata_ty, heap_shared);
179180
(bcx, box)
180181
}
181182
ty::ck_uniq {
182-
let box = malloc_unique_raw(bcx, cdata_ty);
183+
let box = malloc_raw(bcx, cdata_ty, heap_exchange);
183184
(bcx, box)
184185
}
185186
ty::ck_block {

0 commit comments

Comments
 (0)