mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-20 13:06:33 +00:00
Refactor project structure
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
bd878dd1c9
commit
e3c227ae06
44
kernel/libs/aster-util/src/union_read_ptr.rs
Normal file
44
kernel/libs/aster-util/src/union_read_ptr.rs
Normal file
@ -0,0 +1,44 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use pod::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> {
|
||||
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();
|
||||
Self {
|
||||
bytes,
|
||||
marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_at<F: Pod>(&self, offset: *const F) -> F {
|
||||
let offset = offset as usize;
|
||||
F::from_bytes(&self.bytes[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 = aster_frame::value_offset!($container.$($field).*);
|
||||
let union_read_ptr = UnionReadPtr::new(&*$container);
|
||||
union_read_ptr.read_at(field_offset)
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user