1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]

#![allow(
    clippy::module_name_repetitions,
    clippy::cast_sign_loss,
    clippy::cast_possible_truncation,
    clippy::trivially_copy_pass_by_ref
)]

#![deny(missing_debug_implementations)]
#![deny(missing_copy_implementations)]

// why rust does not have this?
//#![deny(missing_clone_implementations)]

// by default #![no_std] is set (unless the std feature enabled)
#![cfg_attr(not(feature = "std"), no_std)]

// if this is a test, define some useful things
#[cfg(test)]
mod _test_utils {
    // if run tests from `cargo test`, this will force disply to stderr
    #[macro_export]
    macro_rules! p {
        ($($args:tt)*) => {{
            let out = format!( $($args)* );
            let n =
                out.lines()
                .map(|x|{ x.len() })
                .max().unwrap_or(20);

            let mut stderr = std::io::stderr().lock();
            writeln!(stderr, "{}", ".".repeat(n)).expect("cannot print to stderr");
            write!(stderr, "{}", out).expect("cannot print to stderr");
            writeln!(stderr, "{}", ".".repeat(n)).expect("cannot print to stderr");
        }};
    }

    #[macro_export]
    macro_rules! pl {
        ($($args:tt)*) => {{
            let mut s = format!( $($args)* );
            s.push('\n');
            $crate::p!("{}", s);
        }}
    }
}

#[cfg(not(target_vendor="apple"))]
mod _err {
    //compile_error!("mach requires macOS or iOS");
}

#[allow(unused_imports)]
use core::{clone, cmp, default, fmt, hash, marker, mem, option};

pub const NONCE_RANDOM: u64 = include!(env!("MACH_SYS_NONCE_RANDOM"));

/// imports C types from core::ffi
pub mod ffi {
    pub use core::ffi::{
        c_void,
        c_uchar, c_char,
        c_ushort,
        c_uint, c_int,
        c_ulong, c_long,
        c_ulonglong, c_longlong,
    };

    pub type clock_t  = c_ulong; /// in all apple/darwin platforms, type `clock_t` is equiv to `c_ulong`.
    pub type policy_t = c_int;   /// in all apple/darwin platforms, type `policy_t` is equiv to `c_int`.

    pub type intptr_t  = isize; /// in all platforms, `intptr_t` equiv to `isize`
    pub type uintptr_t = usize; /// in all platforms, `uintptr_t` is equiv to `usize`

    /// in all apple/darwin platforms,
    /// type `cpu_type_t` and `cpu_subtype_t`,
    /// both equiv to `integer_t`.
    use crate::vm_types::integer_t;
    pub type cpu_type_t    = integer_t;
    pub type cpu_subtype_t = integer_t;

    /// in all apple/darwin platforms,
    /// `pid_t` and `uid_t` both are 32-bit integer.
    /// but PID is signed, UID is unsigned.
    pub type pid_t = i32;
    pub type uid_t = u32;
}

pub mod boolean;
pub mod bootstrap;
pub mod clock;
pub mod clock_priv;
pub mod clock_reply;
pub mod clock_types;

pub mod dyld_kernel;
pub mod error;
pub mod exc;
pub mod exception_types;
pub mod kern_return;

pub mod loader;
pub mod mach_init;
pub mod mach_port;
pub mod mach_time;
pub mod mach_types;
pub mod memory_object_types;
pub mod message;
pub mod ndr;
pub mod port;
pub mod semaphore;
pub mod structs;
pub mod sync_policy;
pub mod task;
pub mod task_info;
pub mod thread_act;
pub mod thread_policy;
pub mod thread_status;
pub mod time_value;
pub mod traps;
pub mod vm;
pub mod vm_attributes;
pub mod vm_behavior;
pub mod vm_inherit;
pub mod vm_page_size;
pub mod vm_prot;
pub mod vm_purgable;
pub mod vm_region;
pub mod vm_statistics;
pub mod vm_sync;
pub mod vm_types;

// added by machx
pub mod dyld;
pub mod dlfcn;
pub mod dyld_images;
pub mod libproc;
pub mod nlist;