Skip to content

Commit

Permalink
Fix Global.realloc by changing to Global.grow
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Apr 6, 2020
1 parent 6cceae6 commit 1d517f8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/vec-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
ptr: Unique<T>,
Expand Down Expand Up @@ -39,9 +47,11 @@ impl<T> RawVec<T> {
} else {
let new_cap = 2 * self.cap;
let c: NonNull<T> = self.ptr.into();
let ptr = Global.realloc(c.cast(),
Layout::array::<T>(self.cap).unwrap(),
Layout::array::<T>(new_cap).unwrap().size());
let ptr = Global.grow(c.cast(),
Layout::array::<T>(self.cap).unwrap(),
Layout::array::<T>(new_cap).unwrap().size(),
ReallocPlacement::MayMove,
AllocInit::Uninitialized);
(new_cap, ptr)
};

Expand All @@ -52,7 +62,7 @@ impl<T> RawVec<T> {
mem::align_of::<T>(),
))
}
let (ptr, _) = ptr.unwrap();
let ptr = ptr.unwrap().ptr;

self.ptr = Unique::new_unchecked(ptr.as_ptr() as *mut _);
self.cap = new_cap;
Expand Down

0 comments on commit 1d517f8

Please sign in to comment.