From a2d606f49c9a140dd38dbd1c6456d28144ed2705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Mon, 6 Apr 2020 01:36:21 +0200 Subject: [PATCH 1/3] Use isize::MAX directly on type instead of module --- src/vec-alloc.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vec-alloc.md b/src/vec-alloc.md index 694059d5..b19de4ab 100644 --- a/src/vec-alloc.md +++ b/src/vec-alloc.md @@ -182,7 +182,7 @@ fn grow(&mut self) { // we need to make. We lose the ability to allocate e.g. 2/3rds of // the address space with a single Vec of i16's on 32-bit though. // Alas, poor Yorick -- I knew him, Horatio. - assert!(old_num_bytes <= (::std::isize::MAX as usize) / 2, + assert!(old_num_bytes <= (isize::MAX as usize) / 2, "capacity overflow"); let new_num_bytes = old_num_bytes * 2; From 6cceae61c90a2d17743729fc337be3f6713fc89b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Mon, 6 Apr 2020 01:55:21 +0200 Subject: [PATCH 2/3] Fix usage of Global.alloc --- src/vec-final.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vec-final.md b/src/vec-final.md index a858547e..0f7b6b99 100644 --- a/src/vec-final.md +++ b/src/vec-final.md @@ -9,7 +9,7 @@ use std::ptr::{Unique, NonNull, self}; use std::mem; use std::ops::{Deref, DerefMut}; use std::marker::PhantomData; -use std::alloc::{AllocRef, GlobalAlloc, Layout, Global, handle_alloc_error}; +use std::alloc::{AllocInit, AllocRef, GlobalAlloc, Layout, Global, handle_alloc_error}; struct RawVec { ptr: Unique, @@ -34,7 +34,7 @@ impl RawVec { assert!(elem_size != 0, "capacity overflow"); let (new_cap, ptr) = if self.cap == 0 { - let ptr = Global.alloc(Layout::array::(1).unwrap()); + let ptr = Global.alloc(Layout::array::(1).unwrap(), AllocInit::Uninitialized); (1, ptr) } else { let new_cap = 2 * self.cap; From 1d517f87dc15990aac053fc6a60610fa7e8b45de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Mon, 6 Apr 2020 02:06:52 +0200 Subject: [PATCH 3/3] Fix Global.realloc by changing to Global.grow --- src/vec-final.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/vec-final.md b/src/vec-final.md index 0f7b6b99..0c3f042d 100644 --- a/src/vec-final.md +++ b/src/vec-final.md @@ -9,7 +9,15 @@ use std::ptr::{Unique, NonNull, self}; use std::mem; use std::ops::{Deref, DerefMut}; use std::marker::PhantomData; -use std::alloc::{AllocInit, AllocRef, GlobalAlloc, Layout, Global, handle_alloc_error}; +use std::alloc::{ + AllocInit, + AllocRef, + Global, + GlobalAlloc, + Layout, + ReallocPlacement, + handle_alloc_error +}; struct RawVec { ptr: Unique, @@ -39,9 +47,11 @@ impl RawVec { } else { let new_cap = 2 * self.cap; let c: NonNull = self.ptr.into(); - let ptr = Global.realloc(c.cast(), - Layout::array::(self.cap).unwrap(), - Layout::array::(new_cap).unwrap().size()); + let ptr = Global.grow(c.cast(), + Layout::array::(self.cap).unwrap(), + Layout::array::(new_cap).unwrap().size(), + ReallocPlacement::MayMove, + AllocInit::Uninitialized); (new_cap, ptr) }; @@ -52,7 +62,7 @@ impl RawVec { mem::align_of::(), )) } - let (ptr, _) = ptr.unwrap(); + let ptr = ptr.unwrap().ptr; self.ptr = Unique::new_unchecked(ptr.as_ptr() as *mut _); self.cap = new_cap;