Created
April 6, 2018 10:21
-
-
Save haudan/08d9111fd09302e651363cc5f0d585e1 to your computer and use it in GitHub Desktop.
Rust allocator locality tester. Analyses whether the current allocator allocates successive allocations sequentially in memory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PAYLOAD_SIZE: usize = 128; | |
const TESTS: usize = 20; | |
fn main() { | |
let mut items: [Box<[u8; PAYLOAD_SIZE]>; TESTS] = unsafe { std::mem::uninitialized() }; | |
for item in items.iter_mut() { | |
unsafe { | |
std::ptr::write(item as *mut _, Box::new([0u8; PAYLOAD_SIZE])); | |
} | |
} | |
println!("Distances (in bytes) between {} sequentially allocated objects with {} bytes in size:", items.len(), PAYLOAD_SIZE); | |
for (i, item) in items.iter().enumerate() { | |
let item = &**item as *const _ as isize; | |
print!("{:02}: {:#X}", i + 1, item); | |
if i > 0 { | |
let prev_item = &*items[i - 1] as *const _ as isize; | |
let diff = item - prev_item; | |
let sign = if diff < 0 { "-" } else { " " }; | |
print!(": {}{:#X} ({})", sign, diff.abs(), diff); | |
} | |
println!(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment