xref: /aosp_15_r20/external/crosvm/rutabaga_gfx/src/rutabaga_os/shm.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2020 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::CString;
6 
7 use crate::rutabaga_os::sys::platform::SharedMemory as SysUtilSharedMemory;
8 use crate::rutabaga_os::AsRawDescriptor;
9 use crate::rutabaga_os::FromRawDescriptor;
10 use crate::rutabaga_os::IntoRawDescriptor;
11 use crate::rutabaga_os::OwnedDescriptor;
12 use crate::rutabaga_os::RawDescriptor;
13 use crate::rutabaga_utils::RutabagaResult;
14 
15 pub struct SharedMemory(pub(crate) SysUtilSharedMemory);
16 impl SharedMemory {
17     /// Creates a new shared memory object of the given size.
18     ///
19     /// |name| is purely for debugging purposes. It does not need to be unique, and it does
20     /// not affect any non-debugging related properties of the constructed shared memory.
new<T: Into<Vec<u8>>>(debug_name: T, size: u64) -> RutabagaResult<SharedMemory>21     pub fn new<T: Into<Vec<u8>>>(debug_name: T, size: u64) -> RutabagaResult<SharedMemory> {
22         let debug_name = CString::new(debug_name)?;
23         SysUtilSharedMemory::new(&debug_name, size).map(SharedMemory)
24     }
25 
size(&self) -> u6426     pub fn size(&self) -> u64 {
27         self.0.size()
28     }
29 }
30 
31 impl AsRawDescriptor for SharedMemory {
as_raw_descriptor(&self) -> RawDescriptor32     fn as_raw_descriptor(&self) -> RawDescriptor {
33         self.0.as_raw_descriptor()
34     }
35 }
36 
37 impl IntoRawDescriptor for SharedMemory {
into_raw_descriptor(self) -> RawDescriptor38     fn into_raw_descriptor(self) -> RawDescriptor {
39         self.0.into_raw_descriptor()
40     }
41 }
42 
43 impl From<SharedMemory> for OwnedDescriptor {
from(sm: SharedMemory) -> OwnedDescriptor44     fn from(sm: SharedMemory) -> OwnedDescriptor {
45         // SAFETY:
46         // Safe because we own the SharedMemory at this point.
47         unsafe { OwnedDescriptor::from_raw_descriptor(sm.into_raw_descriptor()) }
48     }
49 }
50