Page::inc_ref_count conforms to the reference counting invariant

format code

Add detailed annotations

Typo fixed
This commit is contained in:
rikosellic
2024-08-30 17:32:50 +08:00
committed by Tate, Hongliang Tian
parent f94d2e55f7
commit a0ec687315

View File

@ -123,11 +123,18 @@ impl<M: PageMeta> Page<M> {
/// ///
/// # Safety /// # Safety
/// ///
/// The physical address must represent a valid page and the caller must already hold one /// The physical address must represent a valid page and the caller must ensure the corresponding
/// reference count. /// virtual address points to an initialized metadata slot by holding a reference count priorly.
pub(in crate::mm) unsafe fn inc_ref_count(paddr: Paddr) { pub(in crate::mm) unsafe fn inc_ref_count(paddr: Paddr) {
let page = unsafe { ManuallyDrop::new(Self::from_raw(paddr)) }; debug_assert!(paddr % PAGE_SIZE == 0);
let _page = page.clone(); debug_assert!(paddr < MAX_PADDR.load(Ordering::Relaxed) as Paddr);
let vaddr: Vaddr = mapping::page_to_meta::<PagingConsts>(paddr);
// SAFETY: The virtual address points to an initialized metadata slot. The caller can ensure
// it is initialized by holding a reference count before calling this function, preventing
// adding a reference count to an unused page.
(*(vaddr as *const MetaSlot))
.ref_count
.fetch_add(1, Ordering::Relaxed);
} }
/// Get the physical address. /// Get the physical address.
@ -229,11 +236,18 @@ impl DynPage {
/// ///
/// # Safety /// # Safety
/// ///
/// The physical address must represent a valid page and the caller must already hold one /// The physical address must represent a valid page and the caller must ensure the corresponding
/// reference count. /// virtual address points to an initialized metadata slot by holding a reference count priorly.
pub(in crate::mm) unsafe fn inc_ref_count(paddr: Paddr) { pub(in crate::mm) unsafe fn inc_ref_count(paddr: Paddr) {
let page = unsafe { ManuallyDrop::new(Self::from_raw(paddr)) }; debug_assert!(paddr % PAGE_SIZE == 0);
let _page = page.clone(); debug_assert!(paddr < MAX_PADDR.load(Ordering::Relaxed) as Paddr);
let vaddr: Vaddr = mapping::page_to_meta::<PagingConsts>(paddr);
// SAFETY: The virtual address points to an initialized metadata slot. The caller can ensure
// it is initialized by holding a reference count before calling this function, preventing
// adding a reference count to an unused page.
(*(vaddr as *const MetaSlot))
.ref_count
.fetch_add(1, Ordering::Relaxed);
} }
/// Get the physical address of the start of the page /// Get the physical address of the start of the page