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 nursery_ {
58 unsigned int n_blocks;
61 typedef struct generation_ {
62 unsigned int no; // generation number
64 bdescr * blocks; // blocks in this gen
65 unsigned int n_blocks; // number of blocks
66 unsigned int n_words; // number of used words
68 bdescr * large_objects; // large objects (doubly linked)
69 unsigned int n_large_blocks; // no. of blocks used by large objs
70 unsigned long n_new_large_words; // words of new large objects
71 // (for allocation stats)
73 unsigned int max_blocks; // max blocks
74 bdescr *mut_list; // mut objects in this gen (not G0)
76 StgTSO * threads; // threads in this gen
77 // linked via global_link
78 struct generation_ *to; // destination gen for live objects
81 unsigned int collections;
82 unsigned int par_collections;
83 unsigned int failed_promotions;
85 // ------------------------------------
86 // Fields below are used during GC only
88 #if defined(THREADED_RTS)
89 char pad[128]; // make sure the following is
90 // on a separate cache line.
91 SpinLock sync_large_objects; // lock for large_objects
92 // and scavenged_large_objects
95 int mark; // mark (not copy)? (old gen only)
96 int compact; // compact (not sweep)? (old gen only)
98 // During GC, if we are collecting this gen, blocks and n_blocks
99 // are copied into the following two fields. After GC, these blocks
101 bdescr * old_blocks; // bdescr of first from-space block
102 unsigned int n_old_blocks; // number of blocks in from-space
103 unsigned int live_estimate; // for sweeping: estimate of live data
105 bdescr * saved_mut_list;
107 bdescr * part_blocks; // partially-full scanned blocks
108 unsigned int n_part_blocks; // count of above
110 bdescr * scavenged_large_objects; // live large objs after GC (d-link)
111 unsigned int n_scavenged_large_blocks; // size (not count) of above
113 bdescr * bitmap; // bitmap for compacting collection
115 StgTSO * old_threads;
118 extern generation * generations;
119 extern generation * g0;
120 extern generation * oldest_gen;
122 /* -----------------------------------------------------------------------------
125 StgPtr allocate(Capability *cap, nat n)
126 Allocates memory from the nursery in
127 the current Capability. This can be
128 done without taking a global lock,
131 StgPtr allocatePinned(Capability *cap, nat n)
132 Allocates a chunk of contiguous store
133 n words long, which is at a fixed
134 address (won't be moved by GC).
135 Returns a pointer to the first word.
138 NOTE: the GC can't in general handle
139 pinned objects, so allocatePinned()
140 can only be used for ByteArrays at the
143 Don't forget to TICK_ALLOC_XXX(...)
144 after calling allocate or
145 allocatePinned, for the
146 benefit of the ticky-ticky profiler.
148 -------------------------------------------------------------------------- */
150 StgPtr allocate ( Capability *cap, lnat n );
151 StgPtr allocatePinned ( Capability *cap, lnat n );
153 /* memory allocator for executable memory */
154 void * allocateExec(unsigned int len, void **exec_addr);
155 void freeExec (void *p);
157 // Used by GC checks in external .cmm code:
158 extern nat large_alloc_lim;
160 /* -----------------------------------------------------------------------------
161 Performing Garbage Collection
162 -------------------------------------------------------------------------- */
164 void performGC(void);
165 void performMajorGC(void);
167 /* -----------------------------------------------------------------------------
168 The CAF table - used to let us revert CAFs in GHCi
169 -------------------------------------------------------------------------- */
171 void newCAF (StgRegTable *reg, StgClosure *);
172 void newDynCAF (StgRegTable *reg, StgClosure *);
173 void revertCAFs (void);
175 // Request that all CAFs are retained indefinitely.
176 void setKeepCAFs (void);
178 /* -----------------------------------------------------------------------------
180 -------------------------------------------------------------------------- */
182 // Returns the total number of bytes allocated since the start of the program.
183 HsInt64 getAllocations (void);
185 /* -----------------------------------------------------------------------------
186 This is the write barrier for MUT_VARs, a.k.a. IORefs. A
187 MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
188 is. When written to, a MUT_VAR_CLEAN turns into a MUT_VAR_DIRTY
189 and is put on the mutable list.
190 -------------------------------------------------------------------------- */
192 void dirty_MUT_VAR(StgRegTable *reg, StgClosure *p);
194 /* set to disable CAF garbage collection in GHCi. */
195 /* (needed when dynamic libraries are used). */
196 extern rtsBool keepCAFs;
198 INLINE_HEADER void initBdescr(bdescr *bd, generation *gen, generation *dest)
201 bd->gen_no = gen->no;
205 #endif /* RTS_STORAGE_GC_H */