about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbcoopers <coopersmithbrian@gmail.com>2015-03-30 10:35:04 -0400
committerbcoopers <coopersmithbrian@gmail.com>2015-03-30 10:35:04 -0400
commitf8493d0660d5ad92a0c244cd0fed0d72b9630ebb (patch)
treed4b16e0a39e0a1dc0231984ff2aada3602ca6d09
parent4f06cedd0d83e0f6418ab681f6174c2b32e7aca0 (diff)
downloadrust-f8493d0660d5ad92a0c244cd0fed0d72b9630ebb.tar.gz
rust-f8493d0660d5ad92a0c244cd0fed0d72b9630ebb.zip
Change max size to isize
-rw-r--r--src/libcollections/vec.rs22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index aff26587de3..0a73679a7e6 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -65,10 +65,14 @@ use core::ops;
 use core::ptr;
 use core::ptr::Unique;
 use core::slice;
+use core::isize;
 use core::usize;
 
 use borrow::{Cow, IntoCow};
 
+// FIXME- fix places which assume the max vector allowed has memory usize::MAX.
+static MAX_MEMORY_SIZE: usize = isize::MAX as usize;
+
 /// A growable list type, written `Vec<T>` but pronounced 'vector.'
 ///
 /// # Examples
@@ -305,13 +309,15 @@ impl<T> Vec<T> {
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn reserve(&mut self, additional: usize) {
         if self.cap - self.len < additional {
-            let err_msg = "Vec::reserve: `usize` overflow";
+            let err_msg = "Vec::reserve: `isize` overflow";
 
             let new_min_cap = self.len.checked_add(additional).expect(err_msg);
-            match new_min_cap.checked_next_power_of_two() {
-                None => self.grow_capacity(usize::MAX),
-                Some(x) => self.grow_capacity(x),
-            }
+            if new_min_cap > MAX_MEMORY_SIZE { panic!(err_msg) }
+            self.grow_capacity(match new_min_cap.checked_next_power_of_two() {
+                Some(x) if x > MAX_MEMORY_SIZE => MAX_MEMORY_SIZE,
+                None => MAX_MEMORY_SIZE,
+                Some(x) => x,
+            });
         }
     }
 
@@ -642,10 +648,10 @@ impl<T> Vec<T> {
         #[inline(never)]
         fn resize<T>(vec: &mut Vec<T>) {
             let old_size = vec.cap * mem::size_of::<T>();
-            if old_size == usize::MAX { panic!("capacity overflow") }
+            if old_size >= MAX_MEMORY_SIZE { panic!("capacity overflow") }
             let mut size = max(old_size, 2 * mem::size_of::<T>()) * 2;
-            if old_size > size {
-                size = usize::MAX;
+            if old_size > size || size > MAX_MEMORY_SIZE {
+                size = MAX_MEMORY_SIZE;
             }
             unsafe {
                 let ptr = alloc_or_realloc(*vec.ptr, old_size, size);