Remove ostd/src/mm/offset.rs

This commit is contained in:
Ruihan Li
2025-03-19 13:36:23 +08:00
committed by Tate, Hongliang Tian
parent 1a1d9bfb60
commit a1f81df263
11 changed files with 82 additions and 123 deletions

View File

@ -410,21 +410,27 @@ impl<T, M: Debug, R> Debug for SafePtr<T, M, R> {
#[macro_export]
macro_rules! field_ptr {
($ptr:expr, $type:ty, $($field:tt)+) => {{
use ostd::offset_of;
use aster_util::safe_ptr::SafePtr;
#[inline]
fn new_field_ptr<T, M, R: Clone, U>(
container_ptr: &SafePtr<T, M, R>,
field_offset: *const U
field_offset: usize,
_type_infer: *const U,
) -> SafePtr<U, &M, R>
{
let mut ptr = container_ptr.borrow_vm();
ptr.byte_add(field_offset as usize);
ptr.byte_add(field_offset);
ptr.cast()
}
let field_offset = offset_of!($type, $($field)*);
new_field_ptr($ptr, field_offset)
let field_offset = core::mem::offset_of!($type, $($field)*);
let type_infer = ostd::ptr_null_of!({
let ptr: *const $type = core::ptr::null();
// This is not sound, but the code won't be executed.
&raw const (*ptr).$($field)*
});
new_field_ptr($ptr, field_offset, type_infer)
}}
}

View File

@ -1,44 +1,39 @@
// SPDX-License-Identifier: MPL-2.0
use core::marker::PhantomData;
use ostd::Pod;
/// This ptr is designed to read union field safely.
/// Write to union field is safe operation. While reading union field is UB.
/// However, if this field is Pod, we can safely read that field.
pub struct UnionReadPtr<'a, T: Pod> {
/// A reader to read `Pod` fields from a `Pod` type.
pub struct Reader<'a> {
bytes: &'a [u8],
marker: PhantomData<&'a mut T>,
}
impl<'a, T: Pod> UnionReadPtr<'a, T> {
pub fn new(object: &'a T) -> Self {
let bytes = object.as_bytes();
impl<'a> Reader<'a> {
pub fn new<T: Pod>(object: &'a T) -> Self {
Self {
bytes,
marker: PhantomData,
bytes: object.as_bytes(),
}
}
pub fn read_at<F: Pod>(&self, offset: *const F) -> F {
let offset = offset as usize;
F::from_bytes(&self.bytes[offset..])
pub fn read_at<F: Pod>(&self, field_offset: usize, _type_infer: *const F) -> F {
F::from_bytes(&self.bytes[field_offset..])
}
}
/// FIXME: This macro requires the first argument to be a `reference` of a Pod type.
/// This is because the value_offset macro requires
#[macro_export]
macro_rules! read_union_fields {
($container:ident) => ({
let offset = value_offset!($container);
let union_read_ptr = UnionReadPtr::new(&*$container);
union_read_ptr.read_at(offset)
});
($container:ident.$($field:ident).*) => ({
let field_offset = ostd::value_offset!($container.$($field).*);
let union_read_ptr = UnionReadPtr::new(&*$container);
union_read_ptr.read_at(field_offset)
});
macro_rules! read_union_field {
($container:expr, $type:ty, $($field:tt)+) => {{
use $crate::union_read_ptr::Reader;
// Perform type checking first.
let container: &$type = $container;
let reader = Reader::new(container);
let field_offset = core::mem::offset_of!($type, $($field)*);
let type_infer = ostd::ptr_null_of!({
// This is not safe, but the code won't be executed.
&raw const container.$($field)*
});
reader.read_at(field_offset, type_infer)
}}
}