+ /* Guess the amount of live data for stats. */
+ live = calcLive();
+
+ /* Two-space collector:
+ * Free the old to-space, and estimate the amount of live data.
+ */
+ if (RtsFlags.GcFlags.generations == 1) {
+ nat blocks;
+
+ if (old_to_space != NULL) {
+ freeChain(old_to_space);
+ }
+ for (bd = g0s0->to_space; bd != NULL; bd = bd->link) {
+ bd->evacuated = 0; /* now from-space */
+ }
+
+ /* For a two-space collector, we need to resize the nursery. */
+
+ /* set up a new nursery. Allocate a nursery size based on a
+ * function of the amount of live data (currently a factor of 2,
+ * should be configurable (ToDo)). Use the blocks from the old
+ * nursery if possible, freeing up any left over blocks.
+ *
+ * If we get near the maximum heap size, then adjust our nursery
+ * size accordingly. If the nursery is the same size as the live
+ * data (L), then we need 3L bytes. We can reduce the size of the
+ * nursery to bring the required memory down near 2L bytes.
+ *
+ * A normal 2-space collector would need 4L bytes to give the same
+ * performance we get from 3L bytes, reducing to the same
+ * performance at 2L bytes.
+ */
+ blocks = g0s0->n_blocks;
+
+ if ( blocks * RtsFlags.GcFlags.oldGenFactor * 2 >
+ RtsFlags.GcFlags.maxHeapSize ) {
+ int adjusted_blocks; /* signed on purpose */
+ int pc_free;
+
+ adjusted_blocks = (RtsFlags.GcFlags.maxHeapSize - 2 * blocks);
+ IF_DEBUG(gc, fprintf(stderr, "Near maximum heap size of 0x%x blocks, blocks = %d, adjusted to %d\n", RtsFlags.GcFlags.maxHeapSize, blocks, adjusted_blocks));
+ pc_free = adjusted_blocks * 100 / RtsFlags.GcFlags.maxHeapSize;
+ if (pc_free < RtsFlags.GcFlags.pcFreeHeap) /* might even be < 0 */ {
+ heapOverflow();
+ }
+ blocks = adjusted_blocks;
+
+ } else {
+ blocks *= RtsFlags.GcFlags.oldGenFactor;
+ if (blocks < RtsFlags.GcFlags.minAllocAreaSize) {
+ blocks = RtsFlags.GcFlags.minAllocAreaSize;
+ }
+ }
+ resizeNursery(blocks);
+
+ } else {
+ /* Generational collector:
+ * If the user has given us a suggested heap size, adjust our
+ * allocation area to make best use of the memory available.
+ */
+
+ if (RtsFlags.GcFlags.heapSizeSuggestion) {
+ int blocks;
+ nat needed = calcNeeded(); /* approx blocks needed at next GC */
+
+ /* Guess how much will be live in generation 0 step 0 next time.
+ * A good approximation is the amount of data that was live this
+ * time: this assumes (1) that the size of G0S0 will be roughly
+ * the same as last time, and (2) that the promotion rate will be
+ * constant.
+ *
+ * If we don't know how much was live in G0S0 (because there's no
+ * step 1), then assume 30% (which is usually an overestimate).
+ */
+ if (g0->n_steps == 1) {
+ needed += (g0s0->n_blocks * 30) / 100;
+ } else {
+ needed += g0->steps[1].n_blocks;
+ }
+
+ /* Now we have a rough guess at the number of blocks needed for
+ * the next GC, subtract this from the user's suggested heap size
+ * and use the rest for the allocation area.
+ */
+ blocks = (int)RtsFlags.GcFlags.heapSizeSuggestion - (int)needed;
+
+ if (blocks < (int)RtsFlags.GcFlags.minAllocAreaSize) {
+ blocks = RtsFlags.GcFlags.minAllocAreaSize;
+ }
+
+ resizeNursery((nat)blocks);
+ }
+ }
+