aya: move mmap, munmap to mmap utility module

reviewable/pr1261/r7
Omri Steiner 4 weeks ago
parent d7e7ecea6c
commit 9649ccc8b9

@ -1,4 +1,4 @@
//! Safe wrapper around memory-mapped files.
//! mmap/munmap syscall wrappers and a RAII wrapper around memory-mapped files.
use std::{
fs, io,
@ -9,7 +9,42 @@ use std::{
use libc::{MAP_FAILED, MAP_PRIVATE, PROT_READ, c_int, c_void, off_t};
use super::{SyscallError, mmap, munmap};
use super::SyscallError;
#[cfg_attr(test, expect(unused_variables))]
pub(crate) unsafe fn mmap(
addr: *mut c_void,
len: usize,
prot: c_int,
flags: c_int,
fd: BorrowedFd<'_>,
offset: libc::off_t,
) -> *mut c_void {
#[cfg(test)]
{
super::TEST_MMAP_RET.with(|ret| *ret.borrow())
}
#[cfg(not(test))]
{
use std::os::fd::AsRawFd as _;
unsafe { libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset) }
}
}
#[cfg_attr(test, expect(unused_variables))]
pub(crate) unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int {
#[cfg(test)]
{
0
}
#[cfg(not(test))]
{
unsafe { libc::munmap(addr, len) }
}
}
// MMap corresponds to a memory-mapped region.
//

@ -9,7 +9,6 @@ mod perf_event;
mod fake;
use std::{
ffi::{c_int, c_void},
io,
os::fd::{BorrowedFd, OwnedFd},
};
@ -152,41 +151,6 @@ fn syscall(call: Syscall<'_>) -> SysResult {
}
}
#[cfg_attr(test, expect(unused_variables))]
pub(crate) unsafe fn mmap(
addr: *mut c_void,
len: usize,
prot: c_int,
flags: c_int,
fd: BorrowedFd<'_>,
offset: libc::off_t,
) -> *mut c_void {
#[cfg(test)]
{
TEST_MMAP_RET.with(|ret| *ret.borrow())
}
#[cfg(not(test))]
{
use std::os::fd::AsRawFd as _;
unsafe { libc::mmap(addr, len, prot, flags, fd.as_raw_fd(), offset) }
}
}
#[cfg_attr(test, expect(unused_variables))]
pub(crate) unsafe fn munmap(addr: *mut c_void, len: usize) -> c_int {
#[cfg(test)]
{
0
}
#[cfg(not(test))]
{
unsafe { libc::munmap(addr, len) }
}
}
/// The type of eBPF statistic to enable.
#[non_exhaustive]
#[doc(alias = "bpf_stats_type")]

Loading…
Cancel
Save