From 6a0e25431b3bc7dc8fdfd1338f992ab658006167 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Tue, 30 Jun 2020 13:05:38 +0800 Subject: [PATCH] Some code cleanup --- crate/memory/src/memory_set/handler/shared.rs | 10 +++++++--- kernel/src/ipc/mod.rs | 6 ++++++ kernel/src/syscall/mod.rs | 5 ----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/crate/memory/src/memory_set/handler/shared.rs b/crate/memory/src/memory_set/handler/shared.rs index bacaa443..f3deae13 100644 --- a/crate/memory/src/memory_set/handler/shared.rs +++ b/crate/memory/src/memory_set/handler/shared.rs @@ -16,11 +16,12 @@ impl SharedGuard { pub fn new(allocator: T) -> Self { SharedGuard { allocator: allocator, + // size meaningful only for sys_shm size: 0, target: BTreeMap::new(), } - // size meaningful only for sys_shm } + pub fn new_with_size(allocator: T, size: usize) -> Self { SharedGuard { allocator: allocator, @@ -28,22 +29,24 @@ impl SharedGuard { target: BTreeMap::new(), } } + pub fn alloc(&mut self, virt_addr: usize) -> Option { let phys_addr = self.allocator.alloc().expect("failed to allocate frame"); self.target.insert(virt_addr, phys_addr); Some(phys_addr) } + pub fn dealloc(&mut self, virt_addr: usize) { let phys_addr = self.target.get(&virt_addr).unwrap().clone(); self.allocator.dealloc(phys_addr); self.target.remove(&virt_addr); } + pub fn get(&self, addr: usize) -> Option { match self.target.get(&addr) { Some(phys_addr) => Some(phys_addr.clone()), None => None, } - //Some(self.target.get(&addr).unwrap().clone()) } } @@ -131,7 +134,7 @@ impl MemoryHandler for Shared { entry.set_present(true); entry.update(); - //init with zero for delay mmap mode + // init with zero for delay mmap mode let data = pt.get_page_slice_mut(addr); let len = data.len(); for x in data { @@ -157,6 +160,7 @@ impl Shared { guard: Arc::new(Mutex::new(SharedGuard::new(allocator))), } } + pub fn new_with_guard(allocator: T, guard: Arc>>) -> Self { Shared { allocator: allocator.clone(), diff --git a/kernel/src/ipc/mod.rs b/kernel/src/ipc/mod.rs index 60b5b9ac..38c00c27 100644 --- a/kernel/src/ipc/mod.rs +++ b/kernel/src/ipc/mod.rs @@ -42,14 +42,17 @@ impl SemProc { self.arrays.insert(id, array); id } + /// Get a free ID fn get_free_id(&self) -> SemId { (0..).find(|i| self.arrays.get(i).is_none()).unwrap() } + /// Get an semaphore set by `id` pub fn get(&self, id: SemId) -> Option> { self.arrays.get(&id).map(|a| a.clone()) } + /// Add an undo operation pub fn add_undo(&mut self, id: SemId, num: SemNum, op: SemOp) { let old_val = *self.undos.get(&(id, num)).unwrap_or(&0); @@ -95,12 +98,14 @@ impl ShmProc { self.shm_identifiers.insert(id, shm_identifier); id } + /// Get a free ID fn get_free_id(&self) -> ShmId { (0..) .find(|i| self.shm_identifiers.get(i).is_none()) .unwrap() } + /// Get an semaphore set by `id` pub fn get(&self, id: ShmId) -> Option { self.shm_identifiers.get(&id).map(|a| a.clone()) @@ -120,6 +125,7 @@ impl ShmProc { } None } + /// Pop Shared Area pub fn pop(&mut self, id: ShmId) { self.shm_identifiers.remove(&id); diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 2323d9ee..ab82d056 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -363,11 +363,6 @@ impl Syscall<'_> { SYS_SHMAT => self.sys_shmat(args[0], args[1], args[2]), #[cfg(not(target_arch = "mips"))] SYS_SHMDT => self.sys_shmdt(args[0], args[1], args[2]), - /*SYS_SHMCTL => self.sys_shmctl( - args[0], - args[1], - args[2] /* should be shmid_ds *buf */ - ),*/ // system SYS_GETPID => self.sys_getpid(), SYS_GETTID => self.sys_gettid(),