1use std::alloc::Layout;
2
3use twizzler_abi::object::ObjID;
4
5use super::Allocator;
6use crate::{
7 marker::Invariant,
8 ptr::{GlobalPtr, Ref},
9};
10
11pub struct OwnedGlobalPtr<T, A: Allocator> {
13 global: GlobalPtr<T>,
14 alloc: A,
15}
16
17unsafe impl<T: Invariant, A: Allocator> Invariant for OwnedGlobalPtr<T, A> {}
18
19impl<T, A: Allocator> Drop for OwnedGlobalPtr<T, A> {
20 fn drop(&mut self) {
21 let layout = Layout::new::<T>();
22 unsafe { self.alloc.dealloc(self.global().cast(), layout) };
23 }
24}
25
26impl<T, A: Allocator> OwnedGlobalPtr<T, A> {
27 pub fn global(&self) -> GlobalPtr<T> {
28 self.global
29 }
30
31 pub unsafe fn from_global(global: GlobalPtr<T>, alloc: A) -> Self {
32 Self { global, alloc }
33 }
34
35 pub fn resolve<'a>(&'a self) -> Ref<'a, T> {
36 unsafe { self.global.resolve() }
37 }
38
39 pub fn allocator(&self) -> &A {
40 &self.alloc
41 }
42
43 pub fn id(&self) -> ObjID {
45 self.global().id()
46 }
47
48 pub fn offset(&self) -> u64 {
50 self.global().offset()
51 }
52}