mesa/src/util/rust/memory_mapping.rs
Gurchetan Singh e0b1193361 mesa: import mesa3d_util
This is intended to a Rust analog to "src/util", which has
many utilities used by Mesa developers.  This is mostly
a copy of ${crosvm} rutabaga_gfx/src/rutabaga_os (which will
be deleted in favor of this crate).

Key constructs include:
    - SharedMemory
    - MemoryMapping
    - Tube (sockets, essentially)
    - OwnedDescriptor (HANDLE or fd-based)
    - WaitContext (epoll, ..)

As one would expect, Linux implementations are the most complete.

Acked-by: Aaron Ruby <aruby@qnx.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35210>
2025-06-17 22:28:54 +00:00

54 lines
1.4 KiB
Rust

// Copyright 2025 Google
// SPDX-License-Identifier: MIT
use crate::defines::MappedRegion;
use crate::sys::platform::MemoryMapping as PlatformMapping;
use crate::MesaMapping;
use crate::MesaResult;
use crate::OwnedDescriptor;
pub struct MemoryMapping {
mapping: PlatformMapping,
}
impl MemoryMapping {
pub fn from_safe_descriptor(
descriptor: OwnedDescriptor,
size: usize,
map_info: u32,
) -> MesaResult<MemoryMapping> {
let mapping = PlatformMapping::from_safe_descriptor(descriptor, size, map_info)?;
Ok(MemoryMapping { mapping })
}
pub fn from_offset(
descriptor: &OwnedDescriptor,
offset: usize,
size: usize,
) -> MesaResult<MemoryMapping> {
let mapping = PlatformMapping::from_offset(descriptor, offset, size)?;
Ok(MemoryMapping { mapping })
}
pub fn as_mesa_mapping(&self) -> MesaMapping {
MesaMapping {
ptr: self.mapping.addr as u64,
size: self.mapping.size as u64,
}
}
}
// SAFETY: Safe since these functions just access the MemoryMapping structure.
unsafe impl MappedRegion for MemoryMapping {
fn as_ptr(&self) -> *mut u8 {
self.mapping.addr as *mut u8
}
fn size(&self) -> usize {
self.mapping.size
}
fn as_mesa_mapping(&self) -> MesaMapping {
self.as_mesa_mapping()
}
}