����λ�ã���ҳ > �����̳� > �̳� > [rCoreѧϰ�ʼ� 028] Rust �еĶ�̬�ڴ����

[rCoreѧϰ�ʼ� 028] Rust �еĶ�̬�ڴ����

��Դ������������|��ʱ�䣺2024-10-02 09:53:39 |���Ķ���196��|�� ��ǩ�� T 2 S C �� |����������

���� ��������֮ǰ��ѧϰC��ʱ��,�����ᵽmalloc,��������,ʹ��malloc�ֳ�������ڴ������ڶ�,��ֱ�Ӷ���ı����ڴ�����ջ. ���ǵõ���ѧϰSTM32��ʱ��CubeIDEҪ����stack ��heap�Ĵ�С. ��������Ҫ�ǵ�,��ô���õĹ���,ʵ�����Dz���ϵͳ�ڸ���ǰ��. ��ôΪ��ʵ�ֶ�̬�ڴ�

����

��������֮ǰ��ѧϰC��ʱ��,�����ᵽ malloc ,��������,ʹ�� malloc �ֳ�������ڴ������� �� ,��ֱ�Ӷ���ı����ڴ����� ջ .

���ǵõ���ѧϰSTM32��ʱ��CubeIDEҪ���� stack �� heap �Ĵ�С.

��������Ҫ�ǵ�,��ô���õĹ���,ʵ������ ����ϵͳ�ڸ���ǰ�� .

��ôΪ��ʵ�ֶ�̬�ڴ���书��,����ϵͳ��Ҫ�����¹���:

  • ��ʼʱ���ṩһ����ڴ�ռ���Ϊ��ʼ�ġ��ѡ�����û�з�ҳ��������£����ռ��������ڴ�ռ䣬������������ڴ�ռ䡣
  • �ṩ�ڶ��Ϸ�����ͷ��ڴ�ĺ����ӿڡ������������÷�ͨ�������ڴ溯���ӿڵõ���ַ�����Ŀ����ڴ����ж�д��Ҳ��ͨ���ͷ��ڴ溯���ӿڻ����ڴ棬�Ա��������ڴ��������
  • �ṩ���пռ�����������ڴ�����㷨������㷨�ܶ�̬��ά��һϵ�п��к��ѷ�����ڴ�飬�Ӷ���Ч�ع������п顣
  • ����ѡ���ṩ�����ڶ��ϵ����ݽṹ�Ͳ��������������������ڴ�������ͷ�

��̬�ڴ����

ʵ�ַ���

��̬�ڴ�����ʵ�ַ��� :

Ӧ�����������һ����С��������Ӧ�õ����ж�̬�������ڴ�ռ� �C �ѣ�Heap����ͬʱ��Ӧ�û�Ҫ�ܹ�������ѹ�����������֧�������е�ʱ����������һ��ռ�����ű��������ڱ������������ڽ���֮�����ռ���Ҫ�������Դ������ʹ�á�����ѵĴ�С�̶�����ô����ʵ����һ�������ڴ�������⣬ͬѧ�ǿ���ʹ�ò���ϵͳ���������ܵ��� ���������ڴ�����㷨 ��

�ڴ���Ƭ

��̬�ڴ����ı׶�---�ڴ���Ƭ :

Ӧ�ý��ж�β�ͬ��С���ڴ������ͷŲ����󣬻�����ڴ�ռ���˷ѣ��������޷���Ӧ��ʹ�õĿ����ڴ���Ƭ��

�ڴ���Ƭ��ָ�޷��������ʹ�õĿ����ڴ�ռ䡣�ɽ�һ��ϸ��Ϊ����Ƭ������Ƭ��

  • ����Ƭ���ѱ������ȥ������ij�������е�Ӧ�ã��ڴ�����ռ����Щ�����Ӧ�ò���ʹ��������ò£¬²ï¿½ï¿½ï¿½ÏµÍ³Ò²ï¿½Þ·ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½
  • ����Ƭ����û�������ȥ���������κ������е�Ӧ�ã��ڴ������������̫С���޷��������������ڴ�ռ��Ӧ�á�

STD���еĶ�̬�ڴ����

���� �����ᵽ���� STD ���еĶ���ص����ݽṹ.���������Ķ����Ҵ��������ͼ.

[rCoreѧϰ±Ê¼Ç 028] Rust ÖеĶ¯Ì¬ÄÚ´æ·ÖÅä

������һ���������Ǵ������Ϣ��:

  1. rust��̿��Ժ����ŵ�ʵ�ֶ�̬�ڴ����
  2. std���ṩ�˶�̬�ڴ�����ķ���
  3. �������ǵIJ���ϵͳ�ں�ֻ��ʹ��rust��core����ʵ��,�����Ҫ���Ӻͽ�����Щ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 �εĴ���:

  1. bss �α�����һ������δ��ʼ����ȫ�ֱ������ڴ�����
  2. 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)));

�ͺܺ�������:

  1. �ж� a ��ֵ�Ƿ�Ϊ 5
  2. �ж� a ��ָ���Ƿ��� bss �ķ�Χ��

���IJ������Ǵ�����һ�� Vec ����,Ȼ�󴢴��� 0..500 ��ֵ��ȥ,���ҷֱ�ִ�������� a �Ķ����ж�.

�������û�б���,��ô�����Ȼ����� heap_test passed! .

(���ע�� drop ���ڶ�(��̬�ڴ�)���ͷŵ�ij������)

ʹmm���ɵ���

�� 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! ,˵�����Գɹ���.

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������