-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGc.hs
61 lines (53 loc) · 1.59 KB
/
Gc.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Gc where
import Common
import Utils
gc :: GmState -> GmState
gc state = state { gmheap = heap' }
where
heap' = scanHeap $ foldl markFrom heap $ findRoots state
heap = gmheap state
markFrom :: GmHeap -> Addr -> GmHeap
markFrom heap addr =
case node of
NMarked node -> heap
NAp a1 a2 -> heap2
where
heap1 = markFrom heap0 a1
heap2 = markFrom heap1 a2
NGlobal arity code -> heap0
NNum n -> heap0
NChar c -> heap0
NInd addr -> heap1
where
heap1 = markFrom heap0 addr
NConstr tag args -> heap1
where
heap1 = foldl markFrom heap0 args
where
node = hLookup heap addr
heap0 = hUpdate heap addr $ NMarked node
scanHeap :: GmHeap -> GmHeap
scanHeap heap =
foldl analyse heap addrs
where
addrs = hAddresses heap
analyse heap addr =
case node of
NMarked node ->
hUpdate heap addr node
_ ->
hFree heap addr
where
node = hLookup heap addr
findRoots state =
findStackRoots (gmstack state) ++
findDumpRoots (gmdump state) ++
findGlobalRoots (gmglobals state)
findStackRoots :: GmStack -> [Addr]
findStackRoots stack = stack
findDumpRoots :: GmDump -> [Addr]
findDumpRoots dump = foldl collectRoots [] dump
where
collectRoots roots (code, stack, vstack) = roots ++ findStackRoots stack
findGlobalRoots :: GmGlobals -> [Addr]
findGlobalRoots globals = map snd globals