1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * External Storage Manger Interface
7 * ---------------------------------------------------------------------------*/
9 #ifndef RTS_STORAGE_GC_H
10 #define RTS_STORAGE_GC_H
13 #include "rts/OSThreads.h"
15 /* -----------------------------------------------------------------------------
18 * We support an arbitrary number of generations, with an arbitrary number
19 * of steps per generation. Notes (in no particular order):
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
27 * - the oldest generation has one step. There's no point in aging
28 * objects in the oldest generation.
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.
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.
41 * - the mutable-list is per-generation (not per-step). G0 doesn't
42 * have one (since every garbage collection collects at least G0).
44 * - block descriptors contain pointers to both the step and the
45 * generation that the block belongs to, for convenience.
47 * - static objects are stored in per-generation lists. See GC.c for
48 * details of how we collect CAFs in the generational scheme.
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.
54 * ------------------------------------------------------------------------- */
56 typedef struct step_ {
57 unsigned int no; // step number in this generation
58 unsigned int abs_no; // absolute step number
60 struct generation_ * gen; // generation this step belongs to
61 unsigned int gen_no; // generation number (cached)
63 bdescr * blocks; // blocks in this step
64 unsigned int n_blocks; // number of blocks
65 unsigned int n_words; // number of words
67 struct step_ * to; // destination step for live objects
69 bdescr * large_objects; // large objects (doubly linked)
70 unsigned int n_large_blocks; // no. of blocks used by large objs
72 StgTSO * threads; // threads in this step
73 // linked via global_link
75 // ------------------------------------
76 // Fields below are used during GC only
78 #if defined(THREADED_RTS)
79 char pad[128]; // make sure the following is
80 // on a separate cache line.
81 SpinLock sync_large_objects; // lock for large_objects
82 // and scavenged_large_objects
85 int mark; // mark (not copy)? (old gen only)
86 int compact; // compact (not sweep)? (old gen only)
88 // During GC, if we are collecting this step, blocks and n_blocks
89 // are copied into the following two fields. After GC, these blocks
91 bdescr * old_blocks; // bdescr of first from-space block
92 unsigned int n_old_blocks; // number of blocks in from-space
93 unsigned int live_estimate; // for sweeping: estimate of live data
95 bdescr * part_blocks; // partially-full scanned blocks
96 unsigned int n_part_blocks; // count of above
98 bdescr * scavenged_large_objects; // live large objs after GC (d-link)
99 unsigned int n_scavenged_large_blocks; // size (not count) of above
101 bdescr * bitmap; // bitmap for compacting collection
103 StgTSO * old_threads;
108 typedef struct generation_ {
109 unsigned int no; // generation number
110 step * steps; // steps
111 unsigned int n_steps; // number of steps
112 unsigned int max_blocks; // max blocks in step 0
113 bdescr *mut_list; // mut objects in this gen (not G0)
116 unsigned int collections;
117 unsigned int par_collections;
118 unsigned int failed_promotions;
120 // temporary use during GC:
121 bdescr *saved_mut_list;
124 extern generation * generations;
126 extern generation * g0;
127 extern generation * oldest_gen;
128 extern step * all_steps;
129 extern nat total_steps;
131 /* -----------------------------------------------------------------------------
134 StgPtr allocate(Capability *cap, nat n)
135 Allocates memory from the nursery in
136 the current Capability. This can be
137 done without taking a global lock,
140 StgPtr allocatePinned(Capability *cap, nat n)
141 Allocates a chunk of contiguous store
142 n words long, which is at a fixed
143 address (won't be moved by GC).
144 Returns a pointer to the first word.
147 NOTE: the GC can't in general handle
148 pinned objects, so allocatePinned()
149 can only be used for ByteArrays at the
152 Don't forget to TICK_ALLOC_XXX(...)
153 after calling allocate or
154 allocatePinned, for the
155 benefit of the ticky-ticky profiler.
157 -------------------------------------------------------------------------- */
159 StgPtr allocate ( Capability *cap, lnat n );
160 StgPtr allocatePinned ( Capability *cap, lnat n );
162 /* memory allocator for executable memory */
163 void * allocateExec(unsigned int len, void **exec_addr);
164 void freeExec (void *p);
166 // Used by GC checks in external .cmm code:
167 extern nat alloc_blocks_lim;
169 /* -----------------------------------------------------------------------------
170 Performing Garbage Collection
171 -------------------------------------------------------------------------- */
173 void performGC(void);
174 void performMajorGC(void);
176 /* -----------------------------------------------------------------------------
177 The CAF table - used to let us revert CAFs in GHCi
178 -------------------------------------------------------------------------- */
180 void newCAF (StgClosure*);
181 void newDynCAF (StgClosure *);
182 void revertCAFs (void);
184 /* -----------------------------------------------------------------------------
185 This is the write barrier for MUT_VARs, a.k.a. IORefs. A
186 MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
187 is. When written to, a MUT_VAR_CLEAN turns into a MUT_VAR_DIRTY
188 and is put on the mutable list.
189 -------------------------------------------------------------------------- */
191 void dirty_MUT_VAR(StgRegTable *reg, StgClosure *p);
193 /* set to disable CAF garbage collection in GHCi. */
194 /* (needed when dynamic libraries are used). */
195 extern rtsBool keepCAFs;
197 INLINE_HEADER void initBdescr(bdescr *bd, step *step)
200 bd->gen_no = step->gen_no;
204 #endif /* RTS_STORAGE_GC_H */