Added objaddr

This commit is contained in:
François Autin 2023-04-05 16:09:06 +02:00
parent 02dd1f5ccf
commit 8ee7470dc5
No known key found for this signature in database
GPG Key ID: 343F5D382E1DD77C
2 changed files with 52 additions and 1 deletions

View File

@ -1 +1,2 @@
pub mod list;
pub mod list;
pub mod objaddr;

50
src/utility/objaddr.rs Normal file
View File

@ -0,0 +1,50 @@
//! Burritos stores a data structure associating object ids with
//! their references. The ObjAddr struct
//! allows to maintain this data structure.
use std::collections::HashMap;
pub trait SynchObj { }
/// 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.
struct ObjAddr<'a> {
last_id: i32,
map: HashMap<i32, &'a dyn SynchObj>
}
impl<'a> ObjAddr<'a> {
/// Initializes and returns a ObjAddr struct
pub fn init() -> Self {
Self {
last_id: 3,
map: HashMap::<i32, &dyn SynchObj>::new()
}
}
/// Adds the **obj** SynchObj to self
pub fn add_object(&mut self, obj: &'a dyn SynchObj) -> i32 {
self.last_id = self.last_id + 1;
self.map.insert(self.last_id, obj);
self.last_id
}
/// Searches for an object of id **id** in self
pub fn search_object(&self, id: i32) -> Option<&& dyn SynchObj> {
self.map.get(&id)
}
/// Removes the object of id **id** from self if it exists
pub fn remove_object_from_key(&mut self, id: i32) {
self.map.remove(&id);
}
}