vrange.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. use core::{
  2. cmp::Ordering,
  3. fmt::{self, Debug, Formatter},
  4. ops::{Add, RangeBounds, Sub},
  5. };
  6. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
  7. pub struct VAddr(pub usize);
  8. #[derive(Clone, Copy)]
  9. pub struct VRange {
  10. start: VAddr,
  11. end: VAddr,
  12. }
  13. const USER_SPACE_MEMORY_TOP: VAddr = VAddr(0x8000_0000_0000);
  14. impl VAddr {
  15. pub fn floor(&self) -> Self {
  16. VAddr(self.0 & !0xfff)
  17. }
  18. pub fn ceil(&self) -> Self {
  19. VAddr((self.0 + 0xfff) & !0xfff)
  20. }
  21. pub fn is_user(&self) -> bool {
  22. self.0 != 0 && self < &USER_SPACE_MEMORY_TOP
  23. }
  24. }
  25. impl Sub for VAddr {
  26. type Output = usize;
  27. fn sub(self, rhs: Self) -> Self::Output {
  28. self.0 - rhs.0
  29. }
  30. }
  31. impl Add<usize> for VAddr {
  32. type Output = Self;
  33. fn add(self, rhs: usize) -> Self::Output {
  34. VAddr(self.0 + rhs)
  35. }
  36. }
  37. impl Sub<usize> for VAddr {
  38. type Output = Self;
  39. fn sub(self, rhs: usize) -> Self::Output {
  40. VAddr(self.0 - rhs)
  41. }
  42. }
  43. impl Debug for VAddr {
  44. fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
  45. write!(f, "V{:#x}", self.0)
  46. }
  47. }
  48. impl Debug for VRange {
  49. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
  50. write!(f, "[{:?}, {:?})", self.start, self.end)
  51. }
  52. }
  53. impl Eq for VRange {}
  54. impl PartialOrd for VRange {
  55. fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
  56. Some(self.cmp(other))
  57. }
  58. }
  59. impl PartialEq for VRange {
  60. fn eq(&self, other: &Self) -> bool {
  61. self.cmp(other) == Ordering::Equal
  62. }
  63. }
  64. /// Any two ranges that have one of them containing the other are considered equal.
  65. impl Ord for VRange {
  66. fn cmp(&self, other: &Self) -> Ordering {
  67. if self.start == other.start {
  68. return Ordering::Equal;
  69. }
  70. if self.end == other.end {
  71. if self.start == self.end {
  72. return Ordering::Greater;
  73. }
  74. if other.start == other.end {
  75. return Ordering::Less;
  76. }
  77. return Ordering::Equal;
  78. }
  79. if self.start < other.start {
  80. if other.end < self.end {
  81. return Ordering::Equal;
  82. } else {
  83. return Ordering::Less;
  84. }
  85. }
  86. if other.start < self.start {
  87. if self.end < other.end {
  88. return Ordering::Equal;
  89. } else {
  90. return Ordering::Greater;
  91. }
  92. }
  93. unreachable!()
  94. }
  95. }
  96. impl From<VAddr> for VRange {
  97. fn from(addr: VAddr) -> Self {
  98. VRange::new(addr, addr)
  99. }
  100. }
  101. impl VRange {
  102. pub fn new(start: VAddr, end: VAddr) -> Self {
  103. assert!(start <= end);
  104. VRange { start, end }
  105. }
  106. pub fn is_overlapped(&self, other: &Self) -> bool {
  107. self == other
  108. }
  109. pub fn is_user(&self) -> bool {
  110. self.start < USER_SPACE_MEMORY_TOP && self.end <= USER_SPACE_MEMORY_TOP
  111. }
  112. pub fn start(&self) -> VAddr {
  113. self.start
  114. }
  115. pub fn end(&self) -> VAddr {
  116. self.end
  117. }
  118. pub fn len(&self) -> usize {
  119. self.end.0 - self.start.0
  120. }
  121. pub fn shrink(&self, count: usize) -> Self {
  122. assert!(count <= self.len());
  123. VRange::new(self.start, self.end - count)
  124. }
  125. pub fn grow(&self, count: usize) -> Self {
  126. VRange::new(self.start, self.end + count)
  127. }
  128. pub fn into_range(self) -> impl RangeBounds<Self> {
  129. VRange::from(self.start())..VRange::from(self.end())
  130. }
  131. }