|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -use core::{ffi::c_void, mem::transmute}; |
| 15 | +use core::{alloc as heap_alloc, ffi::c_void, mem::transmute}; |
16 | 16 |
|
17 | 17 | use atomic_refcell::AtomicRefCell; |
| 18 | +use linked_list_allocator::LockedHeap; |
18 | 19 | use r_efi::{ |
19 | 20 | efi::{ |
20 | 21 | self, AllocateType, Boolean, CapsuleHeader, Char16, Event, EventNotify, Guid, Handle, |
@@ -52,6 +53,16 @@ struct HandleWrapper { |
52 | 53 |
|
53 | 54 | pub static ALLOCATOR: AtomicRefCell<Allocator> = AtomicRefCell::new(Allocator::new()); |
54 | 55 |
|
| 56 | +#[cfg(not(test))] |
| 57 | +#[global_allocator] |
| 58 | +pub static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty(); |
| 59 | + |
| 60 | +#[cfg(not(test))] |
| 61 | +#[alloc_error_handler] |
| 62 | +fn heap_alloc_error_handler(layout: heap_alloc::Layout) -> ! { |
| 63 | + panic!("heap allocation error: {:?}", layout); |
| 64 | +} |
| 65 | + |
55 | 66 | static mut RS: efi::RuntimeServices = efi::RuntimeServices { |
56 | 67 | hdr: efi::TableHeader { |
57 | 68 | signature: efi::RUNTIME_SERVICES_SIGNATURE, |
@@ -722,6 +733,7 @@ extern "win64" fn image_unload(_: Handle) -> Status { |
722 | 733 | } |
723 | 734 |
|
724 | 735 | const PAGE_SIZE: u64 = 4096; |
| 736 | +const HEAP_SIZE: usize = 256 * 1024 * 1024; |
725 | 737 |
|
726 | 738 | // Populate allocator from E820, fixed ranges for the firmware and the loaded binary. |
727 | 739 | fn populate_allocator(info: &dyn boot::Info, image_address: u64, image_size: u64) { |
@@ -752,8 +764,28 @@ fn populate_allocator(info: &dyn boot::Info, image_address: u64, image_size: u64 |
752 | 764 | image_size / PAGE_SIZE, |
753 | 765 | image_address, |
754 | 766 | ); |
| 767 | + |
| 768 | + // Initialize heap allocator |
| 769 | + init_heap_allocator(HEAP_SIZE); |
| 770 | +} |
| 771 | + |
| 772 | +#[cfg(not(test))] |
| 773 | +fn init_heap_allocator(size: usize) { |
| 774 | + let (status, heap_start) = ALLOCATOR.borrow_mut().allocate_pages( |
| 775 | + AllocateType::AllocateAnyPages, |
| 776 | + MemoryType::BootServicesCode, |
| 777 | + size as u64 / PAGE_SIZE, |
| 778 | + 0, |
| 779 | + ); |
| 780 | + assert!(status == Status::SUCCESS); |
| 781 | + unsafe { |
| 782 | + HEAP_ALLOCATOR.lock().init(heap_start as usize, size); |
| 783 | + } |
755 | 784 | } |
756 | 785 |
|
| 786 | +#[cfg(test)] |
| 787 | +fn init_heap_allocator(_: usize) {} |
| 788 | + |
757 | 789 | #[repr(C)] |
758 | 790 | struct LoadedImageWrapper { |
759 | 791 | hw: HandleWrapper, |
|
0 commit comments