fccae2c71d6aa0ea83c29a3c6cf85ce1e0896082
[ghc-hetmet.git] / rts / sm / GCAux.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team 1998-2008
4  *
5  * Functions called from outside the GC need to be separate from GC.c, 
6  * because GC.c is compiled with register variable(s).
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "Rts.h"
11 #include "Storage.h"
12 #include "MBlock.h"
13 #include "GC.h"
14 #include "Compact.h"
15 #include "Task.h"
16 #include "Capability.h"
17 #include "Trace.h"
18 #include "Schedule.h"
19 // DO NOT include "GCThread.h", we don't want the register variable
20
21 /* -----------------------------------------------------------------------------
22    isAlive determines whether the given closure is still alive (after
23    a garbage collection) or not.  It returns the new address of the
24    closure if it is alive, or NULL otherwise.
25
26    NOTE: Use it before compaction only!
27          It untags and (if needed) retags pointers to closures.
28    -------------------------------------------------------------------------- */
29
30 StgClosure *
31 isAlive(StgClosure *p)
32 {
33   const StgInfoTable *info;
34   bdescr *bd;
35   StgWord tag;
36   StgClosure *q;
37
38   while (1) {
39     /* The tag and the pointer are split, to be merged later when needed. */
40     tag = GET_CLOSURE_TAG(p);
41     q = UNTAG_CLOSURE(p);
42
43     ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
44     info = get_itbl(q);
45
46     // ignore static closures 
47     //
48     // ToDo: for static closures, check the static link field.
49     // Problem here is that we sometimes don't set the link field, eg.
50     // for static closures with an empty SRT or CONSTR_STATIC_NOCAFs.
51     //
52     if (!HEAP_ALLOCED(q)) {
53         return p;
54     }
55
56     // ignore closures in generations that we're not collecting. 
57     bd = Bdescr((P_)q);
58
59     // if it's a pointer into to-space, then we're done
60     if (bd->flags & BF_EVACUATED) {
61         return p;
62     }
63
64     // large objects use the evacuated flag
65     if (bd->flags & BF_LARGE) {
66         return NULL;
67     }
68
69     // check the mark bit for compacted steps
70     if ((bd->flags & BF_COMPACTED) && is_marked((P_)q,bd)) {
71         return p;
72     }
73
74     switch (info->type) {
75
76     case IND:
77     case IND_STATIC:
78     case IND_PERM:
79     case IND_OLDGEN:            // rely on compatible layout with StgInd 
80     case IND_OLDGEN_PERM:
81       // follow indirections 
82       p = ((StgInd *)q)->indirectee;
83       continue;
84
85     case EVACUATED:
86       // alive! 
87       return ((StgEvacuated *)q)->evacuee;
88
89     case TSO:
90       if (((StgTSO *)q)->what_next == ThreadRelocated) {
91         p = (StgClosure *)((StgTSO *)q)->link;
92         continue;
93       } 
94       return NULL;
95
96     default:
97       // dead. 
98       return NULL;
99     }
100   }
101 }
102
103 /* -----------------------------------------------------------------------------
104    Reverting CAFs
105    -------------------------------------------------------------------------- */
106
107 void
108 revertCAFs( void )
109 {
110     StgIndStatic *c;
111
112     for (c = (StgIndStatic *)revertible_caf_list; c != NULL; 
113          c = (StgIndStatic *)c->static_link) 
114     {
115         SET_INFO(c, c->saved_info);
116         c->saved_info = NULL;
117         // could, but not necessary: c->static_link = NULL; 
118     }
119     revertible_caf_list = NULL;
120 }
121
122 void
123 markCAFs (evac_fn evac, void *user)
124 {
125     StgIndStatic *c;
126
127     for (c = (StgIndStatic *)caf_list; c != NULL; 
128          c = (StgIndStatic *)c->static_link) 
129     {
130         evac(user, &c->indirectee);
131     }
132     for (c = (StgIndStatic *)revertible_caf_list; c != NULL; 
133          c = (StgIndStatic *)c->static_link) 
134     {
135         evac(user, &c->indirectee);
136     }
137 }