euv_core/runtime/vfs/mapping/
impl.rs1use super::*;
2
3#[cfg(any(unix, windows))]
5unsafe impl Send for EuvPakOsMap {}
6
7#[cfg(any(unix, windows))]
9unsafe impl Sync for EuvPakOsMap {}
10
11#[cfg(any(unix, windows))]
13impl Drop for EuvPakOsMap {
14 fn drop(&mut self) {
16 #[cfg(unix)]
17 {
18 let Self { ptr, len } = *self;
19 unmap_posix_region(ptr, len);
20 }
21 #[cfg(windows)]
22 {
23 let Self {
24 ptr,
25 mapping_handle,
26 ..
27 } = *self;
28 unmap_win32_region(ptr, mapping_handle);
29 }
30 }
31}
32
33impl Default for EuvPakMapping {
35 fn default() -> Self {
41 Self::from_bytes(Arc::new(Vec::new()))
42 }
43}
44
45impl EuvPakMapping {
47 #[must_use]
57 pub fn from_bytes(bytes: Arc<Vec<u8>>) -> Self {
58 Self {
59 backing: EuvPakBacking::Owned(bytes),
60 }
61 }
62
63 #[cfg(not(target_arch = "wasm32"))]
73 pub fn open_mmap(path: &Path) -> Result<Self, String> {
74 #[cfg(unix)]
75 {
76 let mut file: File = File::open(path)
77 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
78 let raw_fd: i32 = file.as_raw_fd();
79 if raw_fd < 0 {
80 return Err(ERR_EUVPAK_INVALID_HANDLE.to_string());
81 }
82 let file_len: u64 = file
83 .seek(SeekFrom::End(0))
84 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
85 if file_len == 0 {
86 return Ok(Self::from_bytes(Arc::new(Vec::new())));
87 }
88 if file_len > usize::MAX as u64 {
89 return Err(ERR_EUVPAK_TOO_LARGE.to_string());
90 }
91 let os_map: EuvPakOsMap = map_posix_readonly(raw_fd, file_len as usize)?;
92 Ok(Self {
93 backing: EuvPakBacking::OsMapped(Arc::new(os_map)),
94 })
95 }
96 #[cfg(windows)]
97 {
98 let mut file: File = File::open(path)
99 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
100 let raw_handle: *mut c_void = file.as_raw_handle();
101 if raw_handle.is_null() || raw_handle as isize == -1 {
102 return Err(ERR_EUVPAK_INVALID_HANDLE.to_string());
103 }
104 let file_len: u64 = file
105 .seek(SeekFrom::End(0))
106 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
107 if file_len == 0 {
108 return Ok(Self::from_bytes(Arc::new(Vec::new())));
109 }
110 if file_len > usize::MAX as u64 {
111 return Err(ERR_EUVPAK_TOO_LARGE.to_string());
112 }
113 let os_map: EuvPakOsMap = map_win32_readonly(raw_handle, file_len as usize)?;
114 Ok(Self {
115 backing: EuvPakBacking::OsMapped(Arc::new(os_map)),
116 })
117 }
118 #[cfg(not(any(unix, windows)))]
119 {
120 let mut file: File = File::open(path)
121 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
122 let file_len: u64 = file
123 .seek(SeekFrom::End(0))
124 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
125 let _: u64 = file
126 .seek(SeekFrom::Start(0))
127 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
128 let mut buffer: Vec<u8> = Vec::with_capacity(file_len as usize);
129 let _: usize = file
130 .read_to_end(&mut buffer)
131 .map_err(|err: io::Error| format!("{ERR_EUVPAK_OPEN_FAILED}: {err}"))?;
132 Ok(Self::from_bytes(Arc::new(buffer)))
133 }
134 }
135
136 #[cfg(target_arch = "wasm32")]
146 pub fn open_mmap(_path: &Path) -> Result<Self, String> {
147 Err(ERR_EUVPAK_WASM_UNSUPPORTED.to_string())
148 }
149
150 pub fn advise_willneed(&self, offset: usize, len: usize) {
157 #[cfg(unix)]
158 {
159 let Self { backing } = self;
160 if let EuvPakBacking::OsMapped(os_map) = backing {
161 advise_posix_region(os_map, offset, len, POSIX_MADV_WILLNEED);
162 }
163 }
164 #[cfg(not(unix))]
165 {
166 let _: (usize, usize) = (offset, len);
167 }
168 }
169
170 pub fn advise_random(&self, offset: usize, len: usize) {
177 #[cfg(unix)]
178 {
179 let Self { backing } = self;
180 if let EuvPakBacking::OsMapped(os_map) = backing {
181 advise_posix_region(os_map, offset, len, POSIX_MADV_RANDOM);
182 }
183 }
184 #[cfg(not(unix))]
185 {
186 let _: (usize, usize) = (offset, len);
187 }
188 }
189
190 #[must_use]
196 pub fn as_bytes(&self) -> &[u8] {
197 let Self { backing } = self;
198 match backing {
199 EuvPakBacking::Owned(bytes) => bytes.as_slice(),
200 #[cfg(any(unix, windows))]
201 EuvPakBacking::OsMapped(os_map) => {
202 let EuvPakOsMap { ptr, len, .. } = **os_map;
203 unsafe { from_raw_parts(ptr.as_ptr(), len) }
204 }
205 }
206 }
207
208 #[cfg(any(unix, windows))]
214 #[must_use]
215 pub fn base_non_null(&self) -> Option<NonNull<u8>> {
216 let Self { backing } = self;
217 match backing {
218 EuvPakBacking::Owned(bytes) => {
219 if bytes.is_empty() {
220 None
221 } else {
222 NonNull::new(bytes.as_ptr().cast_mut())
223 }
224 }
225 EuvPakBacking::OsMapped(os_map) => Some(os_map.ptr),
226 }
227 }
228
229 #[must_use]
235 pub fn len(&self) -> usize {
236 self.as_bytes().len()
237 }
238
239 #[must_use]
245 pub fn is_empty(&self) -> bool {
246 self.as_bytes().is_empty()
247 }
248
249 #[must_use]
255 pub fn is_os_mapped(&self) -> bool {
256 let Self { backing } = self;
257 match backing {
258 EuvPakBacking::Owned(_) => false,
259 #[cfg(any(unix, windows))]
260 EuvPakBacking::OsMapped(_) => true,
261 }
262 }
263}