Skip to content

Commit 3f9e3fd

Browse files
committed
use Excess in RawVec
1 parent 9b01ca8 commit 3f9e3fd

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/liballoc/raw_vec.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::ops::Drop;
1414
use core::ptr::{self, NonNull, Unique};
1515
use core::slice;
1616

17-
use alloc::{Alloc, Layout, Global, oom};
17+
use alloc::{Alloc, Layout, Global, oom, Excess};
1818
use alloc::CollectionAllocErr;
1919
use alloc::CollectionAllocErr::*;
2020
use boxed::Box;
@@ -92,17 +92,17 @@ impl<T, A: Alloc> RawVec<T, A> {
9292
alloc_guard(alloc_size).unwrap_or_else(|_| capacity_overflow());
9393

9494
// handles ZSTs and `cap = 0` alike
95-
let ptr = if alloc_size == 0 {
96-
NonNull::<T>::dangling().as_opaque()
95+
let (ptr, cap) = if alloc_size == 0 {
96+
(NonNull::<T>::dangling().as_opaque(), cap)
9797
} else {
9898
let align = mem::align_of::<T>();
9999
let result = if zeroed {
100-
a.alloc_zeroed(Layout::from_size_align(alloc_size, align).unwrap())
100+
a.alloc_zeroed_excess(Layout::from_size_align(alloc_size, align).unwrap())
101101
} else {
102-
a.alloc(Layout::from_size_align(alloc_size, align).unwrap())
102+
a.alloc_excess(Layout::from_size_align(alloc_size, align).unwrap())
103103
};
104104
match result {
105-
Ok(ptr) => ptr,
105+
Ok(Excess(ptr, alloc_size)) => (ptr, alloc_size / elem_size),
106106
Err(_) => oom(),
107107
}
108108
};
@@ -407,16 +407,17 @@ impl<T, A: Alloc> RawVec<T, A> {
407407

408408
alloc_guard(new_layout.size())?;
409409

410-
let res = match self.current_layout() {
410+
let Excess(ptr, alloc_size) = match self.current_layout() {
411411
Some(layout) => {
412412
debug_assert!(new_layout.align() == layout.align());
413-
self.a.realloc(NonNull::from(self.ptr).as_opaque(), layout, new_layout.size())
413+
self.a.realloc_excess(NonNull::from(self.ptr).as_opaque(),
414+
layout, new_layout.size())
414415
}
415-
None => self.a.alloc(new_layout),
416-
};
416+
None => self.a.alloc_excess(new_layout),
417+
}?;
417418

418-
self.ptr = res?.cast().into();
419-
self.cap = new_cap;
419+
self.ptr = ptr.cast().into();
420+
self.cap = alloc_size / mem::size_of::<T>();
420421

421422
Ok(())
422423
}
@@ -485,16 +486,16 @@ impl<T, A: Alloc> RawVec<T, A> {
485486
// FIXME: may crash and burn on over-reserve
486487
alloc_guard(new_layout.size())?;
487488

488-
let res = match self.current_layout() {
489+
let Excess(ptr, alloc_size) = match self.current_layout() {
489490
Some(layout) => {
490491
debug_assert!(new_layout.align() == layout.align());
491-
self.a.realloc(NonNull::from(self.ptr).as_opaque(), layout, new_layout.size())
492+
self.a.realloc_excess(NonNull::from(self.ptr).as_opaque(),
493+
layout, new_layout.size())
492494
}
493-
None => self.a.alloc(new_layout),
494-
};
495-
496-
self.ptr = res?.cast().into();
497-
self.cap = new_cap;
495+
None => self.a.alloc_excess(new_layout),
496+
}?;
497+
self.ptr = ptr.cast().into();
498+
self.cap = alloc_size / mem::size_of::<T>();
498499

499500
Ok(())
500501
}
@@ -604,11 +605,11 @@ impl<T, A: Alloc> RawVec<T, A> {
604605
let new_layout = Layout::new::<T>().repeat(new_cap).unwrap().0;
605606
// FIXME: may crash and burn on over-reserve
606607
alloc_guard(new_layout.size()).unwrap_or_else(|_| capacity_overflow());
607-
match self.a.grow_in_place(
608+
match self.a.grow_in_place_excess(
608609
NonNull::from(self.ptr).as_opaque(), old_layout, new_layout.size(),
609610
) {
610-
Ok(_) => {
611-
self.cap = new_cap;
611+
Ok(Excess(_, alloc_size)) => {
612+
self.cap = alloc_size / mem::size_of::<T>();
612613
true
613614
}
614615
Err(_) => {
@@ -666,10 +667,13 @@ impl<T, A: Alloc> RawVec<T, A> {
666667
let new_size = elem_size * amount;
667668
let align = mem::align_of::<T>();
668669
let old_layout = Layout::from_size_align_unchecked(old_size, align);
669-
match self.a.realloc(NonNull::from(self.ptr).as_opaque(),
670+
match self.a.realloc_excess(NonNull::from(self.ptr).as_opaque(),
670671
old_layout,
671672
new_size) {
672-
Ok(p) => self.ptr = p.cast().into(),
673+
Ok(Excess(ptr, alloc_size)) => {
674+
self.ptr = ptr.cast().into();
675+
self.cap = alloc_size / mem::size_of::<T>();
676+
},
673677
Err(_) => oom(),
674678
}
675679
}

0 commit comments

Comments
 (0)