Skip to content

Commit e6cfb56

Browse files
committed
auto merge of #17870 : thestinger/rust/alloc, r=eddyb
Using reallocate(old_ptr, old_size, new_size, align) makes a lot more sense than reallocate(old_ptr, new_size, align, old_size) and matches up with the order used by existing platform APIs like mremap. Closes #17837 [breaking-change]
2 parents 1b46b00 + 1c6fd76 commit e6cfb56

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

src/liballoc/heap.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
3131
/// create the allocation referenced by `ptr`. The `old_size` parameter may also
3232
/// be the value returned by `usable_size` for the requested size.
3333
#[inline]
34-
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
35-
old_size: uint) -> *mut u8 {
36-
imp::reallocate(ptr, size, align, old_size)
34+
pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
35+
imp::reallocate(ptr, old_size, size, align)
3736
}
3837

3938
/// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
@@ -50,9 +49,8 @@ pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
5049
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
5150
/// any value in range_inclusive(requested_size, usable_size).
5251
#[inline]
53-
pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
54-
old_size: uint) -> bool {
55-
imp::reallocate_inplace(ptr, size, align, old_size)
52+
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> bool {
53+
imp::reallocate_inplace(ptr, old_size, size, align)
5654
}
5755

5856
/// Deallocates the memory referenced by `ptr`.
@@ -170,8 +168,7 @@ mod imp {
170168
}
171169

172170
#[inline]
173-
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
174-
_old_size: uint) -> *mut u8 {
171+
pub unsafe fn reallocate(ptr: *mut u8, _old_size: uint, size: uint, align: uint) -> *mut u8 {
175172
let flags = align_to_flags(align);
176173
let ptr = je_rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8;
177174
if ptr.is_null() {
@@ -181,8 +178,8 @@ mod imp {
181178
}
182179

183180
#[inline]
184-
pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
185-
old_size: uint) -> bool {
181+
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint,
182+
align: uint) -> bool {
186183
let flags = align_to_flags(align);
187184
let new_size = je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint;
188185
// checking for failure to shrink is tricky
@@ -243,8 +240,7 @@ mod imp {
243240
}
244241

245242
#[inline]
246-
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
247-
old_size: uint) -> *mut u8 {
243+
pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
248244
if align <= MIN_ALIGN {
249245
libc_heap::realloc_raw(ptr, size)
250246
} else {
@@ -256,8 +252,8 @@ mod imp {
256252
}
257253

258254
#[inline]
259-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
260-
old_size: uint) -> bool {
255+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint,
256+
_align: uint) -> bool {
261257
size == old_size
262258
}
263259

@@ -303,8 +299,7 @@ mod imp {
303299
}
304300

305301
#[inline]
306-
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
307-
_old_size: uint) -> *mut u8 {
302+
pub unsafe fn reallocate(ptr: *mut u8, _old_size: uint, size: uint, align: uint) -> *mut u8 {
308303
if align <= MIN_ALIGN {
309304
libc_heap::realloc_raw(ptr, size)
310305
} else {
@@ -318,8 +313,8 @@ mod imp {
318313
}
319314

320315
#[inline]
321-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
322-
old_size: uint) -> bool {
316+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint,
317+
_align: uint) -> bool {
323318
size == old_size
324319
}
325320

@@ -351,7 +346,7 @@ mod test {
351346
unsafe {
352347
let size = 4000;
353348
let ptr = heap::allocate(size, 8);
354-
let ret = heap::reallocate_inplace(ptr, size, 8, size);
349+
let ret = heap::reallocate_inplace(ptr, size, size, 8);
355350
heap::deallocate(ptr, size, 8);
356351
assert!(ret);
357352
}

src/libcollections/vec.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,11 @@ impl<T: Clone> CloneableVector<T> for Vec<T> {
622622

623623
// FIXME: #13996: need a way to mark the return value as `noalias`
624624
#[inline(never)]
625-
unsafe fn alloc_or_realloc<T>(ptr: *mut T, size: uint, old_size: uint) -> *mut T {
625+
unsafe fn alloc_or_realloc<T>(ptr: *mut T, old_size: uint, size: uint) -> *mut T {
626626
if old_size == 0 {
627627
allocate(size, mem::min_align_of::<T>()) as *mut T
628628
} else {
629-
reallocate(ptr as *mut u8, size,
630-
mem::min_align_of::<T>(), old_size) as *mut T
629+
reallocate(ptr as *mut u8, old_size, size, mem::min_align_of::<T>()) as *mut T
631630
}
632631
}
633632

@@ -720,8 +719,7 @@ impl<T> Vec<T> {
720719
let size = capacity.checked_mul(&mem::size_of::<T>())
721720
.expect("capacity overflow");
722721
unsafe {
723-
self.ptr = alloc_or_realloc(self.ptr, size,
724-
self.cap * mem::size_of::<T>());
722+
self.ptr = alloc_or_realloc(self.ptr, self.cap * mem::size_of::<T>(), size);
725723
}
726724
self.cap = capacity;
727725
}
@@ -751,9 +749,9 @@ impl<T> Vec<T> {
751749
// Overflow check is unnecessary as the vector is already at
752750
// least this large.
753751
self.ptr = reallocate(self.ptr as *mut u8,
752+
self.cap * mem::size_of::<T>(),
754753
self.len * mem::size_of::<T>(),
755-
mem::min_align_of::<T>(),
756-
self.cap * mem::size_of::<T>()) as *mut T;
754+
mem::min_align_of::<T>()) as *mut T;
757755
}
758756
self.cap = self.len;
759757
}
@@ -1736,8 +1734,7 @@ impl<T> MutableSeq<T> for Vec<T> {
17361734
let size = max(old_size, 2 * mem::size_of::<T>()) * 2;
17371735
if old_size > size { fail!("capacity overflow") }
17381736
unsafe {
1739-
self.ptr = alloc_or_realloc(self.ptr, size,
1740-
self.cap * mem::size_of::<T>());
1737+
self.ptr = alloc_or_realloc(self.ptr, self.cap * mem::size_of::<T>(), size);
17411738
}
17421739
self.cap = max(self.cap, 2) * 2;
17431740
}

src/test/run-pass/realloc-16687.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,18 @@ unsafe fn test_triangle() -> bool {
6363

6464
heap::deallocate(ptr, size, align);
6565
}
66-
unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
67-
old_size: uint) -> *mut u8 {
66+
unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
6867
if PRINT {
69-
println!("reallocate(ptr=0x{:010x} size={:u} align={:u} old_size={:u})",
70-
ptr as uint, size, align, old_size);
68+
println!("reallocate(ptr=0x{:010x} old_size={:u} size={:u} align={:u})",
69+
ptr as uint, old_size, size, align);
7170
}
7271

73-
let ret = heap::reallocate(ptr, size, align, old_size);
72+
let ret = heap::reallocate(ptr, old_size, size, align);
7473

7574
if PRINT {
76-
println!("reallocate(ptr=0x{:010x} size={:u} align={:u} old_size={:u}) \
75+
println!("reallocate(ptr=0x{:010x} old_size={:u} size={:u} align={:u}) \
7776
ret: 0x{:010x}",
78-
ptr as uint, size, align, old_size, ret as uint);
77+
ptr as uint, old_size, size, align, ret as uint);
7978
}
8079
ret
8180
}
@@ -125,10 +124,10 @@ unsafe fn test_triangle() -> bool {
125124
let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
126125
assert!(old_size < new_size);
127126

128-
ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
127+
ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
129128
sanity_check(ascend.as_slice());
130129

131-
ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
130+
ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
132131
sanity_check(ascend.as_slice());
133132
}
134133
}
@@ -140,10 +139,10 @@ unsafe fn test_triangle() -> bool {
140139
let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
141140
assert!(new_size < old_size);
142141

143-
ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
142+
ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
144143
sanity_check(ascend.as_slice());
145144

146-
ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
145+
ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
147146
sanity_check(ascend.as_slice());
148147
}
149148
}
@@ -155,10 +154,10 @@ unsafe fn test_triangle() -> bool {
155154
let (p0, p1, old_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
156155
assert!(old_size < new_size);
157156

158-
ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
157+
ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
159158
sanity_check(ascend.as_slice());
160159

161-
ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
160+
ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
162161
sanity_check(ascend.as_slice());
163162
}
164163
}
@@ -170,10 +169,10 @@ unsafe fn test_triangle() -> bool {
170169
let (p0, p1, new_size) = (ascend[2*i], ascend[2*i+1], idx_to_size(i));
171170
assert!(new_size < old_size);
172171

173-
ascend[2*i+1] = reallocate(p1, new_size, ALIGN, old_size);
172+
ascend[2*i+1] = reallocate(p1, old_size, new_size, ALIGN);
174173
sanity_check(ascend.as_slice());
175174

176-
ascend[2*i] = reallocate(p0, new_size, ALIGN, old_size);
175+
ascend[2*i] = reallocate(p0, old_size, new_size, ALIGN);
177176
sanity_check(ascend.as_slice());
178177
}
179178
}

0 commit comments

Comments
 (0)