Skip to content

Commit

Permalink
build: Use core::mem::offset_of!
Browse files Browse the repository at this point in the history
Signed-off-by: Akira Moroo <[email protected]>
  • Loading branch information
retrage committed May 20, 2024
1 parent 97de41f commit 085105c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@ mod tests {
assert_eq!(mem::size_of::<E820Entry>(), 20);
assert_eq!(mem::size_of::<Params>(), 4096);

assert_eq!(offset_of!(Params, hdr), HEADER_START);
assert_eq!(core::mem::offset_of!(Params, hdr), HEADER_START);
}
}
14 changes: 2 additions & 12 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2019 Intel Corporation

#[macro_export]
macro_rules! offset_of {
($container:ty, $field:ident) => {
#[allow(deref_nullptr)]
unsafe {
&(*(core::ptr::null() as *const $container)).$field as *const _ as usize
}
};
}

#[macro_export]
macro_rules! container_of {
($ptr:ident, $container:ty, $field:ident) => {{
(($ptr as usize) - offset_of!($container, $field)) as *const $container
(($ptr as usize) - core::mem::offset_of!($container, $field)) as *const $container
}};
}

#[macro_export]
macro_rules! container_of_mut {
($ptr:ident, $container:ty, $field:ident) => {{
(($ptr as usize) - offset_of!($container, $field)) as *mut $container
(($ptr as usize) - core::mem::offset_of!($container, $field)) as *mut $container
}};
}

Expand Down
2 changes: 1 addition & 1 deletion src/efi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use core::{
cell::SyncUnsafeCell,
ffi::c_void,
mem::{size_of, transmute},
mem::{offset_of, size_of, transmute},
ptr::null_mut,
slice::from_raw_parts,
};
Expand Down

0 comments on commit 085105c

Please sign in to comment.