dtb: remove allocation for in-memory DTB

Replaces the `Once<Vec<u8>>` with `Once<&'static [u8]>` to avoid an
unnecessary allocation.

Adds some basic documentation.

Signed-off-by: Elle Rhumsaa <elle@weathered-steel.dev>
This commit is contained in:
Elle Rhumsaa
2025-09-18 20:25:09 +00:00
committed by Jeremy Soller
parent 68526b2af5
commit 0022bd448c
2 changed files with 17 additions and 13 deletions
+16 -7
View File
@@ -4,7 +4,6 @@ use crate::{
dtb::irqchip::IrqCell,
startup::memory::{register_memory_region, BootloaderMemoryKind},
};
use alloc::vec::Vec;
use byteorder::{ByteOrder, BE};
use core::slice;
use fdt::{
@@ -14,19 +13,29 @@ use fdt::{
};
use spin::once::Once;
pub static DTB_BINARY: Once<Vec<u8>> = Once::new();
/// Represents the in-memory DTB (DeviceTree) binary.
pub static DTB_BINARY: Once<&'static [u8]> = Once::new();
/// Initializes the DTB from the provided base address and size.
///
/// # Safety
///
/// Caller must ensure the base address and size reference valid memory.
///
/// The referenced memory must contain a valid DTB for the underliying system.
///
/// The referenced memory must **not** be mutated for the duration of kernel run-time.
pub unsafe fn init(dtb: Option<(usize, usize)>) {
let mut initialized = false;
DTB_BINARY.call_once(|| {
initialized = true;
let mut binary = Vec::new();
if let Some((dtb_base, dtb_size)) = dtb {
let data = unsafe { slice::from_raw_parts(dtb_base as *const u8, dtb_size) };
binary.extend(data);
};
binary
// SAFETY: `dtb_base` + `dtb_size` reference valid memory due to caller invariants
unsafe { slice::from_raw_parts(dtb_base as *const u8, dtb_size) }
} else {
&[]
}
});
if !initialized {
println!("DTB_BINARY INIT TWICE!");
+1 -6
View File
@@ -40,12 +40,7 @@ impl DtbScheme {
DATA.call_once(|| {
data_init = true;
let dtb = match DTB_BINARY.get() {
Some(dtb) => dtb.as_slice(),
None => &[],
};
Box::from(dtb)
Box::from(DTB_BINARY.get().map(|&d| d).unwrap_or(&[]))
});
if !data_init {