raw_page.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use super::PFN;
  2. use core::sync::atomic::AtomicUsize;
  3. /// A `RawPage` represents a page of memory in the kernel. It is a low-level
  4. /// representation of a page that is used by the kernel to manage memory.
  5. #[doc(notable_trait)]
  6. pub trait RawPage: Clone + Copy + From<PFN> + Into<PFN> {
  7. fn order(&self) -> u32;
  8. fn refcount(&self) -> &AtomicUsize;
  9. fn is_present(&self) -> bool;
  10. }
  11. #[derive(Clone, Copy)]
  12. pub struct UnmanagedRawPage(PFN, u32);
  13. /// Unmanaged raw pages should always have a non-zero refcount to
  14. /// avoid `free()` from being called.
  15. static UNMANAGED_RAW_PAGE_CLONE_COUNT: AtomicUsize = AtomicUsize::new(1);
  16. impl UnmanagedRawPage {
  17. pub const fn new(pfn: PFN, order: u32) -> Self {
  18. Self(pfn, order)
  19. }
  20. }
  21. impl From<PFN> for UnmanagedRawPage {
  22. fn from(value: PFN) -> Self {
  23. Self::new(value, 0)
  24. }
  25. }
  26. impl Into<PFN> for UnmanagedRawPage {
  27. fn into(self) -> PFN {
  28. let Self(pfn, _) = self;
  29. pfn
  30. }
  31. }
  32. impl RawPage for UnmanagedRawPage {
  33. fn order(&self) -> u32 {
  34. self.1
  35. }
  36. fn refcount(&self) -> &AtomicUsize {
  37. &UNMANAGED_RAW_PAGE_CLONE_COUNT
  38. }
  39. fn is_present(&self) -> bool {
  40. true
  41. }
  42. }