auto merge of #7521 : thestinger/rust/vec, r=Aatch

continued from #7495
This commit is contained in:
bors
2013-07-01 02:26:46 -07:00
6 changed files with 67 additions and 46 deletions

View File

@@ -258,6 +258,23 @@ pub fn malloc_raw_dyn(bcx: block,
} }
}; };
if heap == heap_exchange {
// Grab the TypeRef type of box_ptr_ty.
let box_ptr_ty = mk_fn(bcx.tcx(), t);
let llty = type_of(ccx, box_ptr_ty);
let llty_value = type_of::type_of(ccx, t);
let llalign = llalign_of_min(ccx, llty_value);
// Allocate space:
let rval = alloca(bcx, Type::i8p());
let bcx = callee::trans_lang_call(
bcx,
langcall,
[C_i32(llalign as i32), size],
expr::SaveIn(rval));
rslt(bcx, PointerCast(bcx, Load(bcx, rval), llty))
} else {
// Grab the TypeRef type of box_ptr_ty. // Grab the TypeRef type of box_ptr_ty.
let box_ptr_ty = mk_fn(bcx.tcx(), t); let box_ptr_ty = mk_fn(bcx.tcx(), t);
let llty = type_of(ccx, box_ptr_ty); let llty = type_of(ccx, box_ptr_ty);
@@ -277,6 +294,7 @@ pub fn malloc_raw_dyn(bcx: block,
let r = rslt(bcx, PointerCast(bcx, Load(bcx, rval), llty)); let r = rslt(bcx, PointerCast(bcx, Load(bcx, rval), llty));
maybe_set_managed_unique_rc(r.bcx, r.val, heap); maybe_set_managed_unique_rc(r.bcx, r.val, heap);
r r
}
} }
// malloc_raw: expects an unboxed type and returns a pointer to // malloc_raw: expects an unboxed type and returns a pointer to

View File

@@ -1945,7 +1945,7 @@ pub mod funcs {
#[fast_ffi] #[fast_ffi]
unsafe fn malloc(size: size_t) -> *c_void; unsafe fn malloc(size: size_t) -> *c_void;
#[fast_ffi] #[fast_ffi]
unsafe fn realloc(p: *c_void, size: size_t) -> *c_void; unsafe fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
#[fast_ffi] #[fast_ffi]
unsafe fn free(p: *c_void); unsafe fn free(p: *c_void);
unsafe fn abort() -> !; unsafe fn abort() -> !;

View File

@@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use libc::{c_char, c_void, size_t, uintptr_t, free, malloc}; use libc::{c_char, c_void, size_t, uintptr_t, free, malloc, realloc};
use managed::raw::{BoxHeaderRepr, BoxRepr}; use managed::raw::{BoxHeaderRepr, BoxRepr};
use unstable::intrinsics::TyDesc; use unstable::intrinsics::TyDesc;
use sys::size_of; use sys::size_of;
@@ -18,6 +18,7 @@ extern {
fn abort(); fn abort();
} }
#[inline]
fn get_box_size(body_size: uint, body_align: uint) -> uint { fn get_box_size(body_size: uint, body_align: uint) -> uint {
let header_size = size_of::<BoxHeaderRepr>(); let header_size = size_of::<BoxHeaderRepr>();
// FIXME (#2699): This alignment calculation is suspicious. Is it right? // FIXME (#2699): This alignment calculation is suspicious. Is it right?
@@ -27,12 +28,14 @@ fn get_box_size(body_size: uint, body_align: uint) -> uint {
// Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power // Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power
// of two. // of two.
#[inline]
fn align_to(size: uint, align: uint) -> uint { fn align_to(size: uint, align: uint) -> uint {
assert!(align != 0); assert!(align != 0);
(size + align - 1) & !(align - 1) (size + align - 1) & !(align - 1)
} }
/// A wrapper around libc::malloc, aborting on out-of-memory /// A wrapper around libc::malloc, aborting on out-of-memory
#[inline]
pub unsafe fn malloc_raw(size: uint) -> *c_void { pub unsafe fn malloc_raw(size: uint) -> *c_void {
let p = malloc(size as size_t); let p = malloc(size as size_t);
if p.is_null() { if p.is_null() {
@@ -42,6 +45,17 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {
p p
} }
/// A wrapper around libc::realloc, aborting on out-of-memory
#[inline]
pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void {
let p = realloc(ptr, size as size_t);
if p.is_null() {
// we need a non-allocating way to print an error here
abort();
}
p
}
// FIXME #4942: Make these signatures agree with exchange_alloc's signatures // FIXME #4942: Make these signatures agree with exchange_alloc's signatures
#[cfg(stage0, not(test))] #[cfg(stage0, not(test))]
#[lang="exchange_malloc"] #[lang="exchange_malloc"]
@@ -66,13 +80,8 @@ pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
#[cfg(not(stage0), not(test))] #[cfg(not(stage0), not(test))]
#[lang="exchange_malloc"] #[lang="exchange_malloc"]
#[inline] #[inline]
pub unsafe fn exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char { pub unsafe fn exchange_malloc(align: u32, size: uintptr_t) -> *c_char {
let td = td as *TyDesc; let total_size = get_box_size(size as uint, align as uint);
let size = size as uint;
assert!(td.is_not_null());
let total_size = get_box_size(size, (*td).align);
malloc_raw(total_size as uint) as *c_char malloc_raw(total_size as uint) as *c_char
} }

View File

@@ -22,12 +22,14 @@ use iterator::{FromIterator, Iterator, IteratorUtil};
use iter::FromIter; use iter::FromIter;
use kinds::Copy; use kinds::Copy;
use libc; use libc;
use libc::c_void;
use num::Zero; use num::Zero;
use ops::Add; use ops::Add;
use option::{None, Option, Some}; use option::{None, Option, Some};
use ptr::to_unsafe_ptr; use ptr::to_unsafe_ptr;
use ptr; use ptr;
use ptr::RawPtr; use ptr::RawPtr;
use rt::global_heap::realloc_raw;
use sys; use sys;
use sys::size_of; use sys::size_of;
use uint; use uint;
@@ -52,12 +54,6 @@ pub mod rustrt {
#[abi = "cdecl"] #[abi = "cdecl"]
pub extern { pub extern {
// These names are terrible. reserve_shared applies
// to ~[] and reserve_shared_actual applies to @[].
#[fast_ffi]
unsafe fn vec_reserve_shared(t: *TyDesc,
v: **raw::VecRepr,
n: libc::size_t);
#[fast_ffi] #[fast_ffi]
unsafe fn vec_reserve_shared_actual(t: *TyDesc, unsafe fn vec_reserve_shared_actual(t: *TyDesc,
v: **raw::VecRepr, v: **raw::VecRepr,
@@ -1248,13 +1244,16 @@ impl<T> OwnedVector<T> for ~[T] {
use managed; use managed;
if self.capacity() < n { if self.capacity() < n {
unsafe { unsafe {
let ptr: **raw::VecRepr = cast::transmute(self); let ptr: *mut *mut raw::VecRepr = cast::transmute(self);
let td = get_tydesc::<T>(); let td = get_tydesc::<T>();
if ((**ptr).box_header.ref_count == if ((**ptr).box_header.ref_count ==
managed::raw::RC_MANAGED_UNIQUE) { managed::raw::RC_MANAGED_UNIQUE) {
rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t); rustrt::vec_reserve_shared_actual(td, ptr as **raw::VecRepr, n as libc::size_t);
} else { } else {
rustrt::vec_reserve_shared(td, ptr, n as libc::size_t); let alloc = n * sys::nonzero_size_of::<T>();
*ptr = realloc_raw(*ptr as *mut c_void, alloc + size_of::<raw::VecRepr>())
as *mut raw::VecRepr;
(**ptr).unboxed.alloc = alloc;
} }
} }
} }
@@ -1276,12 +1275,15 @@ impl<T> OwnedVector<T> for ~[T] {
// Only make the (slow) call into the runtime if we have to // Only make the (slow) call into the runtime if we have to
if self.capacity() < n { if self.capacity() < n {
unsafe { unsafe {
let ptr: **raw::VecRepr = cast::transmute(self); let ptr: *mut *mut raw::VecRepr = cast::transmute(self);
let td = get_tydesc::<T>(); let td = get_tydesc::<T>();
if contains_managed::<T>() { if contains_managed::<T>() {
rustrt::vec_reserve_shared_actual(td, ptr, n as libc::size_t); rustrt::vec_reserve_shared_actual(td, ptr as **raw::VecRepr, n as libc::size_t);
} else { } else {
rustrt::vec_reserve_shared(td, ptr, n as libc::size_t); let alloc = n * sys::nonzero_size_of::<T>();
*ptr = realloc_raw(*ptr as *mut c_void, alloc + size_of::<raw::VecRepr>())
as *mut raw::VecRepr;
(**ptr).unboxed.alloc = alloc;
} }
} }
} }

View File

@@ -75,13 +75,6 @@ vec_reserve_shared_actual(type_desc* ty, rust_vec_box** vp,
reserve_vec_exact_shared(task, vp, n_elts * ty->size); reserve_vec_exact_shared(task, vp, n_elts * ty->size);
} }
// This is completely misnamed.
extern "C" CDECL void
vec_reserve_shared(type_desc* ty, rust_vec_box** vp,
size_t n_elts) {
reserve_vec_exact(vp, n_elts * ty->size);
}
extern "C" CDECL size_t extern "C" CDECL size_t
rand_seed_size() { rand_seed_size() {
return rng_seed_size(); return rng_seed_size();

View File

@@ -55,7 +55,6 @@ rust_get_c_stack
rust_log_str rust_log_str
start_task start_task
vec_reserve_shared_actual vec_reserve_shared_actual
vec_reserve_shared
task_clear_event_reject task_clear_event_reject
task_wait_event task_wait_event
task_signal_event task_signal_event