提交 f40767a1 编写于 作者: A Alex Crichton

rollup merge of #17705 : thestinger/realloc

......@@ -182,9 +182,15 @@ pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
#[inline]
pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
_old_size: uint) -> bool {
old_size: uint) -> bool {
let flags = align_to_flags(align);
je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) == size as size_t
let new_size = je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint;
// checking for failure to shrink is tricky
if size < old_size {
usable_size(size, align) == new_size as uint
} else {
new_size >= size
}
}
#[inline]
......@@ -250,9 +256,9 @@ pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
}
#[inline]
pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
_old_size: uint) -> bool {
false
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
old_size: uint) -> bool {
size == old_size
}
#[inline]
......@@ -312,9 +318,9 @@ pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
}
#[inline]
pub unsafe fn reallocate_inplace(_ptr: *mut u8, _size: uint, _align: uint,
_old_size: uint) -> bool {
false
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
old_size: uint) -> bool {
size == old_size
}
#[inline]
......@@ -335,10 +341,21 @@ pub fn stats_print() {}
}
#[cfg(test)]
mod bench {
mod test {
extern crate test;
use self::test::Bencher;
#[test]
fn basic_reallocate_inplace_noop() {
unsafe {
let size = 4000;
let ptr = heap::allocate(size, 8);
let ret = heap::reallocate_inplace(ptr, size, 8, size);
heap::deallocate(ptr, size, 8);
assert!(ret);
}
}
#[bench]
fn alloc_owned_small(b: &mut Bencher) {
b.iter(|| {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册