From 7559feb41443385b30f377430a0e94537c2f1214 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] 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..4ec69011 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;