mapping.rs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use crate::kernel::vfs::inode::{Inode, InodeUse};
  2. use eonix_mm::paging::PAGE_SIZE;
  3. #[derive(Debug, Clone)]
  4. pub struct FileMapping {
  5. pub file: InodeUse<dyn Inode>,
  6. /// Offset in the file, aligned to 4KB boundary.
  7. pub offset: usize,
  8. /// Length of the mapping. Exceeding part will be zeroed.
  9. pub length: usize,
  10. }
  11. #[derive(Debug, Clone)]
  12. pub enum Mapping {
  13. // private anonymous memory
  14. Anonymous,
  15. // file-backed memory or shared anonymous memory(tmp file)
  16. File(FileMapping),
  17. }
  18. impl FileMapping {
  19. pub fn new(file: InodeUse<dyn Inode>, offset: usize, length: usize) -> Self {
  20. assert_eq!(offset & (PAGE_SIZE - 1), 0);
  21. Self {
  22. file,
  23. offset,
  24. length,
  25. }
  26. }
  27. pub fn offset(&self, offset: usize) -> Self {
  28. if self.length <= offset {
  29. Self::new(self.file.clone(), self.offset + self.length, 0)
  30. } else {
  31. Self::new(
  32. self.file.clone(),
  33. self.offset + offset,
  34. self.length - offset,
  35. )
  36. }
  37. }
  38. }