Change the interface of placement new to take a tydesc as part of Issue #2831.

This commit is contained in:
Michael Sullivan
2012-07-09 17:23:13 -07:00
parent a7897b3ef3
commit 120773b2a7
7 changed files with 42 additions and 27 deletions

View File

@@ -10,8 +10,8 @@ export log_str;
export lock_and_signal, condition, methods; export lock_and_signal, condition, methods;
enum type_desc = { enum type_desc = {
size: libc::size_t, size: uint,
align: libc::size_t align: uint
// Remaining fields not listed // Remaining fields not listed
}; };

View File

@@ -31,11 +31,11 @@ impl arena for arena {
head = chunk(uint::next_power_of_two(new_min_chunk_size + 1u)); head = chunk(uint::next_power_of_two(new_min_chunk_size + 1u));
self.chunks = @cons(head, self.chunks); self.chunks = @cons(head, self.chunks);
ret self.alloc(n_bytes, align); ret self.alloc_inner(n_bytes, align);
} }
#[inline(always)] #[inline(always)]
fn alloc(n_bytes: uint, align: uint) -> *() { fn alloc_inner(n_bytes: uint, align: uint) -> *() {
let alignm1 = align - 1u; let alignm1 = align - 1u;
let mut head = list::head(self.chunks); let mut head = list::head(self.chunks);
@@ -52,5 +52,13 @@ impl arena for arena {
ret unsafe::reinterpret_cast(p); ret unsafe::reinterpret_cast(p);
} }
} }
#[inline(always)]
fn alloc(tydesc: *()) -> *() {
unsafe {
let tydesc = tydesc as *sys::type_desc;
self.alloc_inner((*tydesc).size, (*tydesc).align)
}
}
} }

View File

@@ -3693,9 +3693,9 @@ fn trans_expr(bcx: block, e: @ast::expr, dest: dest) -> block {
ret trans_assign_op(bcx, e, op, dst, src); ret trans_assign_op(bcx, e, op, dst, src);
} }
ast::expr_new(pool, alloc_id, val) { ast::expr_new(pool, alloc_id, val) {
// First, call pool->alloc(sz, align) to get back a void*. Then, // First, call pool->alloc(tydesc) to get back a void*.
// cast this memory to the required type and evaluate value into // Then, cast this memory to the required type and evaluate value
// it. // into it.
let ccx = bcx.ccx(); let ccx = bcx.ccx();
// Allocate space for the ptr that will be returned from // 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 {
#debug["ptr_ty = %s", ppaux::ty_to_str(tcx, ptr_ty)]; #debug["ptr_ty = %s", ppaux::ty_to_str(tcx, ptr_ty)];
#debug["ptr_ptr_val = %s", val_str(ccx.tn, ptr_ptr_val)]; #debug["ptr_ptr_val = %s", val_str(ccx.tn, ptr_ptr_val)];
let void_ty = ty::mk_ptr(tcx, {ty: ty::mk_nil(tcx), let void_ty = ty::mk_nil_ptr(tcx);
mutbl: ast::m_imm}); let llvoid_ty = type_of(ccx, void_ty);
let voidval = { let voidval = PointerCast(bcx, ptr_ptr_val, T_ptr(llvoid_ty));
let llvoid_ty = type_of(ccx, void_ty);
PointerCast(bcx, ptr_ptr_val, T_ptr(llvoid_ty))
};
#debug["voidval = %s", val_str(ccx.tn, voidval)]; #debug["voidval = %s", val_str(ccx.tn, voidval)];
let llval_ty = type_of(ccx, expr_ty(bcx, val)); let static_ti = get_tydesc(ccx, expr_ty(bcx, val));
let args = lazily_emit_all_tydesc_glue(ccx, static_ti);
~[llsize_of(ccx, llval_ty), llalign_of(ccx, llval_ty)]; let lltydesc = PointerCast(bcx, static_ti.tydesc, llvoid_ty);
let origin = bcx.ccx().maps.method_map.get(alloc_id); let origin = bcx.ccx().maps.method_map.get(alloc_id);
let bcx = trans_call_inner( let bcx = trans_call_inner(
bcx, e.info(), node_id_type(bcx, alloc_id), void_ty, bcx, e.info(), node_id_type(bcx, alloc_id), void_ty,
|bcx| impl::trans_method_callee(bcx, alloc_id, |bcx| impl::trans_method_callee(bcx, alloc_id,
pool, origin), pool, origin),
arg_vals(args), arg_vals(~[lltydesc]),
save_in(voidval)); save_in(voidval));
#debug["dest = %s", dest_str(ccx, dest)]; #debug["dest = %s", dest_str(ccx, dest)];

View File

@@ -1631,17 +1631,15 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
some(entry) { some(entry) {
fcx.ccx.method_map.insert(alloc_id, entry); fcx.ccx.method_map.insert(alloc_id, entry);
// Check that the alloc() method has the expected type, which // Check that the alloc() method has the expected
// should be fn(sz: uint, align: uint) -> *(). // type, which should be fn(tydesc: *()) -> *().
let expected_ty = { let expected_ty = {
let ty_uint = ty::mk_uint(tcx);
let ty_nilp = ty::mk_ptr(tcx, {ty: ty::mk_nil(tcx), let ty_nilp = ty::mk_ptr(tcx, {ty: ty::mk_nil(tcx),
mutbl: ast::m_imm}); mutbl: ast::m_imm});
let m = ast::expl(ty::default_arg_mode_for_ty(ty_uint)); let m = ast::expl(ty::default_arg_mode_for_ty(ty_nilp));
ty::mk_fn(tcx, {purity: ast::impure_fn, ty::mk_fn(tcx, {purity: ast::impure_fn,
proto: ast::proto_any, proto: ast::proto_any,
inputs: ~[{mode: m, ty: ty_uint}, inputs: ~[{mode: m, ty: ty_nilp}],
{mode: m, ty: ty_uint}],
output: ty_nilp, output: ty_nilp,
ret_style: ast::return_val, ret_style: ast::return_val,
constraints: ~[]}) constraints: ~[]})

View File

@@ -11,5 +11,5 @@ impl methods for malloc_pool {
fn main() { fn main() {
let p = &malloc_pool(()); let p = &malloc_pool(());
let x = new(*p) 4u; let x = new(*p) 4u;
//~^ ERROR mismatched types: expected `fn(uint, uint) -> *()` //~^ ERROR mismatched types: expected `fn(*()) -> *()`
} }

View File

@@ -3,11 +3,17 @@ import libc, unsafe;
enum malloc_pool = (); enum malloc_pool = ();
impl methods for malloc_pool { impl methods for malloc_pool {
fn alloc(sz: uint, align: uint) -> *() { fn alloc_inner(sz: uint, align: uint) -> *() {
unsafe { unsafe {
unsafe::reinterpret_cast(libc::malloc(sz as libc::size_t)) unsafe::reinterpret_cast(libc::malloc(sz as libc::size_t))
} }
} }
fn alloc(tydesc: *()) -> *() {
unsafe {
let tydesc = tydesc as *sys::type_desc;
self.alloc_inner((*tydesc).size, (*tydesc).align)
}
}
} }
fn main() { fn main() {

View File

@@ -16,9 +16,15 @@ type ccx = {
}; };
impl arena for arena { impl arena for arena {
fn alloc(sz: uint, _align: uint) -> *() unsafe { fn alloc_inner(sz: uint, _align: uint) -> *() unsafe {
ret unsafe::reinterpret_cast(libc::malloc(sz as libc::size_t)); ret unsafe::reinterpret_cast(libc::malloc(sz as libc::size_t));
} }
fn alloc(tydesc: *()) -> *() {
unsafe {
let tydesc = tydesc as *sys::type_desc;
self.alloc_inner((*tydesc).size, (*tydesc).align)
}
}
} }
fn h(bcx : &bcx) -> &bcx { fn h(bcx : &bcx) -> &bcx {