Skip to content

Commit

Permalink
对代码进行了格式化并加入了格式化的脚本 chyyuu#23
Browse files Browse the repository at this point in the history
  • Loading branch information
LyricZhao committed Jun 23, 2020
1 parent 720bc43 commit cba50f6
Show file tree
Hide file tree
Showing 25 changed files with 96 additions and 63 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ run:

clean:
@make -C user clean
@make -C os clean
@make -C os clean

fmt:
@cd os && cargo fmt
@cd os/src/algorithm && cargo fmt
@cd user && cargo fmt
2 changes: 1 addition & 1 deletion os/src/algorithm/src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pub use segment_tree_allocator::SegmentTreeAllocator;
pub use stacked_allocator::StackedAllocator;

/// 默认使用的分配器
pub type AllocatorImpl = StackedAllocator;
pub type AllocatorImpl = StackedAllocator;
2 changes: 1 addition & 1 deletion os/src/algorithm/src/scheduler/hrrn_scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<ThreadType: Clone + Eq> Scheduler<ThreadType> for HrrnScheduler<ThreadType>
self.current_time += 1;

// 遍历线程池,返回响应比最高者
let current_time = self.current_time; // borrow-check
let current_time = self.current_time; // borrow-check
if let Some(best) = self.pool.iter_mut().max_by(|x, y| {
((current_time - x.birth_time) * y.service_count)
.cmp(&((current_time - y.birth_time) * x.service_count))
Expand Down
6 changes: 3 additions & 3 deletions os/src/algorithm/src/scheduler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ mod fifo_scheduler;
mod hrrn_scheduler;

/// 线程调度器
///
///
/// `ThreadType` 应为 `Arc<Thread>`
///
///
/// ### 使用方法
/// - 在每一个时间片结束后,调用 [`Scheduler::get_next()`] 来获取下一个时间片应当执行的线程。
/// 这个线程可能是上一个时间片所执行的线程。
Expand All @@ -26,4 +26,4 @@ pub trait Scheduler<ThreadType: Clone + Eq>: Default {
pub use fifo_scheduler::FifoScheduler;
pub use hrrn_scheduler::HrrnScheduler;

pub type SchedulerImpl<T> = HrrnScheduler<T>;
pub type SchedulerImpl<T> = HrrnScheduler<T>;
2 changes: 1 addition & 1 deletion os/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ mod inode_ext;
mod stdin;
mod stdout;

pub use rcore_fs::{dev::block_cache::BlockCache, vfs::*};
pub use config::*;
pub use inode_ext::INodeExt;
pub use rcore_fs::{dev::block_cache::BlockCache, vfs::*};
pub use stdin::STDIN;
pub use stdout::STDOUT;

Expand Down
8 changes: 4 additions & 4 deletions os/src/fs/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::*;
use alloc::collections::VecDeque;

lazy_static!{
lazy_static! {
pub static ref STDIN: Arc<Stdin> = Default::default();
}

Expand Down Expand Up @@ -39,12 +39,12 @@ impl INode for Stdin {
}
}

fn poll(&self) -> Result<PollStatus> {
/// Write bytes at `offset` from `buf`, return the number of bytes written.
fn write_at(&self, _offset: usize, _buf: &[u8]) -> Result<usize> {
Err(FsError::NotSupported)
}

/// Write bytes at `offset` from `buf`, return the number of bytes written.
fn write_at(&self, _offset: usize, _buf: &[u8]) -> Result<usize> {
fn poll(&self) -> Result<PollStatus> {
Err(FsError::NotSupported)
}

Expand Down
2 changes: 1 addition & 1 deletion os/src/fs/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use super::*;

lazy_static!{
lazy_static! {
pub static ref STDOUT: Arc<Stdout> = Default::default();
}

Expand Down
9 changes: 4 additions & 5 deletions os/src/interrupt/handler.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::context::Context;
use super::timer;
use crate::process::PROCESSOR;
use crate::fs::STDIN;
use crate::kernel::syscall_handler;
use crate::memory::*;
use crate::process::PROCESSOR;
use crate::sbi::console_getchar;
use crate::fs::STDIN;
use riscv::register::{
scause::{Exception, Interrupt, Scause, Trap},
stvec, sie
sie, stvec,
};

global_asm!(include_str!("../asm/interrupt.asm"));
Expand Down Expand Up @@ -69,7 +69,6 @@ fn supervisor_timer(context: &mut Context) -> *mut Context {
timer::tick();
PROCESSOR.get().park_current_thread(context);
PROCESSOR.get().prepare_next_thread()

}

/// 出现未能解决的异常,终止当前线程
Expand All @@ -95,4 +94,4 @@ fn supervisor_external(context: &mut Context) -> *mut Context {
STDIN.push(c as u8);
}
context
}
}
2 changes: 1 addition & 1 deletion os/src/interrupt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod context;
mod handler;
mod timer;

use riscv::register::{sstatus, sie};
use riscv::register::{sie, sstatus};

pub use context::Context;

Expand Down
6 changes: 4 additions & 2 deletions os/src/kernel/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct Condvar {
impl Condvar {
/// 令当前线程休眠,等待此条件变量
pub fn wait(&self) {
self.watchers.lock().push_back(PROCESSOR.get().current_thread());
self.watchers
.lock()
.push_back(PROCESSOR.get().current_thread());
PROCESSOR.get().sleep_current_thread();
}

Expand All @@ -22,4 +24,4 @@ impl Condvar {
PROCESSOR.get().wake_thread(thread);
}
}
}
}
8 changes: 4 additions & 4 deletions os/src/kernel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
mod condvar;
mod fs;
mod syscall;
mod process;
mod syscall;

use crate::interrupt::*;
use crate::process::*;
use alloc::sync::Arc;
use spin::Mutex;
pub(self) use syscall::*;
pub(self) use fs::*;
pub(self) use process::*;
use spin::Mutex;
pub(self) use syscall::*;

pub use condvar::Condvar;
pub use syscall::syscall_handler;
pub use syscall::syscall_handler;
8 changes: 6 additions & 2 deletions os/src/kernel/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
use super::*;

pub(super) fn sys_exit(code: usize) -> SyscallResult {
println!("Thread {} exit with code {}", PROCESSOR.get().current_thread().id, code);
println!(
"Thread {} exit with code {}",
PROCESSOR.get().current_thread().id,
code
);
SyscallResult::Kill
}
}
4 changes: 2 additions & 2 deletions os/src/kernel/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn syscall_handler(context: &mut Context) -> *mut Context {
SYS_READ => sys_read(args[0], args[1] as *mut u8, args[2]),
SYS_WRITE => sys_write(args[0], args[1] as *mut u8, args[2]),
SYS_EXIT => sys_exit(args[0]),
_ => unimplemented!()
_ => unimplemented!(),
};

match result {
Expand All @@ -50,4 +50,4 @@ pub fn syscall_handler(context: &mut Context) -> *mut Context {
PROCESSOR.get().prepare_next_thread()
}
}
}
}
16 changes: 11 additions & 5 deletions os/src/memory/mapping/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use crate::memory::{
address::*,
config::PAGE_SIZE,
frame::{FrameTracker, FRAME_ALLOCATOR},
mapping::{Flags, MapType, PageTable, PageTableEntry, PageTableTracker, Segment},
MemoryResult,
config::PAGE_SIZE,
};
use alloc::{vec, vec::Vec};
use core::cmp::min;
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Mapping {
}
MapType::Framed => {
// 需要再分配帧进行映射

// 记录所有成功分配的页面映射
let mut allocated_pairs = Vec::new();
for vpn in segment.page_range().iter() {
Expand All @@ -90,15 +90,21 @@ impl Mapping {
frame_address.deref_kernel(),
PAGE_SIZE,
))
.fill(0);
.fill(0);
} else {
// 拷贝时考虑区间与整页不对齐的特殊情况
// 这里默认每个字段的起始位置一定是 4K 对齐的
let copy_size = min(PAGE_SIZE, segment.range.end - VirtualAddress::from(vpn));
let copy_size =
min(PAGE_SIZE, segment.range.end - VirtualAddress::from(vpn));
(&mut *slice_from_raw_parts_mut(
frame_address.deref_kernel(),
copy_size,
)).copy_from_slice(&data[VirtualAddress::from(vpn) - segment.range.start..VirtualAddress::from(vpn) - segment.range.start + copy_size]);
))
.copy_from_slice(
&data[VirtualAddress::from(vpn) - segment.range.start
..VirtualAddress::from(vpn) - segment.range.start
+ copy_size],
);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions os/src/memory/mapping/page_table_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ macro_rules! implement_flags {
};
}

implement_flags!{USER, user, "USER"}
implement_flags!{READABLE, readable, "READABLE"}
implement_flags!{WRITABLE, writable, "WRITABLE"}
implement_flags!{EXECUTABLE, executable, "EXECUTABLE"}
implement_flags! {USER, user, "USER"}
implement_flags! {READABLE, readable, "READABLE"}
implement_flags! {WRITABLE, writable, "WRITABLE"}
implement_flags! {EXECUTABLE, executable, "EXECUTABLE"}
4 changes: 3 additions & 1 deletion os/src/memory/mapping/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ impl Segment {

/// 将地址相应地上下取整,获得虚拟页号区间
pub fn page_range(&self) -> Range<VirtualPageNumber> {
Range::from(VirtualPageNumber::floor(self.range.start)..VirtualPageNumber::ceil(self.range.end))
Range::from(
VirtualPageNumber::floor(self.range.start)..VirtualPageNumber::ceil(self.range.end),
)
}
}
2 changes: 1 addition & 1 deletion os/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod thread;

use crate::interrupt::*;
use crate::memory::*;
use alloc::{sync::Arc, vec::Vec, vec};
use alloc::{sync::Arc, vec, vec::Vec};
use spin::{Mutex, RwLock};

pub use config::*;
Expand Down
13 changes: 8 additions & 5 deletions os/src/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ impl Process {
range.end += alloc_size;
}
// 分配物理页面,建立映射
self.memory_set.add_segment(Segment {
map_type: MapType::Framed,
range,
flags: flags | Flags::user(self.is_user),
}, None)?;
self.memory_set.add_segment(
Segment {
map_type: MapType::Framed,
range,
flags: flags | Flags::user(self.is_user),
},
None,
)?;
// 返回地址区间(使用参数 size,而非向上取整的 alloc_size)
Ok(Range::from(range.start..(range.start + size)))
}
Expand Down
10 changes: 5 additions & 5 deletions os/src/process/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ lazy_static! {
/// 线程调度和管理
///
/// 休眠线程会从调度器中移除,单独保存。在它们被唤醒之前,不会被调度器安排。
///
///
/// # 用例
/// ### 初始化并运行第一个线程
/// ```rust
/// processor.add_thread(thread);
/// processor.run();
/// unreachable!();
/// ```
///
///
/// ### 切换线程(在中断中)
/// ```rust
/// processor.park_current_thread(context);
/// processor.prepare_next_thread()
/// ```
///
///
/// ### 结束线程(在中断中)
/// ```rust
/// processor.kill_current_thread();
/// processor.prepare_next_thread()
/// ```
///
///
/// ### 休眠线程(在中断中)
/// ```rust
/// processor.park_current_thread(context);
/// processor.sleep_current_thread();
/// processor.prepare_next_thread()
/// ```
///
///
/// ### 唤醒线程
/// 线程会根据调度器分配执行,不一定会立即执行。
/// ```rust
Expand Down
2 changes: 1 addition & 1 deletion user/src/bin/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ extern crate user_lib;
pub fn main() -> usize {
println!("Hello world from user mode program!");
0
}
}
2 changes: 1 addition & 1 deletion user/src/bin/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ pub fn main() -> ! {
let string = getchars();
print!("{}", string);
}
}
}
2 changes: 1 addition & 1 deletion user/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/// 每个用户进程所用的堆大小(1M)
pub const USER_HEAP_SIZE: usize = 0x10_0000;
pub const USER_HEAP_SIZE: usize = 0x10_0000;
7 changes: 4 additions & 3 deletions user/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//! 代码与 `os` crate 中的 `console.rs` 基本相同
use crate::syscall::*;
use core::fmt::{self, Write};
use alloc::string::String;
use core::fmt::{self, Write};

/// 实现 [`core::fmt::Write`] trait 来进行格式化输出
struct Stdout;
Expand Down Expand Up @@ -50,8 +50,9 @@ pub fn getchars() -> String {
let mut buffer = [0u8; 64];
loop {
let size = sys_read(STDIN, &mut buffer);
if let Ok(string) = String::from_utf8(buffer.iter().copied().take(size as usize).collect()) {
if let Ok(string) = String::from_utf8(buffer.iter().copied().take(size as usize).collect())
{
return string;
}
}
}
}
Loading

0 comments on commit cba50f6

Please sign in to comment.