Created
November 5, 2019 09:52
-
-
Save tesuji/97e02614084c5c5b69d149c58208bcb9 to your computer and use it in GitHub Desktop.
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
use std::env; | |
use std::mem::size_of; | |
const SIZE_SZ: usize = size_of::<usize>(); | |
#[repr(C)] | |
struct malloc_chunk { | |
mchunk_prev_size: usize, /* Size of previous chunk (if free). */ | |
mchunk_size: usize, /* Size in bytes, including overhead. */ | |
fd: *mut malloc_chunk, /* double links -- used only if free. */ | |
bk: *mut malloc_chunk, | |
} | |
/* The smallest possible chunk */ | |
const MIN_CHUNK_SIZE: usize = size_of::<malloc_chunk>(); | |
const MALLOC_ALIGNMENT: usize = 2 * SIZE_SZ; | |
const MALLOC_ALIGN_MASK: usize = MALLOC_ALIGNMENT - 1; | |
const MINSIZE: usize = (MIN_CHUNK_SIZE + MALLOC_ALIGN_MASK) & MALLOC_ALIGN_MASK; | |
fn request2size(req: usize) -> usize { | |
let size = req + SIZE_SZ + MALLOC_ALIGN_MASK; | |
if size < MINSIZE { | |
MINSIZE | |
} else { | |
size & !MALLOC_ALIGN_MASK | |
} | |
} | |
fn main() { | |
let args: Vec<_> = env::args().collect(); | |
if args.len() != 2 { | |
println!("usage: {} <size>", args[0]); | |
return; | |
} | |
let req = args[1].parse::<usize>().unwrap(); | |
let size = request2size(req); | |
println!( | |
"request: {}\n\ | |
size: {}", | |
req, size, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment