vrange.rs 3.6 KB

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