1
0
mirror of https://github.com/rcore-os/rCore.git synced 2025-01-31 10:04:04 +04:00

Use snake case in memory crate

This commit is contained in:
Jiajie Chen 2020-03-24 20:12:54 +08:00
parent d5c0e0d58c
commit 8cbe626c6a
2 changed files with 45 additions and 42 deletions

View File

@ -1,5 +1,6 @@
#![cfg_attr(not(test), no_std)]
#![feature(nll)]
#![deny(non_snake_case)]
// import macros from log
use log::*;

View File

@ -1,6 +1,6 @@
use super::*;
use alloc::sync::Arc;
use alloc::collections::BTreeMap;
use alloc::sync::Arc;
use spin::Mutex;
// represent physical memory area
@ -9,39 +9,39 @@ pub struct SharedGuard<T: FrameAllocator> {
allocator: T,
pub size: usize,
// indirect mapping now. target: page_offset -> physAddr
target: BTreeMap<usize, usize>
target: BTreeMap<usize, usize>,
}
impl<T: FrameAllocator> SharedGuard<T> {
pub fn new(allocator: T) -> Self {
SharedGuard {
SharedGuard {
allocator: allocator,
size: 0,
target: BTreeMap::new()
}
target: BTreeMap::new(),
}
// size meaningful only for sys_shm
}
pub fn new_with_size(allocator: T, size: usize) -> Self {
SharedGuard {
allocator: allocator,
size: size,
target: BTreeMap::new()
target: BTreeMap::new(),
}
}
pub fn alloc(&mut self, virtAddr: usize) -> Option<usize> {
let physAddr = self.allocator.alloc().expect("failed to allocate frame");
self.target.insert(virtAddr, physAddr);
Some(physAddr)
pub fn alloc(&mut self, virt_addr: usize) -> Option<usize> {
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, virtAddr: usize) {
let physAddr = self.target.get(&virtAddr).unwrap().clone();
self.allocator.dealloc(physAddr);
self.target.remove(&virtAddr);
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<usize> {
match self.target.get(&addr) {
Some(physAddr) => Some(physAddr.clone()),
None => None
Some(phys_addr) => Some(phys_addr.clone()),
None => None,
}
//Some(self.target.get(&addr).unwrap().clone())
}
@ -49,12 +49,12 @@ impl<T: FrameAllocator> SharedGuard<T> {
impl<T: FrameAllocator> Drop for SharedGuard<T> {
fn drop(&mut self) {
let mut freeList = Vec::new();
for (virtAddr, _physAddr) in self.target.iter() {
freeList.push(virtAddr.clone());
let mut free_list = Vec::new();
for (virt_addr, _phys_addr) in self.target.iter() {
free_list.push(virt_addr.clone());
}
for virtAddr in freeList.iter() {
self.dealloc(virtAddr.clone());
for virt_addr in free_list.iter() {
self.dealloc(virt_addr.clone());
}
}
}
@ -63,8 +63,8 @@ impl<T: FrameAllocator> Drop for SharedGuard<T> {
pub struct Shared<T: FrameAllocator> {
allocator: T,
// used as an indirection layer to hack rust "mut" protection
startVirtAddr: Arc<Mutex<Option<usize>>>,
guard: Arc<Mutex<SharedGuard<T>>>
start_virt_addr: Arc<Mutex<Option<usize>>>,
guard: Arc<Mutex<SharedGuard<T>>>,
}
impl<T: FrameAllocator> MemoryHandler for Shared<T> {
@ -80,19 +80,21 @@ impl<T: FrameAllocator> MemoryHandler for Shared<T> {
// Remember that pages can be randomly delayed allocated by all sharing threads
// Take care when to use "addrOffset" instead of "addr"
// Hack
if self.startVirtAddr.lock().is_none() {
let mut initStartVirtAddr = self.startVirtAddr.lock();
*initStartVirtAddr = Some(addr);
if self.start_virt_addr.lock().is_none() {
let mut init_start_virt_addr = self.start_virt_addr.lock();
*init_start_virt_addr = Some(addr);
}
let addrOffset = addr - self.startVirtAddr.lock().unwrap();
let physAddrOpt = self.guard.lock().get(addrOffset);
if physAddrOpt.is_none() { // not mapped yet
let addr_offset = addr - self.start_virt_addr.lock().unwrap();
let phys_addr_opt = self.guard.lock().get(addr_offset);
if phys_addr_opt.is_none() {
// not mapped yet
let entry = pt.map(addr, 0);
entry.set_present(false);
attr.apply(entry);
} else { // physical memory already allocated by other process
let physAddr = physAddrOpt.unwrap().clone();
let entry = pt.map(addr, physAddr);
} else {
// physical memory already allocated by other process
let phys_addr = phys_addr_opt.unwrap().clone();
let entry = pt.map(addr, phys_addr);
attr.apply(entry)
}
}
@ -117,14 +119,14 @@ impl<T: FrameAllocator> MemoryHandler for Shared<T> {
fn handle_page_fault(&self, pt: &mut dyn PageTable, addr: VirtAddr) -> bool {
let entry = pt.get_entry(addr).expect("failed to get entry");
let addrOffset = addr - self.startVirtAddr.lock().unwrap();
let physAddrOpt = self.guard.lock().get(addrOffset);
let addr_offset = addr - self.start_virt_addr.lock().unwrap();
let phys_addr_opt = self.guard.lock().get(addr_offset);
if entry.present() {
// not a delay case
return false;
} else if physAddrOpt.is_none() {
} else if phys_addr_opt.is_none() {
// physical memory not alloced.
let frame = self.guard.lock().alloc(addrOffset).unwrap();
let frame = self.guard.lock().alloc(addr_offset).unwrap();
entry.set_target(frame);
entry.set_present(true);
entry.update();
@ -138,7 +140,7 @@ impl<T: FrameAllocator> MemoryHandler for Shared<T> {
pt.flush_cache_copy_user(addr, addr + len, false);
} else {
// physical memory alloced. update page table
let frame = physAddrOpt.unwrap().clone();
let frame = phys_addr_opt.unwrap().clone();
entry.set_target(frame);
entry.set_present(true);
entry.update();
@ -151,15 +153,15 @@ impl<T: FrameAllocator> Shared<T> {
pub fn new(allocator: T) -> Self {
Shared {
allocator: allocator.clone(),
startVirtAddr: Arc::new(Mutex::new(None)),
guard: Arc::new(Mutex::new(SharedGuard::new(allocator)))
start_virt_addr: Arc::new(Mutex::new(None)),
guard: Arc::new(Mutex::new(SharedGuard::new(allocator))),
}
}
pub fn new_with_guard(allocator: T, guard: Arc<Mutex<SharedGuard<T>>>) -> Self {
Shared {
allocator: allocator.clone(),
startVirtAddr: Arc::new(Mutex::new(None)),
guard: guard.clone()
start_virt_addr: Arc::new(Mutex::new(None)),
guard: guard.clone(),
}
}
}
}