Skip to main content

mesa3d_util/
memory_mapping.rs

1// Copyright 2025 Google
2// SPDX-License-Identifier: MIT
3
4use crate::defines::MappedRegion;
5use crate::sys::platform::MemoryMapping as PlatformMapping;
6use crate::MesaMapping;
7use crate::MesaResult;
8use crate::OwnedDescriptor;
9
10pub struct MemoryMapping {
11    mapping: PlatformMapping,
12}
13
14impl MemoryMapping {
15    pub fn from_safe_descriptor(
16        descriptor: OwnedDescriptor,
17        size: usize,
18        map_info: u32,
19    ) -> MesaResult<MemoryMapping> {
20        let mapping = PlatformMapping::from_safe_descriptor(descriptor, size, map_info)?;
21        Ok(MemoryMapping { mapping })
22    }
23
24    pub fn from_offset(
25        descriptor: &OwnedDescriptor,
26        offset: usize,
27        size: usize,
28    ) -> MesaResult<MemoryMapping> {
29        let mapping = PlatformMapping::from_offset(descriptor, offset, size)?;
30        Ok(MemoryMapping { mapping })
31    }
32
33    pub fn as_mesa_mapping(&self) -> MesaMapping {
34        MesaMapping {
35            ptr: self.mapping.addr as u64,
36            size: self.mapping.size as u64,
37        }
38    }
39}
40
41// SAFETY: Safe since these functions just access the MemoryMapping structure.
42unsafe impl MappedRegion for MemoryMapping {
43    fn as_ptr(&self) -> *mut u8 {
44        self.mapping.addr as *mut u8
45    }
46
47    fn size(&self) -> usize {
48        self.mapping.size
49    }
50
51    fn as_mesa_mapping(&self) -> MesaMapping {
52        self.as_mesa_mapping()
53    }
54}