use crate::sys::{SYS_MMAP, SYS_MUNMAP, syscall2, syscall4};

/// Map memory into the address space
/// # Safety
/// Caller must ensure parameters are valid for mmap semantics
pub fn mmap(addr: *mut u8, length: u64, prot: u32, flags: u32) -> *mut u8 {
    unsafe { syscall4(SYS_MMAP, addr as u64, length, prot as u64, flags as u64) as *mut u8 }
}

/// Unmap memory from the address space
pub fn munmap(addr: *mut u8, length: u64) -> i32 {
    unsafe { syscall2(SYS_MUNMAP, addr as u64, length) as i32 }
}

#[inline]
pub const fn align_up(addr: u64, align: u64) -> u64 {
    debug_assert!(align.is_power_of_two());
    (addr + align - 1) & !(align - 1)
}
