����λ�ã���ҳ > �����̳� > �̳� > [rCoreѧϰ�ʼ� 028] Rust �еĶ�̬�ڴ����
���� ��������֮ǰ��ѧϰC��ʱ��,�����ᵽmalloc,��������,ʹ��malloc�ֳ�������ڴ������ڶ�,��ֱ�Ӷ���ı����ڴ�����ջ. ���ǵõ���ѧϰSTM32��ʱ��CubeIDEҪ����stack ��heap�Ĵ�С. ��������Ҫ�ǵ�,��ô���õĹ���,ʵ�����Dz���ϵͳ�ڸ���ǰ��. ��ôΪ��ʵ�ֶ�̬�ڴ�
��������֮ǰ��ѧϰC��ʱ��,�����ᵽ
malloc
,��������,ʹ��
malloc
�ֳ�������ڴ�������
��
,��ֱ�Ӷ���ı����ڴ�����
Õ»
.
���ǵõ���ѧϰSTM32��ʱ��CubeIDEҪ����
stack
��
heap
�Ĵ�С.
��������Ҫ�ǵ�,��ô���õĹ���,ʵ������ ����ϵͳ�ڸ���ǰ�� .
��ôΪ��ʵ�ֶ�̬�ڴ���书��,����ϵͳ��Ҫ�����¹���:
��̬�ڴ�����ʵ�ַ��� :
Ӧ�����������һ����С��������Ӧ�õ����ж�̬�������ڴ�ռ� �C �ѣ�Heap����ͬʱ��Ӧ�û�Ҫ�ܹ�������ѹ�����������֧�������е�ʱ����������һ��ռ�����ű��������ڱ������������ڽ���֮�����ռ���Ҫ�������Դ������ʹ�á�����ѵĴ�С�̶�����ô����ʵ����һ�������ڴ�������⣬ͬѧ�ǿ���ʹ�ò���ϵͳ���������ܵ��� ���������ڴ�����㷨 ��
��̬�ڴ����ı׶�---�ڴ���Ƭ :
Ӧ�ý��ж�β�ͬ��С���ڴ������ͷŲ����󣬻�����ڴ�ռ���˷ѣ��������޷���Ӧ��ʹ�õĿ����ڴ���Ƭ��
�ڴ���Ƭ��ָ�޷��������ʹ�õĿ����ڴ�ռ䡣�ɽ�һ��ϸ��Ϊ����Ƭ������Ƭ��
���� �����ᵽ���� STD ���еĶ���ص����ݽṹ.���������Ķ����Ҵ��������ͼ.
������һ���������Ǵ������Ϣ��:
���ϲ�����˵:
���������ص�����ָ��������������� Rust �Դ���
alloc
crate ���ҵ���������ʹ�� Rust ��׼��std
��ʱ����Բ��ù������ crate ����Ϊ��׼�����Ѿ��Ѿ�ʵ����һ�׶ѹ����㷨������alloc
�����ݰ�����std
���ֿռ�֮���ÿ����߿���ֱ��ʹ�á�Ȼ������ϵͳ�ں������ڽ��ñ�׼�⣨��no_std
�������ƽ̨�ϣ����Ŀ�core
Ҳ��û�ж�̬�ڴ����Ĺ��ܣ����ʱ���Ҫ��������alloc
�ⶨ��Ľӿ���ʵ�ֻ����Ķ�̬�ڴ��������
����ʵ�����
��̬�ڴ������
,��Ϊ�Լ�ʵ�ֵ����
�ṹ��
,ʵ��
GlobalAlloc
��
Trait
.
alloc
����Ҫ�����ṩ����һ��ȫ�ֵĶ�̬�ڴ������
���������ø÷������������ѿռ䣬�Ӷ�ʹ�������ص�����ָ����������ݽṹ��������������������ԣ����ǵĶ�̬�ڴ��������Ҫʵ�����ṩ��GlobalAlloc
Trait
GlobalAlloc
�ij���ӿ�:
// alloc::alloc::GlobalAlloc
pub unsafe fn alloc(&self, layout: Layout) -> *mut u8;
pub unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout);
���Կ������������� C �����е�
malloc/free
���ֱ�����ѿռ�ķ���ͻ��գ�Ҳͬ��ʹ��һ����ָ�루Ҳ���ǵ�ַ����Ϊ����ķ���ֵ�ͻ��յIJ����������ӿ��ж���һ��alloc::alloc::Layout
���͵IJ����� ��ָ���˷�������󣬷�Ϊ�����֣��ֱ�������ռ�Ĵ�Сsize
���Լ����ص�ַ�Ķ���Ҫ��align
���������Ҫ�������һ�� 2 ���ݴΣ���λΪ�ֽ��������Ʒ��صĵ�ַ������align
�ı�����
��
os/Cargo.toml
������:
buddy_system_allocator = "0.6"
alloc
��
��
os/src/main.rs
������.
// os/src/main.rs
extern crate alloc;
����
os/src/mm/heap_allocator.rs
.
// os/src/mm/heap_allocator.rs
use buddy_system_allocator::LockedHeap;
use crate::config::KERNEL_HEAP_SIZE;
#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
pub fn init_heap() {
unsafe {
HEAP_ALLOCATOR
.lock()
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
}
}
���Կ���
ʵ����
��һ����̬����
HEAP_ALLOCATOR
.����
ʵ����
��һ������
HEAP_SPACE
��������
��
.
����.
HEAP_SPACE
�Ĵ�СΪ
KERNEL_HEAP_SIZE
.
���
KERNEL_HEAP_SIZE
��ȡ��
config
�������.
�������
����ֿ���Ĵ���
������
KERNEL_HEAP_SIZE
�Ĵ�С.
// os/src/config.rs
pub const KERNEL_HEAP_SIZE: usize = 0x30_0000;
��СΪ
3145728
.
ע����һ�εĴ���,Ҫ��ע
#[global_allocator]
����������ڴ���������ܱ�ʶ��Ϊȫ�ֶ�̬�ڴ������.
#[global_allocator]
��Ҫ
������������
,������Ҫ��
main.rs
������:
#![feature(alloc_error_handler)]
��ʱ��Ϳ�����
os/src/mm/heap_allocator.rs
�ﴴ������������:
// os/src/mm/heap_allocator.rs
#[alloc_error_handler]
pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
panic!("Heap allocation error, layout = {:?}", layout);
}
��
os/src/mm/heap_allocator.rs
�ﴴ�����Ժ���.
#[allow(unused)]
pub fn heap_test() {
use alloc::boxed::Box;
use alloc::vec::Vec;
extern "C" {
fn sbss();
fn ebss();
}
let bss_range = sbss as usize..ebss as usize;
let a = Box::new(5);
assert_eq!(*a, 5);
assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
drop(a);
let mut v: Vec = Vec::new();
for i in 0..500 {
v.push(i);
}
for i in 0..500 {
assert_eq!(v[i], i);
}
assert!(bss_range.contains(&(v.as_ptr() as usize)));
drop(v);
println!("heap_test passed!");
}
�����
#[allow(unused)]
������˼,����
��ֹ������������Ϊ������ʱ�����õĺ�������
.
����ע��ʹ����
println
,���ļ����ϱ߼�һ��
use crate::println
.
����IJ��Գ����Ȼ�ȡ��
sbss
�ĵ�
ebss
�ķ�Χ.
����ع�����
bss
�εĴ���:
bss
�α�����һ������δ��ʼ����ȫ�ֱ������ڴ�����sbss
��bss
�Ŀ�ͷebss
��bss
�Ľ�β
��ô
bss_range
ʵ������
bss
�ķ�Χ.
����
����
,����
Box::new(5)
�dz����ڶ��ϴ���
a
��ֵ,�����ֵΪ
5
.
��ô�±ߵĶ������:
assert_eq!(*a, 5);
assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
�ͺܺ�������:
- �ж�
a
��ֵ�Ƿ�Ϊ5
- �ж�
a
��ָ���Ƿ���bss
�ķ�Χ��
���IJ������Ǵ�����һ��
Vec
����,Ȼ�󴢴���
0..500
��ֵ��ȥ,���ҷֱ�ִ��������
a
�Ķ����ж�.
�������û�б���,��ô�����Ȼ�����
heap_test passed!
.
(���ע��
drop
���ڶ�(��̬�ڴ�)���ͷŵ�ij������)
��
os/src/mm
�´���
mod.rs
ʹ��
mm
���Ա�ʶ��Ϊһ����.
Ϊ��ʹ��
heap_allocator
���
init_heap
��
heap_test
,��Ҫ�����������
mod
:
// os/src/mm/mod.rs
pub mod heap_allocator;
main
����,ʵ�ֲ���
// os/src/main.rs
/// the rust entry-point of os
#[no_mangle]
pub fn rust_main() -> ! {
clear_bss();
println!("[kernel] Hello, world!");
logging::init();
println!("[kernel] logging init end");
mm::heap_allocator::init_heap();
println!("[kernel] heap init end");
mm::heap_allocator::heap_test();
println!("heap test passed");
trap::init();
println!("[kernel] trap init end");
loader::load_apps();
trap::enable_timer_interrupt();
timer::set_next_trigger();
task::run_first_task();
panic!("Unreachable in rust_main!");
}
cd os
make run
�õ����н��:
[rustsbi] RustSBI version 0.3.1, adapting to RISC-V SBI v1.0.0
.______ __ __ _______.___________. _______..______ __
| _ \ | | | | / | | / || _ \ | |
| |_) | | | | | | (----`---| |----`| (----`| |_) || |
| / | | | | \ \ | | \ \ | _ < | |
| |\ \----.| `--' |.----) | | | .----) | | |_) || |
| _| `._____| \______/ |_______/ |__| |_______/ |______/ |__|
[rustsbi] Implementation : RustSBI-QEMU Version 0.2.0-alpha.2
[rustsbi] Platform Name : riscv-virtio,qemu
[rustsbi] Platform SMP : 1
[rustsbi] Platform Memory : 0x80000000..0x88000000
[rustsbi] Boot HART : 0
[rustsbi] Device Tree Region : 0x87000000..0x87000f02
[rustsbi] Firmware Address : 0x80000000
[rustsbi] Supervisor Address : 0x80200000
[rustsbi] pmp01: 0x00000000..0x80000000 (-wr)
[rustsbi] pmp02: 0x80000000..0x80200000 (---)
[rustsbi] pmp03: 0x80200000..0x88000000 (xwr)
[rustsbi] pmp04: 0x88000000..0x00000000 (-wr)
[kernel] Hello, world!
heap_test passed!
[kernel] IllegalInstruction in application, kernel killed it.
All applications completed!
������Ϊ��log�Ƚϼ��,��
user
����Ҫ�����appֻ������һ��
user/src/bin/00hello_world.rs
.
���￴log,
heap_test passed!
,˵�����Գɹ���.
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������