Skip to content

Commit 0e158e7

Browse files
committed
refactor(memory): optimize module structure and implement internal BitMap
- Reorganize memory module into hierarchical frame, paging, and heap submodules - Implement internal generic BitMap to remove external bitmap-allocator dependency - Migrate TaskManager to use the new BitMap for TID allocation - Add allocator sanity testing and update memory Kconfig settings
1 parent bf31786 commit 0e158e7

File tree

13 files changed

+711
-228
lines changed

13 files changed

+711
-228
lines changed

kernel/src/interrupts/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub extern "x86-interrupt" fn pagefault_handler(
8181
};
8282

8383
{
84-
let mut ms_lock = crate::memory::vmm::KERNEL_MEMORY_SET.lock();
84+
let mut ms_lock = crate::memory::paging::vmm::KERNEL_MEMORY_SET.lock();
8585
if let Some(ms) = ms_lock.as_mut() {
8686
if ms.handle_page_fault(fault_address).is_ok() {
8787
return;

kernel/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub mod config {
2929
}
3030

3131
// Re-export common memory management types and functions
32-
pub use memory::frame_allocator::{format_bytes, FrameStats, LockedFrameAllocator};
32+
pub use memory::frame::{format_bytes, FrameStats, LockedFrameAllocator};
3333
pub use memory::paging::{
3434
get_hhdm_offset, get_memory_stats, init_frame_allocator, init_offset_page_table,
3535
print_memory_stats,

kernel/src/libs/bitmap.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! Generic Bitmap Allocator
2+
//!
3+
//! A simple bitset implementation for tracking allocation status of resources.
4+
5+
/// A simple bitmap for tracking free/allocated slots.
6+
pub struct BitMap<const N: usize> {
7+
bits: [u64; N],
8+
}
9+
10+
impl<const N: usize> BitMap<N> {
11+
/// Create a new bitmap with all bits set to 0 (all slots free).
12+
pub const fn new() -> Self {
13+
Self { bits: [0; N] }
14+
}
15+
16+
/// Mark a bit as allocated (1).
17+
pub fn set(&mut self, index: usize) {
18+
let word = index / 64;
19+
let bit = index % 64;
20+
if word < N {
21+
self.bits[word] |= 1 << bit;
22+
}
23+
}
24+
25+
/// Mark a bit as free (0).
26+
pub fn clear(&mut self, index: usize) {
27+
let word = index / 64;
28+
let bit = index % 64;
29+
if word < N {
30+
self.bits[word] &= !(1 << bit);
31+
}
32+
}
33+
34+
/// Check if a bit is set (allocated).
35+
pub fn test(&self, index: usize) -> bool {
36+
let word = index / 64;
37+
let bit = index % 64;
38+
if word < N {
39+
(self.bits[word] & (1 << bit)) != 0
40+
} else {
41+
true // Out of bounds is considered allocated
42+
}
43+
}
44+
45+
/// Find the first free slot (0 bit) and mark it as allocated (1).
46+
pub fn alloc(&mut self) -> Option<usize> {
47+
for i in 0..N {
48+
if self.bits[i] != u64::MAX {
49+
// Find first trailing one bit in the inverted word
50+
// which is the first zero bit in the original word.
51+
let bit = (!self.bits[i]).trailing_zeros() as usize;
52+
self.bits[i] |= 1 << bit;
53+
return Some(i * 64 + bit);
54+
}
55+
}
56+
None
57+
}
58+
59+
/// Get total capacity of the bitmap.
60+
pub const fn capacity(&self) -> usize {
61+
N * 64
62+
}
63+
}

kernel/src/libs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod bitmap;
12
pub mod bmp;
23
pub mod initrd;
34
pub mod logger;

kernel/src/memory/Kconfig.toml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
title = "Memory Manager"
22

33
[[config]]
4-
name = "KERNEL_DEFAULT_HEAP_SIZE"
4+
name = "KERNEL_INIT_HEAP_SIZE"
55
type = "int"
66
default = 8388608
7-
desc = "The size of the heap memory (Unit: Bytes)."
8-
help = "Default heap size for the kernel."
7+
desc = "The size of the init heap memory (Unit: Bytes)."
8+
help = "Default init heap size for the kernel."
99
rust_type = "u64"
1010

1111
[[config]]
@@ -15,3 +15,11 @@ default = 4096
1515
desc = "The Page Size (Unit: Bytes)."
1616
help = "The size of per page."
1717
rust_type = "usize"
18+
19+
[[config]]
20+
name = "OOM_EXPAND_SIZE"
21+
type = "int"
22+
default = 4096
23+
desc = "The size of expand when OOM (Unit: Bytes)."
24+
help = "The size of expand when OOM. Maximum expansion is 4MB."
25+
rust_type = "u64"

0 commit comments

Comments
 (0)