Skip to content

Commit 1d517f8

Browse files
committed
Fix Global.realloc by changing to Global.grow
1 parent 6cceae6 commit 1d517f8

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/vec-final.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ use std::ptr::{Unique, NonNull, self};
99
use std::mem;
1010
use std::ops::{Deref, DerefMut};
1111
use std::marker::PhantomData;
12-
use std::alloc::{AllocInit, AllocRef, GlobalAlloc, Layout, Global, handle_alloc_error};
12+
use std::alloc::{
13+
AllocInit,
14+
AllocRef,
15+
Global,
16+
GlobalAlloc,
17+
Layout,
18+
ReallocPlacement,
19+
handle_alloc_error
20+
};
1321

1422
struct RawVec<T> {
1523
ptr: Unique<T>,
@@ -39,9 +47,11 @@ impl<T> RawVec<T> {
3947
} else {
4048
let new_cap = 2 * self.cap;
4149
let c: NonNull<T> = self.ptr.into();
42-
let ptr = Global.realloc(c.cast(),
43-
Layout::array::<T>(self.cap).unwrap(),
44-
Layout::array::<T>(new_cap).unwrap().size());
50+
let ptr = Global.grow(c.cast(),
51+
Layout::array::<T>(self.cap).unwrap(),
52+
Layout::array::<T>(new_cap).unwrap().size(),
53+
ReallocPlacement::MayMove,
54+
AllocInit::Uninitialized);
4555
(new_cap, ptr)
4656
};
4757

@@ -52,7 +62,7 @@ impl<T> RawVec<T> {
5262
mem::align_of::<T>(),
5363
))
5464
}
55-
let (ptr, _) = ptr.unwrap();
65+
let ptr = ptr.unwrap().ptr;
5666

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

0 commit comments

Comments
 (0)