diff options
| author | bors <bors@rust-lang.org> | 2013-07-15 21:01:20 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-07-15 21:01:20 -0700 |
| commit | 274e7a4e4999fe4e59a8ab0d71555f7f3eea3d6f (patch) | |
| tree | 8ebad07a9b590b0e627ebb323423072bbb136f6e /src/libstd | |
| parent | 47ba4583dbf234c4a080496715700c0878472a78 (diff) | |
| parent | e118555ce67aadb0a58039b3e74f44a43b210528 (diff) | |
| download | rust-274e7a4e4999fe4e59a8ab0d71555f7f3eea3d6f.tar.gz rust-274e7a4e4999fe4e59a8ab0d71555f7f3eea3d6f.zip | |
auto merge of #7816 : thestinger/rust/header, r=huonw
Note that the headers are still on `~[T]` when `T` is managed. This is continued from #7605, which removed all the code relying on the headers and removed them from `~T` for non-managed `T`.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/reflect.rs | 8 | ||||
| -rw-r--r-- | src/libstd/repr.rs | 8 | ||||
| -rw-r--r-- | src/libstd/rt/global_heap.rs | 8 | ||||
| -rw-r--r-- | src/libstd/str.rs | 15 | ||||
| -rw-r--r-- | src/libstd/unstable/intrinsics.rs | 1 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 106 |
6 files changed, 129 insertions, 17 deletions
diff --git a/src/libstd/reflect.rs b/src/libstd/reflect.rs index 9075133b086..fd16f4e5c69 100644 --- a/src/libstd/reflect.rs +++ b/src/libstd/reflect.rs @@ -297,6 +297,14 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> { true } + #[cfg(not(stage0))] + fn visit_evec_uniq_managed(&self, mtbl: uint, inner: *TyDesc) -> bool { + self.align_to::<~[@u8]>(); + if ! self.inner.visit_evec_uniq_managed(mtbl, inner) { return false; } + self.bump_past::<~[@u8]>(); + true + } + fn visit_evec_slice(&self, mtbl: uint, inner: *TyDesc) -> bool { self.align_to::<&'static [u8]>(); if ! self.inner.visit_evec_slice(mtbl, inner) { return false; } diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs index 79d11ddca61..7707e83a9ce 100644 --- a/src/libstd/repr.rs +++ b/src/libstd/repr.rs @@ -353,6 +353,14 @@ impl TyVisitor for ReprVisitor { } fn visit_evec_uniq(&self, mtbl: uint, inner: *TyDesc) -> bool { + do self.get::<&UnboxedVecRepr> |b| { + self.writer.write_char('~'); + self.write_unboxed_vec_repr(mtbl, *b, inner); + } + } + + #[cfg(not(stage0))] + fn visit_evec_uniq_managed(&self, mtbl: uint, inner: *TyDesc) -> bool { do self.get::<&VecRepr> |b| { self.writer.write_char('~'); self.write_unboxed_vec_repr(mtbl, &b.unboxed, inner); diff --git a/src/libstd/rt/global_heap.rs b/src/libstd/rt/global_heap.rs index 68e57dd4940..d0b268ace54 100644 --- a/src/libstd/rt/global_heap.rs +++ b/src/libstd/rt/global_heap.rs @@ -84,14 +84,6 @@ pub unsafe fn exchange_malloc(size: uintptr_t) -> *c_char { malloc_raw(size as uint) as *c_char } -#[cfg(not(test))] -#[lang="vector_exchange_malloc"] -#[inline] -pub unsafe fn vector_exchange_malloc(align: u32, size: uintptr_t) -> *c_char { - let total_size = get_box_size(size as uint, align as uint); - malloc_raw(total_size as uint) as *c_char -} - // FIXME: #7496 #[cfg(not(test))] #[lang="closure_exchange_malloc"] diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 1d8a2d404a7..bc3015685bb 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1003,6 +1003,7 @@ pub mod raw { /// Sets the length of the string and adds the null terminator #[inline] + #[cfg(stage0)] pub unsafe fn set_len(v: &mut ~str, new_len: uint) { let v: **mut vec::raw::VecRepr = cast::transmute(v); let repr: *mut vec::raw::VecRepr = *v; @@ -1012,6 +1013,18 @@ pub mod raw { *null = 0u8; } + /// Sets the length of the string and adds the null terminator + #[inline] + #[cfg(not(stage0))] + pub unsafe fn set_len(v: &mut ~str, new_len: uint) { + let v: **mut vec::UnboxedVecRepr = cast::transmute(v); + let repr: *mut vec::UnboxedVecRepr = *v; + (*repr).fill = new_len + 1u; + let null = ptr::mut_offset(cast::transmute(&((*repr).data)), + new_len); + *null = 0u8; + } + #[test] fn test_from_buf_len() { unsafe { @@ -2027,7 +2040,7 @@ impl NullTerminatedStr for @str { */ #[inline] fn as_bytes_with_null<'a>(&'a self) -> &'a [u8] { - let ptr: &'a ~[u8] = unsafe { ::cast::transmute(self) }; + let ptr: &'a @[u8] = unsafe { ::cast::transmute(self) }; let slice: &'a [u8] = *ptr; slice } diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs index ce5ccf2401d..015ecd67c83 100644 --- a/src/libstd/unstable/intrinsics.rs +++ b/src/libstd/unstable/intrinsics.rs @@ -99,6 +99,7 @@ pub trait TyVisitor { fn visit_unboxed_vec(&self, mtbl: uint, inner: *TyDesc) -> bool; fn visit_evec_box(&self, mtbl: uint, inner: *TyDesc) -> bool; fn visit_evec_uniq(&self, mtbl: uint, inner: *TyDesc) -> bool; + fn visit_evec_uniq_managed(&self, mtbl: uint, inner: *TyDesc) -> bool; fn visit_evec_slice(&self, mtbl: uint, inner: *TyDesc) -> bool; fn visit_evec_fixed(&self, n: uint, sz: uint, align: uint, mtbl: uint, inner: *TyDesc) -> bool; diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 8b27ce4235e..c728d4a60f1 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -30,6 +30,7 @@ use ptr::RawPtr; use rt::global_heap::malloc_raw; use rt::global_heap::realloc_raw; use sys; +use sys::size_of; use uint; use unstable::intrinsics; #[cfg(stage0)] @@ -108,9 +109,9 @@ pub fn with_capacity<T>(capacity: uint) -> ~[T] { vec } else { let alloc = capacity * sys::nonzero_size_of::<T>(); - let ptr = malloc_raw(alloc + sys::size_of::<raw::VecRepr>()) as *mut raw::VecRepr; - (*ptr).unboxed.alloc = alloc; - (*ptr).unboxed.fill = 0; + let ptr = malloc_raw(alloc + sys::size_of::<UnboxedVecRepr>()) as *mut UnboxedVecRepr; + (*ptr).alloc = alloc; + (*ptr).fill = 0; cast::transmute(ptr) } } @@ -1150,7 +1151,7 @@ impl<T> OwnedVector<T> for ~[T] { ::at_vec::raw::reserve_raw(td, ptr, n); } else { let alloc = n * sys::nonzero_size_of::<T>(); - *ptr = realloc_raw(*ptr as *mut c_void, alloc + sys::size_of::<raw::VecRepr>()) + *ptr = realloc_raw(*ptr as *mut c_void, alloc + size_of::<raw::VecRepr>()) as *mut raw::VecRepr; (**ptr).unboxed.alloc = alloc; } @@ -1173,19 +1174,20 @@ impl<T> OwnedVector<T> for ~[T] { // Only make the (slow) call into the runtime if we have to if self.capacity() < n { unsafe { - let ptr: *mut *mut raw::VecRepr = cast::transmute(self); let td = get_tydesc::<T>(); if contains_managed::<T>() { + let ptr: *mut *mut raw::VecRepr = cast::transmute(self); ::at_vec::raw::reserve_raw(td, ptr, n); } else { + let ptr: *mut *mut UnboxedVecRepr = cast::transmute(self); let alloc = n * sys::nonzero_size_of::<T>(); - let size = alloc + sys::size_of::<raw::VecRepr>(); + let size = alloc + sys::size_of::<UnboxedVecRepr>(); if alloc / sys::nonzero_size_of::<T>() != n || size < alloc { fail!("vector size is too large: %u", n); } *ptr = realloc_raw(*ptr as *mut c_void, size) - as *mut raw::VecRepr; - (**ptr).unboxed.alloc = alloc; + as *mut UnboxedVecRepr; + (**ptr).alloc = alloc; } } } @@ -1211,6 +1213,7 @@ impl<T> OwnedVector<T> for ~[T] { /// Returns the number of elements the vector can hold without reallocating. #[inline] + #[cfg(stage0)] fn capacity(&self) -> uint { unsafe { let repr: **raw::VecRepr = transmute(self); @@ -1218,8 +1221,24 @@ impl<T> OwnedVector<T> for ~[T] { } } + /// Returns the number of elements the vector can hold without reallocating. + #[inline] + #[cfg(not(stage0))] + fn capacity(&self) -> uint { + unsafe { + if contains_managed::<T>() { + let repr: **raw::VecRepr = transmute(self); + (**repr).unboxed.alloc / sys::nonzero_size_of::<T>() + } else { + let repr: **UnboxedVecRepr = transmute(self); + (**repr).alloc / sys::nonzero_size_of::<T>() + } + } + } + /// Append an element to a vector #[inline] + #[cfg(stage0)] fn push(&mut self, t: T) { unsafe { let repr: **raw::VecRepr = transmute(&mut *self); @@ -1233,8 +1252,36 @@ impl<T> OwnedVector<T> for ~[T] { } } + /// Append an element to a vector + #[inline] + #[cfg(not(stage0))] + fn push(&mut self, t: T) { + unsafe { + if contains_managed::<T>() { + let repr: **raw::VecRepr = transmute(&mut *self); + let fill = (**repr).unboxed.fill; + if (**repr).unboxed.alloc <= fill { + let new_len = self.len() + 1; + self.reserve_at_least(new_len); + } + + self.push_fast(t); + } else { + let repr: **UnboxedVecRepr = transmute(&mut *self); + let fill = (**repr).fill; + if (**repr).alloc <= fill { + let new_len = self.len() + 1; + self.reserve_at_least(new_len); + } + + self.push_fast(t); + } + } + } + // This doesn't bother to make sure we have space. #[inline] // really pretty please + #[cfg(stage0)] unsafe fn push_fast(&mut self, t: T) { let repr: **mut raw::VecRepr = transmute(self); let fill = (**repr).unboxed.fill; @@ -1244,6 +1291,27 @@ impl<T> OwnedVector<T> for ~[T] { intrinsics::move_val_init(&mut(*p), t); } + // This doesn't bother to make sure we have space. + #[inline] // really pretty please + #[cfg(not(stage0))] + unsafe fn push_fast(&mut self, t: T) { + if contains_managed::<T>() { + let repr: **mut raw::VecRepr = transmute(self); + let fill = (**repr).unboxed.fill; + (**repr).unboxed.fill += sys::nonzero_size_of::<T>(); + let p = to_unsafe_ptr(&((**repr).unboxed.data)); + let p = ptr::offset(p, fill) as *mut T; + intrinsics::move_val_init(&mut(*p), t); + } else { + let repr: **mut UnboxedVecRepr = transmute(self); + let fill = (**repr).fill; + (**repr).fill += sys::nonzero_size_of::<T>(); + let p = to_unsafe_ptr(&((**repr).data)); + let p = ptr::offset(p, fill) as *mut T; + intrinsics::move_val_init(&mut(*p), t); + } + } + /// Takes ownership of the vector `rhs`, moving all elements into /// the current vector. This does not copy any elements, and it is /// illegal to use the `rhs` vector after calling this method @@ -1834,6 +1902,8 @@ pub mod raw { use unstable::intrinsics; use vec::{UnboxedVecRepr, with_capacity, ImmutableVector, MutableVector}; use util; + #[cfg(not(stage0))] + use unstable::intrinsics::contains_managed; /// The internal representation of a (boxed) vector #[allow(missing_doc)] @@ -1858,12 +1928,32 @@ pub mod raw { * the vector is actually the specified size. */ #[inline] + #[cfg(stage0)] pub unsafe fn set_len<T>(v: &mut ~[T], new_len: uint) { let repr: **mut VecRepr = transmute(v); (**repr).unboxed.fill = new_len * sys::nonzero_size_of::<T>(); } /** + * Sets the length of a vector + * + * This will explicitly set the size of the vector, without actually + * modifing its buffers, so it is up to the caller to ensure that + * the vector is actually the specified size. + */ + #[inline] + #[cfg(not(stage0))] + pub unsafe fn set_len<T>(v: &mut ~[T], new_len: uint) { + if contains_managed::<T>() { + let repr: **mut VecRepr = transmute(v); + (**repr).unboxed.fill = new_len * sys::nonzero_size_of::<T>(); + } else { + let repr: **mut UnboxedVecRepr = transmute(v); + (**repr).fill = new_len * sys::nonzero_size_of::<T>(); + } + } + + /** * Returns an unsafe pointer to the vector's buffer * * The caller must ensure that the vector outlives the pointer this |
