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.
45use std::ffi::CString;
67use crate::rutabaga_os::sys::platform::SharedMemory as SysUtilSharedMemory;
8use crate::rutabaga_os::AsRawDescriptor;
9use crate::rutabaga_os::FromRawDescriptor;
10use crate::rutabaga_os::IntoRawDescriptor;
11use crate::rutabaga_os::RawDescriptor;
12use crate::rutabaga_os::SafeDescriptor;
13use crate::rutabaga_utils::RutabagaResult;
1415pub struct SharedMemory(pub(crate) SysUtilSharedMemory);
16impl 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.
21pub fn new<T: Into<Vec<u8>>>(debug_name: T, size: u64) -> RutabagaResult<SharedMemory> {
22let debug_name = CString::new(debug_name)?;
23 SysUtilSharedMemory::new(&debug_name, size).map(SharedMemory)
24 }
2526pub fn size(&self) -> u64 {
27self.0.size()
28 }
29}
3031impl AsRawDescriptor for SharedMemory {
32fn as_raw_descriptor(&self) -> RawDescriptor {
33self.0.as_raw_descriptor()
34 }
35}
3637impl IntoRawDescriptor for SharedMemory {
38fn into_raw_descriptor(self) -> RawDescriptor {
39self.0.into_raw_descriptor()
40 }
41}
4243impl From<SharedMemory> for SafeDescriptor {
44fn from(sm: SharedMemory) -> SafeDescriptor {
45// SAFETY:
46 // Safe because we own the SharedMemory at this point.
47unsafe { SafeDescriptor::from_raw_descriptor(sm.into_raw_descriptor()) }
48 }
49}