Skip to content

Commit

Permalink
Remove heap allocation
Browse files Browse the repository at this point in the history
Since all protocol fields are known at compile time, replace the heap
allocation with a static variable.

Signed-off-by: Tim Crawford <[email protected]>
  • Loading branch information
crawfxrd committed Dec 20, 2024
1 parent 384aec7 commit fbc351b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
33 changes: 7 additions & 26 deletions src/gop_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,10 @@ extern "efiapi" fn GetPlatformDockStatus(_CurrentDockStatus: DockStatus) -> Stat
Status::UNSUPPORTED
}

impl GopPolicy {
pub fn new() -> Box<Self> {
Box::new(Self {
Revision: Self::REVISION_03,
GetPlatformLidStatus,
GetVbtData,
GetPlatformDockStatus,
GopOverrideGuid: Guid::NULL,
})
}

pub fn install(self: Box<Self>) -> Result<()> {
let uefi = unsafe { std::system_table_mut() };

let self_ptr = Box::into_raw(self);
let mut handle = Handle(0);
Result::from((uefi.BootServices.InstallProtocolInterface)(
&mut handle,
&Self::GUID,
InterfaceType::Native,
self_ptr as usize,
))?;

Ok(())
}
}
pub static GOP_POLICY: GopPolicy = GopPolicy {
Revision: GopPolicy::REVISION_03,
GetPlatformLidStatus,
GetVbtData,
GetPlatformDockStatus,
GopOverrideGuid: Guid::NULL,
};
23 changes: 13 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
#![no_std]
#![no_main]

#[macro_use]
extern crate uefi_std as std;

use std::prelude::*;

mod gop_policy;

use gop_policy::{GopPolicy, GOP_POLICY};
use std::prelude::*;
use std::uefi::boot::InterfaceType;

#[no_mangle]
pub extern "C" fn main() -> Status {
let gop_policy = gop_policy::GopPolicy::new();
if let Err(err) = gop_policy.install() {
println!("GopPolicy error: {:?}", err);
err
} else {
Status::SUCCESS
}
let uefi = unsafe { std::system_table_mut() };
let mut handle = Handle(0);

(uefi.BootServices.InstallProtocolInterface)(
&mut handle,
&GopPolicy::GUID,
InterfaceType::Native,
core::ptr::addr_of!(GOP_POLICY) as usize,
)
}

0 comments on commit fbc351b

Please sign in to comment.