Refactoring only
[ghc-hetmet.git] / rts / sm / Storage.c
1 /* -----------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 1998-2008
4  *
5  * Storage manager front end
6  *
7  * Documentation on the architecture of the Storage Manager can be
8  * found in the online commentary:
9  * 
10  *   http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage
11  *
12  * ---------------------------------------------------------------------------*/
13
14 #include "PosixSource.h"
15 #include "Rts.h"
16
17 #include "Storage.h"
18 #include "RtsUtils.h"
19 #include "Stats.h"
20 #include "BlockAlloc.h"
21 #include "Weak.h"
22 #include "Sanity.h"
23 #include "Arena.h"
24 #include "Capability.h"
25 #include "Schedule.h"
26 #include "RetainerProfile.h"    // for counting memory blocks (memInventory)
27 #include "OSMem.h"
28 #include "Trace.h"
29 #include "GC.h"
30 #include "Evac.h"
31
32 #include <string.h>
33
34 #include "ffi.h"
35
36 /* 
37  * All these globals require sm_mutex to access in THREADED_RTS mode.
38  */
39 StgClosure    *caf_list         = NULL;
40 StgClosure    *revertible_caf_list = NULL;
41 rtsBool       keepCAFs;
42
43 nat alloc_blocks_lim;    /* GC if n_large_blocks in any nursery
44                           * reaches this. */
45
46 bdescr *exec_block;
47
48 generation *generations = NULL; /* all the generations */
49 generation *g0          = NULL; /* generation 0, for convenience */
50 generation *oldest_gen  = NULL; /* oldest generation, for convenience */
51
52 nat total_steps         = 0;
53 step *all_steps         = NULL; /* single array of steps */
54
55 ullong total_allocated = 0;     /* total memory allocated during run */
56
57 step *nurseries         = NULL; /* array of nurseries, size == n_capabilities */
58
59 #ifdef THREADED_RTS
60 /*
61  * Storage manager mutex:  protects all the above state from
62  * simultaneous access by two STG threads.
63  */
64 Mutex sm_mutex;
65 #endif
66
67 static void allocNurseries ( void );
68
69 static void
70 initStep (step *stp, int g, int s)
71 {
72     stp->no = s;
73     stp->abs_no = RtsFlags.GcFlags.steps * g + s;
74     stp->blocks = NULL;
75     stp->n_blocks = 0;
76     stp->n_words = 0;
77     stp->live_estimate = 0;
78     stp->old_blocks = NULL;
79     stp->n_old_blocks = 0;
80     stp->gen = &generations[g];
81     stp->gen_no = g;
82     stp->large_objects = NULL;
83     stp->n_large_blocks = 0;
84     stp->scavenged_large_objects = NULL;
85     stp->n_scavenged_large_blocks = 0;
86     stp->mark = 0;
87     stp->compact = 0;
88     stp->bitmap = NULL;
89 #ifdef THREADED_RTS
90     initSpinLock(&stp->sync_large_objects);
91 #endif
92     stp->threads = END_TSO_QUEUE;
93     stp->old_threads = END_TSO_QUEUE;
94 }
95
96 void
97 initStorage( void )
98 {
99   nat g, s;
100   generation *gen;
101
102   if (generations != NULL) {
103       // multi-init protection
104       return;
105   }
106
107   initMBlocks();
108
109   /* Sanity check to make sure the LOOKS_LIKE_ macros appear to be
110    * doing something reasonable.
111    */
112   /* We use the NOT_NULL variant or gcc warns that the test is always true */
113   ASSERT(LOOKS_LIKE_INFO_PTR_NOT_NULL((StgWord)&stg_BLACKHOLE_info));
114   ASSERT(LOOKS_LIKE_CLOSURE_PTR(&stg_dummy_ret_closure));
115   ASSERT(!HEAP_ALLOCED(&stg_dummy_ret_closure));
116   
117   if (RtsFlags.GcFlags.maxHeapSize != 0 &&
118       RtsFlags.GcFlags.heapSizeSuggestion > 
119       RtsFlags.GcFlags.maxHeapSize) {
120     RtsFlags.GcFlags.maxHeapSize = RtsFlags.GcFlags.heapSizeSuggestion;
121   }
122
123   if (RtsFlags.GcFlags.maxHeapSize != 0 &&
124       RtsFlags.GcFlags.minAllocAreaSize > 
125       RtsFlags.GcFlags.maxHeapSize) {
126       errorBelch("maximum heap size (-M) is smaller than minimum alloc area size (-A)");
127       RtsFlags.GcFlags.minAllocAreaSize = RtsFlags.GcFlags.maxHeapSize;
128   }
129
130   initBlockAllocator();
131   
132 #if defined(THREADED_RTS)
133   initMutex(&sm_mutex);
134 #endif
135
136   ACQUIRE_SM_LOCK;
137
138   /* allocate generation info array */
139   generations = (generation *)stgMallocBytes(RtsFlags.GcFlags.generations 
140                                              * sizeof(struct generation_),
141                                              "initStorage: gens");
142
143   /* Initialise all generations */
144   for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
145     gen = &generations[g];
146     gen->no = g;
147     gen->mut_list = allocBlock();
148     gen->collections = 0;
149     gen->par_collections = 0;
150     gen->failed_promotions = 0;
151     gen->max_blocks = 0;
152   }
153
154   /* A couple of convenience pointers */
155   g0 = &generations[0];
156   oldest_gen = &generations[RtsFlags.GcFlags.generations-1];
157
158   /* allocate all the steps into an array.  It is important that we do
159      it this way, because we need the invariant that two step pointers
160      can be directly compared to see which is the oldest.
161      Remember that the last generation has only one step. */
162   total_steps = 1 + (RtsFlags.GcFlags.generations - 1) * RtsFlags.GcFlags.steps;
163   all_steps   = stgMallocBytes(total_steps * sizeof(struct step_),
164                                "initStorage: steps");
165
166   /* Allocate step structures in each generation */
167   if (RtsFlags.GcFlags.generations > 1) {
168     /* Only for multiple-generations */
169
170     /* Oldest generation: one step */
171     oldest_gen->n_steps = 1;
172     oldest_gen->steps   = all_steps + (RtsFlags.GcFlags.generations - 1)
173                                       * RtsFlags.GcFlags.steps;
174
175     /* set up all except the oldest generation with 2 steps */
176     for(g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
177       generations[g].n_steps = RtsFlags.GcFlags.steps;
178       generations[g].steps   = all_steps + g * RtsFlags.GcFlags.steps;
179     }
180     
181   } else {
182     /* single generation, i.e. a two-space collector */
183     g0->n_steps = 1;
184     g0->steps   = all_steps;
185   }
186
187   nurseries = stgMallocBytes (n_capabilities * sizeof(struct step_),
188                               "initStorage: nurseries");
189
190   /* Initialise all steps */
191   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
192     for (s = 0; s < generations[g].n_steps; s++) {
193         initStep(&generations[g].steps[s], g, s);
194     }
195   }
196   
197   for (s = 0; s < n_capabilities; s++) {
198       initStep(&nurseries[s], 0, s);
199   }
200   
201   /* Set up the destination pointers in each younger gen. step */
202   for (g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
203     for (s = 0; s < generations[g].n_steps-1; s++) {
204       generations[g].steps[s].to = &generations[g].steps[s+1];
205     }
206     generations[g].steps[s].to = &generations[g+1].steps[0];
207   }
208   oldest_gen->steps[0].to = &oldest_gen->steps[0];
209   
210   for (s = 0; s < n_capabilities; s++) {
211       nurseries[s].to = generations[0].steps[0].to;
212   }
213   
214   /* The oldest generation has one step. */
215   if (RtsFlags.GcFlags.compact || RtsFlags.GcFlags.sweep) {
216       if (RtsFlags.GcFlags.generations == 1) {
217           errorBelch("WARNING: compact/sweep is incompatible with -G1; disabled");
218       } else {
219           oldest_gen->steps[0].mark = 1;
220           if (RtsFlags.GcFlags.compact)
221               oldest_gen->steps[0].compact = 1;
222       }
223   }
224
225   generations[0].max_blocks = 0;
226
227   /* The allocation area.  Policy: keep the allocation area
228    * small to begin with, even if we have a large suggested heap
229    * size.  Reason: we're going to do a major collection first, and we
230    * don't want it to be a big one.  This vague idea is borne out by 
231    * rigorous experimental evidence.
232    */
233   allocNurseries();
234
235   weak_ptr_list = NULL;
236   caf_list = NULL;
237   revertible_caf_list = NULL;
238    
239   /* initialise the allocate() interface */
240   alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
241
242   exec_block = NULL;
243
244 #ifdef THREADED_RTS
245   initSpinLock(&gc_alloc_block_sync);
246   whitehole_spin = 0;
247 #endif
248
249   N = 0;
250
251   initGcThreads();
252
253   IF_DEBUG(gc, statDescribeGens());
254
255   RELEASE_SM_LOCK;
256 }
257
258 void
259 exitStorage (void)
260 {
261     stat_exit(calcAllocated());
262 }
263
264 void
265 freeStorage (void)
266 {
267     stgFree(all_steps); // frees all the steps
268     stgFree(generations);
269     freeAllMBlocks();
270 #if defined(THREADED_RTS)
271     closeMutex(&sm_mutex);
272 #endif
273     stgFree(nurseries);
274     freeGcThreads();
275 }
276
277 /* -----------------------------------------------------------------------------
278    CAF management.
279
280    The entry code for every CAF does the following:
281      
282       - builds a CAF_BLACKHOLE in the heap
283       - pushes an update frame pointing to the CAF_BLACKHOLE
284       - invokes UPD_CAF(), which:
285           - calls newCaf, below
286           - updates the CAF with a static indirection to the CAF_BLACKHOLE
287       
288    Why do we build a BLACKHOLE in the heap rather than just updating
289    the thunk directly?  It's so that we only need one kind of update
290    frame - otherwise we'd need a static version of the update frame too.
291
292    newCaf() does the following:
293        
294       - it puts the CAF on the oldest generation's mut-once list.
295         This is so that we can treat the CAF as a root when collecting
296         younger generations.
297
298    For GHCI, we have additional requirements when dealing with CAFs:
299
300       - we must *retain* all dynamically-loaded CAFs ever entered,
301         just in case we need them again.
302       - we must be able to *revert* CAFs that have been evaluated, to
303         their pre-evaluated form.
304
305       To do this, we use an additional CAF list.  When newCaf() is
306       called on a dynamically-loaded CAF, we add it to the CAF list
307       instead of the old-generation mutable list, and save away its
308       old info pointer (in caf->saved_info) for later reversion.
309
310       To revert all the CAFs, we traverse the CAF list and reset the
311       info pointer to caf->saved_info, then throw away the CAF list.
312       (see GC.c:revertCAFs()).
313
314       -- SDM 29/1/01
315
316    -------------------------------------------------------------------------- */
317
318 void
319 newCAF(StgClosure* caf)
320 {
321   ACQUIRE_SM_LOCK;
322
323 #ifdef DYNAMIC
324   if(keepCAFs)
325   {
326     // HACK:
327     // If we are in GHCi _and_ we are using dynamic libraries,
328     // then we can't redirect newCAF calls to newDynCAF (see below),
329     // so we make newCAF behave almost like newDynCAF.
330     // The dynamic libraries might be used by both the interpreted
331     // program and GHCi itself, so they must not be reverted.
332     // This also means that in GHCi with dynamic libraries, CAFs are not
333     // garbage collected. If this turns out to be a problem, we could
334     // do another hack here and do an address range test on caf to figure
335     // out whether it is from a dynamic library.
336     ((StgIndStatic *)caf)->saved_info  = (StgInfoTable *)caf->header.info;
337     ((StgIndStatic *)caf)->static_link = caf_list;
338     caf_list = caf;
339   }
340   else
341 #endif
342   {
343     /* Put this CAF on the mutable list for the old generation.
344     * This is a HACK - the IND_STATIC closure doesn't really have
345     * a mut_link field, but we pretend it has - in fact we re-use
346     * the STATIC_LINK field for the time being, because when we
347     * come to do a major GC we won't need the mut_link field
348     * any more and can use it as a STATIC_LINK.
349     */
350     ((StgIndStatic *)caf)->saved_info = NULL;
351     recordMutableGen(caf, oldest_gen->no);
352   }
353   
354   RELEASE_SM_LOCK;
355 }
356
357 // An alternate version of newCaf which is used for dynamically loaded
358 // object code in GHCi.  In this case we want to retain *all* CAFs in
359 // the object code, because they might be demanded at any time from an
360 // expression evaluated on the command line.
361 // Also, GHCi might want to revert CAFs, so we add these to the
362 // revertible_caf_list.
363 //
364 // The linker hackily arranges that references to newCaf from dynamic
365 // code end up pointing to newDynCAF.
366 void
367 newDynCAF(StgClosure *caf)
368 {
369     ACQUIRE_SM_LOCK;
370
371     ((StgIndStatic *)caf)->saved_info  = (StgInfoTable *)caf->header.info;
372     ((StgIndStatic *)caf)->static_link = revertible_caf_list;
373     revertible_caf_list = caf;
374
375     RELEASE_SM_LOCK;
376 }
377
378 /* -----------------------------------------------------------------------------
379    Nursery management.
380    -------------------------------------------------------------------------- */
381
382 static bdescr *
383 allocNursery (step *stp, bdescr *tail, nat blocks)
384 {
385     bdescr *bd;
386     nat i;
387
388     // Allocate a nursery: we allocate fresh blocks one at a time and
389     // cons them on to the front of the list, not forgetting to update
390     // the back pointer on the tail of the list to point to the new block.
391     for (i=0; i < blocks; i++) {
392         // @LDV profiling
393         /*
394           processNursery() in LdvProfile.c assumes that every block group in
395           the nursery contains only a single block. So, if a block group is
396           given multiple blocks, change processNursery() accordingly.
397         */
398         bd = allocBlock();
399         bd->link = tail;
400         // double-link the nursery: we might need to insert blocks
401         if (tail != NULL) {
402             tail->u.back = bd;
403         }
404         initBdescr(bd, stp);
405         bd->flags = 0;
406         bd->free = bd->start;
407         tail = bd;
408     }
409     tail->u.back = NULL;
410     return tail;
411 }
412
413 static void
414 assignNurseriesToCapabilities (void)
415 {
416     nat i;
417
418     for (i = 0; i < n_capabilities; i++) {
419         capabilities[i].r.rNursery        = &nurseries[i];
420         capabilities[i].r.rCurrentNursery = nurseries[i].blocks;
421         capabilities[i].r.rCurrentAlloc   = NULL;
422     }
423 }
424
425 static void
426 allocNurseries( void )
427
428     nat i;
429
430     for (i = 0; i < n_capabilities; i++) {
431         nurseries[i].blocks = 
432             allocNursery(&nurseries[i], NULL, 
433                          RtsFlags.GcFlags.minAllocAreaSize);
434         nurseries[i].n_blocks    = RtsFlags.GcFlags.minAllocAreaSize;
435         nurseries[i].old_blocks   = NULL;
436         nurseries[i].n_old_blocks = 0;
437     }
438     assignNurseriesToCapabilities();
439 }
440       
441 void
442 resetNurseries( void )
443 {
444     nat i;
445     bdescr *bd;
446     step *stp;
447
448     for (i = 0; i < n_capabilities; i++) {
449         stp = &nurseries[i];
450         for (bd = stp->blocks; bd; bd = bd->link) {
451             bd->free = bd->start;
452             ASSERT(bd->gen_no == 0);
453             ASSERT(bd->step == stp);
454             IF_DEBUG(sanity,memset(bd->start, 0xaa, BLOCK_SIZE));
455         }
456         // these large objects are dead, since we have just GC'd
457         freeChain(stp->large_objects);
458         stp->large_objects = NULL;
459         stp->n_large_blocks = 0;
460     }
461     assignNurseriesToCapabilities();
462 }
463
464 lnat
465 countNurseryBlocks (void)
466 {
467     nat i;
468     lnat blocks = 0;
469
470     for (i = 0; i < n_capabilities; i++) {
471         blocks += nurseries[i].n_blocks;
472         blocks += nurseries[i].n_large_blocks;
473     }
474     return blocks;
475 }
476
477 static void
478 resizeNursery ( step *stp, nat blocks )
479 {
480   bdescr *bd;
481   nat nursery_blocks;
482
483   nursery_blocks = stp->n_blocks;
484   if (nursery_blocks == blocks) return;
485
486   if (nursery_blocks < blocks) {
487       debugTrace(DEBUG_gc, "increasing size of nursery to %d blocks", 
488                  blocks);
489     stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
490   } 
491   else {
492     bdescr *next_bd;
493     
494     debugTrace(DEBUG_gc, "decreasing size of nursery to %d blocks", 
495                blocks);
496
497     bd = stp->blocks;
498     while (nursery_blocks > blocks) {
499         next_bd = bd->link;
500         next_bd->u.back = NULL;
501         nursery_blocks -= bd->blocks; // might be a large block
502         freeGroup(bd);
503         bd = next_bd;
504     }
505     stp->blocks = bd;
506     // might have gone just under, by freeing a large block, so make
507     // up the difference.
508     if (nursery_blocks < blocks) {
509         stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
510     }
511   }
512   
513   stp->n_blocks = blocks;
514   ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
515 }
516
517 // 
518 // Resize each of the nurseries to the specified size.
519 //
520 void
521 resizeNurseriesFixed (nat blocks)
522 {
523     nat i;
524     for (i = 0; i < n_capabilities; i++) {
525         resizeNursery(&nurseries[i], blocks);
526     }
527 }
528
529 // 
530 // Resize the nurseries to the total specified size.
531 //
532 void
533 resizeNurseries (nat blocks)
534 {
535     // If there are multiple nurseries, then we just divide the number
536     // of available blocks between them.
537     resizeNurseriesFixed(blocks / n_capabilities);
538 }
539
540
541 /* -----------------------------------------------------------------------------
542    move_TSO is called to update the TSO structure after it has been
543    moved from one place to another.
544    -------------------------------------------------------------------------- */
545
546 void
547 move_TSO (StgTSO *src, StgTSO *dest)
548 {
549     ptrdiff_t diff;
550
551     // relocate the stack pointer... 
552     diff = (StgPtr)dest - (StgPtr)src; // In *words* 
553     dest->sp = (StgPtr)dest->sp + diff;
554 }
555
556 /* -----------------------------------------------------------------------------
557    split N blocks off the front of the given bdescr, returning the
558    new block group.  We add the remainder to the large_blocks list
559    in the same step as the original block.
560    -------------------------------------------------------------------------- */
561
562 bdescr *
563 splitLargeBlock (bdescr *bd, nat blocks)
564 {
565     bdescr *new_bd;
566
567     ACQUIRE_SM_LOCK;
568
569     ASSERT(countBlocks(bd->step->large_objects) == bd->step->n_large_blocks);
570
571     // subtract the original number of blocks from the counter first
572     bd->step->n_large_blocks -= bd->blocks;
573
574     new_bd = splitBlockGroup (bd, blocks);
575     initBdescr(new_bd, bd->step);
576     new_bd->flags   = BF_LARGE | (bd->flags & BF_EVACUATED); 
577     // if new_bd is in an old generation, we have to set BF_EVACUATED
578     new_bd->free    = bd->free;
579     dbl_link_onto(new_bd, &bd->step->large_objects);
580
581     ASSERT(new_bd->free <= new_bd->start + new_bd->blocks * BLOCK_SIZE_W);
582
583     // add the new number of blocks to the counter.  Due to the gaps
584     // for block descriptors, new_bd->blocks + bd->blocks might not be
585     // equal to the original bd->blocks, which is why we do it this way.
586     bd->step->n_large_blocks += bd->blocks + new_bd->blocks;
587
588     ASSERT(countBlocks(bd->step->large_objects) == bd->step->n_large_blocks);
589
590     RELEASE_SM_LOCK;
591
592     return new_bd;
593 }
594
595 /* -----------------------------------------------------------------------------
596    allocate()
597
598    This allocates memory in the current thread - it is intended for
599    use primarily from STG-land where we have a Capability.  It is
600    better than allocate() because it doesn't require taking the
601    sm_mutex lock in the common case.
602
603    Memory is allocated directly from the nursery if possible (but not
604    from the current nursery block, so as not to interfere with
605    Hp/HpLim).
606    -------------------------------------------------------------------------- */
607
608 StgPtr
609 allocate (Capability *cap, lnat n)
610 {
611     bdescr *bd;
612     StgPtr p;
613     step *stp;
614
615     if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
616         lnat req_blocks =  (lnat)BLOCK_ROUND_UP(n*sizeof(W_)) / BLOCK_SIZE;
617
618         // Attempting to allocate an object larger than maxHeapSize
619         // should definitely be disallowed.  (bug #1791)
620         if (RtsFlags.GcFlags.maxHeapSize > 0 && 
621             req_blocks >= RtsFlags.GcFlags.maxHeapSize) {
622             heapOverflow();
623             // heapOverflow() doesn't exit (see #2592), but we aren't
624             // in a position to do a clean shutdown here: we
625             // either have to allocate the memory or exit now.
626             // Allocating the memory would be bad, because the user
627             // has requested that we not exceed maxHeapSize, so we
628             // just exit.
629             stg_exit(EXIT_HEAPOVERFLOW);
630         }
631
632         stp = &nurseries[cap->no];
633
634         bd = allocGroup(req_blocks);
635         dbl_link_onto(bd, &stp->large_objects);
636         stp->n_large_blocks += bd->blocks; // might be larger than req_blocks
637         initBdescr(bd, stp);
638         bd->flags = BF_LARGE;
639         bd->free = bd->start + n;
640         return bd->start;
641     }
642
643     /* small allocation (<LARGE_OBJECT_THRESHOLD) */
644
645     TICK_ALLOC_HEAP_NOCTR(n);
646     CCS_ALLOC(CCCS,n);
647     
648     bd = cap->r.rCurrentAlloc;
649     if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
650         
651         // The CurrentAlloc block is full, we need to find another
652         // one.  First, we try taking the next block from the
653         // nursery:
654         bd = cap->r.rCurrentNursery->link;
655         
656         if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
657             // The nursery is empty, or the next block is already
658             // full: allocate a fresh block (we can't fail here).
659             ACQUIRE_SM_LOCK;
660             bd = allocBlock();
661             cap->r.rNursery->n_blocks++;
662             RELEASE_SM_LOCK;
663             initBdescr(bd, cap->r.rNursery);
664             bd->flags = 0;
665             // If we had to allocate a new block, then we'll GC
666             // pretty quickly now, because MAYBE_GC() will
667             // notice that CurrentNursery->link is NULL.
668         } else {
669             // we have a block in the nursery: take it and put
670             // it at the *front* of the nursery list, and use it
671             // to allocate() from.
672             cap->r.rCurrentNursery->link = bd->link;
673             if (bd->link != NULL) {
674                 bd->link->u.back = cap->r.rCurrentNursery;
675             }
676         }
677         dbl_link_onto(bd, &cap->r.rNursery->blocks);
678         cap->r.rCurrentAlloc = bd;
679         IF_DEBUG(sanity, checkNurserySanity(cap->r.rNursery));
680     }
681     p = bd->free;
682     bd->free += n;
683     return p;
684 }
685
686 /* ---------------------------------------------------------------------------
687    Allocate a fixed/pinned object.
688
689    We allocate small pinned objects into a single block, allocating a
690    new block when the current one overflows.  The block is chained
691    onto the large_object_list of generation 0 step 0.
692
693    NOTE: The GC can't in general handle pinned objects.  This
694    interface is only safe to use for ByteArrays, which have no
695    pointers and don't require scavenging.  It works because the
696    block's descriptor has the BF_LARGE flag set, so the block is
697    treated as a large object and chained onto various lists, rather
698    than the individual objects being copied.  However, when it comes
699    to scavenge the block, the GC will only scavenge the first object.
700    The reason is that the GC can't linearly scan a block of pinned
701    objects at the moment (doing so would require using the
702    mostly-copying techniques).  But since we're restricting ourselves
703    to pinned ByteArrays, not scavenging is ok.
704
705    This function is called by newPinnedByteArray# which immediately
706    fills the allocated memory with a MutableByteArray#.
707    ------------------------------------------------------------------------- */
708
709 StgPtr
710 allocatePinned (Capability *cap, lnat n)
711 {
712     StgPtr p;
713     bdescr *bd;
714     step *stp;
715
716     // If the request is for a large object, then allocate()
717     // will give us a pinned object anyway.
718     if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
719         p = allocate(cap, n);
720         Bdescr(p)->flags |= BF_PINNED;
721         return p;
722     }
723
724     TICK_ALLOC_HEAP_NOCTR(n);
725     CCS_ALLOC(CCCS,n);
726
727     bd = cap->pinned_object_block;
728     
729     // If we don't have a block of pinned objects yet, or the current
730     // one isn't large enough to hold the new object, allocate a new one.
731     if (bd == NULL || (bd->free + n) > (bd->start + BLOCK_SIZE_W)) {
732         ACQUIRE_SM_LOCK
733         cap->pinned_object_block = bd = allocBlock();
734         RELEASE_SM_LOCK
735         stp = &nurseries[cap->no];
736         dbl_link_onto(bd, &stp->large_objects);
737         stp->n_large_blocks++;
738         initBdescr(bd, stp);
739         bd->flags  = BF_PINNED | BF_LARGE;
740         bd->free   = bd->start;
741     }
742
743     p = bd->free;
744     bd->free += n;
745     return p;
746 }
747
748 /* -----------------------------------------------------------------------------
749    Write Barriers
750    -------------------------------------------------------------------------- */
751
752 /*
753    This is the write barrier for MUT_VARs, a.k.a. IORefs.  A
754    MUT_VAR_CLEAN object is not on the mutable list; a MUT_VAR_DIRTY
755    is.  When written to, a MUT_VAR_CLEAN turns into a MUT_VAR_DIRTY
756    and is put on the mutable list.
757 */
758 void
759 dirty_MUT_VAR(StgRegTable *reg, StgClosure *p)
760 {
761     Capability *cap = regTableToCapability(reg);
762     bdescr *bd;
763     if (p->header.info == &stg_MUT_VAR_CLEAN_info) {
764         p->header.info = &stg_MUT_VAR_DIRTY_info;
765         bd = Bdescr((StgPtr)p);
766         if (bd->gen_no > 0) recordMutableCap(p,cap,bd->gen_no);
767     }
768 }
769
770 // Setting a TSO's link field with a write barrier.
771 // It is *not* necessary to call this function when
772 //    * setting the link field to END_TSO_QUEUE
773 //    * putting a TSO on the blackhole_queue
774 //    * setting the link field of the currently running TSO, as it
775 //      will already be dirty.
776 void
777 setTSOLink (Capability *cap, StgTSO *tso, StgTSO *target)
778 {
779     bdescr *bd;
780     if (tso->dirty == 0 && (tso->flags & TSO_LINK_DIRTY) == 0) {
781         tso->flags |= TSO_LINK_DIRTY;
782         bd = Bdescr((StgPtr)tso);
783         if (bd->gen_no > 0) recordMutableCap((StgClosure*)tso,cap,bd->gen_no);
784     }
785     tso->_link = target;
786 }
787
788 void
789 dirty_TSO (Capability *cap, StgTSO *tso)
790 {
791     bdescr *bd;
792     if (tso->dirty == 0 && (tso->flags & TSO_LINK_DIRTY) == 0) {
793         bd = Bdescr((StgPtr)tso);
794         if (bd->gen_no > 0) recordMutableCap((StgClosure*)tso,cap,bd->gen_no);
795     }
796     tso->dirty = 1;
797 }
798
799 /*
800    This is the write barrier for MVARs.  An MVAR_CLEAN objects is not
801    on the mutable list; a MVAR_DIRTY is.  When written to, a
802    MVAR_CLEAN turns into a MVAR_DIRTY and is put on the mutable list.
803    The check for MVAR_CLEAN is inlined at the call site for speed,
804    this really does make a difference on concurrency-heavy benchmarks
805    such as Chaneneos and cheap-concurrency.
806 */
807 void
808 dirty_MVAR(StgRegTable *reg, StgClosure *p)
809 {
810     Capability *cap = regTableToCapability(reg);
811     bdescr *bd;
812     bd = Bdescr((StgPtr)p);
813     if (bd->gen_no > 0) recordMutableCap(p,cap,bd->gen_no);
814 }
815
816 /* -----------------------------------------------------------------------------
817  * Stats and stuff
818  * -------------------------------------------------------------------------- */
819
820 /* -----------------------------------------------------------------------------
821  * calcAllocated()
822  *
823  * Approximate how much we've allocated: number of blocks in the
824  * nursery + blocks allocated via allocate() - unused nusery blocks.
825  * This leaves a little slop at the end of each block.
826  * -------------------------------------------------------------------------- */
827
828 lnat
829 calcAllocated( void )
830 {
831   nat allocated;
832   bdescr *bd;
833   nat i;
834
835   allocated = countNurseryBlocks() * BLOCK_SIZE_W;
836   
837   for (i = 0; i < n_capabilities; i++) {
838       Capability *cap;
839       for ( bd = capabilities[i].r.rCurrentNursery->link; 
840             bd != NULL; bd = bd->link ) {
841           allocated -= BLOCK_SIZE_W;
842       }
843       cap = &capabilities[i];
844       if (cap->r.rCurrentNursery->free < 
845           cap->r.rCurrentNursery->start + BLOCK_SIZE_W) {
846           allocated -= (cap->r.rCurrentNursery->start + BLOCK_SIZE_W)
847               - cap->r.rCurrentNursery->free;
848       }
849       if (cap->pinned_object_block != NULL) {
850           allocated -= (cap->pinned_object_block->start + BLOCK_SIZE_W) - 
851               cap->pinned_object_block->free;
852       }
853   }
854
855   total_allocated += allocated;
856   return allocated;
857 }  
858
859 /* Approximate the amount of live data in the heap.  To be called just
860  * after garbage collection (see GarbageCollect()).
861  */
862 lnat 
863 calcLiveBlocks(void)
864 {
865   nat g, s;
866   lnat live = 0;
867   step *stp;
868
869   for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
870     for (s = 0; s < generations[g].n_steps; s++) {
871       /* approximate amount of live data (doesn't take into account slop
872        * at end of each block).
873        */
874       if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) { 
875           continue; 
876       }
877       stp = &generations[g].steps[s];
878       live += stp->n_large_blocks + stp->n_blocks;
879     }
880   }
881   return live;
882 }
883
884 lnat
885 countOccupied(bdescr *bd)
886 {
887     lnat words;
888
889     words = 0;
890     for (; bd != NULL; bd = bd->link) {
891         ASSERT(bd->free <= bd->start + bd->blocks * BLOCK_SIZE_W);
892         words += bd->free - bd->start;
893     }
894     return words;
895 }
896
897 // Return an accurate count of the live data in the heap, excluding
898 // generation 0.
899 lnat
900 calcLiveWords(void)
901 {
902     nat g, s;
903     lnat live;
904     step *stp;
905     
906     live = 0;
907     for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
908         for (s = 0; s < generations[g].n_steps; s++) {
909             if (g == 0 && s == 0 && RtsFlags.GcFlags.generations > 1) continue; 
910             stp = &generations[g].steps[s];
911             live += stp->n_words + countOccupied(stp->large_objects);
912         } 
913     }
914     return live;
915 }
916
917 /* Approximate the number of blocks that will be needed at the next
918  * garbage collection.
919  *
920  * Assume: all data currently live will remain live.  Steps that will
921  * be collected next time will therefore need twice as many blocks
922  * since all the data will be copied.
923  */
924 extern lnat 
925 calcNeeded(void)
926 {
927     lnat needed = 0;
928     nat g, s;
929     step *stp;
930     
931     for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
932         for (s = 0; s < generations[g].n_steps; s++) {
933             if (g == 0 && s == 0) { continue; }
934             stp = &generations[g].steps[s];
935
936             // we need at least this much space
937             needed += stp->n_blocks + stp->n_large_blocks;
938
939             // any additional space needed to collect this gen next time?
940             if (g == 0 || // always collect gen 0
941                 (generations[g].steps[0].n_blocks +
942                  generations[g].steps[0].n_large_blocks 
943                  > generations[g].max_blocks)) {
944                 // we will collect this gen next time
945                 if (stp->mark) {
946                     //  bitmap:
947                     needed += stp->n_blocks / BITS_IN(W_);
948                     //  mark stack:
949                     needed += stp->n_blocks / 100;
950                 }
951                 if (stp->compact) {
952                     continue; // no additional space needed for compaction
953                 } else {
954                     needed += stp->n_blocks;
955                 }
956             }
957         }
958     }
959     return needed;
960 }
961
962 /* ----------------------------------------------------------------------------
963    Executable memory
964
965    Executable memory must be managed separately from non-executable
966    memory.  Most OSs these days require you to jump through hoops to
967    dynamically allocate executable memory, due to various security
968    measures.
969
970    Here we provide a small memory allocator for executable memory.
971    Memory is managed with a page granularity; we allocate linearly
972    in the page, and when the page is emptied (all objects on the page
973    are free) we free the page again, not forgetting to make it
974    non-executable.
975
976    TODO: The inability to handle objects bigger than BLOCK_SIZE_W means that
977          the linker cannot use allocateExec for loading object code files
978          on Windows. Once allocateExec can handle larger objects, the linker
979          should be modified to use allocateExec instead of VirtualAlloc.
980    ------------------------------------------------------------------------- */
981
982 #if defined(linux_HOST_OS)
983
984 // On Linux we need to use libffi for allocating executable memory,
985 // because it knows how to work around the restrictions put in place
986 // by SELinux.
987
988 void *allocateExec (nat bytes, void **exec_ret)
989 {
990     void **ret, **exec;
991     ACQUIRE_SM_LOCK;
992     ret = ffi_closure_alloc (sizeof(void *) + (size_t)bytes, (void**)&exec);
993     RELEASE_SM_LOCK;
994     if (ret == NULL) return ret;
995     *ret = ret; // save the address of the writable mapping, for freeExec().
996     *exec_ret = exec + 1;
997     return (ret + 1);
998 }
999
1000 // freeExec gets passed the executable address, not the writable address. 
1001 void freeExec (void *addr)
1002 {
1003     void *writable;
1004     writable = *((void**)addr - 1);
1005     ACQUIRE_SM_LOCK;
1006     ffi_closure_free (writable);
1007     RELEASE_SM_LOCK
1008 }
1009
1010 #else
1011
1012 void *allocateExec (nat bytes, void **exec_ret)
1013 {
1014     void *ret;
1015     nat n;
1016
1017     ACQUIRE_SM_LOCK;
1018
1019     // round up to words.
1020     n  = (bytes + sizeof(W_) + 1) / sizeof(W_);
1021
1022     if (n+1 > BLOCK_SIZE_W) {
1023         barf("allocateExec: can't handle large objects");
1024     }
1025
1026     if (exec_block == NULL || 
1027         exec_block->free + n + 1 > exec_block->start + BLOCK_SIZE_W) {
1028         bdescr *bd;
1029         lnat pagesize = getPageSize();
1030         bd = allocGroup(stg_max(1, pagesize / BLOCK_SIZE));
1031         debugTrace(DEBUG_gc, "allocate exec block %p", bd->start);
1032         bd->gen_no = 0;
1033         bd->flags = BF_EXEC;
1034         bd->link = exec_block;
1035         if (exec_block != NULL) {
1036             exec_block->u.back = bd;
1037         }
1038         bd->u.back = NULL;
1039         setExecutable(bd->start, bd->blocks * BLOCK_SIZE, rtsTrue);
1040         exec_block = bd;
1041     }
1042     *(exec_block->free) = n;  // store the size of this chunk
1043     exec_block->gen_no += n;  // gen_no stores the number of words allocated
1044     ret = exec_block->free + 1;
1045     exec_block->free += n + 1;
1046
1047     RELEASE_SM_LOCK
1048     *exec_ret = ret;
1049     return ret;
1050 }
1051
1052 void freeExec (void *addr)
1053 {
1054     StgPtr p = (StgPtr)addr - 1;
1055     bdescr *bd = Bdescr((StgPtr)p);
1056
1057     if ((bd->flags & BF_EXEC) == 0) {
1058         barf("freeExec: not executable");
1059     }
1060
1061     if (*(StgPtr)p == 0) {
1062         barf("freeExec: already free?");
1063     }
1064
1065     ACQUIRE_SM_LOCK;
1066
1067     bd->gen_no -= *(StgPtr)p;
1068     *(StgPtr)p = 0;
1069
1070     if (bd->gen_no == 0) {
1071         // Free the block if it is empty, but not if it is the block at
1072         // the head of the queue.
1073         if (bd != exec_block) {
1074             debugTrace(DEBUG_gc, "free exec block %p", bd->start);
1075             dbl_link_remove(bd, &exec_block);
1076             setExecutable(bd->start, bd->blocks * BLOCK_SIZE, rtsFalse);
1077             freeGroup(bd);
1078         } else {
1079             bd->free = bd->start;
1080         }
1081     }
1082
1083     RELEASE_SM_LOCK
1084 }    
1085
1086 #endif /* mingw32_HOST_OS */
1087
1088 #ifdef DEBUG
1089
1090 // handy function for use in gdb, because Bdescr() is inlined.
1091 extern bdescr *_bdescr( StgPtr p );
1092
1093 bdescr *
1094 _bdescr( StgPtr p )
1095 {
1096     return Bdescr(p);
1097 }
1098
1099 #endif