Skip to content

Commit

Permalink
fix: remove bad code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed May 8, 2024
1 parent ab08770 commit 68aea4c
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 156 deletions.
34 changes: 13 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ syscall-table = { git = "https://github.com/os-module/syscall-table.git" }
smpscheduler = { git = "https://github.com/os-module/smpscheduler.git" }
page-table = { git = "https://github.com/os-module/page-table.git", branch = "dev" }
netcore = { git = "https://github.com/os-module/simple-net" }

small-index = { git = "https://github.com/os-module/small-index" }

[features]
default = ["smp","test"]
Expand Down
59 changes: 0 additions & 59 deletions kernel/src/task/heap.rs

This file was deleted.

4 changes: 2 additions & 2 deletions kernel/src/task/kthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use crate::{
mm::map::MMapInfo,
task::{
context::Context,
heap::HeapInfo,
resource::{HeapInfo, TidHandle},
stack::Stack,
task::{TaskInner, TaskTimer, TidHandle},
task::{TaskInner, TaskTimer},
FsContext, StatisticalData, Task, TaskState, GLOBAL_TASK_MANAGER,
},
};
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{fs::read_all, task::schedule::schedule_now};

mod context;
mod cpu;
mod heap;
mod kthread;
mod resource;
pub mod schedule;
mod stack;
mod task;
Expand Down
55 changes: 55 additions & 0 deletions kernel/src/task/resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use config::MAX_THREAD_NUM;
use ksync::Mutex;
use small_index::IndexAllocator;
use spin::Lazy;

/// 这里把MinimalManager复用为tid分配器,通常,MinimalManager会将数据插入到最小可用位置并返回位置,
/// 但tid的分配并不需要实际存储信息,因此可以插入任意的数据,这里为了节省空间,将数据定义为u8
pub static TID_MANAGER: Lazy<Mutex<IndexAllocator<MAX_THREAD_NUM>>> =
Lazy::new(|| Mutex::new(IndexAllocator::new()));
/// 用于存储线程的tid
#[derive(Debug)]
pub struct TidHandle(pub usize);

impl TidHandle {
/// 获取一个新的线程 tid (来自于 `TID_MANAGER` 分配)
pub fn new() -> Option<Self> {
let tid = TID_MANAGER.lock().allocate().ok();
tid.map(|tid| TidHandle(tid))
}
#[allow(unused)]
pub fn raw(&self) -> usize {
self.0
}
}

impl Drop for TidHandle {
fn drop(&mut self) {
TID_MANAGER.lock().deallocate(self.0).unwrap();
}
}

/// 记录进程的堆空间的相关信息
#[derive(Debug, Clone)]
pub struct HeapInfo {
/// 堆使用到的位置
pub current: usize,
/// 堆空间的起始位置
pub start: usize,
/// 堆空间的末尾位置
pub end: usize,
}

impl HeapInfo {
pub fn new(start: usize, end: usize) -> Self {
HeapInfo {
current: start,
start,
end,
}
}

pub fn contains(&self, addr: usize) -> bool {
addr >= self.start && addr < self.end
}
}
33 changes: 5 additions & 28 deletions kernel/src/task/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use page_table::{
pte::MappingFlags,
table::Sv39PageTable,
};
use spin::Lazy;
use timer::{read_timer, ITimerVal, TimeNow, ToClock};
use vfs::kfile::File;
use vfscore::dentry::VfsDentry;
Expand All @@ -50,38 +49,16 @@ use crate::{
},
map::{MMapInfo, MMapRegion, ProtFlags},
},
task::{context::Context, heap::HeapInfo, stack::Stack},
task::{
context::Context,
resource::{HeapInfo, TidHandle},
stack::Stack,
},
trap::{trap_common_read_file, trap_return, user_trap_vector, TrapFrame},
};

type FdManager = MinimalManager<Arc<dyn File>>;

/// 这里把MinimalManager复用为tid分配器,通常,MinimalManager会将数据插入到最小可用位置并返回位置,
/// 但tid的分配并不需要实际存储信息,因此可以插入任意的数据,这里为了节省空间,将数据定义为u8
pub static TID_MANAGER: Lazy<Mutex<MinimalManager<u8>>> =
Lazy::new(|| Mutex::new(MinimalManager::new(MAX_THREAD_NUM)));

/// 用于存储线程的tid
#[derive(Debug)]
pub struct TidHandle(pub usize);

impl TidHandle {
/// 获取一个新的线程 tid (来自于 `TID_MANAGER` 分配)
pub fn new() -> Option<Self> {
let tid = TID_MANAGER.lock().insert(0);
if tid.is_err() {
return None;
}
Some(Self(tid.unwrap()))
}
}

impl Drop for TidHandle {
fn drop(&mut self) {
TID_MANAGER.lock().remove(self.0).unwrap();
}
}

#[derive(Debug)]
pub struct Task {
/// 任务的唯一标识
Expand Down
10 changes: 1 addition & 9 deletions subsystems/mem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,16 @@ ksync = { path = "../ksync" }
pager = { git = "https://github.com/os-module/pager", default-features = false, optional = true }
platform = { path = "../platform" }
log = "0"
talc = { version = "1.0", optional = true }
talc = { version = "4", optional = true }
buddy_system_allocator = { version = "0.9.0", optional = true }
rslab = { version = "0.2.1", optional = true }

spin = "0"

page-table = { git = "https://github.com/os-module/page-table.git", branch = "dev" }

#page_table = { git = "https://github.com/rcore-os/arceos",rev = "7eeebc5" }
#memory_addr = { git = "https://github.com/rcore-os/arceos",rev = "7eeebc5" }



[features]
default = ["pager_bitmap","talloc"]
pager_buddy = ["pager/buddy"]
pager_bitmap = ["pager/bitmap"]
slab = ["rslab"]
talloc = ["talc"]
buddy = ["buddy_system_allocator"]
initrd = []
36 changes: 4 additions & 32 deletions subsystems/mem/src/heap.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
use core::alloc::GlobalAlloc;

#[cfg(feature = "buddy")]
use buddy_system_allocator::LockedHeap;
use config::FRAME_SIZE;
use ksync::Mutex;
use log::trace;
#[cfg(feature = "rslab")]
use rslab::{init_slab_system, SlabAllocator};
#[cfg(feature = "talloc")]
use talc::{Talc, Talck};

use crate::frame::{alloc_frames, free_frames};
use crate::{frame::alloc_frames, free_frames};

pub struct HeapAllocator {
#[cfg(feature = "talloc")]
allocator: Mutex<Talck>,
#[cfg(feature = "buddy")]
allocator: Mutex<LockedHeap<32>>,
#[cfg(feature = "slab")]
allocator: Mutex<SlabAllocator>,
allocator: ksync::Mutex<LockedHeap<32>>,
}

unsafe impl GlobalAlloc for HeapAllocator {
unsafe impl core::alloc::GlobalAlloc for HeapAllocator {
unsafe fn alloc(&self, layout: core::alloc::Layout) -> *mut u8 {
if layout.size() >= 5 * 1024 * 1024 {
let need_page = (layout.size() + FRAME_SIZE - 1) / FRAME_SIZE;
Expand All @@ -45,31 +32,16 @@ unsafe impl GlobalAlloc for HeapAllocator {
impl HeapAllocator {
pub const fn new() -> Self {
Self {
#[cfg(feature = "talloc")]
allocator: Mutex::new(Talc::new().spin_lock()),
#[cfg(feature = "buddy")]
allocator: Mutex::new(LockedHeap::<32>::new()),
#[cfg(feature = "slab")]
allocator: Mutex::new(SlabAllocator),
allocator: ksync::Mutex::new(LockedHeap::<32>::new()),
}
}
pub fn init(&self, heap: &mut [u8]) {
#[cfg(feature = "talloc")]
unsafe {
self.allocator.lock().talc().init(heap.into())
}
#[cfg(feature = "buddy")]
unsafe {
self.allocator
.lock()
.lock()
.init(heap.as_mut_ptr() as usize, heap.len())
}
#[cfg(feature = "slab")]
unsafe {
init_slab_system(FRAME_SIZE, 64);
}
#[cfg(any(feature = "talc", feature = "buddy"))]
println!("Kernel Heap size: {:#x}MB", heap.len() / 1024 / 1024);
}
}
Loading

0 comments on commit 68aea4c

Please sign in to comment.