akern-gkgoat-fork/kernel/src/handle.rs

75 lines
1.5 KiB
Rust

use crate::interp::objects::{Object, OBJECTS};
use {
crate::arch::hardware_random_u64,
alloc::vec::Vec,
core::fmt::{self, Formatter},
};
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
pub struct OSHandle {
pub id: u64,
}
impl OSHandle {
pub fn new_from_u64(id: u64) -> Self {
Self { id }
}
pub fn random_new() -> Self {
Self {
id: hardware_random_u64(),
}
}
}
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
pub struct Handle {
id: OSHandle,
perms: Permissions,
r#ref: usize,
}
impl Handle {
pub fn new(r#ref: usize) -> Handle {
Handle {
id: OSHandle::random_new(),
perms: Permissions::new(),
r#ref,
}
}
pub fn as_u64(&self) -> u64 {
self.id.id
}
pub fn get<R, F: for <'a> FnOnce(Option<&'a mut Object>) -> R>(&self,f: F) -> R{
let l = OBJECTS;
let mut olock = l.lock();
let a = olock.get_mut(self.r#ref).and_then(|a|{
a.as_mut()
});
return f(a);
}
}
impl fmt::Display for Handle {
fn fmt(&self, w: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
write!(w, "{:?}", self.id);
Ok(())
}
}
#[derive(PartialEq, Hash, Eq, Debug, Clone, Copy)]
pub struct Permissions {
edit_children: bool,
edit_attributes: bool,
}
impl Permissions {
pub fn new() -> Self {
Self {
edit_children: true,
edit_attributes: true,
}
}
}