1 // Copyright 2022 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use std::ffi::CStr; 6 7 use win_util::create_file_mapping; 8 use winapi::um::winnt::PAGE_EXECUTE_READWRITE; 9 10 use crate::descriptor::FromRawDescriptor; 11 use crate::descriptor::SafeDescriptor; 12 use crate::shm::PlatformSharedMemory; 13 use crate::Result; 14 use crate::SharedMemory; 15 16 impl PlatformSharedMemory for SharedMemory { new(_debug_name: &CStr, size: u64) -> Result<SharedMemory>17 fn new(_debug_name: &CStr, size: u64) -> Result<SharedMemory> { 18 let mapping_handle = 19 // SAFETY: 20 // Safe because we do not provide a handle. 21 unsafe { create_file_mapping(None, size, PAGE_EXECUTE_READWRITE, None) } 22 .map_err(super::Error::from)?; 23 24 Self::from_safe_descriptor( 25 // SAFETY: 26 // Safe because we have exclusive ownership of mapping_handle & it is valid. 27 unsafe { SafeDescriptor::from_raw_descriptor(mapping_handle) }, 28 size, 29 ) 30 } 31 from_safe_descriptor(mapping_handle: SafeDescriptor, size: u64) -> Result<SharedMemory>32 fn from_safe_descriptor(mapping_handle: SafeDescriptor, size: u64) -> Result<SharedMemory> { 33 Ok(SharedMemory { 34 descriptor: mapping_handle, 35 size, 36 }) 37 } 38 } 39 40 #[cfg(test)] 41 mod test { 42 use winapi::shared::winerror::ERROR_NOT_ENOUGH_MEMORY; 43 44 use super::*; 45 46 #[cfg_attr(all(target_os = "windows", target_env = "gnu"), ignore)] 47 #[test] new_too_huge()48 fn new_too_huge() { 49 let result = SharedMemory::new("test", 0x8000_0000_0000_0000); 50 assert_eq!( 51 result.err().unwrap().errno(), 52 ERROR_NOT_ENOUGH_MEMORY as i32 53 ); 54 } 55 } 56