use crate::util::address::Address;
use crate::util::os::imp::unix_like::unix_common;
use crate::util::os::*;
use std::io::Result;
pub struct MacOS;
impl OSMemory for MacOS {
fn dzmmap(
start: Address,
size: usize,
strategy: MmapStrategy,
annotation: &MmapAnnotation<'_>,
) -> MmapResult<Address> {
let addr = unix_common::mmap(start, size, strategy, annotation)?;
if strategy.reserve {
crate::util::memory::zero(start, size);
}
Ok(addr)
}
fn dzmmap_anywhere(
size: usize,
align: usize,
strategy: MmapStrategy,
annotation: &MmapAnnotation<'_>,
) -> MmapResult<Address> {
unix_common::mmap_anywhere(size, align, strategy, annotation)
}
fn dzmmap_preferred(
start: Address,
size: usize,
align: usize,
strategy: MmapStrategy,
annotation: &MmapAnnotation<'_>,
) -> MmapResult<Address> {
let addr = unix_common::mmap_preferred(start, size, align, strategy, annotation)?;
if strategy.reserve {
crate::util::memory::zero(addr, size);
}
Ok(addr)
}
fn munmap(start: Address, size: usize) -> Result<()> {
unix_common::munmap(start, size)
}
fn set_memory_access(start: Address, size: usize, prot: MmapProtection) -> Result<()> {
unix_common::mprotect(start, size, prot)
}
fn is_mmap_oom(os_errno: i32) -> bool {
unix_common::is_mmap_oom(os_errno)
}
fn panic_if_unmapped(_start: Address, _size: usize) {
}
}
impl MmapStrategy {
pub fn get_posix_mmap_flags(&self, fixed: bool) -> i32 {
let mut flags = libc::MAP_PRIVATE | libc::MAP_ANONYMOUS;
if fixed {
flags |= libc::MAP_FIXED;
}
if !self.reserve {
flags |= libc::MAP_NORESERVE;
}
flags
}
}
impl OSProcess for MacOS {
type ProcessIDType = unix_common::ProcessIDType;
type ThreadIDType = unix_common::ThreadIDType;
fn get_process_memory_maps() -> Result<String> {
let pid = std::process::id();
let output = std::process::Command::new("vmmap")
.arg(pid.to_string()) .output() .expect("Failed to execute vmmap command");
if output.status.success() {
let output_str =
std::str::from_utf8(&output.stdout).expect("Failed to convert output to string");
Ok(output_str.to_string())
} else {
let error_message = std::str::from_utf8(&output.stderr)
.expect("Failed to convert error message to string");
Err(std::io::Error::other(format!(
"Failed to get process memory map: {}",
error_message
)))
}
}
fn get_process_id() -> Result<Self::ProcessIDType> {
unix_common::get_process_id()
}
fn get_thread_id() -> Result<Self::ThreadIDType> {
unix_common::get_thread_id()
}
fn get_total_num_cpus() -> CoreNum {
unimplemented!()
}
fn bind_current_thread_to_core(_core_id: CoreId) {
unimplemented!()
}
fn bind_current_thread_to_cpuset(_core_ids: &[CoreId]) {
unimplemented!()
}
}