/rust/registry/src/index.crates.io-1949cf8c6b5b557f/mesa3d_util-0.1.75/shm.rs
Line | Count | Source |
1 | | // Copyright 2025 Google |
2 | | // SPDX-License-Identifier: MIT |
3 | | |
4 | | use std::ffi::CString; |
5 | | |
6 | | use crate::sys::platform::page_size; |
7 | | use crate::sys::platform::SharedMemory as PlatformSharedMemory; |
8 | | use crate::AsRawDescriptor; |
9 | | use crate::FromRawDescriptor; |
10 | | use crate::IntoRawDescriptor; |
11 | | use crate::MesaError; |
12 | | use crate::MesaResult; |
13 | | use crate::OwnedDescriptor; |
14 | | use crate::RawDescriptor; |
15 | | |
16 | | pub struct SharedMemory(pub(crate) PlatformSharedMemory); |
17 | | impl SharedMemory { |
18 | | /// Creates a new shared memory object of the given size. |
19 | | /// |
20 | | /// |name| is purely for debugging purposes. It does not need to be unique, and it does |
21 | | /// not affect any non-debugging related properties of the constructed shared memory. |
22 | 0 | pub fn new<T: Into<Vec<u8>>>(debug_name: T, size: u64) -> MesaResult<SharedMemory> { |
23 | 0 | let debug_name = CString::new(debug_name)?; |
24 | 0 | PlatformSharedMemory::new(&debug_name, size).map(SharedMemory) |
25 | 0 | } Unexecuted instantiation: <mesa3d_util::shm::SharedMemory>::new::<&str> Unexecuted instantiation: <mesa3d_util::shm::SharedMemory>::new::<_> |
26 | | |
27 | 0 | pub fn size(&self) -> u64 { |
28 | 0 | self.0.size() |
29 | 0 | } |
30 | | } |
31 | | |
32 | | impl AsRawDescriptor for SharedMemory { |
33 | 0 | fn as_raw_descriptor(&self) -> RawDescriptor { |
34 | 0 | self.0.as_raw_descriptor() |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl IntoRawDescriptor for SharedMemory { |
39 | 0 | fn into_raw_descriptor(self) -> RawDescriptor { |
40 | 0 | self.0.into_raw_descriptor() |
41 | 0 | } |
42 | | } |
43 | | |
44 | | impl From<SharedMemory> for OwnedDescriptor { |
45 | 0 | fn from(sm: SharedMemory) -> OwnedDescriptor { |
46 | | // SAFETY: |
47 | | // Safe because we own the SharedMemory at this point. |
48 | 0 | unsafe { OwnedDescriptor::from_raw_descriptor(sm.into_raw_descriptor()) } |
49 | 0 | } |
50 | | } |
51 | | |
52 | | /// Uses the system's page size in bytes to round the given value up to the nearest page boundary. |
53 | 0 | pub fn round_up_to_page_size(v: u64) -> MesaResult<u64> { |
54 | 0 | v.checked_next_multiple_of(page_size()? as _) |
55 | 0 | .ok_or(MesaError::WithContext("rounding up caused overflow")) |
56 | 0 | } |