module MarkCompact.TwoFinger where
import Debug.Trace
-import Data.List (sortBy)
+import Data.Ord (Down(..))
+import Data.List
import Data.Function (on)
import Data.Array
import Control.Monad.State
pure $ Right $ ObjectPtr p
Nothing -> pure $ Left "Out of memory"
where
- findIndexWithNothing h = foldl step Nothing $ sortBy (flip compare `on` fst) $ assocs h
- step _ (i, Nothing) = Just i
- step acc _ = acc
+ findIndexWithNothing h = ((+)1) <$> (elemIndex Nothing $ map snd $ sortBy (flip compare `on` Down . fst) $ assocs h)
mark :: [ObjectPtr] -> Heap -> Bitmap -> Bitmap
mark [] _ bm = bm
collect :: GcM ()
collect = do
- traceShowM "mark!!!"
markFromRoots
compact
| free < scan = do
gc <- get
let bm = bitmap gc
- let indicesToUnmark = fmap fst $ takeWhile (\(_, marked) -> marked) $ assocs bm
+ let indicesToUnmark = fmap fst $ takeWhile (\(_, marked) -> marked) $ sortBy (flip compare `on` Down . fst) $ assocs bm
free' = if indicesToUnmark == [] then free else maximum indicesToUnmark
bm' = bm // [(i, False) | i <- indicesToUnmark]
- put $ gc { bitmap = traceShow (free', indicesToUnmark) bm' }
+ put $ gc { bitmap = traceShow (free', indicesToUnmark) bm', heap = heap gc // [(i, Nothing) | (i, x) <- assocs bm, x == False] }
let
- findIndexStep marked (ok, idx)
- | not marked && idx > free' = (ok, idx - 1)
- | otherwise = (True, idx)
- scan' = snd $ foldr findIndexStep (False, scan) bm'
-
+ findIndexStep (_, marked) idx
+ | not marked && idx > free' = idx - 1
+ | otherwise = idx
+ scan' = traceShowId $ foldr findIndexStep scan $ traceShowId $ take scan $ sortBy (flip compare `on` Down . fst) $ assocs bm'
+
if scan' > free' then do
put $ gc { bitmap = bm' // [(scan', False)]}
ptr7 <- new (IntVal 7)
ptr8 <- new (IntVal 8)
+ writeToRoots 5 ptr6
collect
-
- return [ptr, ptr2]
+ ptr2' <- new (IntVal 2)
+ collect
+
+ return [ptr, ptr2, ptr2']
print res