Skip to content

Commit dac40f7

Browse files
committed
refactor: remove lazy_static
Signed-off-by: YdrMaster <[email protected]>
1 parent 01ca0eb commit dac40f7

File tree

9 files changed

+43
-70
lines changed

9 files changed

+43
-70
lines changed

Cargo.lock

+4-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ch5/Cargo.toml

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
name = "ch5"
33
version = "0.0.1"
44
edition = "2021"
5-
authors = ["YdrMaster <ydrml@hotmail.com>"]
5+
authors = ["zflcs <1491657576@qq.com>"]
66

77
[dependencies]
88
sbi-rt = { git = "https://github.com/rustsbi/sbi-rt", branch = "dev" }
99
customizable-buddy = "0.0.2"
1010
xmas-elf = "0.8.0"
1111
riscv = "0.8.0"
1212
spin = "0.9.4"
13-
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
14-
1513

1614
console = { path = "../console" }
1715
utils = { path = "../utils" }

ch5/src/loader.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
use alloc::vec::Vec;
2-
/// 根据应用名加载用户进程
3-
use lazy_static::*;
2+
use spin::Lazy;
43

5-
lazy_static! {
6-
static ref APP_NAMES: Vec<&'static str> = {
7-
extern "C" {
8-
static apps: utils::AppMeta;
9-
fn app_names();
10-
}
11-
let app_num = unsafe { apps.get_app_num() };
12-
let mut start = app_names as usize as *const u8;
13-
let mut v = Vec::new();
14-
unsafe {
15-
for _ in 0..app_num {
16-
let mut end = start;
17-
while end.read_volatile() != b'\0' {
18-
end = end.add(1);
19-
}
20-
let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
21-
let str = core::str::from_utf8(slice).unwrap();
22-
v.push(str);
23-
start = end.add(1);
4+
/// 根据应用名加载用户进程
5+
static APP_NAMES: Lazy<Vec<&'static str>> = Lazy::new(|| {
6+
extern "C" {
7+
static apps: utils::AppMeta;
8+
fn app_names();
9+
}
10+
let app_num = unsafe { apps.get_app_num() };
11+
let mut start = app_names as usize as *const u8;
12+
let mut v = Vec::new();
13+
unsafe {
14+
for _ in 0..app_num {
15+
let mut end = start;
16+
while end.read_volatile() != b'\0' {
17+
end = end.add(1);
2418
}
19+
let slice = core::slice::from_raw_parts(start, end as usize - start as usize);
20+
let str = core::str::from_utf8(slice).unwrap();
21+
v.push(str);
22+
start = end.add(1);
2523
}
26-
v
27-
};
28-
}
24+
}
25+
v
26+
});
2927

3028
/// 获取应用程序 elf 数据
3129
pub fn get_app_data(app_name: &str) -> Option<&'static [u8]> {

ch6/Cargo.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
name = "ch6"
33
version = "0.0.1"
44
edition = "2021"
5-
authors = ["YdrMaster <[email protected]>", "tkf2019 <[email protected]>"]
5+
authors = ["tkf2019 <[email protected]>"]
66

77
[dependencies]
8+
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "4ee80e5" }
89
sbi-rt = { git = "https://github.com/rustsbi/sbi-rt", branch = "dev" }
910
customizable-buddy = "0.0.2"
1011
xmas-elf = "0.8.0"
1112
riscv = "0.8.0"
12-
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
13-
virtio-drivers = { git = "https://github.com/rcore-os/virtio-drivers", rev = "4ee80e5" }
1413
spin = "0.7.0"
1514

1615
console = { path = "../console" }
@@ -19,5 +18,4 @@ kernel-context = { path = "../kernel-context" }
1918
kernel-vm = { path = "../kernel-vm" }
2019
syscall = { path = "../syscall", features = ["kernel"] }
2120
task-manage = { path = "../task-manage" }
22-
2321
easy-fs = { path = "../easy-fs" }

ch6/src/fs.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use alloc::sync::Arc;
2-
use alloc::{string::String, vec::Vec};
1+
use alloc::{string::String, sync::Arc, vec::Vec};
32
use easy_fs::{EasyFileSystem, FSManager, FileHandle, Inode, OpenFlags};
4-
use lazy_static::*;
3+
use spin::Lazy;
54

65
use crate::virtio_block::BLOCK_DEVICE;
76

@@ -59,9 +58,7 @@ impl FileSystem {
5958
}
6059
}
6160

62-
lazy_static! {
63-
pub static ref FS: Arc<FileSystem> = Arc::new(FileSystem::new());
64-
}
61+
pub static FS: Lazy<Arc<FileSystem>> = Lazy::new(|| Arc::new(FileSystem::new()));
6562

6663
pub fn read_all(fd: Arc<FileHandle>) -> Vec<u8> {
6764
let mut offset = 0usize;

ch6/src/virtio_block.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1+
use crate::{mm::PAGE, KERNEL_SPACE};
12
use alloc::sync::Arc;
23
use core::{alloc::Layout, ptr::NonNull};
34
use easy_fs::BlockDevice;
45
use kernel_vm::page_table::{MmuMeta, Sv39, VAddr, VmFlags};
5-
use lazy_static::*;
6-
use spin::Mutex;
6+
use spin::{Lazy, Mutex};
77
use virtio_drivers::{Hal, VirtIOBlk, VirtIOHeader};
88

9-
use crate::{mm::PAGE, KERNEL_SPACE};
10-
119
const VIRTIO0: usize = 0x10001000;
1210

1311
pub struct VirtIOBlock(Mutex<VirtIOBlk<'static, VirtioHal>>);
@@ -76,6 +74,4 @@ impl Hal for VirtioHal {
7674
}
7775
}
7876

79-
lazy_static! {
80-
pub static ref BLOCK_DEVICE: Arc<dyn BlockDevice> = Arc::new(VirtIOBlock::new());
81-
}
77+
pub static BLOCK_DEVICE: Lazy<Arc<dyn BlockDevice>> = Lazy::new(|| Arc::new(VirtIOBlock::new()));

easy-fs/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ edition = "2018"
88

99
[dependencies]
1010
spin = "0.7.0"
11-
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
1211
bitflags = "1.2.1"

easy-fs/src/block_cache.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::{BlockDevice, BLOCK_SZ};
2-
use alloc::collections::VecDeque;
3-
use alloc::sync::Arc;
4-
use lazy_static::*;
5-
use spin::Mutex;
2+
use alloc::{collections::VecDeque, sync::Arc};
3+
use spin::{Lazy, Mutex};
4+
65
/// Cached block inside memory
76
pub struct BlockCache {
87
/// cached block data
@@ -121,11 +120,10 @@ impl BlockCacheManager {
121120
}
122121
}
123122

124-
lazy_static! {
125-
/// The global block cache manager
126-
pub static ref BLOCK_CACHE_MANAGER: Mutex<BlockCacheManager> =
127-
Mutex::new(BlockCacheManager::new());
128-
}
123+
/// The global block cache manager
124+
pub static BLOCK_CACHE_MANAGER: Lazy<Mutex<BlockCacheManager>> =
125+
Lazy::new(|| Mutex::new(BlockCacheManager::new()));
126+
129127
/// Get the block cache corresponding to the given block id and block device
130128
pub fn get_block_cache(
131129
block_id: usize,

task-manage/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "task-manage"
33
version = "0.1.0"
44
edition = "2021"
5+
authors = ["zflcs <[email protected]>"]
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
78

0 commit comments

Comments
 (0)