Files
simple-rust-tests/__internal/memory
..
2023-01-19 23:00:54 +08:00
2023-01-19 22:19:54 +08:00
2023-01-19 22:19:54 +08:00
2023-01-19 23:10:49 +08:00

Reference:

rust-memalloc implements reallocate:

pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 {
    if old_size > new_size {
        let mut buf = Vec::from_raw_parts(ptr, new_size, old_size);
        buf.shrink_to_fit();

        ptr_from_vec(buf)
    } else if new_size > old_size {
        let additional = new_size - old_size;

        let mut buf = Vec::from_raw_parts(ptr, 0, old_size);
        buf.reserve_exact(additional);

        ptr_from_vec(buf)
    } else {
        ptr
    }
}