From 8ee7470dc58185b0cf0086fb6b039c8ab6eeb050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Wed, 5 Apr 2023 16:09:06 +0200 Subject: [PATCH] Added objaddr --- src/utility/mod.rs | 3 ++- src/utility/objaddr.rs | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/utility/objaddr.rs 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