Improve safe comment

This commit is contained in:
rikosellic 2024-09-01 19:33:25 +08:00 committed by Tate, Hongliang Tian
parent a0ec687315
commit 92680ea25d
1 changed files with 7 additions and 10 deletions

View File

@ -123,15 +123,16 @@ impl<M: PageMeta> Page<M> {
///
/// # Safety
///
/// The physical address must represent a valid page and the caller must ensure the corresponding
/// virtual address points to an initialized metadata slot by holding a reference count priorly.
/// The physical address must represent a valid page.
///
/// And the caller must ensure the metadata slot pointed through the corresponding
/// virtual address is initialized by holding a reference count of the page firstly.
/// Otherwise the function may add a reference count to an unused page.
pub(in crate::mm) unsafe fn inc_ref_count(paddr: Paddr) {
debug_assert!(paddr % PAGE_SIZE == 0);
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.
// SAFETY: The virtual address points to an initialized metadata slot.
(*(vaddr as *const MetaSlot))
.ref_count
.fetch_add(1, Ordering::Relaxed);
@ -236,15 +237,11 @@ impl DynPage {
///
/// # Safety
///
/// The physical address must represent a valid page and the caller must ensure the corresponding
/// virtual address points to an initialized metadata slot by holding a reference count priorly.
/// This is the same as [`Page::inc_ref_count`].
pub(in crate::mm) unsafe fn inc_ref_count(paddr: Paddr) {
debug_assert!(paddr % PAGE_SIZE == 0);
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);