Make allocatePinned use local storage, and other refactorings
[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 step_ {
57     unsigned int         no;            // step number in this generation
58     unsigned int         abs_no;        // absolute step number
59
60     struct generation_ * gen;           // generation this step belongs to
61     unsigned int         gen_no;        // generation number (cached)
62
63     bdescr *             blocks;        // blocks in this step
64     unsigned int         n_blocks;      // number of blocks
65     unsigned int         n_words;       // number of words
66
67     struct step_ *       to;            // destination step for live objects
68
69     bdescr *             large_objects;  // large objects (doubly linked)
70     unsigned int         n_large_blocks; // no. of blocks used by large objs
71
72     StgTSO *             threads;       // threads in this step
73                                         // linked via global_link
74
75     // ------------------------------------
76     // Fields below are used during GC only
77
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
83 #endif
84
85     int          mark;                  // mark (not copy)? (old gen only)
86     int          compact;               // compact (not sweep)? (old gen only)
87
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
90     // are freed.
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
94     
95     bdescr *     part_blocks;           // partially-full scanned blocks
96     unsigned int n_part_blocks;         // count of above
97
98     bdescr *     scavenged_large_objects;  // live large objs after GC (d-link)
99     unsigned int n_scavenged_large_blocks; // size (not count) of above
100
101     bdescr *     bitmap;                // bitmap for compacting collection
102
103     StgTSO *     old_threads;
104
105 } step;
106
107
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)
114     
115     // stats information
116     unsigned int collections;
117     unsigned int par_collections;
118     unsigned int failed_promotions;
119
120     // temporary use during GC:
121     bdescr        *saved_mut_list;
122 } generation;
123
124 extern generation * generations;
125
126 extern generation * g0;
127 extern generation * oldest_gen;
128 extern step * all_steps;
129 extern nat total_steps;
130
131 /* -----------------------------------------------------------------------------
132    Generic allocation
133
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,
138                                 unlike allocate().
139
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.
145                                 Always succeeds.
146                                 
147                                 NOTE: the GC can't in general handle
148                                 pinned objects, so allocatePinned()
149                                 can only be used for ByteArrays at the
150                                 moment.
151
152                                 Don't forget to TICK_ALLOC_XXX(...)
153                                 after calling allocate or
154                                 allocatePinned, for the
155                                 benefit of the ticky-ticky profiler.
156
157    -------------------------------------------------------------------------- */
158
159 StgPtr  allocate        ( Capability *cap, lnat n );
160 StgPtr  allocatePinned  ( Capability *cap, lnat n );
161
162 /* memory allocator for executable memory */
163 void * allocateExec(unsigned int len, void **exec_addr);
164 void   freeExec (void *p);
165
166 // Used by GC checks in external .cmm code:
167 extern nat alloc_blocks_lim;
168
169 /* -----------------------------------------------------------------------------
170    Performing Garbage Collection
171    -------------------------------------------------------------------------- */
172
173 void performGC(void);
174 void performMajorGC(void);
175
176 /* -----------------------------------------------------------------------------
177    The CAF table - used to let us revert CAFs in GHCi
178    -------------------------------------------------------------------------- */
179
180 void newCAF     (StgClosure*);
181 void newDynCAF  (StgClosure *);
182 void revertCAFs (void);
183
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    -------------------------------------------------------------------------- */
190
191 void dirty_MUT_VAR(StgRegTable *reg, StgClosure *p);
192
193 /* set to disable CAF garbage collection in GHCi. */
194 /* (needed when dynamic libraries are used). */
195 extern rtsBool keepCAFs;
196
197 INLINE_HEADER void initBdescr(bdescr *bd, step *step)
198 {
199     bd->step   = step;
200     bd->gen_no = step->gen_no;
201     bd->dest   = step->to;
202 }
203
204 #endif /* RTS_STORAGE_GC_H */