GCCompact.c 21.3 KB
Newer Older
1 2 3 4 5 6 7 8
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team 2001
 *
 * Compacting garbage collector
 *
 * ---------------------------------------------------------------------------*/

9
#include "PosixSource.h"
10 11 12
#include "Rts.h"
#include "RtsUtils.h"
#include "RtsFlags.h"
13
#include "OSThreads.h"
14 15 16 17 18
#include "Storage.h"
#include "BlockAlloc.h"
#include "MBlock.h"
#include "GCCompact.h"
#include "Schedule.h"
19
#include "Apply.h"
20

21 22
// Turn off inlining when debugging - it obfuscates things
#ifdef DEBUG
sof's avatar
sof committed
23 24
# undef  STATIC_INLINE
# define STATIC_INLINE static
25 26
#endif

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/* -----------------------------------------------------------------------------
   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.

43 44 45 46
   We use a trick to identify the info pointer: when swapping pointers
   for threading, we set the low bit of the original pointer, with the
   result that all the pointers in the chain have their low bits set
   except for the info pointer.
47 48
   -------------------------------------------------------------------------- */

sof's avatar
sof committed
49
STATIC_INLINE void
50 51 52
thread( StgPtr p )
{
    StgPtr q = (StgPtr)*p;
53 54
    bdescr *bd;

55 56 57 58
    // It doesn't look like a closure at the moment, because the info
    // ptr is possibly threaded:
    // ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));

59
    if (HEAP_ALLOCED(q)) {
60 61 62 63 64 65
	bd = Bdescr(q); 
	// a handy way to discover whether the ptr is into the
	// compacted area of the old gen, is that the EVACUATED flag
	// is zero (it's non-zero for all the other areas of live
	// memory).
	if ((bd->flags & BF_EVACUATED) == 0) {
66 67 68
	    *p = (StgWord)*q;
	    *q = (StgWord)p + 1;	// set the low bit
	}
69 70 71
    }
}

sof's avatar
sof committed
72
STATIC_INLINE void
73 74
unthread( StgPtr p, StgPtr free )
{
75
    StgWord q = *p, r;
76
    
77 78 79 80
    while ((q & 1) != 0) {
	q -= 1;	// unset the low bit again
	r = *((StgPtr)q);
	*((StgPtr)q) = (StgWord)free;
81 82
	q = r;
    }
83
    *p = q;
84 85
}

sof's avatar
sof committed
86
STATIC_INLINE StgInfoTable *
87 88 89 90
get_threaded_info( StgPtr p )
{
    StgPtr q = (P_)GET_INFO((StgClosure *)p);

91 92
    while (((StgWord)q & 1) != 0) {
	q = (P_)*((StgPtr)((StgWord)q-1));
93
    }
94 95

    ASSERT(LOOKS_LIKE_INFO_PTR(q));
96 97 98 99 100
    return INFO_PTR_TO_STRUCT((StgInfoTable *)q);
}

// 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
101
STATIC_INLINE void
102 103 104 105 106 107 108
move(StgPtr to, StgPtr from, nat size)
{
    for(; size > 0; --size) {
	*to++ = *from++;
    }
}

sof's avatar
sof committed
109
STATIC_INLINE nat
110 111 112
obj_sizeW( StgClosure *p, StgInfoTable *info )
{
    switch (info->type) {
113 114 115
    case THUNK_0_1:
    case THUNK_1_0:
	return sizeofW(StgThunk) + 1;
116 117 118 119
    case FUN_0_1:
    case CONSTR_0_1:
    case FUN_1_0:
    case CONSTR_1_0:
120
	return sizeofW(StgHeader) + 1;
121
    case THUNK_0_2:
122 123 124
    case THUNK_1_1:
    case THUNK_2_0:
	return sizeofW(StgThunk) + 2;
125 126 127 128 129 130
    case FUN_0_2:
    case CONSTR_0_2:
    case FUN_1_1:
    case CONSTR_1_1:
    case FUN_2_0:
    case CONSTR_2_0:
131
	return sizeofW(StgHeader) + 2;
132 133
    case THUNK_SELECTOR:
	return THUNK_SELECTOR_sizeW();
134 135 136
    case AP_STACK:
	return ap_stack_sizeW((StgAP_STACK *)p);
    case AP:
137 138 139 140 141 142
    case PAP:
	return pap_sizeW((StgPAP *)p);
    case ARR_WORDS:
	return arr_words_sizeW((StgArrWords *)p);
    case MUT_ARR_PTRS:
    case MUT_ARR_PTRS_FROZEN:
143
    case MUT_ARR_PTRS_FROZEN0:
144 145 146
	return mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
    case TSO:
	return tso_sizeW((StgTSO *)p);
147 148
    case BCO:
	return bco_sizeW((StgBCO *)p);
149 150 151 152 153 154 155 156
    case TVAR_WAIT_QUEUE:
        return sizeofW(StgTVarWaitQueue);
    case TVAR:
        return sizeofW(StgTVar);
    case TREC_CHUNK:
        return sizeofW(StgTRecChunk);
    case TREC_HEADER:
        return sizeofW(StgTRecHeader);
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    default:
	return sizeW_fromITBL(info);
    }
}

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:
	thread((StgPtr)&((StgInd *)p)->indirectee);
176
	p = *IND_STATIC_LINK(p);
177
	continue;
178 179
      
    case THUNK_STATIC:
180
	p = *THUNK_STATIC_LINK(p);
181
	continue;
182
    case FUN_STATIC:
183
	p = *FUN_STATIC_LINK(p);
184
	continue;
185
    case CONSTR_STATIC:
186
	p = *STATIC_LINK(info,p);
187
	continue;
188 189 190 191 192 193 194 195
      
    default:
	barf("thread_static: strange closure %d", (int)(info->type));
    }

  }
}

sof's avatar
sof committed
196
STATIC_INLINE void
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
thread_large_bitmap( StgPtr p, StgLargeBitmap *large_bitmap, nat size )
{
    nat i, b;
    StgWord bitmap;

    b = 0;
    bitmap = large_bitmap->bitmap[b];
    for (i = 0; i < size; ) {
	if ((bitmap & 1) == 0) {
	    thread(p);
	}
	i++;
	p++;
	if (i % BITS_IN(W_) == 0) {
	    b++;
	    bitmap = large_bitmap->bitmap[b];
	} else {
	    bitmap = bitmap >> 1;
	}
    }
}

sof's avatar
sof committed
219
STATIC_INLINE StgPtr
220 221 222 223 224 225 226
thread_arg_block (StgFunInfoTable *fun_info, StgClosure **args)
{
    StgPtr p;
    StgWord bitmap;
    nat size;

    p = (StgPtr)args;
227
    switch (fun_info->f.fun_type) {
228
    case ARG_GEN:
229 230
	bitmap = BITMAP_BITS(fun_info->f.b.bitmap);
	size = BITMAP_SIZE(fun_info->f.b.bitmap);
231 232
	goto small_bitmap;
    case ARG_GEN_BIG:
233 234
	size = GET_FUN_LARGE_BITMAP(fun_info)->size;
	thread_large_bitmap(p, GET_FUN_LARGE_BITMAP(fun_info), size);
235 236 237
	p += size;
	break;
    default:
238 239
	bitmap = BITMAP_BITS(stg_arg_bitmaps[fun_info->f.fun_type]);
	size = BITMAP_SIZE(stg_arg_bitmaps[fun_info->f.fun_type]);
240 241 242 243 244 245 246 247 248 249 250 251 252 253
    small_bitmap:
	while (size > 0) {
	    if ((bitmap & 1) == 0) {
		thread(p);
	    }
	    p++;
	    bitmap = bitmap >> 1;
	    size--;
	}
	break;
    }
    return p;
}

254 255 256
static void
thread_stack(StgPtr p, StgPtr stack_end)
{
257
    const StgRetInfoTable* info;
ken's avatar
ken committed
258
    StgWord bitmap;
259
    nat size;
260 261 262 263 264
    
    // highly similar to scavenge_stack, but we do pointer threading here.
    
    while (p < stack_end) {

265
	// *p must be the info pointer of an activation
266 267 268
	// record.  All activation records have 'bitmap' style layout
	// info.
	//
269
	info  = get_ret_itbl((StgClosure *)p);
270
	
271
	switch (info->i.type) {
272 273 274
	    
	    // Dynamic bitmap: the mask is stored on the stack 
	case RET_DYN:
275 276 277 278 279
	{
	    StgWord dyn;
	    dyn = ((StgRetDyn *)p)->liveness;

	    // traverse the bitmap first
280
	    bitmap = RET_DYN_LIVENESS(dyn);
281
	    p      = (P_)&((StgRetDyn *)p)->payload[0];
282
	    size   = RET_DYN_BITMAP_SIZE;
283 284 285 286 287 288 289 290
	    while (size > 0) {
		if ((bitmap & 1) == 0) {
		    thread(p);
		}
		p++;
		bitmap = bitmap >> 1;
		size--;
	    }
291
	    
292
	    // skip over the non-ptr words
293
	    p += RET_DYN_NONPTRS(dyn) + RET_DYN_NONPTR_REGS_SIZE;
294 295
	    
	    // follow the ptr words
296
	    for (size = RET_DYN_PTRS(dyn); size > 0; size--) {
297 298 299
		thread(p);
		p++;
	    }
300
	    continue;
301
	}
302
	    
ken's avatar
ken committed
303
	    // small bitmap (<= 32 entries, or 64 on a 64-bit machine) 
304 305 306
        case CATCH_RETRY_FRAME:
        case CATCH_STM_FRAME:
        case ATOMICALLY_FRAME:
307 308 309 310 311
	case UPDATE_FRAME:
	case STOP_FRAME:
	case CATCH_FRAME:
	case RET_SMALL:
	case RET_VEC_SMALL:
312 313
	    bitmap = BITMAP_BITS(info->i.layout.bitmap);
	    size   = BITMAP_SIZE(info->i.layout.bitmap);
314
	    p++;
315 316 317
	    // 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) {
318 319 320 321 322
		if ((bitmap & 1) == 0) {
		    thread(p);
		}
		p++;
		bitmap = bitmap >> 1;
323
		size--;
324 325 326
	    }
	    continue;

327 328 329 330 331 332
	case RET_BCO: {
	    StgBCO *bco;
	    nat size;
	    
	    p++;
	    bco = (StgBCO *)*p;
333
	    thread(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 344
	case RET_BIG:
	case RET_VEC_BIG:
	    p++;
345 346
	    size = GET_LARGE_BITMAP(&info->i)->size;
	    thread_large_bitmap(p, GET_LARGE_BITMAP(&info->i), size);
347 348
	    p += size;
	    continue;
349

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

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

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

    fun_info = itbl_to_fun_itbl(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 399 400 401 402 403 404 405 406
    small_bitmap:
	while (size > 0) {
	    if ((bitmap & 1) == 0) {
		thread(p);
	    }
	    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((StgPtr)&pap->fun);
417 418
    return p;
}
419 420 421 422 423 424 425 426 427
    
STATIC_INLINE StgPtr
thread_AP (StgAP *ap)
{
    StgPtr p;
    p = thread_PAP_payload(ap->fun, ap->payload, ap->n_args);
    thread((StgPtr)&ap->fun);
    return p;
}    
428

sof's avatar
sof committed
429
STATIC_INLINE StgPtr
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
thread_AP_STACK (StgAP_STACK *ap)
{
    thread((StgPtr)&ap->fun);
    thread_stack((P_)ap->payload, (P_)ap->payload + ap->size);
    return (P_)ap + sizeofW(StgAP_STACK) + ap->size;
}

static StgPtr
thread_TSO (StgTSO *tso)
{
    thread((StgPtr)&tso->link);
    thread((StgPtr)&tso->global_link);

    if (   tso->why_blocked == BlockedOnMVar
	|| tso->why_blocked == BlockedOnBlackHole
	|| tso->why_blocked == BlockedOnException
#if defined(PAR)
	|| tso->why_blocked == BlockedOnGA
	|| tso->why_blocked == BlockedOnGA_NoSend
#endif
	) {
	thread((StgPtr)&tso->block_info.closure);
    }
    if ( tso->blocked_exceptions != NULL ) {
	thread((StgPtr)&tso->blocked_exceptions);
    }
    
457 458
    thread((StgPtr)&tso->trec);

459 460 461 462 463
    thread_stack(tso->sp, &(tso->stack[tso->stack_size]));
    return (StgPtr)tso + tso_sizeW(tso);
}


464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
static void
update_fwd_large( bdescr *bd )
{
  StgPtr p;
  const StgInfoTable* info;

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

    p = bd->start;
    info  = get_itbl((StgClosure *)p);

    switch (info->type) {

    case ARR_WORDS:
      // nothing to follow 
      continue;

    case MUT_ARR_PTRS:
    case MUT_ARR_PTRS_FROZEN:
483
    case MUT_ARR_PTRS_FROZEN0:
484 485 486 487 488 489 490 491 492 493 494 495
      // follow everything 
      {
	StgPtr next;

	next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
	for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
	    thread(p);
	}
	continue;
      }

    case TSO:
496 497 498 499 500
	thread_TSO((StgTSO *)p);
	continue;

    case AP_STACK:
	thread_AP_STACK((StgAP_STACK *)p);
501 502 503
	continue;

    case PAP:
504
	thread_PAP((StgPAP *)p);
505 506 507 508 509 510 511 512
	continue;

    default:
      barf("update_fwd_large: unknown/strange object  %d", (int)(info->type));
    }
  }
}

sof's avatar
sof committed
513
STATIC_INLINE StgPtr
514 515 516
thread_obj (StgInfoTable *info, StgPtr p)
{
    switch (info->type) {
517 518 519
    case THUNK_0_1:
	return p + sizeofW(StgThunk) + 1;

520 521 522 523 524 525 526 527 528 529
    case FUN_0_1:
    case CONSTR_0_1:
	return p + sizeofW(StgHeader) + 1;
	
    case FUN_1_0:
    case CONSTR_1_0:
	thread((StgPtr)&((StgClosure *)p)->payload[0]);
	return p + sizeofW(StgHeader) + 1;
	
    case THUNK_1_0:
530 531
	thread((StgPtr)&((StgThunk *)p)->payload[0]);
	return p + sizeofW(StgThunk) + 1;
532 533
	
    case THUNK_0_2:
534 535
	return p + sizeofW(StgThunk) + 2;

536 537 538 539 540
    case FUN_0_2:
    case CONSTR_0_2:
	return p + sizeofW(StgHeader) + 2;
	
    case THUNK_1_1:
541 542 543
	thread((StgPtr)&((StgThunk *)p)->payload[0]);
	return p + sizeofW(StgThunk) + 2;

544 545 546 547 548 549
    case FUN_1_1:
    case CONSTR_1_1:
	thread((StgPtr)&((StgClosure *)p)->payload[0]);
	return p + sizeofW(StgHeader) + 2;
	
    case THUNK_2_0:
550 551 552 553
	thread((StgPtr)&((StgThunk *)p)->payload[0]);
	thread((StgPtr)&((StgThunk *)p)->payload[1]);
	return p + sizeofW(StgThunk) + 2;

554 555 556 557 558 559
    case FUN_2_0:
    case CONSTR_2_0:
	thread((StgPtr)&((StgClosure *)p)->payload[0]);
	thread((StgPtr)&((StgClosure *)p)->payload[1]);
	return p + sizeofW(StgHeader) + 2;
	
560 561 562 563 564 565 566 567 568
    case BCO: {
	StgBCO *bco = (StgBCO *)p;
	thread((StgPtr)&bco->instrs);
	thread((StgPtr)&bco->literals);
	thread((StgPtr)&bco->ptrs);
	thread((StgPtr)&bco->itbls);
	return p + bco_sizeW(bco);
    }

569
    case THUNK:
570 571 572 573 574 575 576 577 578 579 580 581
    {
	StgPtr end;
	
	end = (P_)((StgThunk *)p)->payload + 
	    info->layout.payload.ptrs;
	for (p = (P_)((StgThunk *)p)->payload; p < end; p++) {
	    thread(p);
	}
	return p + info->layout.payload.nptrs;
    }

    case FUN:
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    case CONSTR:
    case FOREIGN:
    case STABLE_NAME:
    case IND_PERM:
    case MUT_VAR:
    case CAF_BLACKHOLE:
    case SE_CAF_BLACKHOLE:
    case SE_BLACKHOLE:
    case BLACKHOLE:
    {
	StgPtr end;
	
	end = (P_)((StgClosure *)p)->payload + 
	    info->layout.payload.ptrs;
	for (p = (P_)((StgClosure *)p)->payload; p < end; p++) {
	    thread(p);
	}
	return p + info->layout.payload.nptrs;
    }
    
    case WEAK:
    {
	StgWeak *w = (StgWeak *)p;
	thread((StgPtr)&w->key);
	thread((StgPtr)&w->value);
	thread((StgPtr)&w->finalizer);
	if (w->link != NULL) {
	    thread((StgPtr)&w->link);
	}
	return p + sizeofW(StgWeak);
    }
    
    case MVAR:
    { 
	StgMVar *mvar = (StgMVar *)p;
	thread((StgPtr)&mvar->head);
	thread((StgPtr)&mvar->tail);
	thread((StgPtr)&mvar->value);
	return p + sizeofW(StgMVar);
    }
    
    case IND_OLDGEN:
    case IND_OLDGEN_PERM:
625 626
	thread((StgPtr)&((StgInd *)p)->indirectee);
	return p + sizeofW(StgInd);
627 628 629 630 631 632 633 634 635 636 637 638 639

    case THUNK_SELECTOR:
    { 
	StgSelector *s = (StgSelector *)p;
	thread((StgPtr)&s->selectee);
	return p + THUNK_SELECTOR_sizeW();
    }
    
    case AP_STACK:
	return thread_AP_STACK((StgAP_STACK *)p);
	
    case PAP:
	return thread_PAP((StgPAP *)p);
640 641 642

    case AP:
	return thread_AP((StgAP *)p);
643 644 645 646 647 648
	
    case ARR_WORDS:
	return p + arr_words_sizeW((StgArrWords *)p);
	
    case MUT_ARR_PTRS:
    case MUT_ARR_PTRS_FROZEN:
649
    case MUT_ARR_PTRS_FROZEN0:
650 651 652 653 654 655 656 657 658 659 660 661 662 663
	// follow everything 
    {
	StgPtr next;
	
	next = p + mut_arr_ptrs_sizeW((StgMutArrPtrs*)p);
	for (p = (P_)((StgMutArrPtrs *)p)->payload; p < next; p++) {
	    thread(p);
	}
	return p;
    }
    
    case TSO:
	return thread_TSO((StgTSO *)p);
    
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    case TVAR_WAIT_QUEUE:
    {
        StgTVarWaitQueue *wq = (StgTVarWaitQueue *)p;
	thread((StgPtr)&wq->waiting_tso);
	thread((StgPtr)&wq->next_queue_entry);
	thread((StgPtr)&wq->prev_queue_entry);
	return p + sizeofW(StgTVarWaitQueue);
    }
    
    case TVAR:
    {
        StgTVar *tvar = (StgTVar *)p;
	thread((StgPtr)&tvar->current_value);
	thread((StgPtr)&tvar->first_wait_queue_entry);
	return p + sizeofW(StgTVar);
    }
    
    case TREC_HEADER:
    {
        StgTRecHeader *trec = (StgTRecHeader *)p;
	thread((StgPtr)&trec->enclosing_trec);
	thread((StgPtr)&trec->current_chunk);
	return p + sizeofW(StgTRecHeader);
    }

    case TREC_CHUNK:
    {
691
        StgWord i;
692 693 694 695 696 697 698 699 700 701 702
        StgTRecChunk *tc = (StgTRecChunk *)p;
	TRecEntry *e = &(tc -> entries[0]);
	thread((StgPtr)&tc->prev_chunk);
	for (i = 0; i < tc -> next_entry_idx; i ++, e++ ) {
	  thread((StgPtr)&e->tvar);
	  thread((StgPtr)&e->expected_value);
	  thread((StgPtr)&e->new_value);
	}
	return p + sizeofW(StgTRecChunk);
    }

703 704
    default:
	barf("update_fwd: unknown/strange object  %d", (int)(info->type));
sof's avatar
sof committed
705
	return NULL;
706 707 708
    }
}

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
static void
update_fwd( bdescr *blocks )
{
    StgPtr p;
    bdescr *bd;
    StgInfoTable *info;

    bd = blocks;

#if defined(PAR)
    barf("update_fwd: ToDo");
#endif

    // 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) {
728
	    ASSERT(LOOKS_LIKE_CLOSURE_PTR(p));
729
	    info = get_itbl((StgClosure *)p);
730
	    p = thread_obj(info, p);
731 732 733 734 735 736 737 738
	}
    }
} 

static void
update_fwd_compact( bdescr *blocks )
{
    StgPtr p, q, free;
739
#if 0
740
    StgWord m;
741
#endif
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    bdescr *bd, *free_bd;
    StgInfoTable *info;
    nat size;

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

#if defined(PAR)
    barf("update_fwd: ToDo");
#endif

    // 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.
	    info = get_threaded_info(p);

	    q = p;

794
	    p = thread_obj(info, p);
795 796 797

	    size = p - q;
	    if (free + size > free_bd->start + BLOCK_SIZE_W) {
798 799 800 801 802
		// unset the next bit in the bitmap to indicate that
		// 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.
		unmark(q+1,bd);
803 804
		free_bd = free_bd->link;
		free = free_bd->start;
805 806
	    } else {
		ASSERT(is_marked(q+1,bd));
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	    }

	    unthread(q,free);
	    free += size;
#if 0
	    goto next;
#endif
	}
    }
}

static nat
update_bkwd_compact( step *stp )
{
    StgPtr p, free;
822
#if 0
823
    StgWord m;
824
#endif
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
    bdescr *bd, *free_bd;
    StgInfoTable *info;
    nat size, free_blocks;

    bd = free_bd = stp->blocks;
    free = free_bd->start;
    free_blocks = 1;

#if defined(PAR)
    barf("update_bkwd: ToDo");
#endif

    // 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

868
	    if (!is_marked(p+1,bd)) {
869 870 871 872 873 874 875 876
		// 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++;
	    }

	    unthread(p,free);
877
	    ASSERT(LOOKS_LIKE_INFO_PTR(((StgClosure *)p)->header.info));
878 879 880
	    info = get_itbl((StgClosure *)p);
	    size = obj_sizeW((StgClosure *)p,info);

881 882 883
	    if (free != p) {
		move(free,p,size);
	    }
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

	    // relocate TSOs
	    if (info->type == TSO) {
		move_TSO((StgTSO *)p, (StgTSO *)free);
	    }

	    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;
    }
    stp->n_blocks = free_blocks;

    return free_blocks;
907 908
}

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
void
compact( void (*get_roots)(evac_fn) )
{
    nat g, s, blocks;
    step *stp;

    // 1. thread the roots
    get_roots((evac_fn)thread);

    // the weak pointer lists...
    if (weak_ptr_list != NULL) {
	thread((StgPtr)&weak_ptr_list);
    }
    if (old_weak_ptr_list != NULL) {
	thread((StgPtr)&old_weak_ptr_list); // tmp
    }

926 927
    // mutable lists
    for (g = 1; g < RtsFlags.GcFlags.generations; g++) {
928 929 930 931 932 933 934
	bdescr *bd;
	StgPtr p;
	for (bd = generations[g].mut_list; bd != NULL; bd = bd->link) {
	    for (p = bd->start; p < bd->free; p++) {
		thread(p);
	    }
	}
935
    }
936 937 938 939

    // the global thread list
    thread((StgPtr)&all_threads);

940 941 942 943 944 945 946 947 948 949 950
    // any threads resurrected during this GC
    thread((StgPtr)&resurrected_threads);

    // the main threads list
    {
	StgMainThread *m;
	for (m = main_threads; m != NULL; m = m->link) {
	    thread((StgPtr)&m->tso);
	}
    }

951 952 953 954 955 956
    // the static objects
    thread_static(scavenged_static_objects);

    // the stable pointer table
    threadStablePtrTable((evac_fn)thread);

957 958 959
    // the CAF list (used by GHCi)
    markCAFs((evac_fn)thread);

960 961 962 963
    // 2. update forward ptrs
    for (g = 0; g < RtsFlags.GcFlags.generations; g++) {
	for (s = 0; s < generations[g].n_steps; s++) {
	    stp = &generations[g].steps[s];
964
	    IF_DEBUG(gc, debugBelch("update_fwd:  %d.%d\n", stp->gen->no, stp->no););
965 966 967 968

	    update_fwd(stp->to_blocks);
	    update_fwd_large(stp->scavenged_large_objects);
	    if (g == RtsFlags.GcFlags.generations-1 && stp->blocks != NULL) {
969
		IF_DEBUG(gc, debugBelch("update_fwd:  %d.%d (compact)\n", stp->gen->no, stp->no););
970 971 972 973 974 975
		update_fwd_compact(stp->blocks);
	    }
	}
    }

    // 3. update backward ptrs
976 977 978
    stp = &oldest_gen->steps[0];
    if (stp->blocks != NULL) {
	blocks = update_bkwd_compact(stp);
979
	IF_DEBUG(gc, debugBelch("update_bkwd: %d.%d (compact, old: %d blocks, now %d blocks)\n", 
980 981 982
			     stp->gen->no, stp->no,
			     stp->n_blocks, blocks););
	stp->n_blocks = blocks;
983 984
    }
}