Skip to content

add new vector representation as a library #11682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/guide-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<T: Send> Drop for Unique<T> {
// We need to move the object out of the box, so that
// the destructor is called (at the end of this scope.)
ptr::replace_ptr(self.ptr, x);
free(self.ptr as *c_void)
free(self.ptr as *mut c_void)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/c_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod tests {
let mem = malloc_raw(n);

CVec::new_with_dtor(mem as *mut u8, n,
proc() { libc::free(mem as *c_void); })
proc() { libc::free(mem as *mut c_void); })
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libextra/flate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
assert!(res as int != 0);
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
libc::free(res);
libc::free(res as *mut c_void);
out
}
}
Expand All @@ -76,7 +76,7 @@ fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> ~[u8] {
assert!(res as int != 0);
let out = vec::raw::from_buf_raw(res as *u8,
outsz as uint);
libc::free(res);
libc::free(res as *mut c_void);
out
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libnative/io/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,13 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
let p = Path::new(p);
let star = p.join("*");
as_utf16_p(star.as_str().unwrap(), |path_ptr| {
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint) as *c_void;
let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
let find_handle = FindFirstFileW(path_ptr, wfd_ptr as HANDLE);
if find_handle as libc::c_int != INVALID_HANDLE_VALUE {
let mut paths = ~[];
let mut more_files = 1 as libc::c_int;
while more_files != 0 {
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr);
let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr as *c_void);
if fp_buf as uint == 0 {
fail!("os::list_dir() failure: got null ptr from wfd");
}
Expand All @@ -567,7 +567,7 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
more_files = FindNextFileW(find_handle, wfd_ptr as HANDLE);
}
FindClose(find_handle);
free(wfd_ptr);
free(wfd_ptr as *mut c_void);
Ok(paths)
} else {
Err(super::last_error())
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@ impl TypeNames {
unsafe {
let s = llvm::LLVMTypeToString(ty.to_ref());
let ret = from_c_str(s);
free(s as *c_void);
free(s as *mut c_void);
ret
}
}
Expand All @@ -1850,7 +1850,7 @@ impl TypeNames {
unsafe {
let s = llvm::LLVMValueToString(val);
let ret = from_c_str(s);
free(s as *c_void);
free(s as *mut c_void);
ret
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustuv/uvll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
}

pub unsafe fn free_handle(v: *c_void) {
free(v)
free(v as *mut c_void)
}

pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
Expand All @@ -383,7 +383,7 @@ pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
}

pub unsafe fn free_req(v: *c_void) {
free(v)
free(v as *mut c_void)
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Drop for CString {
fn drop(&mut self) {
if self.owns_buffer_ {
unsafe {
libc::free(self.buf as *libc::c_void)
libc::free(self.buf as *mut libc::c_void)
}
}
}
Expand Down Expand Up @@ -459,7 +459,7 @@ mod tests {
#[test]
fn test_unwrap() {
let c_str = "hello".to_c_str();
unsafe { libc::free(c_str.unwrap() as *libc::c_void) }
unsafe { libc::free(c_str.unwrap() as *mut libc::c_void) }
}

#[test]
Expand Down
50 changes: 25 additions & 25 deletions src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ use rand::Rng;
use rand;
use uint;
use util::replace;
use vec::{ImmutableVector, MutableVector, OwnedVector};
use vec;
use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems};
use vec_ng;
use vec_ng::Vec;

static INITIAL_CAPACITY: uint = 32u; // 2^5

Expand All @@ -90,7 +91,7 @@ pub struct HashMap<K,V> {
priv k1: u64,
priv resize_at: uint,
priv size: uint,
priv buckets: ~[Option<Bucket<K, V>>],
priv buckets: Vec<Option<Bucket<K, V>>>
}

// We could rewrite FoundEntry to have type Option<&Bucket<K, V>>
Expand Down Expand Up @@ -151,7 +152,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
-> SearchResult {
let mut ret = TableFull;
self.bucket_sequence(hash, |i| {
match self.buckets[i] {
match self.buckets.as_slice()[i] {
Some(ref bkt) if bkt.hash == hash && *k == bkt.key => {
ret = FoundEntry(i); false
},
Expand All @@ -169,7 +170,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
-> SearchResult {
let mut ret = TableFull;
self.bucket_sequence(hash, |i| {
match self.buckets[i] {
match self.buckets.as_slice()[i] {
Some(ref bkt) if bkt.hash == hash && k.equiv(&bkt.key) => {
ret = FoundEntry(i); false
},
Expand All @@ -194,7 +195,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
self.resize_at = resize_at(new_capacity);

let old_buckets = replace(&mut self.buckets,
vec::from_fn(new_capacity, |_| None));
Vec::from_fn(new_capacity, |_| None));

self.size = 0;
for bucket in old_buckets.move_iter() {
Expand All @@ -213,15 +214,15 @@ impl<K:Hash + Eq,V> HashMap<K, V> {

#[inline]
fn value_for_bucket<'a>(&'a self, idx: uint) -> &'a V {
match self.buckets[idx] {
match self.buckets.as_slice()[idx] {
Some(ref bkt) => &bkt.value,
None => fail!("HashMap::find: internal logic error"),
}
}

#[inline]
fn mut_value_for_bucket<'a>(&'a mut self, idx: uint) -> &'a mut V {
match self.buckets[idx] {
match self.buckets.as_mut_slice()[idx] {
Some(ref mut bkt) => &mut bkt.value,
None => unreachable!()
}
Expand All @@ -234,13 +235,12 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
match self.bucket_for_key_with_hash(hash, &k) {
TableFull => { fail!("Internal logic error"); }
FoundHole(idx) => {
self.buckets[idx] = Some(Bucket{hash: hash, key: k,
value: v});
self.buckets.as_mut_slice()[idx] = Some(Bucket{hash: hash, key: k, value: v});
self.size += 1;
None
}
FoundEntry(idx) => {
match self.buckets[idx] {
match self.buckets.as_mut_slice()[idx] {
None => { fail!("insert_internal: Internal logic error") }
Some(ref mut b) => {
b.hash = hash;
Expand Down Expand Up @@ -273,16 +273,16 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
};

let len_buckets = self.buckets.len();
let bucket = self.buckets[idx].take();
let bucket = self.buckets.as_mut_slice()[idx].take();

let value = bucket.map(|bucket| bucket.value);

/* re-inserting buckets may cause changes in size, so remember
what our new size is ahead of time before we start insertions */
let size = self.size - 1;
idx = self.next_bucket(idx, len_buckets);
while self.buckets[idx].is_some() {
let bucket = self.buckets[idx].take();
while self.buckets.as_slice()[idx].is_some() {
let bucket = self.buckets.as_mut_slice()[idx].take();
self.insert_opt_bucket(bucket);
idx = self.next_bucket(idx, len_buckets);
}
Expand All @@ -300,7 +300,7 @@ impl<K:Hash + Eq,V> Container for HashMap<K, V> {
impl<K:Hash + Eq,V> Mutable for HashMap<K, V> {
/// Clear the map, removing all key-value pairs.
fn clear(&mut self) {
for bkt in self.buckets.mut_iter() {
for bkt in self.buckets.as_mut_slice().mut_iter() {
*bkt = None;
}
self.size = 0;
Expand Down Expand Up @@ -380,7 +380,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
k0: k0, k1: k1,
resize_at: resize_at(cap),
size: 0,
buckets: vec::from_fn(cap, |_| None)
buckets: Vec::from_fn(cap, |_| None)
}
}

Expand Down Expand Up @@ -455,7 +455,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
FoundEntry(idx) => { found(&k, self.mut_value_for_bucket(idx), a); idx }
FoundHole(idx) => {
let v = not_found(&k, a);
self.buckets[idx] = Some(Bucket{hash: hash, key: k, value: v});
self.buckets.as_mut_slice()[idx] = Some(Bucket{hash: hash, key: k, value: v});
self.size += 1;
idx
}
Expand Down Expand Up @@ -541,14 +541,14 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
/// An iterator visiting all key-value pairs in arbitrary order.
/// Iterator element type is (&'a K, &'a V).
pub fn iter<'a>(&'a self) -> Entries<'a, K, V> {
Entries { iter: self.buckets.iter() }
Entries { iter: self.buckets.as_slice().iter() }
}

/// An iterator visiting all key-value pairs in arbitrary order,
/// with mutable references to the values.
/// Iterator element type is (&'a K, &'a mut V).
pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
MutEntries { iter: self.buckets.mut_iter() }
MutEntries { iter: self.buckets.as_mut_slice().mut_iter() }
}

/// Creates a consuming iterator, that is, one that moves each key-value
Expand Down Expand Up @@ -599,17 +599,17 @@ impl<K:Hash + Eq + Clone,V:Clone> Clone for HashMap<K,V> {
/// HashMap iterator
#[deriving(Clone)]
pub struct Entries<'a, K, V> {
priv iter: vec::Items<'a, Option<Bucket<K, V>>>,
priv iter: Items<'a, Option<Bucket<K, V>>>,
}

/// HashMap mutable values iterator
pub struct MutEntries<'a, K, V> {
priv iter: vec::MutItems<'a, Option<Bucket<K, V>>>,
priv iter: MutItems<'a, Option<Bucket<K, V>>>,
}

/// HashMap move iterator
pub struct MoveEntries<K, V> {
priv iter: vec::MoveItems<Option<Bucket<K, V>>>,
priv iter: vec_ng::MoveItems<Option<Bucket<K, V>>>,
}

/// HashMap keys iterator
Expand All @@ -623,12 +623,12 @@ pub type Values<'a, K, V> =
/// HashSet iterator
#[deriving(Clone)]
pub struct SetItems<'a, K> {
priv iter: vec::Items<'a, Option<Bucket<K, ()>>>,
priv iter: Items<'a, Option<Bucket<K, ()>>>,
}

/// HashSet move iterator
pub struct SetMoveItems<K> {
priv iter: vec::MoveItems<Option<Bucket<K, ()>>>,
priv iter: vec_ng::MoveItems<Option<Bucket<K, ()>>>,
}

impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> {
Expand Down Expand Up @@ -807,7 +807,7 @@ impl<T:Hash + Eq> HashSet<T> {
/// An iterator visiting all elements in arbitrary order.
/// Iterator element type is &'a T.
pub fn iter<'a>(&'a self) -> SetItems<'a, T> {
SetItems { iter: self.map.buckets.iter() }
SetItems { iter: self.map.buckets.as_slice().iter() }
}

/// Creates a consuming iterator, that is, one that moves each value out
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub mod char;
pub mod tuple;

pub mod vec;
pub mod vec_ng;
pub mod at_vec;
pub mod str;

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3225,7 +3225,7 @@ pub mod funcs {
pub fn calloc(nobj: size_t, size: size_t) -> *c_void;
pub fn malloc(size: size_t) -> *mut c_void;
pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
pub fn free(p: *c_void);
pub fn free(p: *mut c_void);
pub fn exit(status: c_int) -> !;
// Omitted: atexit.
pub fn system(s: *c_char) -> c_int;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/rt/global_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
// `realloc(ptr, 0)` may allocate, but it may also return a null pointer
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/realloc.html
if size == 0 {
free(ptr as *c_void);
free(ptr as *mut c_void);
mut_null()
} else {
let p = realloc(ptr as *mut c_void, size as size_t);
Expand Down Expand Up @@ -107,7 +107,7 @@ pub unsafe fn exchange_free_(ptr: *u8) {

#[inline]
pub unsafe fn exchange_free(ptr: *u8) {
free(ptr as *c_void);
free(ptr as *mut c_void);
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl<T: Send> Buffer<T> {
impl<T: Send> Drop for Buffer<T> {
fn drop(&mut self) {
// It is assumed that all buffers are empty on drop.
unsafe { libc::free(self.storage as *libc::c_void) }
unsafe { libc::free(self.storage as *mut libc::c_void) }
}
}

Expand Down
Loading