GC refactoring, remove "steps"
[ghc-hetmet.git] / includes / rts / storage / GC.h
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2004
4  *
5  * External Storage Manger Interface
6  *
7  * ---------------------------------------------------------------------------*/
8
9 #ifndef RTS_STORAGE_GC_H
10 #define RTS_STORAGE_GC_H
11
12 #include <stddef.h>
13 #include "rts/OSThreads.h"
14
15 /* -----------------------------------------------------------------------------
16  * Generational GC
17  *
18  * We support an arbitrary number of generations, with an arbitrary number
19  * of steps per generation.  Notes (in no particular order):
20  *
21  *       - all generations except the oldest should have the same
22  *         number of steps.  Multiple steps gives objects a decent
23  *         chance to age before being promoted, and helps ensure that
24  *         we don't end up with too many thunks being updated in older
25  *         generations.
26  *
27  *       - the oldest generation has one step.  There's no point in aging
28  *         objects in the oldest generation.
29  *
30  *       - generation 0, step 0 (G0S0) is the allocation area.  It is given
31  *         a fixed set of blocks during initialisation, and these blocks
32  *         normally stay in G0S0.  In parallel execution, each
33  *         Capability has its own nursery.
34  *
35  *       - during garbage collection, each step which is an evacuation
36  *         destination (i.e. all steps except G0S0) is allocated a to-space.
37  *         evacuated objects are allocated into the step's to-space until
38  *         GC is finished, when the original step's contents may be freed
39  *         and replaced by the to-space.
40  *
41  *       - the mutable-list is per-generation (not per-step).  G0 doesn't 
42  *         have one (since every garbage collection collects at least G0).
43  * 
44  *       - block descriptors contain pointers to both the step and the
45  *         generation that the block belongs to, for convenience.
46  *
47  *       - static objects are stored in per-generation lists.  See GC.c for
48  *         details of how we collect CAFs in the generational scheme.
49  *
50  *       - large objects are per-step, and are promoted in the same way
51  *         as small objects, except that we may allocate large objects into
52  *         generation 1 initially.
53  *
54  * ------------------------------------------------------------------------- */
55
56 typedef struct nursery_ {
57     bdescr *       blocks;
58     unsigned int   n_blocks;
59 } nursery;
60
61 typedef struct generation_ {
62     unsigned int   no;                  // generation number
63
64     bdescr *       blocks;              // blocks in this gen
65     unsigned int   n_blocks;            // number of blocks
66     unsigned int   n_words;             // number of words
67
68     bdescr *       large_objects;       // large objects (doubly linked)
69     unsigned int   n_large_blocks;      // no. of blocks used by large objs
70
71     unsigned int   max_blocks;          // max blocks
72     bdescr        *mut_list;            // mut objects in this gen (not G0)
73     
74     StgTSO *       threads;             // threads in this gen
75                                         // linked via global_link
76     struct generation_ *to;             // destination gen for live objects
77
78     // stats information
79     unsigned int collections;
80     unsigned int par_collections;
81     unsigned int failed_promotions;
82
83     // ------------------------------------
84     // Fields below are used during GC only
85
86 #if defined(THREADED_RTS)
87     char pad[128];                      // make sure the following is
88                                         // on a separate cache line.
89     SpinLock     sync_large_objects;    // lock for large_objects
90                                         //    and scavenged_large_objects
91 #endif
92
93     int          mark;                  // mark (not copy)? (old gen only)
94     int          compact;               // compact (not sweep)? (old gen only)
95
96     // During GC, if we are collecting this gen, blocks and n_blocks
97     // are copied into the following two fields.  After GC, these blocks
98     // are freed.
99     bdescr *     old_blocks;            // bdescr of first from-space block
100     unsigned int n_old_blocks;          // number of blocks in from-space
101     unsigned int live_estimate;         // for sweeping: estimate of live data
102     
103     bdescr *     saved_mut_list;
104
105     bdescr *     part_blocks;           // partially-full scanned blocks
106     unsigned int n_part_blocks;         // count of above
107
108     bdescr *     scavenged_large_objects;  // live large objs after GC (d-link)
109     unsigned int n_scavenged_large_blocks; // size (not count) of above
110
111     bdescr *     bitmap;                // bitmap for compacting collection
112
113     StgTSO *     old_threads;
114 } generation;
115
116 extern generation * generations;
117 extern generation * g0;
118 extern generation * oldest_gen;
119
120 /* -----------------------------------------------------------------------------
121    Generic allocation
122
123    StgPtr allocate(Capability *cap, nat n)
124                                 Allocates memory from the nursery in
125                                 the current Capability.  This can be
126                                 done without taking a global lock,
127                                 unlike allocate().
128
129    StgPtr allocatePinned(Capability *cap, nat n) 
130                                 Allocates a chunk of contiguous store
131                                 n words long, which is at a fixed
132                                 address (won't be moved by GC).  
133                                 Returns a pointer to the first word.
134                                 Always succeeds.
135                                 
136                                 NOTE: the GC can't in general handle
137                                 pinned objects, so allocatePinned()
138                                 can only be used for ByteArrays at the
139                                 moment.
140
141                                 Don't forget to TICK_ALLOC_XXX(...)
142                                 after calling allocate or
143                                 allocatePinned, for the
144                                 benefit of the ticky-ticky profiler.
145
146    -------------------------------------------------------------------------- */
147
148 StgPtr  allocate        ( Capability *cap, lnat n );
149 StgPtr  allocatePinned  ( Capability *cap, lnat n );
150
151 /* memory allocator for executable memory */
152 void * allocateExec(unsigned int len, void **exec_addr);
153 void   freeExec (void *p);
154
155 // Used by GC checks in external .cmm code:
156 extern nat alloc_blocks_lim;
157
158 /* -----------------------------------------------------------------------------
159    Performing Garbage Collection
160    -------------------------------------------------------------------------- */
161
162 void performGC(void);
163 void performMajorGC(void);
164
165 /* -----------------------------------------------------------------------------
166    The CAF table - used to let us revert CAFs in GHCi
167    -------------------------------------------------------------------------- */
168
169 void newCAF     (StgClosure*);
170 void newDynCAF  (StgClosure *);
171 void revertCAFs (void);
172
173 /* -----------------------------------------------------------------------------
174    This is the write barrier for MUT_VARs, a.k.a. IORefs.  A
175    MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
176    is.  When written to, a MUT_VAR_CLEAN turns into a MUT_VAR_DIRTY
177    and is put on the mutable list.
178    -------------------------------------------------------------------------- */
179
180 void dirty_MUT_VAR(StgRegTable *reg, StgClosure *p);
181
182 /* set to disable CAF garbage collection in GHCi. */
183 /* (needed when dynamic libraries are used). */
184 extern rtsBool keepCAFs;
185
186 INLINE_HEADER void initBdescr(bdescr *bd, generation *gen, generation *dest)
187 {
188     bd->gen    = gen;
189     bd->gen_no = gen->no;
190     bd->dest   = dest;
191 }
192
193 #endif /* RTS_STORAGE_GC_H */