Skip to content

Commit 9ccaa42

Browse files
committed
Use talc to handle allocations.
1 parent 4c64893 commit 9ccaa42

5 files changed

Lines changed: 77 additions & 2 deletions

File tree

pixie-uefi/Cargo.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pixie-uefi/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ postcard = { version = "1.1.3", default-features = false, features = ["alloc"] }
2727
rand = { version = "0.8.5", default-features = false }
2828
rand_xoshiro = { version = "0.6.0", default-features = false }
2929
smoltcp = { version = "0.12.0", default-features = false, features = ["alloc", "proto-ipv4", "medium-ethernet", "socket-udp", "socket-tcp", "socket-dhcpv4", "async", "socket-tcp-cubic"] }
30-
uefi = { version = "0.33.0", features = ["alloc", "global_allocator", "panic_handler"] }
30+
spin = "0.10.0"
31+
talc = { version = "4.4.3", default-features = false, features = ["lock_api"] }
32+
uefi = { version = "0.33.0", features = ["panic_handler", "alloc"] }
3133

3234
[dependencies.pixie-shared]
3335
path = "../pixie-shared"

pixie-uefi/src/os/allocator.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use talc::*;
2+
use uefi::boot::MemoryType;
3+
4+
const MAX_BLOCK_SIZE: usize = 1 << 30;
5+
6+
struct AllocOnOom {
7+
next_block_size: usize,
8+
}
9+
10+
impl OomHandler for AllocOnOom {
11+
fn handle_oom(talc: &mut Talc<Self>, layout: core::alloc::Layout) -> Result<(), ()> {
12+
let bs = talc.oom_handler.next_block_size;
13+
talc.oom_handler.next_block_size =
14+
(talc.oom_handler.next_block_size * 2).min(MAX_BLOCK_SIZE);
15+
match uefi::boot::allocate_pool(MemoryType::LOADER_DATA, bs) {
16+
Err(e) => {
17+
uefi::println!(
18+
"Cannot allocate new block ({e}). Triggered by allocation {layout:?}"
19+
);
20+
return Err(());
21+
}
22+
Ok(ptr) => {
23+
let span = talc::Span::from_base_size(ptr.as_ptr(), bs);
24+
// SAFETY: the memory was just allocated, so we have exclusive access to it
25+
// and we can transfer ownership.
26+
unsafe { talc.claim(span) }?;
27+
return Ok(());
28+
}
29+
}
30+
}
31+
}
32+
33+
#[global_allocator]
34+
static ALLOCATOR: Talck<spin::Mutex<()>, AllocOnOom> = Talc::new(AllocOnOom {
35+
next_block_size: 1 << 20,
36+
})
37+
.lock();

pixie-uefi/src/os/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use uefi::{
4444
CStr16, Event, Handle, Status,
4545
};
4646

47+
mod allocator;
4748
mod boot_options;
4849
pub mod disk;
4950
pub mod error;

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ RUSTFLAGS='-C instrument-coverage' LLVM_PROFILE_FILE=../prof-out/test-pixie-shar
1919
popd
2020

2121
pushd pixie-uefi
22-
RUSTFLAGS='-Cinstrument-coverage -Zno-profiler-runtime' cargo +nightly build -F coverage
22+
RUSTFLAGS='-Cinstrument-coverage -Zno-profiler-runtime' cargo +nightly build -F coverage -Zbuild-std
2323
popd
2424

2525
pushd pixie-web

0 commit comments

Comments
 (0)