diff --git a/src/utility/mod.rs b/src/utility/mod.rs index 651aed7..33f3cdb 100644 --- a/src/utility/mod.rs +++ b/src/utility/mod.rs @@ -1 +1,2 @@ -pub mod list; \ No newline at end of file +pub mod list; +pub mod objaddr; \ No newline at end of file diff --git a/src/utility/objaddr.rs b/src/utility/objaddr.rs new file mode 100644 index 0000000..d876056 --- /dev/null +++ b/src/utility/objaddr.rs @@ -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 +} + +impl<'a> ObjAddr<'a> { + + /// Initializes and returns a ObjAddr struct + pub fn init() -> Self { + Self { + last_id: 3, + map: HashMap::::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); + } + +} \ No newline at end of file