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
#![allow(unused, non_camel_case_types)]
pub extern crate libc;
pub mod constants;
pub mod types;
pub mod unix;
pub fn memory <T> (size: usize, read: bool, write: bool, exec: bool, shared: bool) -> *mut T {
use crate::{constants::{map, mprot}, unix::sys_mmap};
let mut prot = mprot::NONE;
let mut flags = map::ANON;
if read { prot |= mprot::READ; }
if write { prot |= mprot::WRITE; }
if exec { prot |= mprot::EXEC; }
if shared { flags |= map::SHARED; }
else { flags |= map::PRIVATE; }
sys_mmap(0 as *mut (), size, prot, flags, -1, 0) as *mut T
}
pub fn release <T> (mem: *mut T, size: usize) {
crate::unix::sys_munmap(mem as *mut (), size);
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}