userspace/memory/
alloc.rs

1use crate::target::os::syscall;
2
3pub fn alloc<T>(n: usize) -> *mut T {
4    let t_size = core::mem::size_of::<T>();
5
6    let aligned_size = n * t_size; //(t_size + ::memory::page::SIZE - 1) & !(::memory::page::SIZE - 1);
7
8    match syscall::mmap(
9        core::ptr::null_mut(),
10        aligned_size,
11        (syscall::mmap::Prot::Read | syscall::mmap::Prot::Write) as i32,
12        (syscall::mmap::Flag::Anonymous | syscall::mmap::Flag::Private) as i32,
13        -1,
14        0,
15    ) {
16        core::result::Result::Ok(crate::Ok::Target(crate::target::Ok::Os(
17            crate::target::os::Ok::Syscall(crate::target::os::syscall::Ok::MMap(
18                crate::target::os::syscall::mmap::Ok::Default(m),
19            )),
20        ))) => m as *mut T,
21        _ => panic!("Failed to allocate memory"),
22    }
23}