BurritOS/src/utility/objaddr.rs

69 lines
1.9 KiB
Rust
Raw Normal View History

2023-04-05 16:09:06 +02:00
//! Burritos stores a data structure associating object ids with
//! their references. The ObjAddr struct
//! allows to maintain this data structure.
use std::collections::HashMap;
2023-04-12 13:25:33 +02:00
use crate::kernel::synch::{ Semaphore, Lock };
2023-04-05 16:09:06 +02:00
/// Brief Definition of object identifiers:
///
/// The struct stores the list of created objects (Semaphore, Lock, ...) and for each of them
/// associates an object identifier than can be passed to subsequent
/// system calls on the object.
///
/// A method allows to detect of an object corresponding to a given
/// identifier exists; this is used to check the parameters of system
/// calls.
2023-04-12 13:25:33 +02:00
pub struct ObjAddr {
2023-04-05 16:09:06 +02:00
last_id: i32,
2023-04-12 13:25:33 +02:00
semaphores: HashMap<i32, Semaphore>,
locks: HashMap<i32, Lock>
2023-04-05 16:09:06 +02:00
}
2023-04-12 13:25:33 +02:00
impl ObjAddr {
2023-04-05 16:09:06 +02:00
/// Initializes and returns a ObjAddr struct
pub fn init() -> Self {
Self {
last_id: 3,
2023-04-12 13:25:33 +02:00
semaphores: HashMap::<i32, Semaphore>::new(),
locks: HashMap::<i32, Lock>::new()
2023-04-05 16:09:06 +02:00
}
}
2023-04-12 13:25:33 +02:00
/// Adds the **obj** Semaphore to self
pub fn add_semaphore(&mut self, obj: Semaphore) -> i32 {
self.last_id = self.last_id + 1;
self.semaphores.insert(self.last_id, obj);
self.last_id
}
/// Adds the **obj** Lock to self
pub fn add_lock(&mut self, obj: Lock) -> i32 {
2023-04-05 16:09:06 +02:00
self.last_id = self.last_id + 1;
2023-04-12 13:25:33 +02:00
self.locks.insert(self.last_id, obj);
2023-04-05 16:09:06 +02:00
self.last_id
}
2023-04-12 13:25:33 +02:00
/// Searches for a semaphore of id **id** in self
pub fn search_semaphore(&self, id: i32) -> Option<&mut Semaphore> {
self.semaphores.get(&id)
}
/// Searches for a lock of id **id** in self
pub fn search_lock(&self, id:i32) -> Option<&mut Lock> {
self.locks.get(&id)
2023-04-05 16:09:06 +02:00
}
/// Removes the object of id **id** from self if it exists
2023-04-12 13:25:33 +02:00
pub fn remove_semaphore(&mut self, id: i32) {
self.semaphores.remove(&id)
2023-04-05 16:09:06 +02:00
}
2023-04-12 13:25:33 +02:00
pub fn remove_semaphore(&mut self, id:i32) {
self.locks.remove(&id)
}
2023-04-05 16:09:06 +02:00
}