Skip to content

Commit a96e82f

Browse files
authored
Merge pull request #1498 from nicholasbishop/bishop-mem-cleanup
Use size_of/size_of_val/align_of/align_of_val from the prelude
2 parents dfbed8e + a5e9ba1 commit a96e82f

File tree

21 files changed

+78
-88
lines changed

21 files changed

+78
-88
lines changed

uefi-raw/src/status.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub enum Status: usize => {
9898

9999
impl Status {
100100
/// Bit indicating that an UEFI status code is an error.
101-
pub const ERROR_BIT: usize = 1 << (core::mem::size_of::<usize>() * 8 - 1);
101+
pub const ERROR_BIT: usize = 1 << (usize::BITS - 1);
102102

103103
/// Returns true if status code indicates success.
104104
#[inline]

uefi-raw/src/table/system.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::table::configuration::ConfigurationTable;
44
use crate::table::runtime::RuntimeServices;
55
use crate::table::Header;
66
use crate::{Char16, Handle};
7-
use core::{mem, ptr};
7+
use core::ptr;
88

99
#[derive(Clone, Debug, Eq, PartialEq)]
1010
#[repr(C)]
@@ -44,7 +44,7 @@ impl Default for SystemTable {
4444
Self {
4545
header: Header {
4646
signature: Self::SIGNATURE,
47-
size: u32::try_from(mem::size_of::<Self>()).unwrap(),
47+
size: u32::try_from(size_of::<Self>()).unwrap(),
4848
..Header::default()
4949
},
5050

uefi-test-runner/examples/sierpinski.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ extern crate alloc;
66

77
use alloc::vec;
88
use alloc::vec::Vec;
9-
use core::mem;
109
use uefi::prelude::*;
1110
use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput};
1211
use uefi::proto::rng::Rng;
@@ -77,7 +76,7 @@ impl Buffer {
7776

7877
/// Get a random `usize` value.
7978
fn get_random_usize(rng: &mut Rng) -> usize {
80-
let mut buf = [0; mem::size_of::<usize>()];
79+
let mut buf = [0; size_of::<usize>()];
8180
rng.get_rng(None, &mut buf).expect("get_rng failed");
8281
usize::from_le_bytes(buf)
8382
}

uefi-test-runner/src/boot/misc.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::ffi::c_void;
2-
use core::mem;
32
use core::ptr::{self, NonNull};
43

54
use uefi::boot::{
@@ -121,13 +120,11 @@ fn test_register_protocol_notify() {
121120
fn test_install_protocol_interface() {
122121
info!("Installing TestProtocol");
123122

124-
let alloc: *mut TestProtocol = boot::allocate_pool(
125-
MemoryType::BOOT_SERVICES_DATA,
126-
mem::size_of::<TestProtocol>(),
127-
)
128-
.unwrap()
129-
.cast()
130-
.as_ptr();
123+
let alloc: *mut TestProtocol =
124+
boot::allocate_pool(MemoryType::BOOT_SERVICES_DATA, size_of::<TestProtocol>())
125+
.unwrap()
126+
.cast()
127+
.as_ptr();
131128
unsafe { alloc.write(TestProtocol { data: 123 }) };
132129

133130
let _ = unsafe {

uefi/src/boot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub(crate) fn get_memory_map(buf: &mut [u8]) -> Result<MemoryMapMeta> {
277277
let mut desc_version = 0;
278278

279279
assert_eq!(
280-
(map_buffer as usize) % mem::align_of::<MemoryDescriptor>(),
280+
(map_buffer as usize) % align_of::<MemoryDescriptor>(),
281281
0,
282282
"Memory map buffers must be aligned like a MemoryDescriptor"
283283
);
@@ -793,11 +793,11 @@ pub fn locate_handle<'buf>(
793793
SearchType::ByProtocol(guid) => (2, ptr::from_ref(guid), ptr::null()),
794794
};
795795

796-
let mut buffer_size = buffer.len() * mem::size_of::<Handle>();
796+
let mut buffer_size = buffer.len() * size_of::<Handle>();
797797
let status =
798798
unsafe { (bt.locate_handle)(ty, guid, key, &mut buffer_size, buffer.as_mut_ptr().cast()) };
799799

800-
let num_handles = buffer_size / mem::size_of::<Handle>();
800+
let num_handles = buffer_size / size_of::<Handle>();
801801

802802
match status {
803803
Status::SUCCESS => {

uefi/src/data_types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ impl Event {
9999
/// Trait for querying the alignment of a struct.
100100
///
101101
/// For a statically-sized type the alignment can be retrieved with
102-
/// [`core::mem::align_of`]. For a dynamically-sized type (DST),
103-
/// [`core::mem::align_of_val`] provides the alignment given a reference. But in
102+
/// [`align_of`]. For a dynamically-sized type (DST),
103+
/// [`align_of_val`] provides the alignment given a reference. But in
104104
/// some cases it's helpful to know the alignment of a DST prior to having a
105105
/// value, meaning there's no reference to pass to `align_of_val`. For example,
106106
/// when using an API that creates a value using a `[u8]` buffer, the alignment

uefi/src/mem/memory_map/impl_.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use super::*;
55
use crate::boot;
66
use core::fmt::{Debug, Display, Formatter};
77
use core::ops::{Index, IndexMut};
8+
use core::ptr;
89
use core::ptr::NonNull;
9-
use core::{mem, ptr};
1010
use uefi_raw::PhysicalAddress;
1111

1212
/// Errors that may happen when constructing a [`MemoryMapRef`] or
@@ -283,7 +283,7 @@ impl MemoryMapBackingMemory {
283283

284284
// Should be fine as UEFI always has allocations with a guaranteed
285285
// alignment of 8 bytes.
286-
assert_eq!(ptr.align_offset(mem::align_of::<MemoryDescriptor>()), 0);
286+
assert_eq!(ptr.align_offset(align_of::<MemoryDescriptor>()), 0);
287287

288288
// If this panics, the UEFI implementation is broken.
289289
assert_eq!(memory_map_meta.map_size % memory_map_meta.desc_size, 0);
@@ -292,7 +292,7 @@ impl MemoryMapBackingMemory {
292292
}
293293

294294
unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
295-
assert_eq!(ptr.align_offset(mem::align_of::<MemoryDescriptor>()), 0);
295+
assert_eq!(ptr.align_offset(align_of::<MemoryDescriptor>()), 0);
296296

297297
let ptr = NonNull::new(ptr).expect("UEFI should never return a null ptr. An error should have been reflected via an Err earlier.");
298298
let slice = NonNull::slice_from_raw_parts(ptr, len);
@@ -366,7 +366,7 @@ impl MemoryMapOwned {
366366
/// (stored inside the provided buffer) and the corresponding
367367
/// [`MemoryMapMeta`].
368368
pub(crate) fn from_initialized_mem(buf: MemoryMapBackingMemory, meta: MemoryMapMeta) -> Self {
369-
assert!(meta.desc_size >= mem::size_of::<MemoryDescriptor>());
369+
assert!(meta.desc_size >= size_of::<MemoryDescriptor>());
370370
let len = meta.entry_count();
371371
Self { buf, meta, len }
372372
}
@@ -430,7 +430,7 @@ impl IndexMut<usize> for MemoryMapOwned {
430430
mod tests {
431431
use super::*;
432432
use alloc::vec::Vec;
433-
use core::mem::size_of;
433+
use size_of;
434434

435435
const BASE_MMAP_UNSORTED: [MemoryDescriptor; 3] = [
436436
MemoryDescriptor {

uefi/src/mem/memory_map/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ pub use iter::*;
4141
pub use uefi_raw::table::boot::{MemoryAttribute, MemoryDescriptor, MemoryType};
4242

4343
use crate::data_types::Align;
44-
use core::mem;
4544

4645
impl Align for MemoryDescriptor {
4746
fn alignment() -> usize {
48-
mem::align_of::<Self>()
47+
align_of::<Self>()
4948
}
5049
}
5150

@@ -90,7 +89,7 @@ impl MemoryMapMeta {
9089
// extended by a future UEFI revision by a significant amount, we
9190
// update the struct, but an old UEFI implementation reports a small
9291
// size.
93-
assert!(self.desc_size >= mem::size_of::<MemoryDescriptor>());
92+
assert!(self.desc_size >= size_of::<MemoryDescriptor>());
9493
assert!(self.map_size > 0);
9594

9695
// Ensure the mmap size is (somehow) sane.
@@ -238,8 +237,8 @@ mod tests_mmap_artificial {
238237
mod tests_mmap_real {
239238
use super::*;
240239
use alloc::vec::Vec;
241-
use core::mem::size_of;
242240
use core::slice;
241+
use size_of;
243242

244243
const MMAP_META: MemoryMapMeta = MemoryMapMeta {
245244
map_size: MMAP_RAW.len() * size_of::<u64>(),

uefi/src/proto/console/gop.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use crate::util::usize_from_u32;
5555
use crate::{boot, Result, StatusExt};
5656
use core::fmt::{Debug, Formatter};
5757
use core::marker::PhantomData;
58-
use core::mem;
5958
use core::ptr::{self, NonNull};
6059
use uefi_raw::protocol::console::{
6160
GraphicsOutputBltOperation, GraphicsOutputModeInformation, GraphicsOutputProtocol,
@@ -181,7 +180,7 @@ impl GraphicsOutput {
181180
dest_y,
182181
width,
183182
height,
184-
px_stride * core::mem::size_of::<BltPixel>(),
183+
px_stride * size_of::<BltPixel>(),
185184
)
186185
.to_result(),
187186
}
@@ -221,7 +220,7 @@ impl GraphicsOutput {
221220
dest_y,
222221
width,
223222
height,
224-
px_stride * core::mem::size_of::<BltPixel>(),
223+
px_stride * size_of::<BltPixel>(),
225224
)
226225
.to_result(),
227226
}
@@ -620,7 +619,7 @@ impl FrameBuffer<'_> {
620619
#[inline]
621620
pub unsafe fn write_value<T>(&mut self, index: usize, value: T) {
622621
debug_assert!(
623-
index.saturating_add(mem::size_of::<T>()) <= self.size,
622+
index.saturating_add(size_of::<T>()) <= self.size,
624623
"Frame buffer accessed out of bounds"
625624
);
626625
let ptr = self.base.add(index).cast::<T>();
@@ -643,7 +642,7 @@ impl FrameBuffer<'_> {
643642
#[must_use]
644643
pub unsafe fn read_value<T>(&self, index: usize) -> T {
645644
debug_assert!(
646-
index.saturating_add(mem::size_of::<T>()) <= self.size,
645+
index.saturating_add(size_of::<T>()) <= self.size,
647646
"Frame buffer accessed out of bounds"
648647
);
649648
(self.base.add(index) as *const T).read_volatile()

uefi/src/proto/device_path/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ mod tests {
244244
use crate::proto::device_path::messaging::{
245245
Ipv4AddressOrigin, IscsiLoginOptions, IscsiProtocol, RestServiceAccessMode, RestServiceType,
246246
};
247-
use core::{mem, slice};
247+
use core::slice;
248248

249249
fn path_to_bytes(path: &DevicePath) -> &[u8] {
250-
unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::<u8>(), mem::size_of_val(path)) }
250+
unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::<u8>(), size_of_val(path)) }
251251
}
252252

253253
/// Test building an ACPI ADR node.

0 commit comments

Comments
 (0)