Compact.c 23.9 KB
Newer Older
1 2
/* -----------------------------------------------------------------------------
 *
3
 * (c) The GHC Team 2001-2008
4 5 6
 *
 * Compacting garbage collector
 *
7 8 9
 * Documentation on the architecture of the Garbage Collector can be
 * found in the online commentary:
 * 
10
 *   http://ghc.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
11
 *
12 13
 * ---------------------------------------------------------------------------*/

14
#include "PosixSource.h"
15
#include "Rts.h"
Simon Marlow's avatar
Simon Marlow committed
16

Simon Marlow's avatar
Simon Marlow committed
17
#include "GCThread.h"
Simon Marlow's avatar
Simon Marlow committed
18
#include "Storage.h"
19 20
#include "RtsUtils.h"
#include "BlockAlloc.h"
21 22
#include "GC.h"
#include "Compact.h"
23
#include "Schedule.h"
24
#include "Apply.h"
Simon Marlow's avatar
Simon Marlow committed
25
#include "Trace.h"
Simon Marlow's avatar
Simon Marlow committed
26 27 28
#include "Weak.h"
#include "MarkWeak.h"
#include "Stable.h"
29

30 31
// Turn off inlining when debugging - it obfuscates things
#ifdef DEBUG
sof's avatar
sof committed
32 33
# undef  STATIC_INLINE
# define STATIC_INLINE static
34 35
#endif

36
/* ----------------------------------------------------------------------------
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
   Threading / unthreading pointers.

   The basic idea here is to chain together all the fields pointing at
   a particular object, with the root of the chain in the object's
   info table field.  The original contents of the info pointer goes
   at the end of the chain.

   Adding a new field to the chain is a matter of swapping the
   contents of the field with the contents of the object's info table
   field.

   To unthread the chain, we walk down it updating all the fields on
   the chain with the new location of the object.  We stop when we
   reach the info pointer at the end.

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   The main difficulty here is that we need to be able to identify the
   info pointer at the end of the chain.  We can't use the low bits of
   the pointer for this; they are already being used for
   pointer-tagging.  What's more, we need to retain the
   pointer-tagging tag bits on each pointer during the
   threading/unthreading process.

   Our solution is as follows: 
     - an info pointer (chain length zero) is identified by having tag 0
     - in a threaded chain of length > 0:
        - the pointer-tagging tag bits are attached to the info pointer
        - the first entry in the chain has tag 1
        - second and subsequent entries in the chain have tag 2

   This exploits the fact that the tag on each pointer to a given
   closure is normally the same (if they are not the same, then
   presumably the tag is not essential and it therefore doesn't matter
   if we throw away some of the tags).
   ------------------------------------------------------------------------- */
71

sof's avatar
sof committed
72
STATIC_INLINE void
73
thread (StgClosure **p)
74
{
75 76 77
    StgClosure *q0;
    StgPtr q;
    StgWord iptr;
78 79
    bdescr *bd;

80 81 82
    q0  = *p;
    q   = (StgPtr)UNTAG_CLOSURE(q0);

83 84 85
    // It doesn't look like a closure at the moment, because the info
    // ptr is possibly threaded:
    // ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
Simon Marlow's avatar
Simon Marlow committed
86
    
87
    if (HEAP_ALLOCED(q)) {
88
	bd = Bdescr(q); 
89

90
	if (bd->flags & BF_MARKED)
Simon Marlow's avatar
Simon Marlow committed
91
        {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
            iptr = *q;
            switch (GET_CLOSURE_TAG((StgClosure *)iptr))
            {
            case 0: 
                // this is the info pointer; we are creating a new chain.
                // save the original tag at the end of the chain.
                *p = (StgClosure *)((StgWord)iptr + GET_CLOSURE_TAG(q0));
                *q = (StgWord)p + 1;
                break;
            case 1:
            case 2:
                // this is a chain of length 1 or more
                *p = (StgClosure *)iptr;
                *q = (StgWord)p + 2;
                break;
            }
        }
109 110 111
    }
}

112 113 114 115 116 117
static void
thread_root (void *user STG_UNUSED, StgClosure **p)
{
    thread(p);
}

118 119 120 121
// This version of thread() takes a (void *), used to circumvent
// warnings from gcc about pointer punning and strict aliasing.
STATIC_INLINE void thread_ (void *p) { thread((StgClosure **)p); }

sof's avatar
sof committed
122
STATIC_INLINE void
123
unthread( StgPtr p, StgWord free )
124
{
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    StgWord q, r;
    StgPtr q0;

    q = *p;
loop:
    switch (GET_CLOSURE_TAG((StgClosure *)q))
    {
    case 0:
        // nothing to do; the chain is length zero
        return;
    case 1:
        q0 = (StgPtr)(q-1);
        r = *q0;  // r is the info ptr, tagged with the pointer-tag
        *q0 = free;
        *p = (StgWord)UNTAG_CLOSURE((StgClosure *)r);
        return;
    case 2:
        q0 = (StgPtr)(q-2);
        r = *q0;
        *q0 = free;
        q = r;
        goto loop;
    default:
        barf("unthread");
149 150 151
    }
}

152 153 154 155 156
// Traverse a threaded chain and pull out the info pointer at the end.
// The info pointer is also tagged with the appropriate pointer tag
// for this closure, which should be attached to the pointer
// subsequently passed to unthread().
STATIC_INLINE StgWord
157 158
get_threaded_info( StgPtr p )
{
159 160 161
    StgWord q;
    
    q = (W_)GET_INFO(UNTAG_CLOSURE((StgClosure *)p));
162

163 164 165 166 167 168 169 170 171
loop:
    switch (GET_CLOSURE_TAG((StgClosure *)q)) 
    {
    case 0:
        ASSERT(LOOKS_LIKE_INFO_PTR(q));
        return q;
    case 1:
    {
        StgWord r = *(StgPtr)(q-1);
Simon Marlow's avatar
Simon Marlow committed
172
        ASSERT(LOOKS_LIKE_INFO_PTR((StgWord)UNTAG_CLOSURE((StgClosure *)r)));
173 174 175 176 177 178 179
        return r;
    }
    case 2:
        q = *(StgPtr)(q-2);
        goto loop;
    default:
        barf("get_threaded_info");
180 181 182 183 184
    }
}

// A word-aligned memmove will be faster for small objects than libc's or gcc's.
// Remember, the two regions *might* overlap, but: to <= from.
sof's avatar
sof committed
185
STATIC_INLINE void
186
move(StgPtr to, StgPtr from, StgWord size)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
{
    for(; size > 0; --size) {
	*to++ = *from++;
    }
}

static void
thread_static( StgClosure* p )
{
  const StgInfoTable *info;

  // keep going until we've threaded all the objects on the linked
  // list... 
  while (p != END_OF_STATIC_LIST) {

    info = get_itbl(p);
    switch (info->type) {
      
    case IND_STATIC:
206
	thread(&((StgInd *)p)->indirectee);
207
	p = *IND_STATIC_LINK(p);
208
	continue;
209 210
      
    case THUNK_STATIC:
211
	p = *THUNK_STATIC_LINK(p);
212
	continue;
213
    case FUN_STATIC:
214
	p = *FUN_STATIC_LINK(p);
215
	continue;
216
    case CONSTR_STATIC:
217
	p = *STATIC_LINK(info,p);
218
	continue;
219 220 221 222 223 224 225 226
      
    default:
	barf("thread_static: strange closure %d", (int)(info->type));
    }

  }
}

sof's avatar
sof committed
227
STATIC_INLINE void
228
thread_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, StgWord size )
229
{
230
    W_ i, b;
231 232 233 234 235 236
    StgWord bitmap;

    b = 0;
    bitmap = large_bitmap->bitmap[b];
    for (i = 0; i < size; ) {
	if ((bitmap & 1) == 0) {
237
	    thread((StgClosure **)p);
238 239 240 241 242 243 244 245 246 247 248 249
	}
	i++;
	p++;
	if (i % BITS_IN(W_) == 0) {
	    b++;
	    bitmap = large_bitmap->bitmap[b];
	} else {
	    bitmap = bitmap >> 1;
	}
    }
}

sof's avatar
sof committed
250
STATIC_INLINE StgPtr
251 252 253 254
thread_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
{
    StgPtr p;
    StgWord bitmap;
255
    StgWord size;
256 257

    p = (StgPtr)args;
258
    switch (fun_info->f.fun_type) {
259
    case ARG_GEN:
260 261
	bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
	size = BITMAP_SIZE(fun_info->f.b.bitmap);
262 263
	goto small_bitmap;
    case ARG_GEN_BIG:
264 265
	size = GET_FUN_LARGE_BITMAP(fun_info)->size;
	thread_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
266 267 268
	p += size;
	break;
    default:
269 270
	bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
	size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
271 272 273
    small_bitmap:
	while (size > 0) {
	    if ((bitmap & 1) == 0) {
274
		thread((StgClosure **)p);
275 276 277 278 279 280 281 282 283 284
	    }
	    p++;
	    bitmap = bitmap >> 1;
	    size--;
	}
	break;
    }
    return p;
}

285 286 287
static void
thread_stack(StgPtr p, StgPtr stack_end)
{
288
    const StgRetInfoTable* info;
ken's avatar
ken committed
289
    StgWord bitmap;
290
    StgWord size;
291 292 293 294 295
    
    // highly similar to scavenge_stack, but we do pointer threading here.
    
    while (p < stack_end) {

296
	// *p must be the info pointer of an activation
297 298 299
	// record.  All activation records have 'bitmap' style layout
	// info.
	//
300
	info  = get_ret_itbl((StgClosure *)p);
301
	
302
	switch (info->i.type) {
303
	    
304
            // small bitmap (<= 32 entries, or 64 on a 64-bit machine)
305 306 307
        case CATCH_RETRY_FRAME:
        case CATCH_STM_FRAME:
        case ATOMICALLY_FRAME:
308
	case UPDATE_FRAME:
309 310 311
        case UNDERFLOW_FRAME:
        case STOP_FRAME:
        case CATCH_FRAME:
312
	case RET_SMALL:
313 314
	    bitmap = BITMAP_BITS(info->i.layout.bitmap);
	    size   = BITMAP_SIZE(info->i.layout.bitmap);
315
	    p++;
316 317 318
	    // NOTE: the payload starts immediately after the info-ptr, we
	    // don't have an StgHeader in the same sense as a heap closure.
	    while (size > 0) {
319
		if ((bitmap & 1) == 0) {
320
		    thread((StgClosure **)p);
321 322 323
		}
		p++;
		bitmap = bitmap >> 1;
324
		size--;
325 326 327
	    }
	    continue;

328 329 330 331 332
	case RET_BCO: {
	    StgBCO *bco;
	    
	    p++;
	    bco = (StgBCO *)*p;
333
	    thread((StgClosure **)p);
334 335 336 337 338 339 340
	    p++;
	    size = BCO_BITMAP_SIZE(bco);
	    thread_large_bitmap(p, BCO_BITMAP(bco), size);
	    p += size;
	    continue;
	}

ken's avatar
ken committed
341
	    // large bitmap (> 32 entries, or 64 on a 64-bit machine) 
342 343
	case RET_BIG:
	    p++;
344 345
	    size = GET_LARGE_BITMAP(&info->i)->size;
	    thread_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
346 347
	    p += size;
	    continue;
348

349 350 351 352 353
	case RET_FUN:
	{
	    StgRetFun *ret_fun = (StgRetFun *)p;
	    StgFunInfoTable *fun_info;
	    
354
	    fun_info = FUN_INFO_PTR_TO_STRUCT((StgInfoTable *)UNTAG_CLOSURE((StgClosure *)
355
                           get_threaded_info((StgPtr)ret_fun->fun)));
356
	         // *before* threading it!
357
	    thread(&ret_fun->fun);
358
	    p = thread_arg_block(fun_info, ret_fun->payload);
359 360 361 362 363
	    continue;
	}

	default:
	    barf("thread_stack: weird activation record found on stack: %d", 
364
		 (int)(info->i.type));
365 366 367 368
	}
    }
}

sof's avatar
sof committed
369
STATIC_INLINE StgPtr
370
thread_PAP_payload (StgClosure *fun, StgClosure **payload, StgWord size)
371 372
{
    StgPtr p;
373
    StgWord bitmap;
374
    StgFunInfoTable *fun_info;
375

376
    fun_info = FUN_INFO_PTR_TO_STRUCT((StgInfoTable *)UNTAG_CLOSURE((StgClosure *)
377
                        get_threaded_info((StgPtr)fun)));
378 379
    ASSERT(fun_info->i.type != PAP);

380
    p = (StgPtr)payload;
381

382
    switch (fun_info->f.fun_type) {
383
    case ARG_GEN:
384
	bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
385 386
	goto small_bitmap;
    case ARG_GEN_BIG:
387
	thread_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
388 389 390
	p += size;
	break;
    case ARG_BCO:
391
	thread_large_bitmap((StgPtr)payload, BCO_BITMAP(fun), size);
392 393 394
	p += size;
	break;
    default:
395
	bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
396 397 398
    small_bitmap:
	while (size > 0) {
	    if ((bitmap & 1) == 0) {
399
		thread((StgClosure **)p);
400 401 402 403 404 405 406
	    }
	    p++;
	    bitmap = bitmap >> 1;
	    size--;
	}
	break;
    }
407

408 409 410 411 412 413 414 415
    return p;
}

STATIC_INLINE StgPtr
thread_PAP (StgPAP *pap)
{
    StgPtr p;
    p = thread_PAP_payload(pap->fun, pap->payload, pap->n_args);
416
    thread(&pap->fun);
417 418
    return p;
}
419 420 421 422 423 424
    
STATIC_INLINE StgPtr
thread_AP (StgAP *ap)
{
    StgPtr p;
    p = thread_PAP_payload(ap->fun, ap->payload, ap->n_args);
425
    thread(&ap->fun);
426 427
    return p;
}    
428

sof's avatar
sof committed
429
STATIC_INLINE StgPtr
430 431
thread_AP_STACK (StgAP_STACK *ap)
{
432
    thread(&ap->fun);
433 434 435 436 437 438 439
    thread_stack((P_)ap->payload, (P_)ap->payload + ap->size);
    return (P_)ap + sizeofW(StgAP_STACK) + ap->size;
}

static StgPtr
thread_TSO (StgTSO *tso)
{
440
    thread_(&tso->_link);
441
    thread_(&tso->global_link);
442 443

    if (   tso->why_blocked == BlockedOnMVar
444
        || tso->why_blocked == BlockedOnMVarRead
445
	|| tso->why_blocked == BlockedOnBlackHole
446
	|| tso->why_blocked == BlockedOnMsgThrowTo
447 448
        || tso->why_blocked == NotBlocked
        ) {
449
	thread_(&tso->block_info.closure);
450
    }
451
    thread_(&tso->blocked_exceptions);
452
    thread_(&tso->bq);
453
    
454
    thread_(&tso->trec);
455

456 457
    thread_(&tso->stackobj);
    return (StgPtr)tso + sizeofW(StgTSO);
458 459 460
}


461 462 463 464 465 466 467 468
static void
update_fwd_large( bdescr *bd )
{
  StgPtr p;
  const StgInfoTable* info;

  for (; bd != NULL; bd = bd->link) {

Simon Marlow's avatar
Simon Marlow committed
469 470 471 472
    // nothing to do in a pinned block; it might not even have an object
    // at the beginning.
    if (bd->flags & BF_PINNED) continue;

473 474 475 476 477 478 479 480 481
    p = bd->start;
    info  = get_itbl((StgClosure *)p);

    switch (info->type) {

    case ARR_WORDS:
      // nothing to follow 
      continue;

482 483
    case MUT_ARR_PTRS_CLEAN:
    case MUT_ARR_PTRS_DIRTY:
484
    case MUT_ARR_PTRS_FROZEN:
485
    case MUT_ARR_PTRS_FROZEN0:
486 487
      // follow everything 
      {
488
          StgMutArrPtrs *a;
489

490 491 492 493 494
          a = (StgMutArrPtrs*)p;
          for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) {
              thread((StgClosure **)p);
          }
          continue;
495 496
      }

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
    case SMALL_MUT_ARR_PTRS_CLEAN:
    case SMALL_MUT_ARR_PTRS_DIRTY:
    case SMALL_MUT_ARR_PTRS_FROZEN:
    case SMALL_MUT_ARR_PTRS_FROZEN0:
      // follow everything 
      {
          StgSmallMutArrPtrs *a;

          a = (StgSmallMutArrPtrs*)p;
          for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) {
              thread((StgClosure **)p);
          }
          continue;
      }

512 513 514 515 516 517
    case STACK:
    {
        StgStack *stack = (StgStack*)p;
        thread_stack(stack->sp, stack->stack + stack->stack_size);
        continue;
    }
518 519 520

    case AP_STACK:
	thread_AP_STACK((StgAP_STACK *)p);
521 522 523
	continue;

    case PAP:
524
	thread_PAP((StgPAP *)p);
525 526
	continue;

527 528 529 530 531
    case TREC_CHUNK:
    {
        StgWord i;
        StgTRecChunk *tc = (StgTRecChunk *)p;
	TRecEntry *e = &(tc -> entries[0]);
532
	thread_(&tc->prev_chunk);
533
	for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
534 535 536
	  thread_(&e->tvar);
	  thread(&e->expected_value);
	  thread(&e->new_value);
537 538 539 540
	}
	continue;
    }

541 542 543 544 545 546
    default:
      barf("update_fwd_large: unknown/strange object  %d", (int)(info->type));
    }
  }
}

547 548
// ToDo: too big to inline
static /* STATIC_INLINE */ StgPtr
549 550 551
thread_obj (StgInfoTable *info, StgPtr p)
{
    switch (info->type) {
552 553 554
    case THUNK_0_1:
	return p + sizeofW(StgThunk) + 1;

555 556 557 558 559 560
    case FUN_0_1:
    case CONSTR_0_1:
	return p + sizeofW(StgHeader) + 1;
	
    case FUN_1_0:
    case CONSTR_1_0:
561
	thread(&((StgClosure *)p)->payload[0]);
562 563 564
	return p + sizeofW(StgHeader) + 1;
	
    case THUNK_1_0:
565
	thread(&((StgThunk *)p)->payload[0]);
566
	return p + sizeofW(StgThunk) + 1;
567 568
	
    case THUNK_0_2:
569 570
	return p + sizeofW(StgThunk) + 2;

571 572 573 574 575
    case FUN_0_2:
    case CONSTR_0_2:
	return p + sizeofW(StgHeader) + 2;
	
    case THUNK_1_1:
576
	thread(&((StgThunk *)p)->payload[0]);
577 578
	return p + sizeofW(StgThunk) + 2;

579 580
    case FUN_1_1:
    case CONSTR_1_1:
581
	thread(&((StgClosure *)p)->payload[0]);
582 583 584
	return p + sizeofW(StgHeader) + 2;
	
    case THUNK_2_0:
585 586
	thread(&((StgThunk *)p)->payload[0]);
	thread(&((StgThunk *)p)->payload[1]);
587 588
	return p + sizeofW(StgThunk) + 2;

589 590
    case FUN_2_0:
    case CONSTR_2_0:
591 592
	thread(&((StgClosure *)p)->payload[0]);
	thread(&((StgClosure *)p)->payload[1]);
593 594
	return p + sizeofW(StgHeader) + 2;
	
595 596
    case BCO: {
	StgBCO *bco = (StgBCO *)p;
597 598 599
	thread_(&bco->instrs);
	thread_(&bco->literals);
	thread_(&bco->ptrs);
600 601 602
	return p + bco_sizeW(bco);
    }

603
    case THUNK:
604 605 606 607 608 609
    {
	StgPtr end;
	
	end = (P_)((StgThunk *)p)->payload + 
	    info->layout.payload.ptrs;
	for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
610
	    thread((StgClosure **)p);
611 612 613 614 615
	}
	return p + info->layout.payload.nptrs;
    }

    case FUN:
616
    case CONSTR:
617 618
    case PRIM:
    case MUT_PRIM:
619 620
    case MUT_VAR_CLEAN:
    case MUT_VAR_DIRTY:
621
    case TVAR:
622
    case BLACKHOLE:
623
    case BLOCKING_QUEUE:
624 625 626 627 628 629
    {
	StgPtr end;
	
	end = (P_)((StgClosure *)p)->payload + 
	    info->layout.payload.ptrs;
	for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
630
	    thread((StgClosure **)p);
631 632 633 634 635 636 637
	}
	return p + info->layout.payload.nptrs;
    }
    
    case WEAK:
    {
	StgWeak *w = (StgWeak *)p;
638
	thread(&w->cfinalizers);
639 640 641
	thread(&w->key);
	thread(&w->value);
	thread(&w->finalizer);
642
	if (w->link != NULL) {
643
	    thread_(&w->link);
644 645 646 647
	}
	return p + sizeofW(StgWeak);
    }
    
648 649
    case MVAR_CLEAN:
    case MVAR_DIRTY:
650 651
    { 
	StgMVar *mvar = (StgMVar *)p;
652 653 654
	thread_(&mvar->head);
	thread_(&mvar->tail);
	thread(&mvar->value);
655 656 657
	return p + sizeofW(StgMVar);
    }
    
658 659
    case IND:
    case IND_PERM:
660
	thread(&((StgInd *)p)->indirectee);
661
	return p + sizeofW(StgInd);
662 663 664 665

    case THUNK_SELECTOR:
    { 
	StgSelector *s = (StgSelector *)p;
666
	thread(&s->selectee);
667 668 669 670 671 672 673 674
	return p + THUNK_SELECTOR_sizeW();
    }
    
    case AP_STACK:
	return thread_AP_STACK((StgAP_STACK *)p);
	
    case PAP:
	return thread_PAP((StgPAP *)p);
675 676 677

    case AP:
	return thread_AP((StgAP *)p);
678 679 680 681
	
    case ARR_WORDS:
	return p + arr_words_sizeW((StgArrWords *)p);
	
682 683
    case MUT_ARR_PTRS_CLEAN:
    case MUT_ARR_PTRS_DIRTY:
684
    case MUT_ARR_PTRS_FROZEN:
685
    case MUT_ARR_PTRS_FROZEN0:
686 687
	// follow everything 
    {
688 689 690 691
        StgMutArrPtrs *a;

        a = (StgMutArrPtrs *)p;
	for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) {
692
	    thread((StgClosure **)p);
693
	}
694 695

	return (StgPtr)a + mut_arr_ptrs_sizeW(a);
696
    }
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

    case SMALL_MUT_ARR_PTRS_CLEAN:
    case SMALL_MUT_ARR_PTRS_DIRTY:
    case SMALL_MUT_ARR_PTRS_FROZEN:
    case SMALL_MUT_ARR_PTRS_FROZEN0:
	// follow everything 
    {
        StgSmallMutArrPtrs *a;

        a = (StgSmallMutArrPtrs *)p;
	for (p = (P_)a->payload; p < (P_)&a->payload[a->ptrs]; p++) {
	    thread((StgClosure **)p);
	}

	return (StgPtr)a + small_mut_arr_ptrs_sizeW(a);
    }
713 714 715 716
    
    case TSO:
	return thread_TSO((StgTSO *)p);
    
717 718 719 720 721 722 723
    case STACK:
    {
        StgStack *stack = (StgStack*)p;
        thread_stack(stack->sp, stack->stack + stack->stack_size);
        return p + stack_sizeW(stack);
    }

724 725
    case TREC_CHUNK:
    {
726
        StgWord i;
727 728
        StgTRecChunk *tc = (StgTRecChunk *)p;
	TRecEntry *e = &(tc -> entries[0]);
729
	thread_(&tc->prev_chunk);
730
	for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
731 732 733
	  thread_(&e->tvar);
	  thread(&e->expected_value);
	  thread(&e->new_value);
734 735 736 737
	}
	return p + sizeofW(StgTRecChunk);
    }

738 739
    default:
	barf("update_fwd: unknown/strange object  %d", (int)(info->type));
sof's avatar
sof committed
740
	return NULL;
741 742 743
    }
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
static void
update_fwd( bdescr *blocks )
{
    StgPtr p;
    bdescr *bd;
    StgInfoTable *info;

    bd = blocks;

    // cycle through all the blocks in the step
    for (; bd != NULL; bd = bd->link) {
	p = bd->start;

	// linearly scan the objects in this block
	while (p < bd->free) {
759
	    ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
760
	    info = get_itbl((StgClosure *)p);
761
	    p = thread_obj(info, p);
762 763 764 765 766 767 768 769
	}
    }
} 

static void
update_fwd_compact( bdescr *blocks )
{
    StgPtr p, q, free;
770
#if 0
771
    StgWord m;
772
#endif
773 774
    bdescr *bd, *free_bd;
    StgInfoTable *info;
775
    StgWord size;
776
    StgWord iptr;
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

    bd = blocks;
    free_bd = blocks;
    free = free_bd->start;

    // cycle through all the blocks in the step
    for (; bd != NULL; bd = bd->link) {
	p = bd->start;

	while (p < bd->free ) {

	    while ( p < bd->free && !is_marked(p,bd) ) {
		p++;
	    }
	    if (p >= bd->free) {
		break;
	    }

#if 0
    next:
	m = * ((StgPtr)bd->u.bitmap + ((p - bd->start) / (BITS_IN(StgWord))));
	m >>= ((p - bd->start) & (BITS_IN(StgWord) - 1));

	while ( p < bd->free ) {

	    if ((m & 1) == 0) {
		m >>= 1;
		p++;
		if (((StgWord)p & (sizeof(W_) * BITS_IN(StgWord))) == 0) {
		    goto next;
		} else {
		    continue;
		}
	    }
#endif

	    // Problem: we need to know the destination for this cell
	    // in order to unthread its info pointer.  But we can't
	    // know the destination without the size, because we may
	    // spill into the next block.  So we have to run down the 
	    // threaded list and get the info ptr first.
818 819 820 821
            //
            // ToDo: one possible avenue of attack is to use the fact
            // that if (p&BLOCK_MASK) >= (free&BLOCK_MASK), then we
            // definitely have enough room.  Also see bug #1147.
822
            iptr = get_threaded_info(p);
823
	    info = INFO_PTR_TO_STRUCT((StgInfoTable *)UNTAG_CLOSURE((StgClosure *)iptr));
824 825 826

	    q = p;

827
	    p = thread_obj(info, p);
828 829 830

	    size = p - q;
	    if (free + size > free_bd->start + BLOCK_SIZE_W) {
831
		// set the next bit in the bitmap to indicate that
832 833 834
		// this object needs to be pushed into the next
		// block.  This saves us having to run down the
		// threaded info pointer list twice during the next pass.
835
		mark(q+1,bd);
836 837
		free_bd = free_bd->link;
		free = free_bd->start;
838
	    } else {
839
		ASSERT(!is_marked(q+1,bd));
840 841
	    }

842
	    unthread(q,(StgWord)free + GET_CLOSURE_TAG((StgClosure *)iptr));
843 844 845 846 847 848 849 850
	    free += size;
#if 0
	    goto next;
#endif
	}
    }
}

851
static W_
852
update_bkwd_compact( generation *gen )
853 854
{
    StgPtr p, free;
855
#if 0
856
    StgWord m;
857
#endif
858 859
    bdescr *bd, *free_bd;
    StgInfoTable *info;
860 861
    StgWord size;
    W_ free_blocks;
862
    StgWord iptr;
863

864
    bd = free_bd = gen->old_blocks;
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
    free = free_bd->start;
    free_blocks = 1;

    // cycle through all the blocks in the step
    for (; bd != NULL; bd = bd->link) {
	p = bd->start;

	while (p < bd->free ) {

	    while ( p < bd->free && !is_marked(p,bd) ) {
		p++;
	    }
	    if (p >= bd->free) {
		break;
	    }

#if 0
    next:
	m = * ((StgPtr)bd->u.bitmap + ((p - bd->start) / (BITS_IN(StgWord))));
	m >>= ((p - bd->start) & (BITS_IN(StgWord) - 1));

	while ( p < bd->free ) {

	    if ((m & 1) == 0) {
		m >>= 1;
		p++;
		if (((StgWord)p & (sizeof(W_) * BITS_IN(StgWord))) == 0) {
		    goto next;
		} else {
		    continue;
		}
	    }
#endif

899
	    if (is_marked(p+1,bd)) {
900 901 902 903 904 905 906
		// don't forget to update the free ptr in the block desc.
		free_bd->free = free;
		free_bd = free_bd->link;
		free = free_bd->start;
		free_blocks++;
	    }

907 908
            iptr = get_threaded_info(p);
	    unthread(p, (StgWord)free + GET_CLOSURE_TAG((StgClosure *)iptr));
Simon Marlow's avatar
Simon Marlow committed
909
	    ASSERT(LOOKS_LIKE_INFO_PTR((StgWord)((StgClosure *)p)->header.info));
910
	    info = get_itbl((StgClosure *)p);
911
	    size = closure_sizeW_((StgClosure *)p,info);
912

913 914 915
	    if (free != p) {
		move(free,p,size);
	    }
916 917

	    // relocate TSOs
918 919
            if (info->type == STACK) {
                move_STACK((StgStack *)p, (StgStack *)free);
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
	    }

	    free += size;
	    p += size;
#if 0
	    goto next;
#endif
	}
    }

    // free the remaining blocks and count what's left.
    free_bd->free = free;
    if (free_bd->link != NULL) {
	freeChain(free_bd->link);
	free_bd->link = NULL;
    }

    return free_blocks;
938 939
}

940
void
941
compact(StgClosure *static_objects)
942
{
943
    W_ n, g, blocks;
944
    generation *gen;
945 946

    // 1. thread the roots
947
    markCapabilities((evac_fn)thread_root, NULL);
948

Simon Marlow's avatar
Simon Marlow committed
949 950
    markScheduler((evac_fn)thread_root, NULL);

951
    // the weak pointer lists...
952
    for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
953
        if (generations[g].weak_ptr_list != NULL) {
954
            thread((void *)&generations[g].weak_ptr_list);
955
        }
956
    }
957 958

    if (dead_weak_ptr_list != NULL) {
959
        thread((void *)&dead_weak_ptr_list); // tmp
960 961
    }

962 963
    // mutable lists
    for (g = 1; g < RtsFlags.GcFlags.generations; g++) {
964 965
        bdescr *bd;
        StgPtr p;
966
        for (n = 0; n < n_capabilities; n++) {
967
            for (bd = capabilities[n]->mut_lists[g];
968 969 970 971 972 973
                 bd != NULL; bd = bd->link) {
                for (p = bd->start; p < bd->free; p++) {
                    thread((StgClosure **)p);
                }
            }
        }
974
    }
975 976

    // the global thread list
977 978
    for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
        thread((void *)&generations[g].threads);
979
    }
980

981
    // any threads resurrected during this GC
982
    thread((void *)&resurrected_threads);
983

984
    // the task list
985
    {
986
	Task *task;
987
        InCall *incall;
988
        for (task = all_tasks; task != NULL; task = task->all_next) {
989 990 991 992 993 994
            for (incall = task->incall; incall != NULL; 
                 incall = incall->prev_stack) {
                if (incall->tso) {
                    thread_(&incall->tso);
                }
            }
995 996 997
	}
    }

998
    // the static objects
999
    thread_static(static_objects /* ToDo: ok? */);
1000 1001

    // the stable pointer table
1002
    threadStableTables((evac_fn)thread_root, NULL);
1003

1004
    // the CAF list (used by GHCi)
1005
    markCAFs((evac_fn)thread_root, NULL);
1006

1007 1008
    // 2. update forward ptrs
    for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
1009 1010 1011 1012
        gen = &generations[g];
        debugTrace(DEBUG_gc, "update_fwd:  %d", g);

        update_fwd(gen->blocks);
Simon Marlow's avatar
Simon Marlow committed
1013 1014 1015 1016
        for (n = 0; n < n_capabilities; n++) {
            update_fwd(gc_threads[n]->gens[g].todo_bd);
            update_fwd(gc_threads[n]->gens[g].part_list);
        }
1017 1018 1019 1020
        update_fwd_large(gen->scavenged_large_objects);
        if (g == RtsFlags.GcFlags.generations-1 && gen->old_blocks != NULL) {
            debugTrace(DEBUG_gc, "update_fwd:  %d (compact)", g);
            update_fwd_compact(gen->old_blocks);
1021 1022 1023 1024
	}
    }

    // 3. update backward ptrs
1025 1026 1027
    gen = oldest_gen;
    if (gen->old_blocks != NULL) {
	blocks = update_bkwd_compact(gen);
Simon Marlow's avatar
Simon Marlow committed
1028
	debugTrace(DEBUG_gc, 
1029 1030 1031
		   "update_bkwd: %d (compact, old: %d blocks, now %d blocks)",
		   gen->no, gen->n_old_blocks, blocks);
	gen->n_old_blocks = blocks;
1032 1033
    }
}