mesa3d_util 0.1.6

Utility crate part of Mesa3D project
Documentation
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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_ptr() 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_ptr() as *mut u8
    }

    fn size(&self) -> usize {
        self.mapping.size
    }

    fn as_mesa_mapping(&self) -> MesaMapping {
        self.as_mesa_mapping()
    }
}