| 1 |
#![cfg_attr(test, deny(warnings))] |
| 2 |
#![deny(missing_docs)] |
| 3 |
|
| 4 |
//! # memalloc |
| 5 |
//! |
| 6 |
//! Memory allocation in stable rust, providing a similar interface to `std::rt::heap`, |
| 7 |
//! notably these functions align everything according to the alignment of `u8`, rather |
| 8 |
//! than using a user-provided alignment. |
| 9 |
//! |
| 10 |
//! Additionally, they do not allow for handling allocation failure, and will simply |
| 11 |
//! abort the process on OOM. Unfortunately, this limitation is unavoidable if we want |
| 12 |
//! to use only stable APIs. |
| 13 |
//! |
| 14 |
|
| 15 |
use std::mem; |
| 16 |
|
| 17 |
/// Returns a pointer to `size` bytes of memory aligned to `mem::align_of::<u8>()`. |
| 18 |
/// |
| 19 |
/// On failure, aborts the process. |
| 20 |
/// |
| 21 |
/// Behavior is undefined if the requested size is 0. |
| 22 |
#[inline] |
| 23 |
pub unsafe fn allocate(size: usize) -> *mut u8 { |
| 24 |
ptr_from_vec(Vec::with_capacity(size)) |
| 25 |
} |
| 26 |
|
| 27 |
/// Resizes the allocation referenced by `ptr` to `new_size` bytes. |
| 28 |
/// |
| 29 |
/// On failure, aborts the process. |
| 30 |
/// |
| 31 |
/// If the allocation was relocated, the memory at the passed-in pointer is |
| 32 |
/// undefined after the call. |
| 33 |
/// |
| 34 |
/// Behavior is undefined if the requested `new_size` is 0. |
| 35 |
/// |
| 36 |
/// The `old_size` parameter is the size used to create the allocation |
| 37 |
/// referenced by `ptr`, or the `new_size` passed to previous reallocations. |
| 38 |
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 { |
| 39 |
if old_size > new_size { |
| 40 |
let mut buf = Vec::from_raw_parts(ptr, new_size, old_size); |
| 41 |
buf.shrink_to_fit(); |
| 42 |
|
| 43 |
ptr_from_vec(buf) |
| 44 |
} else if new_size > old_size { |
| 45 |
let additional = new_size - old_size; |
| 46 |
|
| 47 |
let mut buf = Vec::from_raw_parts(ptr, 0, old_size); |
| 48 |
buf.reserve_exact(additional); |
| 49 |
|
| 50 |
ptr_from_vec(buf) |
| 51 |
} else { |
| 52 |
ptr |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// Deallocates the memory referenced by `ptr`. |
| 57 |
/// |
| 58 |
/// Behavior is undefined if `ptr` is null. |
| 59 |
/// |
| 60 |
/// The `old_size` parameter is the size used to create the allocation |
| 61 |
/// referenced by `ptr`, or the `new_size` passed to the last reallocation. |
| 62 |
#[inline] |
| 63 |
pub unsafe fn deallocate(ptr: *mut u8, old_size: usize) { |
| 64 |
Vec::from_raw_parts(ptr, 0, old_size); |
| 65 |
} |
| 66 |
|
| 67 |
/// A token empty allocation which cannot be read from or written to, |
| 68 |
/// but which can be used as a placeholder when a 0-sized allocation is |
| 69 |
/// required. |
| 70 |
pub fn empty() -> *mut u8 { |
| 71 |
1 as *mut u8 |
| 72 |
} |
| 73 |
|
| 74 |
#[inline] |
| 75 |
fn ptr_from_vec(mut buf: Vec<u8>) -> *mut u8 { |
| 76 |
let ptr = buf.as_mut_ptr(); |
| 77 |
mem::forget(buf); |
| 78 |
|
| 79 |
ptr |
| 80 |
} |
| 81 |
|
| 82 |
#[cfg(test)] |
| 83 |
mod tests { |
| 84 |
use std::ptr; |
| 85 |
use {allocate, reallocate, deallocate, empty}; |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn test_empty() { |
| 89 |
let ptr = empty(); |
| 90 |
assert!(ptr != ptr::null_mut()); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_allocate() { |
| 95 |
let buffer = unsafe { allocate(8) }; |
| 96 |
|
| 97 |
assert!(buffer != ptr::null_mut()); |
| 98 |
|
| 99 |
unsafe { |
| 100 |
ptr::write(buffer.offset(0), 8); |
| 101 |
ptr::write(buffer.offset(1), 4); |
| 102 |
ptr::write(buffer.offset(3), 5); |
| 103 |
ptr::write(buffer.offset(5), 3); |
| 104 |
ptr::write(buffer.offset(7), 6); |
| 105 |
|
| 106 |
assert_eq!(ptr::read(buffer.offset(0)), 8); |
| 107 |
assert_eq!(ptr::read(buffer.offset(1)), 4); |
| 108 |
assert_eq!(ptr::read(buffer.offset(3)), 5); |
| 109 |
assert_eq!(ptr::read(buffer.offset(5)), 3); |
| 110 |
assert_eq!(ptr::read(buffer.offset(7)), 6); |
| 111 |
}; |
| 112 |
|
| 113 |
unsafe { deallocate(buffer, 8); } |
| 114 |
|
| 115 |
// Try a large buffer |
| 116 |
let buffer = unsafe { allocate(1024 * 1024) }; |
| 117 |
assert!(buffer != ptr::null_mut()); |
| 118 |
|
| 119 |
unsafe { |
| 120 |
ptr::write(buffer.offset(1024 * 1024 - 1), 12); |
| 121 |
assert_eq!(ptr::read(buffer.offset(1024 * 1024 - 1)), 12); |
| 122 |
}; |
| 123 |
|
| 124 |
unsafe { deallocate(buffer, 1024 * 1024); } |
| 125 |
} |
| 126 |
|
| 127 |
#[test] |
| 128 |
fn test_reallocate() { |
| 129 |
let mut buffer = unsafe { allocate(8) }; |
| 130 |
assert!(buffer != ptr::null_mut()); |
| 131 |
|
| 132 |
buffer = unsafe { reallocate(buffer, 8, 16) }; |
| 133 |
assert!(buffer != ptr::null_mut()); |
| 134 |
|
| 135 |
unsafe { |
| 136 |
// Put some data in the buffer |
| 137 |
ptr::write(buffer.offset(0), 8); |
| 138 |
ptr::write(buffer.offset(1), 4); |
| 139 |
ptr::write(buffer.offset(5), 3); |
| 140 |
ptr::write(buffer.offset(7), 6); |
| 141 |
}; |
| 142 |
|
| 143 |
// Allocate so in-place fails. |
| 144 |
unsafe { allocate(128) }; |
| 145 |
|
| 146 |
buffer = unsafe { reallocate(buffer, 16, 32) }; |
| 147 |
assert!(buffer != ptr::null_mut()); |
| 148 |
|
| 149 |
unsafe { |
| 150 |
// Ensure the data is still there. |
| 151 |
assert_eq!(ptr::read(buffer.offset(0)), 8); |
| 152 |
assert_eq!(ptr::read(buffer.offset(1)), 4); |
| 153 |
assert_eq!(ptr::read(buffer.offset(5)), 3); |
| 154 |
assert_eq!(ptr::read(buffer.offset(7)), 6); |
| 155 |
}; |
| 156 |
} |
| 157 |
} |
| 158 |
|