Skip to content

Commit 14fa840

Browse files
authored
fix(lab/2): fix memory map alias (#59)
1 parent 43400f4 commit 14fa840

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

docs/labs/0x07/tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
在 Lab 4 的加分项中,提到了尝试实现帧分配器的内存回收。在本次实验中将进一步完善这一功能。
3535

36-
在进行帧分配器初始化的过程中,内核从 bootloader 获取到了一个 `MemoryMap` 数组,其中包含了所有可用的物理内存区域,并且内核使用 `into_iter()` 将这一数据结构的所有权交给了一个迭代器,你可以在 `crates/kernel/src/memory/frames.rs` 中了解到相关类型和实现。
36+
在进行帧分配器初始化的过程中,内核从 bootloader 获取到了一个 `BootMemoryMap` 数组,其中包含了所有可用的物理内存区域,并且内核使用 `into_iter()` 将这一数据结构的所有权交给了一个迭代器,你可以在 `crates/kernel/src/memory/frames.rs` 中了解到相关类型和实现。
3737

3838
迭代器是懒惰的,只有在需要时才会进行计算,因此在进行逐帧分配时,并没有额外的内存开销。但是,当需要进行内存回收时,就需要额外的数据结构来记录已经分配的帧,以便进行再次分配。
3939

src/0x01/crates/boot/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ pub use fs::*;
2525
#[macro_use]
2626
extern crate log;
2727

28+
pub type BootMemoryMap = ArrayVec<MemoryDescriptor, 256>;
29+
2830
/// This structure represents the information that the bootloader passes to the
2931
/// kernel.
3032
pub struct BootInfo {
3133
/// The memory map
32-
pub memory_map: ArrayVec<MemoryDescriptor, 256>,
34+
pub memory_map: BootMemoryMap,
3335

3436
/// The offset into the virtual address space where the physical memory is
3537
/// mapped.

src/0x02/crates/kernel/src/memory/frames.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use alloc::boxed::Box;
22

3-
use boot::{MemoryMap, MemoryType};
3+
use boot::{BootMemoryMap, MemoryType};
44
use x86_64::{
55
PhysAddr,
66
structures::paging::{FrameAllocator, FrameDeallocator, PhysFrame, Size4KiB},
@@ -28,7 +28,7 @@ impl BootInfoFrameAllocator {
2828
/// This function is unsafe because the caller must guarantee that the
2929
/// passed memory map is valid. The main requirement is that all frames
3030
/// that are marked as `USABLE` in it are really unused.
31-
pub unsafe fn init(memory_map: &MemoryMap, size: usize) -> Self {
31+
pub unsafe fn init(memory_map: &BootMemoryMap, size: usize) -> Self {
3232
BootInfoFrameAllocator {
3333
size,
3434
frames: create_frame_iter(memory_map),
@@ -58,7 +58,7 @@ impl FrameDeallocator<Size4KiB> for BootInfoFrameAllocator {
5858
}
5959
}
6060

61-
fn create_frame_iter(memory_map: &MemoryMap) -> BootInfoFrameIter {
61+
fn create_frame_iter(memory_map: &BootMemoryMap) -> BootInfoFrameIter {
6262
let iter = memory_map
6363
.clone()
6464
.into_iter()

0 commit comments

Comments
 (0)