Advertisement
VladNitu

Paret Garbage Collector Mark&Sweep

Mar 13th, 2023
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.33 KB | None | 0 0
  1. object Solution {
  2.  
  3.   type PointerEnvironment = List[Pointer]
  4.   type Store = List[Cell]
  5.  
  6.   /**
  7.    * Identifies which store cells are not accessible from the given environment (direct nor indirectly),
  8.    * and removes those cells from the store, reporting the number of cells removed.
  9.    *
  10.    * @return A tuple of the number of removed cells and the garbage collected store.
  11.    */
  12.   def collectGarbage(nv: PointerEnvironment, st: Store): (Int, Store) = {
  13.   // Mark all cells that are reachable from pointers in the environment
  14.   val marked = mark(nv)(st, Set.empty)
  15.  
  16.   // Remove all unmarked cells from the store
  17.   val (removed, newSt) = st.foldLeft((0, List.empty[Cell])) { case ((count, acc), cell) =>
  18.     if (marked.contains(cell.location)) {
  19.       (count, cell :: acc)
  20.     } else {
  21.       (count + 1, acc)
  22.     }
  23.   }
  24.   (removed, newSt.reverse)
  25. }
  26.  
  27. // Mark all cells that are reachable from pointers in the environment
  28. def mark(nv: PointerEnvironment)(st: Store, marked: Set[Int]): Set[Int] = {
  29.   nv.foldLeft(marked) { case (acc, ptr) =>
  30.     markPointer(ptr)(st, acc)
  31.   }
  32. }
  33.  
  34. // Mark all cells that are reachable from a pointer
  35. def markPointer(ptr: Pointer)(st: Store, marked: Set[Int]): Set[Int] = {
  36.   if (marked.contains(ptr.location)) { // Already reached
  37.     marked
  38.   } else {
  39.     val cell = st.find(_.location == ptr.location).getOrElse(throw new RuntimeException(s"Invalid pointer: $ptr"))
  40.     markValue(cell.value)(st, marked + ptr.location)
  41.   }
  42. }
  43.  
  44. // Mark all cells reachable from a Value
  45. def markValue(value: Value)(st: Store, marked: Set[Int]): Set[Int] = {
  46.   value match {
  47.     case ConsV(hd, tl) =>
  48.       markCons(hd, tl)(st, marked)
  49.     case PointerClosV(_, env) =>
  50.       mark(env)(st, marked)
  51.     case BoxV(l) =>
  52.       markBox(l)(st, marked)
  53.     case _ =>
  54.       // primitive values and NilV don't have pointers to other cells, so don't mark anything
  55.       marked
  56.   }
  57. }
  58.  
  59. // Mark all cells reachable from a Cons cell
  60. def markCons(hd: Value, tl: Value)(st: Store, marked: Set[Int]): Set[Int] = {
  61.   markValue(hd)(st, markValue(tl)(st, marked))
  62. }
  63.  
  64. // Mark all cells reachable from a Box
  65. def markBox(l: Int)(st: Store, marked: Set[Int]): Set[Int] = {
  66.   val cell = st.find(_.location == l).getOrElse(throw new RuntimeException(s"Invalid box location: $l"))
  67.   markValue(cell.value)(st, marked + l)
  68. }
  69.  
  70.  
  71.  
  72.  
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement