diff --git a/ostd/src/mm/frame/meta.rs b/ostd/src/mm/frame/meta.rs index 65896c742..29b5b367f 100644 --- a/ostd/src/mm/frame/meta.rs +++ b/ostd/src/mm/frame/meta.rs @@ -82,7 +82,7 @@ pub(in crate::mm) struct MetaSlot { /// - the implementation can simply cast a `*const MetaSlot` /// to a `*const AnyFrameMeta` for manipulation; /// - if the metadata need special alignment, we can provide - /// at most `PAGE_METADATA_ALIGN` bytes of alignment; + /// at most [`FRAME_METADATA_MAX_ALIGN`] bytes of alignment; /// - the subsequent fields can utilize the padding of the /// reference count to save space. /// @@ -161,6 +161,17 @@ pub unsafe trait AnyFrameMeta: Any + Send + Sync { } } +/// Checks that a frame metadata type has valid size and alignment. +#[macro_export] +macro_rules! check_frame_meta_layout { + ($t:ty) => { + $crate::const_assert!(size_of::<$t>() <= $crate::mm::frame::meta::FRAME_METADATA_MAX_SIZE); + $crate::const_assert!( + $crate::mm::frame::meta::FRAME_METADATA_MAX_ALIGN % align_of::<$t>() == 0 + ); + }; +} + /// Makes a structure usable as a frame metadata. #[macro_export] macro_rules! impl_frame_meta_for { @@ -169,10 +180,7 @@ macro_rules! impl_frame_meta_for { // SAFETY: `on_drop` won't read the page. unsafe impl $crate::mm::frame::meta::AnyFrameMeta for $t {} - $crate::const_assert!(size_of::<$t>() <= $crate::mm::frame::meta::FRAME_METADATA_MAX_SIZE); - $crate::const_assert!( - $crate::mm::frame::meta::FRAME_METADATA_MAX_ALIGN % align_of::<$t>() == 0 - ); + $crate::check_frame_meta_layout!($t); }; } diff --git a/ostd/src/mm/frame/untyped.rs b/ostd/src/mm/frame/untyped.rs index d545a0b1e..a15d30c73 100644 --- a/ostd/src/mm/frame/untyped.rs +++ b/ostd/src/mm/frame/untyped.rs @@ -46,6 +46,8 @@ macro_rules! impl_untyped_frame_meta_for { } } impl $crate::mm::frame::untyped::AnyUFrameMeta for $t {} + + $crate::check_frame_meta_layout!($t); }; // Implement with a customized drop function. ($t:ty, $body:expr) => { @@ -60,6 +62,8 @@ macro_rules! impl_untyped_frame_meta_for { } } impl $crate::mm::frame::untyped::AnyUFrameMeta for $t {} + + $crate::check_frame_meta_layout!($t); }; }