Skip to main content

vm/
vm_object.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use core::marker::{PhantomData, PhantomPinned};
6use core::ptr::NonNull;
7use kalloc::AllocError;
8
9#[repr(C)]
10/// The base vm object that holds a range of bytes of data
11///
12/// Can be created without mapping and used as a container of data, or mappable
13/// into an address space via VmAddressRegion::CreateVmMapping
14pub struct VmObject {
15    _data: [u8; 0],
16    _marker: PhantomData<PhantomPinned>,
17}
18
19unsafe extern "C" {
20    fn cpp_vm_object_get_ref_counted(vmo: *const VmObject) -> *mut fbl::RefCounted;
21    fn cpp_vm_object_free(vmo: *mut VmObject);
22}
23
24impl fbl::HasRefCount for VmObject {
25    fn ref_count(&self) -> &fbl::RefCounted {
26        unsafe { &*cpp_vm_object_get_ref_counted(self as *const _) }
27    }
28}
29
30unsafe impl fbl::Recyclable for VmObject {
31    unsafe fn recycle(ptr: NonNull<Self>) {
32        unsafe {
33            cpp_vm_object_free(ptr.as_ptr());
34        }
35    }
36
37    fn allocate(_value: Self) -> Result<NonNull<Self>, AllocError> {
38        Err(AllocError)
39    }
40}