2.3 `buddy_system_allocator`

03-buddy_system_allocator — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/bare-metal/useful-crates/buddy_system_allocator.html

2.3 buddy_system_allocator

buddy_system_allocator 是一个实现基本好友系统的 crate 分配器。它可以用来实现 GlobalAlloc(使用 LockedHeap)这样你就可以使用标准alloc板条箱(正如我们所看到的 之前),或用于分配其他地址空间(使用 FrameAllocator) 。例如,我们可能想要分配 MMIO 空间 PCI BAR:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
use buddy_system_allocator::FrameAllocator;
use core::alloc::Layout;

fn main() {
    let mut allocator = FrameAllocator::<32>::new();
    allocator.add_frame(0x200_0000, 0x400_0000);

    let layout = Layout::from_size_align(0x100, 0x100).unwrap();
    let bar = allocator
        .alloc_aligned(layout)
        .expect("Failed to allocate 0x100 byte MMIO region");
    println!("Allocated 0x100 byte MMIO region at {:#x}", bar);
}
  • PCI BAR 的对齐方式始终等于其大小。
  • 运行该示例cargo run在下面 src/bare-metal/useful-crates/allocator-example/。 (它不会运行在 由于板条箱依赖性,游乐场。)
最后修改 August 11, 2026: 更新 (70a5af133)