1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2004
5 * Storage manager front end
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
15 #include "BlockAlloc.h"
20 #include "OSThreads.h"
21 #include "Capability.h"
24 #include "RetainerProfile.h" // for counting memory blocks (memInventory)
30 * All these globals require sm_mutex to access in SMP mode.
32 StgClosure *caf_list = NULL;
33 StgClosure *revertible_caf_list = NULL;
36 bdescr *small_alloc_list; /* allocate()d small objects */
37 bdescr *pinned_object_block; /* allocate pinned objects into this block */
38 nat alloc_blocks; /* number of allocate()d blocks since GC */
39 nat alloc_blocks_lim; /* approximate limit on alloc_blocks */
41 StgPtr alloc_Hp = NULL; /* next free byte in small_alloc_list */
42 StgPtr alloc_HpLim = NULL; /* end of block at small_alloc_list */
44 generation *generations = NULL; /* all the generations */
45 generation *g0 = NULL; /* generation 0, for convenience */
46 generation *oldest_gen = NULL; /* oldest generation, for convenience */
47 step *g0s0 = NULL; /* generation 0, step 0, for convenience */
49 ullong total_allocated = 0; /* total memory allocated during run */
51 nat n_nurseries = 0; /* == RtsFlags.ParFlags.nNodes, convenience */
52 step *nurseries = NULL; /* array of nurseries, >1 only if SMP */
55 * Storage manager mutex: protects all the above state from
56 * simultaneous access by two STG threads.
59 Mutex sm_mutex = INIT_MUTEX_VAR;
65 static void *stgAllocForGMP (size_t size_in_bytes);
66 static void *stgReallocForGMP (void *ptr, size_t old_size, size_t new_size);
67 static void stgDeallocForGMP (void *ptr, size_t size);
70 initStep (step *stp, int g, int s)
75 stp->old_blocks = NULL;
76 stp->n_old_blocks = 0;
77 stp->gen = &generations[g];
83 stp->scavd_hpLim = NULL;
86 stp->large_objects = NULL;
87 stp->n_large_blocks = 0;
88 stp->new_large_objects = NULL;
89 stp->scavenged_large_objects = NULL;
90 stp->n_scavenged_large_blocks = 0;
91 stp->is_compacted = 0;
101 if (generations != NULL) {
102 // multi-init protection
106 /* Sanity check to make sure the LOOKS_LIKE_ macros appear to be
107 * doing something reasonable.
109 ASSERT(LOOKS_LIKE_INFO_PTR(&stg_BLACKHOLE_info));
110 ASSERT(LOOKS_LIKE_CLOSURE_PTR(&stg_dummy_ret_closure));
111 ASSERT(!HEAP_ALLOCED(&stg_dummy_ret_closure));
113 if (RtsFlags.GcFlags.maxHeapSize != 0 &&
114 RtsFlags.GcFlags.heapSizeSuggestion >
115 RtsFlags.GcFlags.maxHeapSize) {
116 RtsFlags.GcFlags.maxHeapSize = RtsFlags.GcFlags.heapSizeSuggestion;
119 if (RtsFlags.GcFlags.maxHeapSize != 0 &&
120 RtsFlags.GcFlags.minAllocAreaSize >
121 RtsFlags.GcFlags.maxHeapSize) {
122 errorBelch("maximum heap size (-M) is smaller than minimum alloc area size (-A)");
126 initBlockAllocator();
129 initMutex(&sm_mutex);
132 /* allocate generation info array */
133 generations = (generation *)stgMallocBytes(RtsFlags.GcFlags.generations
134 * sizeof(struct generation_),
135 "initStorage: gens");
137 /* Initialise all generations */
138 for(g = 0; g < RtsFlags.GcFlags.generations; g++) {
139 gen = &generations[g];
141 gen->mut_list = allocBlock();
142 gen->collections = 0;
143 gen->failed_promotions = 0;
147 /* A couple of convenience pointers */
148 g0 = &generations[0];
149 oldest_gen = &generations[RtsFlags.GcFlags.generations-1];
151 /* Allocate step structures in each generation */
152 if (RtsFlags.GcFlags.generations > 1) {
153 /* Only for multiple-generations */
155 /* Oldest generation: one step */
156 oldest_gen->n_steps = 1;
158 stgMallocBytes(1 * sizeof(struct step_), "initStorage: last step");
160 /* set up all except the oldest generation with 2 steps */
161 for(g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
162 generations[g].n_steps = RtsFlags.GcFlags.steps;
163 generations[g].steps =
164 stgMallocBytes (RtsFlags.GcFlags.steps * sizeof(struct step_),
165 "initStorage: steps");
169 /* single generation, i.e. a two-space collector */
171 g0->steps = stgMallocBytes (sizeof(struct step_), "initStorage: steps");
175 n_nurseries = RtsFlags.ParFlags.nNodes;
176 nurseries = stgMallocBytes (n_nurseries * sizeof(struct step_),
177 "initStorage: nurseries");
180 nurseries = g0->steps; // just share nurseries[0] with g0s0
183 /* Initialise all steps */
184 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
185 for (s = 0; s < generations[g].n_steps; s++) {
186 initStep(&generations[g].steps[s], g, s);
191 for (s = 0; s < n_nurseries; s++) {
192 initStep(&nurseries[s], 0, s);
196 /* Set up the destination pointers in each younger gen. step */
197 for (g = 0; g < RtsFlags.GcFlags.generations-1; g++) {
198 for (s = 0; s < generations[g].n_steps-1; s++) {
199 generations[g].steps[s].to = &generations[g].steps[s+1];
201 generations[g].steps[s].to = &generations[g+1].steps[0];
203 oldest_gen->steps[0].to = &oldest_gen->steps[0];
206 for (s = 0; s < n_nurseries; s++) {
207 nurseries[s].to = generations[0].steps[0].to;
211 /* The oldest generation has one step. */
212 if (RtsFlags.GcFlags.compact) {
213 if (RtsFlags.GcFlags.generations == 1) {
214 errorBelch("WARNING: compaction is incompatible with -G1; disabled");
216 oldest_gen->steps[0].is_compacted = 1;
221 if (RtsFlags.GcFlags.generations == 1) {
222 errorBelch("-G1 is incompatible with SMP");
227 /* generation 0 is special: that's the nursery */
228 generations[0].max_blocks = 0;
230 /* G0S0: the allocation area. Policy: keep the allocation area
231 * small to begin with, even if we have a large suggested heap
232 * size. Reason: we're going to do a major collection first, and we
233 * don't want it to be a big one. This vague idea is borne out by
234 * rigorous experimental evidence.
236 g0s0 = &generations[0].steps[0];
240 weak_ptr_list = NULL;
242 revertible_caf_list = NULL;
244 /* initialise the allocate() interface */
245 small_alloc_list = NULL;
247 alloc_blocks_lim = RtsFlags.GcFlags.minAllocAreaSize;
249 /* Tell GNU multi-precision pkg about our custom alloc functions */
250 mp_set_memory_functions(stgAllocForGMP, stgReallocForGMP, stgDeallocForGMP);
252 IF_DEBUG(gc, statDescribeGens());
258 stat_exit(calcAllocated());
261 /* -----------------------------------------------------------------------------
264 The entry code for every CAF does the following:
266 - builds a CAF_BLACKHOLE in the heap
267 - pushes an update frame pointing to the CAF_BLACKHOLE
268 - invokes UPD_CAF(), which:
269 - calls newCaf, below
270 - updates the CAF with a static indirection to the CAF_BLACKHOLE
272 Why do we build a BLACKHOLE in the heap rather than just updating
273 the thunk directly? It's so that we only need one kind of update
274 frame - otherwise we'd need a static version of the update frame too.
276 newCaf() does the following:
278 - it puts the CAF on the oldest generation's mut-once list.
279 This is so that we can treat the CAF as a root when collecting
282 For GHCI, we have additional requirements when dealing with CAFs:
284 - we must *retain* all dynamically-loaded CAFs ever entered,
285 just in case we need them again.
286 - we must be able to *revert* CAFs that have been evaluated, to
287 their pre-evaluated form.
289 To do this, we use an additional CAF list. When newCaf() is
290 called on a dynamically-loaded CAF, we add it to the CAF list
291 instead of the old-generation mutable list, and save away its
292 old info pointer (in caf->saved_info) for later reversion.
294 To revert all the CAFs, we traverse the CAF list and reset the
295 info pointer to caf->saved_info, then throw away the CAF list.
296 (see GC.c:revertCAFs()).
300 -------------------------------------------------------------------------- */
303 newCAF(StgClosure* caf)
310 // If we are in GHCi _and_ we are using dynamic libraries,
311 // then we can't redirect newCAF calls to newDynCAF (see below),
312 // so we make newCAF behave almost like newDynCAF.
313 // The dynamic libraries might be used by both the interpreted
314 // program and GHCi itself, so they must not be reverted.
315 // This also means that in GHCi with dynamic libraries, CAFs are not
316 // garbage collected. If this turns out to be a problem, we could
317 // do another hack here and do an address range test on caf to figure
318 // out whether it is from a dynamic library.
319 ((StgIndStatic *)caf)->saved_info = (StgInfoTable *)caf->header.info;
320 ((StgIndStatic *)caf)->static_link = caf_list;
325 /* Put this CAF on the mutable list for the old generation.
326 * This is a HACK - the IND_STATIC closure doesn't really have
327 * a mut_link field, but we pretend it has - in fact we re-use
328 * the STATIC_LINK field for the time being, because when we
329 * come to do a major GC we won't need the mut_link field
330 * any more and can use it as a STATIC_LINK.
332 ((StgIndStatic *)caf)->saved_info = NULL;
333 recordMutableGen(caf, oldest_gen);
339 /* If we are PAR or DIST then we never forget a CAF */
341 //debugBelch("<##> Globalising CAF %08x %s",caf,info_type(caf));
342 newGA=makeGlobal(caf,rtsTrue); /*given full weight*/
348 // An alternate version of newCaf which is used for dynamically loaded
349 // object code in GHCi. In this case we want to retain *all* CAFs in
350 // the object code, because they might be demanded at any time from an
351 // expression evaluated on the command line.
352 // Also, GHCi might want to revert CAFs, so we add these to the
353 // revertible_caf_list.
355 // The linker hackily arranges that references to newCaf from dynamic
356 // code end up pointing to newDynCAF.
358 newDynCAF(StgClosure *caf)
362 ((StgIndStatic *)caf)->saved_info = (StgInfoTable *)caf->header.info;
363 ((StgIndStatic *)caf)->static_link = revertible_caf_list;
364 revertible_caf_list = caf;
369 /* -----------------------------------------------------------------------------
371 -------------------------------------------------------------------------- */
374 allocNursery (step *stp, bdescr *tail, nat blocks)
379 // Allocate a nursery: we allocate fresh blocks one at a time and
380 // cons them on to the front of the list, not forgetting to update
381 // the back pointer on the tail of the list to point to the new block.
382 for (i=0; i < blocks; i++) {
385 processNursery() in LdvProfile.c assumes that every block group in
386 the nursery contains only a single block. So, if a block group is
387 given multiple blocks, change processNursery() accordingly.
391 // double-link the nursery: we might need to insert blocks
398 bd->free = bd->start;
406 assignNurseriesToCapabilities (void)
411 for (i = 0; i < n_nurseries; i++) {
412 capabilities[i].r.rNursery = &nurseries[i];
413 capabilities[i].r.rCurrentNursery = nurseries[i].blocks;
414 capabilities[i].r.rCurrentAlloc = NULL;
417 MainCapability.r.rNursery = &nurseries[0];
418 MainCapability.r.rCurrentNursery = nurseries[0].blocks;
419 MainCapability.r.rCurrentAlloc = NULL;
424 allocNurseries( void )
428 for (i = 0; i < n_nurseries; i++) {
429 nurseries[i].blocks =
430 allocNursery(&nurseries[i], NULL,
431 RtsFlags.GcFlags.minAllocAreaSize);
432 nurseries[i].n_blocks = RtsFlags.GcFlags.minAllocAreaSize;
433 nurseries[i].old_blocks = NULL;
434 nurseries[i].n_old_blocks = 0;
435 /* hp, hpLim, hp_bd, to_space etc. aren't used in the nursery */
437 assignNurseriesToCapabilities();
441 resetNurseries( void )
447 for (i = 0; i < n_nurseries; i++) {
449 for (bd = stp->blocks; bd; bd = bd->link) {
450 bd->free = bd->start;
451 ASSERT(bd->gen_no == 0);
452 ASSERT(bd->step == stp);
453 IF_DEBUG(sanity,memset(bd->start, 0xaa, BLOCK_SIZE));
456 assignNurseriesToCapabilities();
460 countNurseryBlocks (void)
465 for (i = 0; i < n_nurseries; i++) {
466 blocks += nurseries[i].n_blocks;
472 resizeNursery ( step *stp, nat blocks )
477 nursery_blocks = stp->n_blocks;
478 if (nursery_blocks == blocks) return;
480 if (nursery_blocks < blocks) {
481 IF_DEBUG(gc, debugBelch("Increasing size of nursery to %d blocks\n",
483 stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
488 IF_DEBUG(gc, debugBelch("Decreasing size of nursery to %d blocks\n",
492 while (nursery_blocks > blocks) {
494 next_bd->u.back = NULL;
495 nursery_blocks -= bd->blocks; // might be a large block
500 // might have gone just under, by freeing a large block, so make
501 // up the difference.
502 if (nursery_blocks < blocks) {
503 stp->blocks = allocNursery(stp, stp->blocks, blocks-nursery_blocks);
507 stp->n_blocks = blocks;
508 ASSERT(countBlocks(stp->blocks) == stp->n_blocks);
512 // Resize each of the nurseries to the specified size.
515 resizeNurseriesFixed (nat blocks)
518 for (i = 0; i < n_nurseries; i++) {
519 resizeNursery(&nurseries[i], blocks);
524 // Resize the nurseries to the total specified size.
527 resizeNurseries (nat blocks)
529 // If there are multiple nurseries, then we just divide the number
530 // of available blocks between them.
531 resizeNurseriesFixed(blocks / n_nurseries);
534 /* -----------------------------------------------------------------------------
535 The allocate() interface
537 allocate(n) always succeeds, and returns a chunk of memory n words
538 long. n can be larger than the size of a block if necessary, in
539 which case a contiguous block group will be allocated.
540 -------------------------------------------------------------------------- */
550 TICK_ALLOC_HEAP_NOCTR(n);
553 /* big allocation (>LARGE_OBJECT_THRESHOLD) */
554 /* ToDo: allocate directly into generation 1 */
555 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
556 nat req_blocks = (lnat)BLOCK_ROUND_UP(n*sizeof(W_)) / BLOCK_SIZE;
557 bd = allocGroup(req_blocks);
558 dbl_link_onto(bd, &g0s0->large_objects);
559 g0s0->n_large_blocks += req_blocks;
562 bd->flags = BF_LARGE;
563 bd->free = bd->start + n;
564 alloc_blocks += req_blocks;
568 /* small allocation (<LARGE_OBJECT_THRESHOLD) */
569 } else if (small_alloc_list == NULL || alloc_Hp + n > alloc_HpLim) {
570 if (small_alloc_list) {
571 small_alloc_list->free = alloc_Hp;
574 bd->link = small_alloc_list;
575 small_alloc_list = bd;
579 alloc_Hp = bd->start;
580 alloc_HpLim = bd->start + BLOCK_SIZE_W;
591 allocated_bytes( void )
595 allocated = alloc_blocks * BLOCK_SIZE_W - (alloc_HpLim - alloc_Hp);
596 if (pinned_object_block != NULL) {
597 allocated -= (pinned_object_block->start + BLOCK_SIZE_W) -
598 pinned_object_block->free;
605 tidyAllocateLists (void)
607 if (small_alloc_list != NULL) {
608 ASSERT(alloc_Hp >= small_alloc_list->start &&
609 alloc_Hp <= small_alloc_list->start + BLOCK_SIZE);
610 small_alloc_list->free = alloc_Hp;
614 /* -----------------------------------------------------------------------------
617 This allocates memory in the current thread - it is intended for
618 use primarily from STG-land where we have a Capability. It is
619 better than allocate() because it doesn't require taking the
620 sm_mutex lock in the common case.
622 Memory is allocated directly from the nursery if possible (but not
623 from the current nursery block, so as not to interfere with
625 -------------------------------------------------------------------------- */
628 allocateLocal( StgRegTable *reg, nat n )
633 TICK_ALLOC_HEAP_NOCTR(n);
636 /* big allocation (>LARGE_OBJECT_THRESHOLD) */
637 /* ToDo: allocate directly into generation 1 */
638 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
639 nat req_blocks = (lnat)BLOCK_ROUND_UP(n*sizeof(W_)) / BLOCK_SIZE;
641 bd = allocGroup(req_blocks);
642 dbl_link_onto(bd, &g0s0->large_objects);
643 g0s0->n_large_blocks += req_blocks;
646 bd->flags = BF_LARGE;
647 bd->free = bd->start + n;
648 alloc_blocks += req_blocks;
652 /* small allocation (<LARGE_OBJECT_THRESHOLD) */
655 bd = reg->rCurrentAlloc;
656 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
658 // The CurrentAlloc block is full, we need to find another
659 // one. First, we try taking the next block from the
661 bd = reg->rCurrentNursery->link;
663 if (bd == NULL || bd->free + n > bd->start + BLOCK_SIZE_W) {
664 // The nursery is empty, or the next block is already
665 // full: allocate a fresh block (we can't fail here).
668 reg->rNursery->n_blocks++;
674 // we have a block in the nursery: take it and put
675 // it at the *front* of the nursery list, and use it
676 // to allocate() from.
677 reg->rCurrentNursery->link = bd->link;
678 if (bd->link != NULL) {
679 bd->link->u.back = reg->rCurrentNursery;
682 dbl_link_onto(bd, ®->rNursery->blocks);
683 reg->rCurrentAlloc = bd;
684 IF_DEBUG(sanity, checkNurserySanity(reg->rNursery));
692 /* ---------------------------------------------------------------------------
693 Allocate a fixed/pinned object.
695 We allocate small pinned objects into a single block, allocating a
696 new block when the current one overflows. The block is chained
697 onto the large_object_list of generation 0 step 0.
699 NOTE: The GC can't in general handle pinned objects. This
700 interface is only safe to use for ByteArrays, which have no
701 pointers and don't require scavenging. It works because the
702 block's descriptor has the BF_LARGE flag set, so the block is
703 treated as a large object and chained onto various lists, rather
704 than the individual objects being copied. However, when it comes
705 to scavenge the block, the GC will only scavenge the first object.
706 The reason is that the GC can't linearly scan a block of pinned
707 objects at the moment (doing so would require using the
708 mostly-copying techniques). But since we're restricting ourselves
709 to pinned ByteArrays, not scavenging is ok.
711 This function is called by newPinnedByteArray# which immediately
712 fills the allocated memory with a MutableByteArray#.
713 ------------------------------------------------------------------------- */
716 allocatePinned( nat n )
719 bdescr *bd = pinned_object_block;
721 // If the request is for a large object, then allocate()
722 // will give us a pinned object anyway.
723 if (n >= LARGE_OBJECT_THRESHOLD/sizeof(W_)) {
729 TICK_ALLOC_HEAP_NOCTR(n);
732 // we always return 8-byte aligned memory. bd->free must be
733 // 8-byte aligned to begin with, so we just round up n to
734 // the nearest multiple of 8 bytes.
735 if (sizeof(StgWord) == 4) {
739 // If we don't have a block of pinned objects yet, or the current
740 // one isn't large enough to hold the new object, allocate a new one.
741 if (bd == NULL || (bd->free + n) > (bd->start + BLOCK_SIZE_W)) {
742 pinned_object_block = bd = allocBlock();
743 dbl_link_onto(bd, &g0s0->large_objects);
746 bd->flags = BF_PINNED | BF_LARGE;
747 bd->free = bd->start;
757 /* -----------------------------------------------------------------------------
758 Allocation functions for GMP.
760 These all use the allocate() interface - we can't have any garbage
761 collection going on during a gmp operation, so we use allocate()
762 which always succeeds. The gmp operations which might need to
763 allocate will ask the storage manager (via doYouWantToGC()) whether
764 a garbage collection is required, in case we get into a loop doing
765 only allocate() style allocation.
766 -------------------------------------------------------------------------- */
769 stgAllocForGMP (size_t size_in_bytes)
772 nat data_size_in_words, total_size_in_words;
774 /* round up to a whole number of words */
775 data_size_in_words = (size_in_bytes + sizeof(W_) + 1) / sizeof(W_);
776 total_size_in_words = sizeofW(StgArrWords) + data_size_in_words;
778 /* allocate and fill it in. */
780 arr = (StgArrWords *)allocateLocal(&(myCapability()->r), total_size_in_words);
782 arr = (StgArrWords *)allocateLocal(&MainCapability.r, total_size_in_words);
784 SET_ARR_HDR(arr, &stg_ARR_WORDS_info, CCCS, data_size_in_words);
786 /* and return a ptr to the goods inside the array */
791 stgReallocForGMP (void *ptr, size_t old_size, size_t new_size)
793 void *new_stuff_ptr = stgAllocForGMP(new_size);
795 char *p = (char *) ptr;
796 char *q = (char *) new_stuff_ptr;
798 for (; i < old_size; i++, p++, q++) {
802 return(new_stuff_ptr);
806 stgDeallocForGMP (void *ptr STG_UNUSED,
807 size_t size STG_UNUSED)
809 /* easy for us: the garbage collector does the dealloc'n */
812 /* -----------------------------------------------------------------------------
814 * -------------------------------------------------------------------------- */
816 /* -----------------------------------------------------------------------------
819 * Approximate how much we've allocated: number of blocks in the
820 * nursery + blocks allocated via allocate() - unused nusery blocks.
821 * This leaves a little slop at the end of each block, and doesn't
822 * take into account large objects (ToDo).
823 * -------------------------------------------------------------------------- */
826 calcAllocated( void )
831 allocated = allocated_bytes();
832 allocated += countNurseryBlocks() * BLOCK_SIZE_W;
837 for (i = 0; i < n_nurseries; i++) {
839 for ( bd = capabilities[i].r.rCurrentNursery->link;
840 bd != NULL; bd = bd->link ) {
841 allocated -= BLOCK_SIZE_W;
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;
851 bdescr *current_nursery = MainCapability.r.rCurrentNursery;
853 for ( bd = current_nursery->link; bd != NULL; bd = bd->link ) {
854 allocated -= BLOCK_SIZE_W;
856 if (current_nursery->free < current_nursery->start + BLOCK_SIZE_W) {
857 allocated -= (current_nursery->start + BLOCK_SIZE_W)
858 - current_nursery->free;
863 total_allocated += allocated;
867 /* Approximate the amount of live data in the heap. To be called just
868 * after garbage collection (see GarbageCollect()).
877 if (RtsFlags.GcFlags.generations == 1) {
878 live = (g0s0->n_blocks - 1) * BLOCK_SIZE_W +
879 ((lnat)g0s0->hp_bd->free - (lnat)g0s0->hp_bd->start) / sizeof(W_);
883 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
884 for (s = 0; s < generations[g].n_steps; s++) {
885 /* approximate amount of live data (doesn't take into account slop
886 * at end of each block).
888 if (g == 0 && s == 0) {
891 stp = &generations[g].steps[s];
892 live += (stp->n_large_blocks + stp->n_blocks - 1) * BLOCK_SIZE_W;
893 if (stp->hp_bd != NULL) {
894 live += ((lnat)stp->hp_bd->free - (lnat)stp->hp_bd->start)
897 if (stp->scavd_hp != NULL) {
898 live -= (P_)(BLOCK_ROUND_UP(stp->scavd_hp)) - stp->scavd_hp;
905 /* Approximate the number of blocks that will be needed at the next
906 * garbage collection.
908 * Assume: all data currently live will remain live. Steps that will
909 * be collected next time will therefore need twice as many blocks
910 * since all the data will be copied.
919 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
920 for (s = 0; s < generations[g].n_steps; s++) {
921 if (g == 0 && s == 0) { continue; }
922 stp = &generations[g].steps[s];
923 if (generations[g].steps[0].n_blocks +
924 generations[g].steps[0].n_large_blocks
925 > generations[g].max_blocks
926 && stp->is_compacted == 0) {
927 needed += 2 * stp->n_blocks;
929 needed += stp->n_blocks;
936 /* -----------------------------------------------------------------------------
939 memInventory() checks for memory leaks by counting up all the
940 blocks we know about and comparing that to the number of blocks
941 allegedly floating around in the system.
942 -------------------------------------------------------------------------- */
947 stepBlocks (step *stp)
952 total_blocks = stp->n_blocks;
953 total_blocks += stp->n_old_blocks;
954 for (bd = stp->large_objects; bd; bd = bd->link) {
955 total_blocks += bd->blocks;
956 /* hack for megablock groups: they have an extra block or two in
957 the second and subsequent megablocks where the block
958 descriptors would normally go.
960 if (bd->blocks > BLOCKS_PER_MBLOCK) {
961 total_blocks -= (MBLOCK_SIZE / BLOCK_SIZE - BLOCKS_PER_MBLOCK)
962 * (bd->blocks/(MBLOCK_SIZE/BLOCK_SIZE));
974 lnat total_blocks = 0, free_blocks = 0;
976 /* count the blocks we current have */
978 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
979 for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
980 total_blocks += bd->blocks;
982 for (s = 0; s < generations[g].n_steps; s++) {
983 if (g==0 && s==0) continue;
984 stp = &generations[g].steps[s];
985 total_blocks += stepBlocks(stp);
989 for (i = 0; i < n_nurseries; i++) {
990 total_blocks += stepBlocks(&nurseries[i]);
993 // We put pinned object blocks in g0s0, so better count blocks there too.
994 total_blocks += stepBlocks(g0s0);
997 /* any blocks held by allocate() */
998 for (bd = small_alloc_list; bd; bd = bd->link) {
999 total_blocks += bd->blocks;
1003 if (RtsFlags.ProfFlags.doHeapProfile == HEAP_BY_RETAINER) {
1004 total_blocks += retainerStackBlocks();
1008 // count the blocks allocated by the arena allocator
1009 total_blocks += arenaBlocks();
1011 /* count the blocks on the free list */
1012 free_blocks = countFreeList();
1014 if (total_blocks + free_blocks != mblocks_allocated *
1015 BLOCKS_PER_MBLOCK) {
1016 debugBelch("Blocks: %ld live + %ld free = %ld total (%ld around)\n",
1017 total_blocks, free_blocks, total_blocks + free_blocks,
1018 mblocks_allocated * BLOCKS_PER_MBLOCK);
1021 ASSERT(total_blocks + free_blocks == mblocks_allocated * BLOCKS_PER_MBLOCK);
1026 countBlocks(bdescr *bd)
1029 for (n=0; bd != NULL; bd=bd->link) {
1035 /* Full heap sanity check. */
1041 if (RtsFlags.GcFlags.generations == 1) {
1042 checkHeap(g0s0->blocks);
1043 checkChain(g0s0->large_objects);
1046 for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1047 for (s = 0; s < generations[g].n_steps; s++) {
1048 if (g == 0 && s == 0) { continue; }
1049 ASSERT(countBlocks(generations[g].steps[s].blocks)
1050 == generations[g].steps[s].n_blocks);
1051 ASSERT(countBlocks(generations[g].steps[s].large_objects)
1052 == generations[g].steps[s].n_large_blocks);
1053 checkHeap(generations[g].steps[s].blocks);
1054 checkChain(generations[g].steps[s].large_objects);
1056 checkMutableList(generations[g].mut_list, g);
1061 for (s = 0; s < n_nurseries; s++) {
1062 ASSERT(countBlocks(nurseries[s].blocks)
1063 == nurseries[s].n_blocks);
1064 ASSERT(countBlocks(nurseries[s].large_objects)
1065 == nurseries[s].n_large_blocks);
1068 checkFreeListSanity();
1072 /* Nursery sanity check */
1074 checkNurserySanity( step *stp )
1080 for (bd = stp->blocks; bd != NULL; bd = bd->link) {
1081 ASSERT(bd->u.back == prev);
1083 blocks += bd->blocks;
1085 ASSERT(blocks == stp->n_blocks);
1088 // handy function for use in gdb, because Bdescr() is inlined.
1089 extern bdescr *_bdescr( StgPtr p );