mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-30 00:23:24 +00:00
Remove ostd/src/mm/offset.rs
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
1a1d9bfb60
commit
a1f81df263
@ -13,7 +13,6 @@ pub mod frame;
|
||||
pub mod heap;
|
||||
mod io;
|
||||
pub(crate) mod kspace;
|
||||
mod offset;
|
||||
pub(crate) mod page_prop;
|
||||
pub(crate) mod page_table;
|
||||
pub mod tlb;
|
||||
|
@ -1,69 +0,0 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
/// Gets the offset of a field within a type as a pointer.
|
||||
///
|
||||
/// ```rust
|
||||
/// #[repr(C)]
|
||||
/// pub struct Foo {
|
||||
/// first: u8,
|
||||
/// second: u32,
|
||||
/// }
|
||||
///
|
||||
/// assert!(offset_of(Foo, first) == (0 as *const u8));
|
||||
/// assert!(offset_of(Foo, second) == (4 as *const u32));
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! offset_of {
|
||||
($container:ty, $($field:tt)+) => ({
|
||||
// SAFETY: It is ok to have this uninitialized value because
|
||||
// 1) Its memory won't be accessed;
|
||||
// 2) It will be forgotten rather than being dropped;
|
||||
// 3) Before it gets forgotten, the code won't return prematurely or panic.
|
||||
let tmp: $container = unsafe { core::mem::MaybeUninit::uninit().assume_init() };
|
||||
|
||||
let container_addr = &tmp as *const _;
|
||||
let field_addr = &tmp.$($field)* as *const _;
|
||||
|
||||
#[expect(clippy::forget_non_drop)]
|
||||
::core::mem::forget(tmp);
|
||||
|
||||
let field_offset = (field_addr as usize - container_addr as usize) as *const _;
|
||||
|
||||
// Let Rust compiler infer our intended pointer type of field_offset
|
||||
// by comparing it with another pointer.
|
||||
let _: bool = field_offset == field_addr;
|
||||
|
||||
field_offset
|
||||
});
|
||||
}
|
||||
|
||||
/// Gets the offset of a field within an object as a pointer.
|
||||
///
|
||||
/// ```rust
|
||||
/// #[repr(C)]
|
||||
/// pub struct Foo {
|
||||
/// first: u8,
|
||||
/// second: u32,
|
||||
/// }
|
||||
/// let foo = &Foo {first: 0, second: 0};
|
||||
/// assert!(value_offset!(foo) == (0 as *const Foo));
|
||||
/// assert!(value_offset!(foo.first) == (0 as *const u8));
|
||||
/// assert!(value_offset!(foo.second) == (4 as *const u32));
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! value_offset {
|
||||
($container:ident) => ({
|
||||
let container_addr = &*$container as *const _;
|
||||
let offset = 0 as *const _;
|
||||
let _: bool = offset == container_addr;
|
||||
offset
|
||||
});
|
||||
($container:ident.$($field:ident).*) => ({
|
||||
let container_addr = &*$container as *const _;
|
||||
// SAFETY: This is safe since we never access the field
|
||||
let field_addr = unsafe {&($container.$($field).*)} as *const _;
|
||||
let field_offset = (field_addr as usize- container_addr as usize) as *const _;
|
||||
let _: bool = field_offset == field_addr;
|
||||
field_offset
|
||||
});
|
||||
}
|
@ -14,3 +14,35 @@ macro_rules! const_assert {
|
||||
($cond:expr $(,)?) => { const _: () = assert!($cond); };
|
||||
($cond:expr, $($arg:tt)+) => { const _: () = assert!($cond, $($arg)*); };
|
||||
}
|
||||
|
||||
/// Creates a pointer whose type matches the expression, but whose value is always NULL.
|
||||
///
|
||||
/// This is a helper macro, typically used in another macro to help with type inference.
|
||||
///
|
||||
/// The expression is guaranteed never to be executed, so it can contain arbitrarily unsafe code
|
||||
/// without causing any soundness problems.
|
||||
#[macro_export]
|
||||
macro_rules! ptr_null_of {
|
||||
($expr:expr $(,)?) => {
|
||||
if true {
|
||||
core::ptr::null()
|
||||
} else {
|
||||
unreachable!();
|
||||
|
||||
// SAFETY: This is dead code and will never be executed.
|
||||
//
|
||||
// One may wonder: is it possible for the dead code to
|
||||
// trigger UBs by simply being compiled, rather than being executed?
|
||||
// More specifically, what if the caller attempts to
|
||||
// trick the macro into defining unsafe language items,
|
||||
// like static variables, functions, implementation blocks, or attributes,
|
||||
// those that are not executed.
|
||||
// Luckily for us, in such cases, the Rust compiler would complain that
|
||||
// "items do not inherit unsafety from separate enclosing items".
|
||||
#[expect(unreachable_code)]
|
||||
unsafe {
|
||||
$expr
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user