|
1 | 1 | // SPDX-License-Identifier: MIT OR Apache-2.0
|
2 | 2 |
|
3 | 3 | use alloc::vec::Vec;
|
4 |
| -use uefi::boot::{self, AllocateType}; |
5 |
| -use uefi::mem::memory_map::{MemoryMap, MemoryMapMut, MemoryType}; |
| 4 | +use uefi::boot; |
| 5 | +use uefi::mem::memory_map::{MemoryMap, MemoryMapMut}; |
| 6 | +use uefi_raw::table::boot::MemoryType; |
6 | 7 |
|
7 | 8 | pub fn test() {
|
8 | 9 | info!("Testing memory functions");
|
9 | 10 |
|
10 |
| - test_allocate_pages(); |
11 |
| - test_allocate_pool(); |
| 11 | + bootservices::allocate_pages(); |
| 12 | + bootservices::allocate_pool(); |
12 | 13 |
|
13 |
| - vec_alloc(); |
14 |
| - alloc_alignment(); |
| 14 | + global::alloc_vec(); |
| 15 | + global::alloc_alignment(); |
15 | 16 |
|
16 | 17 | test_memory_map();
|
17 | 18 | }
|
18 | 19 |
|
19 |
| -fn test_allocate_pages() { |
20 |
| - let num_pages = 1; |
21 |
| - let ptr = |
22 |
| - boot::allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, num_pages).unwrap(); |
23 |
| - let addr = ptr.as_ptr() as usize; |
24 |
| - assert_eq!(addr % 4096, 0, "Page pointer is not page-aligned"); |
| 20 | +/// Tests that directly use UEFI boot services to allocate memory. |
| 21 | +mod bootservices { |
| 22 | + use uefi::boot; |
| 23 | + use uefi::boot::AllocateType; |
| 24 | + use uefi_raw::table::boot::MemoryType; |
| 25 | + |
| 26 | + /// Tests the `allocate_pages` boot service. |
| 27 | + pub fn allocate_pages() { |
| 28 | + let num_pages = 1; |
| 29 | + let ptr = boot::allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, num_pages) |
| 30 | + .unwrap(); |
| 31 | + let addr = ptr.as_ptr() as usize; |
| 32 | + assert_eq!(addr % 4096, 0, "Page pointer is not page-aligned"); |
| 33 | + |
| 34 | + // Verify the page can be written to. |
| 35 | + { |
| 36 | + let ptr = ptr.as_ptr(); |
| 37 | + unsafe { ptr.write_volatile(0xff) }; |
| 38 | + unsafe { ptr.add(4095).write_volatile(0xff) }; |
| 39 | + } |
25 | 40 |
|
26 |
| - // Verify the page can be written to. |
27 |
| - { |
28 |
| - let ptr = ptr.as_ptr(); |
29 |
| - unsafe { ptr.write_volatile(0xff) }; |
30 |
| - unsafe { ptr.add(4095).write_volatile(0xff) }; |
| 41 | + unsafe { boot::free_pages(ptr, num_pages) }.unwrap(); |
31 | 42 | }
|
32 | 43 |
|
33 |
| - unsafe { boot::free_pages(ptr, num_pages) }.unwrap(); |
34 |
| -} |
| 44 | + /// Tests the `allocate_pool` boot service. |
| 45 | + pub fn allocate_pool() { |
| 46 | + let ptr = boot::allocate_pool(MemoryType::LOADER_DATA, 10).unwrap(); |
35 | 47 |
|
36 |
| -fn test_allocate_pool() { |
37 |
| - let ptr = boot::allocate_pool(MemoryType::LOADER_DATA, 10).unwrap(); |
38 |
| - |
39 |
| - // Verify the allocation can be written to. |
40 |
| - { |
41 |
| - let ptr = ptr.as_ptr(); |
42 |
| - unsafe { ptr.write_volatile(0xff) }; |
43 |
| - unsafe { ptr.add(9).write_volatile(0xff) }; |
| 48 | + // Verify the allocation can be written to. |
| 49 | + { |
| 50 | + let ptr = ptr.as_ptr(); |
| 51 | + unsafe { ptr.write_volatile(0xff) }; |
| 52 | + unsafe { ptr.add(9).write_volatile(0xff) }; |
| 53 | + } |
| 54 | + unsafe { boot::free_pool(ptr) }.unwrap(); |
44 | 55 | }
|
45 |
| - unsafe { boot::free_pool(ptr) }.unwrap(); |
46 | 56 | }
|
47 | 57 |
|
48 |
| -// Simple test to ensure our custom allocator works with the `alloc` crate. |
49 |
| -fn vec_alloc() { |
50 |
| - info!("Allocating a vector through the `alloc` crate"); |
| 58 | +/// Tests that use [`uefi::allocator::Allocator`], which is configured as the |
| 59 | +/// global allocator. |
| 60 | +mod global { |
| 61 | + /// Simple test to ensure our custom allocator works with the `alloc` crate. |
| 62 | + pub fn alloc_vec() { |
| 63 | + info!("Allocating a vector through the `alloc` crate"); |
51 | 64 |
|
52 |
| - #[allow(clippy::useless_vec)] |
53 |
| - let mut values = vec![-5, 16, 23, 4, 0]; |
| 65 | + #[allow(clippy::useless_vec)] |
| 66 | + let mut values = vec![-5, 16, 23, 4, 0]; |
54 | 67 |
|
55 |
| - values.sort_unstable(); |
| 68 | + values.sort_unstable(); |
56 | 69 |
|
57 |
| - assert_eq!(values[..], [-5, 0, 4, 16, 23], "Failed to sort vector"); |
58 |
| -} |
| 70 | + assert_eq!(values[..], [-5, 0, 4, 16, 23], "Failed to sort vector"); |
| 71 | + } |
59 | 72 |
|
60 |
| -// Simple test to ensure our custom allocator works with correct alignment. |
61 |
| -fn alloc_alignment() { |
62 |
| - info!("Allocating a structure with alignment to 0x100"); |
| 73 | + /// Simple test to ensure our custom allocator works with correct alignment. |
| 74 | + pub fn alloc_alignment() { |
| 75 | + info!("Allocating a structure with alignment to 0x100"); |
63 | 76 |
|
64 |
| - #[repr(align(0x100))] |
65 |
| - struct Block( |
66 |
| - // Ignore warning due to field not being read. |
67 |
| - #[allow(dead_code)] [u8; 0x100], |
68 |
| - ); |
| 77 | + #[repr(align(0x100))] |
| 78 | + struct Block( |
| 79 | + // Ignore warning due to field not being read. |
| 80 | + #[allow(dead_code)] [u8; 0x100], |
| 81 | + ); |
69 | 82 |
|
70 |
| - let value = vec![Block([1; 0x100])]; |
71 |
| - assert_eq!(value.as_ptr() as usize % 0x100, 0, "Wrong alignment"); |
| 83 | + let value = vec![Block([1; 0x100])]; |
| 84 | + assert_eq!(value.as_ptr() as usize % 0x100, 0, "Wrong alignment"); |
| 85 | + } |
72 | 86 | }
|
73 | 87 |
|
74 | 88 | fn test_memory_map() {
|
|
0 commit comments