Skip to content

Commit c957e99

Browse files
committed
realloc with a new size only, not a full new layout.
Changing the alignment with realloc is not supported.
1 parent b017742 commit c957e99

File tree

5 files changed

+74
-102
lines changed

5 files changed

+74
-102
lines changed

src/liballoc/alloc.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,17 @@ unsafe impl Alloc for Global {
9191
unsafe fn realloc(&mut self,
9292
ptr: *mut u8,
9393
layout: Layout,
94-
new_layout: Layout)
94+
new_size: usize)
9595
-> Result<*mut u8, AllocErr>
9696
{
97-
if layout.align() == new_layout.align() {
98-
#[cfg(not(stage0))]
99-
let ptr = __rust_realloc(ptr, layout.size(), layout.align(), new_layout.size());
100-
#[cfg(stage0)]
101-
let ptr = __rust_realloc(ptr, layout.size(), layout.align(),
102-
new_layout.size(), new_layout.align(), &mut 0);
103-
104-
if !ptr.is_null() {
105-
Ok(ptr)
106-
} else {
107-
Err(AllocErr)
108-
}
97+
#[cfg(not(stage0))]
98+
let ptr = __rust_realloc(ptr, layout.size(), layout.align(), new_size);
99+
#[cfg(stage0)]
100+
let ptr = __rust_realloc(ptr, layout.size(), layout.align(),
101+
new_size, layout.align(), &mut 0);
102+
103+
if !ptr.is_null() {
104+
Ok(ptr)
109105
} else {
110106
Err(AllocErr)
111107
}

src/liballoc/heap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
6464
ptr: *mut u8,
6565
layout: Layout,
6666
new_layout: Layout) -> Result<*mut u8, AllocErr> {
67-
CoreAlloc::realloc(self, ptr, layout, new_layout)
67+
CoreAlloc::realloc(self, ptr, layout, new_layout.size())
6868
}
6969

7070
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
@@ -79,20 +79,20 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
7979
ptr: *mut u8,
8080
layout: Layout,
8181
new_layout: Layout) -> Result<Excess, AllocErr> {
82-
CoreAlloc::realloc_excess(self, ptr, layout, new_layout)
82+
CoreAlloc::realloc_excess(self, ptr, layout, new_layout.size())
8383
}
8484

8585
unsafe fn grow_in_place(&mut self,
8686
ptr: *mut u8,
8787
layout: Layout,
8888
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
89-
CoreAlloc::grow_in_place(self, ptr, layout, new_layout)
89+
CoreAlloc::grow_in_place(self, ptr, layout, new_layout.size())
9090
}
9191

9292
unsafe fn shrink_in_place(&mut self,
9393
ptr: *mut u8,
9494
layout: Layout,
9595
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
96-
CoreAlloc::shrink_in_place(self, ptr, layout, new_layout)
96+
CoreAlloc::shrink_in_place(self, ptr, layout, new_layout.size())
9797
}
9898
}

src/liballoc/raw_vec.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,10 @@ impl<T, A: Alloc> RawVec<T, A> {
309309
// `from_size_align_unchecked`.
310310
let new_cap = 2 * self.cap;
311311
let new_size = new_cap * elem_size;
312-
let new_layout = Layout::from_size_align_unchecked(new_size, cur.align());
313312
alloc_guard(new_size).expect("capacity overflow");
314313
let ptr_res = self.a.realloc(self.ptr.as_ptr() as *mut u8,
315314
cur,
316-
new_layout);
315+
new_size);
317316
match ptr_res {
318317
Ok(ptr) => (new_cap, Unique::new_unchecked(ptr as *mut T)),
319318
Err(_) => self.a.oom(),
@@ -371,8 +370,7 @@ impl<T, A: Alloc> RawVec<T, A> {
371370
let new_size = new_cap * elem_size;
372371
alloc_guard(new_size).expect("capacity overflow");
373372
let ptr = self.ptr() as *mut _;
374-
let new_layout = Layout::from_size_align_unchecked(new_size, old_layout.align());
375-
match self.a.grow_in_place(ptr, old_layout, new_layout) {
373+
match self.a.grow_in_place(ptr, old_layout, new_size) {
376374
Ok(_) => {
377375
// We can't directly divide `size`.
378376
self.cap = new_cap;
@@ -428,8 +426,9 @@ impl<T, A: Alloc> RawVec<T, A> {
428426

429427
let res = match self.current_layout() {
430428
Some(layout) => {
429+
debug_assert!(new_layout.align() == layout.align());
431430
let old_ptr = self.ptr.as_ptr() as *mut u8;
432-
self.a.realloc(old_ptr, layout, new_layout)
431+
self.a.realloc(old_ptr, layout, new_layout.size())
433432
}
434433
None => self.a.alloc(new_layout),
435434
};
@@ -537,8 +536,9 @@ impl<T, A: Alloc> RawVec<T, A> {
537536

538537
let res = match self.current_layout() {
539538
Some(layout) => {
539+
debug_assert!(new_layout.align() == layout.align());
540540
let old_ptr = self.ptr.as_ptr() as *mut u8;
541-
self.a.realloc(old_ptr, layout, new_layout)
541+
self.a.realloc(old_ptr, layout, new_layout.size())
542542
}
543543
None => self.a.alloc(new_layout),
544544
};
@@ -604,7 +604,7 @@ impl<T, A: Alloc> RawVec<T, A> {
604604
let new_layout = Layout::new::<T>().repeat(new_cap).unwrap().0;
605605
// FIXME: may crash and burn on over-reserve
606606
alloc_guard(new_layout.size()).expect("capacity overflow");
607-
match self.a.grow_in_place(ptr, old_layout, new_layout) {
607+
match self.a.grow_in_place(ptr, old_layout, new_layout.size()) {
608608
Ok(_) => {
609609
self.cap = new_cap;
610610
true
@@ -664,10 +664,9 @@ impl<T, A: Alloc> RawVec<T, A> {
664664
let new_size = elem_size * amount;
665665
let align = mem::align_of::<T>();
666666
let old_layout = Layout::from_size_align_unchecked(old_size, align);
667-
let new_layout = Layout::from_size_align_unchecked(new_size, align);
668667
match self.a.realloc(self.ptr.as_ptr() as *mut u8,
669668
old_layout,
670-
new_layout) {
669+
new_size) {
671670
Ok(p) => self.ptr = Unique::new_unchecked(p as *mut T),
672671
Err(_) => self.a.oom(),
673672
}

src/liballoc_system/lib.rs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ unsafe impl Alloc for System {
6969
unsafe fn realloc(&mut self,
7070
ptr: *mut u8,
7171
old_layout: Layout,
72-
new_layout: Layout) -> Result<*mut u8, AllocErr> {
73-
Alloc::realloc(&mut &*self, ptr, old_layout, new_layout)
72+
new_size: usize) -> Result<*mut u8, AllocErr> {
73+
Alloc::realloc(&mut &*self, ptr, old_layout, new_size)
7474
}
7575

7676
fn oom(&mut self) -> ! {
@@ -91,24 +91,24 @@ unsafe impl Alloc for System {
9191
unsafe fn realloc_excess(&mut self,
9292
ptr: *mut u8,
9393
layout: Layout,
94-
new_layout: Layout) -> Result<Excess, AllocErr> {
95-
Alloc::realloc_excess(&mut &*self, ptr, layout, new_layout)
94+
new_size: usize) -> Result<Excess, AllocErr> {
95+
Alloc::realloc_excess(&mut &*self, ptr, layout, new_size)
9696
}
9797

9898
#[inline]
9999
unsafe fn grow_in_place(&mut self,
100100
ptr: *mut u8,
101101
layout: Layout,
102-
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
103-
Alloc::grow_in_place(&mut &*self, ptr, layout, new_layout)
102+
new_size: usize) -> Result<(), CannotReallocInPlace> {
103+
Alloc::grow_in_place(&mut &*self, ptr, layout, new_size)
104104
}
105105

106106
#[inline]
107107
unsafe fn shrink_in_place(&mut self,
108108
ptr: *mut u8,
109109
layout: Layout,
110-
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
111-
Alloc::shrink_in_place(&mut &*self, ptr, layout, new_layout)
110+
new_size: usize) -> Result<(), CannotReallocInPlace> {
111+
Alloc::shrink_in_place(&mut &*self, ptr, layout, new_size)
112112
}
113113
}
114114

@@ -166,12 +166,8 @@ macro_rules! alloc_methods_based_on_global_alloc {
166166
unsafe fn realloc(&mut self,
167167
ptr: *mut u8,
168168
old_layout: Layout,
169-
new_layout: Layout) -> Result<*mut u8, AllocErr> {
170-
if old_layout.align() != new_layout.align() {
171-
return Err(AllocErr)
172-
}
173-
174-
let ptr = GlobalAlloc::realloc(*self, ptr as *mut Void, old_layout, new_layout.size());
169+
new_size: usize) -> Result<*mut u8, AllocErr> {
170+
let ptr = GlobalAlloc::realloc(*self, ptr as *mut Void, old_layout, new_size);
175171
if !ptr.is_null() {
176172
Ok(ptr as *mut u8)
177173
} else {
@@ -428,30 +424,26 @@ mod platform {
428424
unsafe fn grow_in_place(&mut self,
429425
ptr: *mut u8,
430426
layout: Layout,
431-
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
432-
self.shrink_in_place(ptr, layout, new_layout)
427+
new_size: usize) -> Result<(), CannotReallocInPlace> {
428+
self.shrink_in_place(ptr, layout, new_size)
433429
}
434430

435431
#[inline]
436432
unsafe fn shrink_in_place(&mut self,
437433
ptr: *mut u8,
438-
old_layout: Layout,
439-
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
440-
if old_layout.align() != new_layout.align() {
441-
return Err(CannotReallocInPlace)
442-
}
443-
444-
let new = if new_layout.align() <= MIN_ALIGN {
434+
layout: Layout,
435+
new_size: usize) -> Result<(), CannotReallocInPlace> {
436+
let new = if layout.align() <= MIN_ALIGN {
445437
HeapReAlloc(GetProcessHeap(),
446438
HEAP_REALLOC_IN_PLACE_ONLY,
447439
ptr as LPVOID,
448-
new_layout.size())
440+
new_size)
449441
} else {
450442
let header = get_header(ptr);
451443
HeapReAlloc(GetProcessHeap(),
452444
HEAP_REALLOC_IN_PLACE_ONLY,
453445
header.0 as LPVOID,
454-
new_layout.size() + new_layout.align())
446+
new_size + layout.align())
455447
};
456448
if new.is_null() {
457449
Err(CannotReallocInPlace)

0 commit comments

Comments
 (0)