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