Skip to content

Commit 9e11361

Browse files
committed
test2:complete alt_alloc!
1 parent 12041f2 commit 9e11361

File tree

10 files changed

+374
-23
lines changed

10 files changed

+374
-23
lines changed

arceos/exercises/alt_alloc/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![cfg_attr(feature = "axstd", no_std)]
2-
#![cfg_attr(feature = "axstd", no_main)]
2+
//如果启用了 axstd 特性,那么应用 #![no_std] 属性" 即不使用rust 标准库
3+
#![cfg_attr(feature = "axstd", no_main)]//不使用标准main
34

45
#[macro_use]
56
#[cfg(feature = "axstd")]
@@ -8,7 +9,7 @@ extern crate alloc;
89

910
use alloc::vec::Vec;
1011

11-
#[cfg_attr(feature = "axstd", no_mangle)]
12+
#[cfg_attr(feature = "axstd", no_mangle)]//禁止rust名称修饰
1213
fn main() {
1314
println!("Running bump tests...");
1415

arceos/modules/alt_axalloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ unsafe impl GlobalAlloc for GlobalAllocator {
9696
GlobalAllocator::dealloc(self, NonNull::new(ptr).expect("dealloc null ptr"), layout)
9797
}
9898
}
99-
99+
//满足条件时作为全局分配器 all()代表同时满足 条件1 target_os = "none" 目标系统是裸机 条件2 not(test) 不是测试环境
100100
#[cfg_attr(all(target_os = "none", not(test)), global_allocator)]
101101
static GLOBAL_ALLOCATOR: GlobalAllocator = GlobalAllocator::new();
102102

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#![no_std]
22

33
use allocator::{BaseAllocator, ByteAllocator, PageAllocator};
4+
use core::ptr::NonNull;
5+
//这个allocator是外部库 在cargo.toml中有
6+
//它定义了这三个BaseAllocator, ByteAllocator, PageAllocator trait
7+
//我只要考虑具体实现它们就好
8+
49

510
/// Early memory allocator
611
/// Use it before formal bytes-allocator and pages-allocator can work!
@@ -16,73 +21,115 @@ use allocator::{BaseAllocator, ByteAllocator, PageAllocator};
1621
/// When it goes down to ZERO, free bytes-used area.
1722
/// For pages area, it will never be freed!
1823
///
19-
pub struct EarlyAllocator<const SIZE: usize> {}
24+
pub struct EarlyAllocator<const SIZE: usize> {
25+
start:usize,//内存起始地址
26+
b_pos:usize,
27+
p_pos:usize,
28+
end:usize,//内存结束地址
29+
count:usize,//记录字节分配的次数
30+
//由于bump分配器不好归还分配区域 所以使用count来帮助
31+
}
2032

21-
impl<const SIZE: usize> EarlyAllocator<SIZE> {
33+
impl<const SIZE: usize> EarlyAllocator<SIZE> {// 创建未初始化的分配器
2234
pub const fn new() -> Self {
23-
Self {}
35+
Self {
36+
start:0,
37+
b_pos:0,
38+
p_pos:0,
39+
end:0,
40+
count:0,
41+
}
2442
}
2543
}
2644

2745
impl<const SIZE: usize> BaseAllocator for EarlyAllocator<SIZE> {
28-
fn init(&mut self, start: usize, size: usize) {
29-
todo!()
46+
fn init(&mut self, start: usize, size: usize) {//真正的初始化 运行时设置内存区域
47+
self.start=start;
48+
self.b_pos=start;
49+
self.p_pos=start+size;
50+
self.end=start+size;
51+
self.count=0;
3052
}
3153

3254
fn add_memory(&mut self, start: usize, size: usize) -> allocator::AllocResult {
55+
//扩展新的内存空间 但bump算法本身不好支持扩展
3356
todo!()
3457
}
3558
}
3659

3760
impl<const SIZE: usize> ByteAllocator for EarlyAllocator<SIZE> {
3861
fn alloc(
3962
&mut self,
40-
layout: core::alloc::Layout,
63+
layout: core::alloc::Layout,//layout是rust标准库里定义的结构体
64+
// 两个成员size: usize, // 需要分配的字节数 align: usize, // 内存对齐要求(必须是2的幂)
4165
) -> allocator::AllocResult<core::ptr::NonNull<u8>> {
42-
todo!()
66+
//AllocResult<NonNull<u8>> 是Result<NonNull<u8>, AllocError>的别名
67+
//成功返回一个nonnull非空指针 指向u8的 失败返回allocerror类型
68+
let align=layout.align();
69+
let size=layout.size();//两个量是私有的 通过方法访问
70+
//分配的内存起始地址要是align的倍数
71+
let aligned_addr = (self.b_pos + align - 1) & !(align - 1);//经典的对齐公式 !(align - 1) 创造了掩码 与之前结果按位与 将低位置零
72+
self.b_pos+=size;
73+
self.count+=1;
74+
Ok(unsafe { NonNull::new_unchecked(aligned_addr as *mut u8) })
75+
4376
}
4477

4578
fn dealloc(&mut self, pos: core::ptr::NonNull<u8>, layout: core::alloc::Layout) {
46-
todo!()
79+
//当dealloc使得 count变0时 再释放字节分配区域
80+
self.count-=1;
81+
if self.count==0 {
82+
// println!("[BUMP]重置字节区域 b_pos:{}--->{}",self.b_pos,self.start);
83+
self.b_pos=self.start;
84+
}
4785
}
4886

49-
fn total_bytes(&self) -> usize {
50-
todo!()
87+
fn total_bytes(&self) -> usize {//总内存大小
88+
self.end-self.start
5189
}
5290

53-
fn used_bytes(&self) -> usize {
54-
todo!()
91+
fn used_bytes(&self) -> usize {//已使用字节
92+
self.b_pos-self.start
5593
}
5694

57-
fn available_bytes(&self) -> usize {
58-
todo!()
95+
fn available_bytes(&self) -> usize {//可用字节 即还能分配多少字节
96+
self.p_pos-self.b_pos
5997
}
6098
}
6199

62100
impl<const SIZE: usize> PageAllocator for EarlyAllocator<SIZE> {
63101
const PAGE_SIZE: usize = SIZE;
64-
102+
//在 Rust 中,trait 里定义的常量是关联常量 需要通过类型来访问 Self::PAGE_SIZE
65103
fn alloc_pages(
66104
&mut self,
67105
num_pages: usize,
68106
align_pow2: usize,
107+
//align_pow2 表示对齐值的2的幂指数,而不是对齐值本身
69108
) -> allocator::AllocResult<usize> {
70-
todo!()
109+
// 使用 align_pow2 可以直接进行位运算
110+
let align = 1 << align_pow2; // 快速计算对齐值
111+
//计算边界
112+
let aligned_addr = (self.p_pos - align + 1) & !(align - 1);
113+
114+
//更新p_pos
115+
self.p_pos=aligned_addr-num_pages*Self::PAGE_SIZE;
116+
117+
Ok(aligned_addr)
71118
}
72119

73-
fn dealloc_pages(&mut self, pos: usize, num_pages: usize) {
120+
fn dealloc_pages(&mut self, pos: usize, num_pages: usize) {//页分配区域不会free
74121
todo!()
75122
}
76123

77124
fn total_pages(&self) -> usize {
78-
todo!()
125+
(self.end-self.start)/Self::PAGE_SIZE
79126
}
80127

81128
fn used_pages(&self) -> usize {
82-
todo!()
129+
(self.end-self.p_pos)/Self::PAGE_SIZE
83130
}
84131

85132
fn available_pages(&self) -> usize {
86-
todo!()
133+
(self.p_pos-self.b_pos)/Self::PAGE_SIZE
87134
}
88135
}

arceos/rust-toolchain.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
profile = "minimal"
33
# channel = "nightly-2024-05-02"
44
channel = "nightly-2024-09-04"
5+
# channel = "nightly-2024-10-15"
6+
# channel = "nightly-2024-08-01"
7+
# channel = "nightly-2024-07-01"
58
components = ["rust-src", "llvm-tools", "rustfmt", "clippy"]
69
targets = ["x86_64-unknown-none", "riscv64gc-unknown-none-elf", "aarch64-unknown-none", "aarch64-unknown-none-softfloat"]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
thread 'rustc' panicked at compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs:244:1:
2+
DefId(4:51 ~ allocator[45e5]::PageAllocator::PAGE_SIZE) does not have a "fn_arg_names"
3+
stack backtrace:
4+
0: 0x7013c0262f25 - std::backtrace::Backtrace::create::hecd2f73b2ce7b07a
5+
1: 0x7013be9c7695 - std::backtrace::Backtrace::force_capture::h170b9a096c05276d
6+
2: 0x7013bdb43d47 - std[23d648ca06a6b5c2]::panicking::update_hook::<alloc[5896b17b70d00093]::boxed::Box<rustc_driver_impl[c6327b079c7bb00b]::install_ice_hook::{closure#0}>>::{closure#0}
7+
3: 0x7013be9deab8 - std::panicking::rust_panic_with_hook::h0a4b940661a26423
8+
4: 0x7013be9de887 - std::panicking::begin_panic_handler::{{closure}}::h72160a9fdca8fdc8
9+
5: 0x7013be9dc4c9 - std::sys::backtrace::__rust_end_short_backtrace::hb6826ea75cd9c94b
10+
6: 0x7013be9de554 - rust_begin_unwind
11+
7: 0x7013bb898f63 - core::panicking::panic_fmt::h47b5abafd035aaaa
12+
8: 0x7013be0a97c2 - rustc_metadata[e2ce326aece1cd9a]::rmeta::decoder::cstore_impl::provide_extern::fn_arg_names::{closure#2}
13+
9: 0x7013be0a964e - rustc_metadata[e2ce326aece1cd9a]::rmeta::decoder::cstore_impl::provide_extern::fn_arg_names
14+
10: 0x7013c0734fe9 - rustc_query_impl[a11ca8e41e94db86]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a11ca8e41e94db86]::query_impl::fn_arg_names::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d764cafae524af68]::query::erase::Erased<[u8; 16usize]>>.cold
15+
11: 0x7013bf4a1055 - <rustc_query_impl[a11ca8e41e94db86]::query_impl::fn_arg_names::dynamic_query::{closure#2} as core[ad5a27a180104093]::ops::function::FnOnce<(rustc_middle[d764cafae524af68]::ty::context::TyCtxt, rustc_span[35307b2b70fcf344]::def_id::DefId)>>::call_once
16+
12: 0x7013bf6173c1 - rustc_query_system[2ff07926b95779e9]::query::plumbing::try_execute_query::<rustc_query_impl[a11ca8e41e94db86]::DynamicConfig<rustc_query_system[2ff07926b95779e9]::query::caches::DefIdCache<rustc_middle[d764cafae524af68]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[a11ca8e41e94db86]::plumbing::QueryCtxt, false>
17+
13: 0x7013bf616cae - rustc_query_impl[a11ca8e41e94db86]::query_impl::fn_arg_names::get_query_non_incr::__rust_end_short_backtrace
18+
14: 0x7013bf615b23 - rustc_middle[d764cafae524af68]::query::plumbing::query_get_at::<rustc_query_system[2ff07926b95779e9]::query::caches::DefIdCache<rustc_middle[d764cafae524af68]::query::erase::Erased<[u8; 16usize]>>>
19+
15: 0x7013be5e51a0 - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor>::try_lookup_name_relaxed
20+
16: 0x7013be5dd9d1 - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor>::smart_resolve_report_errors
21+
17: 0x7013bc10e47c - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor>::smart_resolve_path_fragment::{closure#0}
22+
18: 0x7013bfd6ead6 - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor>::smart_resolve_path_fragment
23+
19: 0x7013bfd5fab6 - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor>::resolve_expr
24+
20: 0x7013bfd60c94 - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor>::resolve_expr
25+
21: 0x7013bfd5f4d0 - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor>::resolve_block
26+
22: 0x7013bbb256d9 - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor as rustc_ast[425c09b15625e00f]::visit::Visitor>::visit_fn
27+
23: 0x7013bc1d22ef - <rustc_resolve[2e8d4a957ac066c6]::late::LateResolutionVisitor as rustc_ast[425c09b15625e00f]::visit::Visitor>::visit_item
28+
24: 0x7013c0420f6f - <rustc_resolve[2e8d4a957ac066c6]::Resolver>::resolve_crate::{closure#0}
29+
25: 0x7013c041b340 - <rustc_resolve[2e8d4a957ac066c6]::Resolver>::resolve_crate
30+
26: 0x7013bf72723e - rustc_interface[1a90780ba77f0571]::passes::resolver_for_lowering_raw
31+
27: 0x7013bf72646b - rustc_query_impl[a11ca8e41e94db86]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[a11ca8e41e94db86]::query_impl::resolver_for_lowering_raw::dynamic_query::{closure#2}::{closure#0}, rustc_middle[d764cafae524af68]::query::erase::Erased<[u8; 16usize]>>
32+
28: 0x7013bf726459 - <rustc_query_impl[a11ca8e41e94db86]::query_impl::resolver_for_lowering_raw::dynamic_query::{closure#2} as core[ad5a27a180104093]::ops::function::FnOnce<(rustc_middle[d764cafae524af68]::ty::context::TyCtxt, ())>>::call_once
33+
29: 0x7013c0173d12 - rustc_query_system[2ff07926b95779e9]::query::plumbing::try_execute_query::<rustc_query_impl[a11ca8e41e94db86]::DynamicConfig<rustc_query_system[2ff07926b95779e9]::query::caches::SingleCache<rustc_middle[d764cafae524af68]::query::erase::Erased<[u8; 16usize]>>, false, false, false>, rustc_query_impl[a11ca8e41e94db86]::plumbing::QueryCtxt, false>
34+
30: 0x7013c01739ad - rustc_query_impl[a11ca8e41e94db86]::query_impl::resolver_for_lowering_raw::get_query_non_incr::__rust_end_short_backtrace
35+
31: 0x7013bffc276e - rustc_interface[1a90780ba77f0571]::interface::run_compiler::<core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>, rustc_driver_impl[c6327b079c7bb00b]::run_compiler::{closure#0}>::{closure#1}
36+
32: 0x7013c009aed0 - std[23d648ca06a6b5c2]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[1a90780ba77f0571]::util::run_in_thread_with_globals<rustc_interface[1a90780ba77f0571]::util::run_in_thread_pool_with_globals<rustc_interface[1a90780ba77f0571]::interface::run_compiler<core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>, rustc_driver_impl[c6327b079c7bb00b]::run_compiler::{closure#0}>::{closure#1}, core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>>::{closure#0}, core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>>
37+
33: 0x7013c009b53a - <<std[23d648ca06a6b5c2]::thread::Builder>::spawn_unchecked_<rustc_interface[1a90780ba77f0571]::util::run_in_thread_with_globals<rustc_interface[1a90780ba77f0571]::util::run_in_thread_pool_with_globals<rustc_interface[1a90780ba77f0571]::interface::run_compiler<core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>, rustc_driver_impl[c6327b079c7bb00b]::run_compiler::{closure#0}>::{closure#1}, core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>>::{closure#0}, core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[ad5a27a180104093]::result::Result<(), rustc_span[35307b2b70fcf344]::ErrorGuaranteed>>::{closure#1} as core[ad5a27a180104093]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
38+
34: 0x7013c009b92b - std::sys::pal::unix::thread::Thread::new::thread_start::h17951d08fc5208d5
39+
35: 0x7013ba294ac3 - <unknown>
40+
36: 0x7013ba3268c0 - <unknown>
41+
37: 0x0 - <unknown>
42+
43+
44+
rustc version: 1.83.0-nightly (d6c8169c1 2024-09-03)
45+
platform: x86_64-unknown-linux-gnu
46+
47+
query stack during panic:
48+
#0 [fn_arg_names] looking up function parameter names for `allocator::PageAllocator::PAGE_SIZE`
49+
#1 [resolver_for_lowering_raw] getting the resolver for lowering
50+
end of query stack

0 commit comments

Comments
 (0)