about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2018-04-04 17:19:16 +0200
committerSimon Sapin <simon.sapin@exyr.org>2018-04-12 22:53:13 +0200
commitc957e99b305ecee113442a7ce0edd6b565200ca9 (patch)
tree02c7f8f7f8f35577d74dfb057625a2a4dae717a7 /src/liballoc
parentb017742136a5d02b6ba0f2080e97d18a8bfeba4b (diff)
downloadrust-c957e99b305ecee113442a7ce0edd6b565200ca9.tar.gz
rust-c957e99b305ecee113442a7ce0edd6b565200ca9.zip
realloc with a new size only, not a full new layout.
Changing the alignment with realloc is not supported.
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/alloc.rs22
-rw-r--r--src/liballoc/heap.rs8
-rw-r--r--src/liballoc/raw_vec.rs17
3 files changed, 21 insertions, 26 deletions
diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs
index a7b5864016c..a6fc8d5004c 100644
--- a/src/liballoc/alloc.rs
+++ b/src/liballoc/alloc.rs
@@ -91,21 +91,17 @@ unsafe impl Alloc for Global {
     unsafe fn realloc(&mut self,
                       ptr: *mut u8,
                       layout: Layout,
-                      new_layout: Layout)
+                      new_size: usize)
                       -> Result<*mut u8, AllocErr>
     {
-        if layout.align() == new_layout.align() {
-            #[cfg(not(stage0))]
-            let ptr = __rust_realloc(ptr, layout.size(), layout.align(), new_layout.size());
-            #[cfg(stage0)]
-            let ptr = __rust_realloc(ptr, layout.size(), layout.align(),
-                                     new_layout.size(), new_layout.align(), &mut 0);
-
-            if !ptr.is_null() {
-                Ok(ptr)
-            } else {
-                Err(AllocErr)
-            }
+        #[cfg(not(stage0))]
+        let ptr = __rust_realloc(ptr, layout.size(), layout.align(), new_size);
+        #[cfg(stage0)]
+        let ptr = __rust_realloc(ptr, layout.size(), layout.align(),
+                                 new_size, layout.align(), &mut 0);
+
+        if !ptr.is_null() {
+            Ok(ptr)
         } else {
             Err(AllocErr)
         }
diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs
index 765fb8458d1..e79383331e1 100644
--- a/src/liballoc/heap.rs
+++ b/src/liballoc/heap.rs
@@ -64,7 +64,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
                       ptr: *mut u8,
                       layout: Layout,
                       new_layout: Layout) -> Result<*mut u8, AllocErr> {
-        CoreAlloc::realloc(self, ptr, layout, new_layout)
+        CoreAlloc::realloc(self, ptr, layout, new_layout.size())
     }
 
     unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
@@ -79,20 +79,20 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
                              ptr: *mut u8,
                              layout: Layout,
                              new_layout: Layout) -> Result<Excess, AllocErr> {
-        CoreAlloc::realloc_excess(self, ptr, layout, new_layout)
+        CoreAlloc::realloc_excess(self, ptr, layout, new_layout.size())
     }
 
     unsafe fn grow_in_place(&mut self,
                             ptr: *mut u8,
                             layout: Layout,
                             new_layout: Layout) -> Result<(), CannotReallocInPlace> {
-        CoreAlloc::grow_in_place(self, ptr, layout, new_layout)
+        CoreAlloc::grow_in_place(self, ptr, layout, new_layout.size())
     }
 
     unsafe fn shrink_in_place(&mut self,
                               ptr: *mut u8,
                               layout: Layout,
                               new_layout: Layout) -> Result<(), CannotReallocInPlace> {
-        CoreAlloc::shrink_in_place(self, ptr, layout, new_layout)
+        CoreAlloc::shrink_in_place(self, ptr, layout, new_layout.size())
     }
 }
diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs
index 18aaf1de08e..80b816878fb 100644
--- a/src/liballoc/raw_vec.rs
+++ b/src/liballoc/raw_vec.rs
@@ -309,11 +309,10 @@ impl<T, A: Alloc> RawVec<T, A> {
                     // `from_size_align_unchecked`.
                     let new_cap = 2 * self.cap;
                     let new_size = new_cap * elem_size;
-                    let new_layout = Layout::from_size_align_unchecked(new_size, cur.align());
                     alloc_guard(new_size).expect("capacity overflow");
                     let ptr_res = self.a.realloc(self.ptr.as_ptr() as *mut u8,
                                                  cur,
-                                                 new_layout);
+                                                 new_size);
                     match ptr_res {
                         Ok(ptr) => (new_cap, Unique::new_unchecked(ptr as *mut T)),
                         Err(_) => self.a.oom(),
@@ -371,8 +370,7 @@ impl<T, A: Alloc> RawVec<T, A> {
             let new_size = new_cap * elem_size;
             alloc_guard(new_size).expect("capacity overflow");
             let ptr = self.ptr() as *mut _;
-            let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
-            match self.a.grow_in_place(ptr, old_layout, new_layout) {
+            match self.a.grow_in_place(ptr, old_layout, new_size) {
                 Ok(_) => {
                     // We can't directly divide `size`.
                     self.cap = new_cap;
@@ -428,8 +426,9 @@ impl<T, A: Alloc> RawVec<T, A> {
 
             let res = match self.current_layout() {
                 Some(layout) => {
+                    debug_assert!(new_layout.align() == layout.align());
                     let old_ptr = self.ptr.as_ptr() as *mut u8;
-                    self.a.realloc(old_ptr, layout, new_layout)
+                    self.a.realloc(old_ptr, layout, new_layout.size())
                 }
                 None => self.a.alloc(new_layout),
             };
@@ -537,8 +536,9 @@ impl<T, A: Alloc> RawVec<T, A> {
 
             let res = match self.current_layout() {
                 Some(layout) => {
+                    debug_assert!(new_layout.align() == layout.align());
                     let old_ptr = self.ptr.as_ptr() as *mut u8;
-                    self.a.realloc(old_ptr, layout, new_layout)
+                    self.a.realloc(old_ptr, layout, new_layout.size())
                 }
                 None => self.a.alloc(new_layout),
             };
@@ -604,7 +604,7 @@ impl<T, A: Alloc> RawVec<T, A> {
             let new_layout = Layout::new::<T>().repeat(new_cap).unwrap().0;
             // FIXME: may crash and burn on over-reserve
             alloc_guard(new_layout.size()).expect("capacity overflow");
-            match self.a.grow_in_place(ptr, old_layout, new_layout) {
+            match self.a.grow_in_place(ptr, old_layout, new_layout.size()) {
                 Ok(_) => {
                     self.cap = new_cap;
                     true
@@ -664,10 +664,9 @@ impl<T, A: Alloc> RawVec<T, A> {
                 let new_size = elem_size * amount;
                 let align = mem::align_of::<T>();
                 let old_layout = Layout::from_size_align_unchecked(old_size, align);
-                let new_layout = Layout::from_size_align_unchecked(new_size, align);
                 match self.a.realloc(self.ptr.as_ptr() as *mut u8,
                                      old_layout,
-                                     new_layout) {
+                                     new_size) {
                     Ok(p) => self.ptr = Unique::new_unchecked(p as *mut T),
                     Err(_) => self.a.oom(),
                 }