You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-15 22:00:26 +02:00
Add page about buddy_system_allocator.
This commit is contained in:
@ -270,7 +270,7 @@
|
||||
- [Useful crates](bare-metal/useful-crates.md)
|
||||
- [zerocopy](bare-metal/useful-crates/zerocopy.md)
|
||||
- [aarch64-paging](bare-metal/useful-crates/aarch64-paging.md)
|
||||
- [buddy_system_allocator]()
|
||||
- [buddy_system_allocator](bare-metal/useful-crates/buddy_system_allocator.md)
|
||||
- [tinyvec]()
|
||||
- [spin and once_cell]()
|
||||
- [Android]()
|
||||
|
@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "allocator-example"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
buddy_system_allocator = "0.9.0"
|
28
src/bare-metal/useful-crates/allocator-example/src/main.rs
Normal file
28
src/bare-metal/useful-crates/allocator-example/src/main.rs
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// ANCHOR: main
|
||||
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);
|
||||
}
|
21
src/bare-metal/useful-crates/buddy_system_allocator.md
Normal file
21
src/bare-metal/useful-crates/buddy_system_allocator.md
Normal file
@ -0,0 +1,21 @@
|
||||
# `buddy_system_allocator`
|
||||
|
||||
[`buddy_system_allocator`][1] is a third-party crate implementing a basic buddy system allocator.
|
||||
It can be used both for [`LockedHeap`][2] implementing [`GlobalAlloc`][3] so you can use the
|
||||
standard `alloc` crate (as we saw [before][4]), or for allocating other address space. For example,
|
||||
we might want to allocate MMIO space for PCI BARs:
|
||||
|
||||
```rust,editable,compile_fail
|
||||
{{#include allocator-example/src/main.rs:main}}
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
* PCI BARs always have alignment equal to their size.
|
||||
|
||||
</details>
|
||||
|
||||
[1]: https://crates.io/crates/buddy_system_allocator
|
||||
[2]: https://docs.rs/buddy_system_allocator/0.9.0/buddy_system_allocator/struct.LockedHeap.html
|
||||
[3]: https://doc.rust-lang.org/core/alloc/trait.GlobalAlloc.html
|
||||
[4]: ../alloc.md
|
Reference in New Issue
Block a user