Skip to content

Commit 120773b

Browse files
committed
Change the interface of placement new to take a tydesc as part of Issue #2831.
1 parent a7897b3 commit 120773b

File tree

7 files changed

+42
-27
lines changed

7 files changed

+42
-27
lines changed

src/libcore/sys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export log_str;
1010
export lock_and_signal, condition, methods;
1111

1212
enum type_desc = {
13-
size: libc::size_t,
14-
align: libc::size_t
13+
size: uint,
14+
align: uint
1515
// Remaining fields not listed
1616
};
1717

src/libstd/arena.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ impl arena for arena {
3131
head = chunk(uint::next_power_of_two(new_min_chunk_size + 1u));
3232
self.chunks = @cons(head, self.chunks);
3333

34-
ret self.alloc(n_bytes, align);
34+
ret self.alloc_inner(n_bytes, align);
3535
}
3636

3737
#[inline(always)]
38-
fn alloc(n_bytes: uint, align: uint) -> *() {
38+
fn alloc_inner(n_bytes: uint, align: uint) -> *() {
3939
let alignm1 = align - 1u;
4040
let mut head = list::head(self.chunks);
4141

@@ -52,5 +52,13 @@ impl arena for arena {
5252
ret unsafe::reinterpret_cast(p);
5353
}
5454
}
55+
56+
#[inline(always)]
57+
fn alloc(tydesc: *()) -> *() {
58+
unsafe {
59+
let tydesc = tydesc as *sys::type_desc;
60+
self.alloc_inner((*tydesc).size, (*tydesc).align)
61+
}
62+
}
5563
}
5664

src/rustc/middle/trans/base.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3693,9 +3693,9 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
36933693
ret trans_assign_op(bcx, e, op, dst, src);
36943694
}
36953695
ast::expr_new(pool, alloc_id, val) {
3696-
// First, call pool->alloc(sz, align) to get back a void*. Then,
3697-
// cast this memory to the required type and evaluate value into
3698-
// it.
3696+
// First, call pool->alloc(tydesc) to get back a void*.
3697+
// Then, cast this memory to the required type and evaluate value
3698+
// into it.
36993699
let ccx = bcx.ccx();
37003700

37013701
// Allocate space for the ptr that will be returned from
@@ -3706,24 +3706,21 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
37063706
#debug["ptr_ty = %s", ppaux::ty_to_str(tcx, ptr_ty)];
37073707
#debug["ptr_ptr_val = %s", val_str(ccx.tn, ptr_ptr_val)];
37083708

3709-
let void_ty = ty::mk_ptr(tcx, {ty: ty::mk_nil(tcx),
3710-
mutbl: ast::m_imm});
3711-
let voidval = {
3712-
let llvoid_ty = type_of(ccx, void_ty);
3713-
PointerCast(bcx, ptr_ptr_val, T_ptr(llvoid_ty))
3714-
};
3715-
3709+
let void_ty = ty::mk_nil_ptr(tcx);
3710+
let llvoid_ty = type_of(ccx, void_ty);
3711+
let voidval = PointerCast(bcx, ptr_ptr_val, T_ptr(llvoid_ty));
37163712
#debug["voidval = %s", val_str(ccx.tn, voidval)];
37173713

3718-
let llval_ty = type_of(ccx, expr_ty(bcx, val));
3719-
let args =
3720-
~[llsize_of(ccx, llval_ty), llalign_of(ccx, llval_ty)];
3714+
let static_ti = get_tydesc(ccx, expr_ty(bcx, val));
3715+
lazily_emit_all_tydesc_glue(ccx, static_ti);
3716+
let lltydesc = PointerCast(bcx, static_ti.tydesc, llvoid_ty);
3717+
37213718
let origin = bcx.ccx().maps.method_map.get(alloc_id);
37223719
let bcx = trans_call_inner(
37233720
bcx, e.info(), node_id_type(bcx, alloc_id), void_ty,
37243721
|bcx| impl::trans_method_callee(bcx, alloc_id,
37253722
pool, origin),
3726-
arg_vals(args),
3723+
arg_vals(~[lltydesc]),
37273724
save_in(voidval));
37283725

37293726
#debug["dest = %s", dest_str(ccx, dest)];

src/rustc/middle/typeck/check.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,17 +1631,15 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
16311631
some(entry) {
16321632
fcx.ccx.method_map.insert(alloc_id, entry);
16331633

1634-
// Check that the alloc() method has the expected type, which
1635-
// should be fn(sz: uint, align: uint) -> *().
1634+
// Check that the alloc() method has the expected
1635+
// type, which should be fn(tydesc: *()) -> *().
16361636
let expected_ty = {
1637-
let ty_uint = ty::mk_uint(tcx);
16381637
let ty_nilp = ty::mk_ptr(tcx, {ty: ty::mk_nil(tcx),
16391638
mutbl: ast::m_imm});
1640-
let m = ast::expl(ty::default_arg_mode_for_ty(ty_uint));
1639+
let m = ast::expl(ty::default_arg_mode_for_ty(ty_nilp));
16411640
ty::mk_fn(tcx, {purity: ast::impure_fn,
16421641
proto: ast::proto_any,
1643-
inputs: ~[{mode: m, ty: ty_uint},
1644-
{mode: m, ty: ty_uint}],
1642+
inputs: ~[{mode: m, ty: ty_nilp}],
16451643
output: ty_nilp,
16461644
ret_style: ast::return_val,
16471645
constraints: ~[]})

src/test/compile-fail/placement-new-bad-method-type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ impl methods for malloc_pool {
1111
fn main() {
1212
let p = &malloc_pool(());
1313
let x = new(*p) 4u;
14-
//~^ ERROR mismatched types: expected `fn(uint, uint) -> *()`
14+
//~^ ERROR mismatched types: expected `fn(*()) -> *()`
1515
}

src/test/run-pass/placement-new-leaky.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import libc, unsafe;
33
enum malloc_pool = ();
44

55
impl methods for malloc_pool {
6-
fn alloc(sz: uint, align: uint) -> *() {
6+
fn alloc_inner(sz: uint, align: uint) -> *() {
77
unsafe {
88
unsafe::reinterpret_cast(libc::malloc(sz as libc::size_t))
99
}
1010
}
11+
fn alloc(tydesc: *()) -> *() {
12+
unsafe {
13+
let tydesc = tydesc as *sys::type_desc;
14+
self.alloc_inner((*tydesc).size, (*tydesc).align)
15+
}
16+
}
1117
}
1218

1319
fn main() {

src/test/run-pass/regions-mock-trans-impls.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ type ccx = {
1616
};
1717

1818
impl arena for arena {
19-
fn alloc(sz: uint, _align: uint) -> *() unsafe {
19+
fn alloc_inner(sz: uint, _align: uint) -> *() unsafe {
2020
ret unsafe::reinterpret_cast(libc::malloc(sz as libc::size_t));
2121
}
22+
fn alloc(tydesc: *()) -> *() {
23+
unsafe {
24+
let tydesc = tydesc as *sys::type_desc;
25+
self.alloc_inner((*tydesc).size, (*tydesc).align)
26+
}
27+
}
2228
}
2329

2430
fn h(bcx : &bcx) -> &bcx {

0 commit comments

Comments
 (0)