xref: /relibc/src/header/sys_mman/mod.rs (revision 07329c9efeaa4017d7c5f7f0ffca7d9d56f2d21f)
1 use crate::{
2     c_str::{CStr, CString},
3     header::{fcntl, unistd},
4     platform::{types::*, Pal, Sys},
5 };
6 
7 pub use self::sys::*;
8 
9 #[cfg(target_os = "linux")]
10 #[path = "linux.rs"]
11 pub mod sys;
12 
13 #[cfg(target_os = "redox")]
14 #[path = "redox.rs"]
15 pub mod sys;
16 
17 pub const MADV_NORMAL: c_int = 0;
18 pub const MADV_RANDOM: c_int = 1;
19 pub const MADV_SEQUENTIAL: c_int = 2;
20 pub const MADV_WILLNEED: c_int = 3;
21 pub const MADV_DONTNEED: c_int = 4;
22 
23 pub const MAP_SHARED: c_int = 0x0001;
24 pub const MAP_PRIVATE: c_int = 0x0002;
25 pub const MAP_TYPE: c_int = 0x000F;
26 pub const MAP_ANON: c_int = 0x0020;
27 pub const MAP_ANONYMOUS: c_int = MAP_ANON;
28 
29 pub const MS_ASYNC: c_int = 0x0001;
30 pub const MS_INVALIDATE: c_int = 0x0002;
31 pub const MS_SYNC: c_int = 0x0004;
32 
33 pub const MCL_CURRENT: c_int = 1;
34 pub const MCL_FUTURE: c_int = 2;
35 
36 pub const POSIX_MADV_NORMAL: c_int = 0;
37 pub const POSIX_MADV_RANDOM: c_int = 1;
38 pub const POSIX_MADV_SEQUENTIAL: c_int = 2;
39 pub const POSIX_MADV_WILLNEED: c_int = 3;
40 pub const POSIX_MADV_WONTNEED: c_int = 4;
41 
42 #[no_mangle]
43 pub unsafe extern "C" fn mlock(addr: *const c_void, len: usize) -> c_int {
44     Sys::mlock(addr, len)
45 }
46 
47 #[no_mangle]
48 pub extern "C" fn mlockall(flags: c_int) -> c_int {
49     Sys::mlockall(flags)
50 }
51 
52 #[no_mangle]
53 pub unsafe extern "C" fn mmap(
54     addr: *mut c_void,
55     len: size_t,
56     prot: c_int,
57     flags: c_int,
58     fildes: c_int,
59     off: off_t,
60 ) -> *mut c_void {
61     Sys::mmap(addr, len, prot, flags, fildes, off)
62 }
63 
64 #[no_mangle]
65 pub unsafe extern "C" fn mprotect(addr: *mut c_void, len: size_t, prot: c_int) -> c_int {
66     Sys::mprotect(addr, len, prot)
67 }
68 
69 #[no_mangle]
70 pub unsafe extern "C" fn msync(addr: *mut c_void, len: size_t, flags: c_int) -> c_int {
71     Sys::msync(addr, len, flags)
72 }
73 
74 #[no_mangle]
75 pub unsafe extern "C" fn munlock(addr: *const c_void, len: usize) -> c_int {
76     Sys::munlock(addr, len)
77 }
78 
79 #[no_mangle]
80 pub extern "C" fn munlockall() -> c_int {
81     Sys::munlockall()
82 }
83 
84 #[no_mangle]
85 pub unsafe extern "C" fn munmap(addr: *mut c_void, len: size_t) -> c_int {
86     Sys::munmap(addr, len)
87 }
88 
89 #[cfg(target_os = "linux")]
90 static SHM_PATH: &'static [u8] = b"/dev/shm/";
91 
92 #[cfg(target_os = "redox")]
93 static SHM_PATH: &'static [u8] = b"shm:";
94 
95 unsafe fn shm_path(name: *const c_char) -> CString {
96     let name_c = CStr::from_ptr(name);
97 
98     let mut path = SHM_PATH.to_vec();
99 
100     let mut skip_slash = true;
101     for &b in name_c.to_bytes() {
102         if skip_slash {
103             if b == b'/' {
104                 continue;
105             } else {
106                 skip_slash = false;
107             }
108         }
109         path.push(b);
110     }
111 
112     CString::from_vec_unchecked(path)
113 }
114 
115 #[no_mangle]
116 pub unsafe extern "C" fn shm_open(name: *const c_char, oflag: c_int, mode: mode_t) -> c_int {
117     let path = shm_path(name);
118     fcntl::sys_open(path.as_ptr(), oflag, mode)
119 }
120 
121 #[no_mangle]
122 pub unsafe extern "C" fn shm_unlink(name: *const c_char) -> c_int {
123     let path = shm_path(name);
124     unistd::unlink(path.as_ptr())
125 }
126