-
-REGPARM1 void
-evacuate(StgClosure **p)
-{
- bdescr *bd = NULL;
- step *stp;
- StgClosure *q;
- const StgInfoTable *info;
- StgWord tag;
-
- q = *p;
-
-loop:
- /* The tag and the pointer are split, to be merged after evacing */
- tag = GET_CLOSURE_TAG(q);
- q = UNTAG_CLOSURE(q);
-
- ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
-
- if (!HEAP_ALLOCED(q)) {
-
- if (!major_gc) return;
-
- info = get_itbl(q);
- switch (info->type) {
-
- case THUNK_STATIC:
- if (info->srt_bitmap != 0 &&
- *THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
- ACQUIRE_SPIN_LOCK(&static_objects_sync);
- if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
- *THUNK_STATIC_LINK((StgClosure *)q) = static_objects;
- static_objects = (StgClosure *)q;
- }
- RELEASE_SPIN_LOCK(&static_objects_sync);
- }
- return;
-
- case FUN_STATIC:
- if (info->srt_bitmap != 0 &&
- *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
- ACQUIRE_SPIN_LOCK(&static_objects_sync);
- if (*FUN_STATIC_LINK((StgClosure *)q) == NULL) {
- *FUN_STATIC_LINK((StgClosure *)q) = static_objects;
- static_objects = (StgClosure *)q;
- }
- RELEASE_SPIN_LOCK(&static_objects_sync);
- }
- return;
-
- case IND_STATIC:
- /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
- * on the CAF list, so don't do anything with it here (we'll
- * scavenge it later).
- */
- if (((StgIndStatic *)q)->saved_info == NULL) {
- ACQUIRE_SPIN_LOCK(&static_objects_sync);
- if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
- *IND_STATIC_LINK((StgClosure *)q) = static_objects;
- static_objects = (StgClosure *)q;
- }
- RELEASE_SPIN_LOCK(&static_objects_sync);
- }
- return;
-
- case CONSTR_STATIC:
- if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
- ACQUIRE_SPIN_LOCK(&static_objects_sync);
- // re-test, after acquiring lock
- if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
- *STATIC_LINK(info,(StgClosure *)q) = static_objects;
- static_objects = (StgClosure *)q;
- }
- RELEASE_SPIN_LOCK(&static_objects_sync);
- /* I am assuming that static_objects pointers are not
- * written to other objects, and thus, no need to retag. */
- }
- return;
-
- case CONSTR_NOCAF_STATIC:
- /* no need to put these on the static linked list, they don't need
- * to be scavenged.
- */
- return;
-
- default:
- barf("evacuate(static): strange closure type %d", (int)(info->type));
- }
- }
-
- bd = Bdescr((P_)q);
-
- if (bd->gen_no > N) {
- /* Can't evacuate this object, because it's in a generation
- * older than the ones we're collecting. Let's hope that it's
- * in gct->evac_step or older, or we will have to arrange to track
- * this pointer using the mutable list.
- */
- if (bd->step < gct->evac_step) {
- // nope
- gct->failed_to_evac = rtsTrue;
- TICK_GC_FAILED_PROMOTION();
- }
- return;
- }
-
- if ((bd->flags & (BF_LARGE | BF_COMPACTED | BF_EVACUATED)) != 0) {
-
- /* pointer into to-space: just return it. This normally
- * shouldn't happen, but alllowing it makes certain things
- * slightly easier (eg. the mutable list can contain the same
- * object twice, for example).
- */
- if (bd->flags & BF_EVACUATED) {
- if (bd->step < gct->evac_step) {
- gct->failed_to_evac = rtsTrue;
- TICK_GC_FAILED_PROMOTION();
- }
- return;
- }
-
- /* evacuate large objects by re-linking them onto a different list.
- */
- if (bd->flags & BF_LARGE) {
- info = get_itbl(q);
- if (info->type == TSO &&
- ((StgTSO *)q)->what_next == ThreadRelocated) {
- q = (StgClosure *)((StgTSO *)q)->link;
- *p = q;
- goto loop;
- }
- evacuate_large((P_)q);
- return;
- }
-
- /* If the object is in a step that we're compacting, then we
- * need to use an alternative evacuate procedure.
- */
- if (bd->flags & BF_COMPACTED) {
- if (!is_marked((P_)q,bd)) {
- mark((P_)q,bd);
- if (mark_stack_full()) {
- mark_stack_overflowed = rtsTrue;
- reset_mark_stack();
- }
- push_mark_stack((P_)q);
- }
- return;
- }
- }
-
- stp = bd->step->to;
-
- info = get_itbl(q);
-
- switch (info->type) {
-
- case WHITEHOLE:
- goto loop;
-
- case MUT_VAR_CLEAN:
- case MUT_VAR_DIRTY:
- case MVAR_CLEAN:
- case MVAR_DIRTY:
- copy(p,q,sizeW_fromITBL(info),stp);
- return;
-
- case CONSTR_0_1:
- {
- StgWord w = (StgWord)q->payload[0];
- if (q->header.info == Czh_con_info &&
- // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
- (StgChar)w <= MAX_CHARLIKE) {
- *p = TAG_CLOSURE(tag,
- (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
- );
- }
- if (q->header.info == Izh_con_info &&
- (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
- *p = TAG_CLOSURE(tag,
- (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
- );
- }
- else {
- copy_tag(p,q,sizeofW(StgHeader)+1,stp,tag);
- }
- return;
- }
-
- case FUN_0_1:
- case FUN_1_0:
- case CONSTR_1_0:
- copy_tag(p,q,sizeofW(StgHeader)+1,stp,tag);
- return;
-
- case THUNK_1_0:
- case THUNK_0_1:
- copy(p,q,sizeofW(StgThunk)+1,stp);
- return;
-
- case THUNK_1_1:
- case THUNK_2_0:
- case THUNK_0_2:
-#ifdef NO_PROMOTE_THUNKS
- if (bd->gen_no == 0 &&
- bd->step->no != 0 &&
- bd->step->no == generations[bd->gen_no].n_steps-1) {
- stp = bd->step;
- }
-#endif
- copy(p,q,sizeofW(StgThunk)+2,stp);
- return;
-
- case FUN_1_1:
- case FUN_2_0:
- case FUN_0_2:
- case CONSTR_1_1:
- case CONSTR_2_0:
- copy_tag(p,q,sizeofW(StgHeader)+2,stp,tag);
- return;
-
- case CONSTR_0_2:
- copy_tag(p,q,sizeofW(StgHeader)+2,stp,tag);
- return;
-
- case THUNK:
- copy(p,q,thunk_sizeW_fromITBL(info),stp);
- return;
-
- case FUN:
- case IND_PERM:
- case IND_OLDGEN_PERM:
- case WEAK:
- case STABLE_NAME:
- case CONSTR:
- copy_tag(p,q,sizeW_fromITBL(info),stp,tag);
- return;
-
- case BCO:
- copy(p,q,bco_sizeW((StgBCO *)q),stp);
- return;
-
- case CAF_BLACKHOLE:
- case SE_CAF_BLACKHOLE:
- case SE_BLACKHOLE:
- case BLACKHOLE:
- copyPart(p,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
- return;
-
- case THUNK_SELECTOR:
- eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
- return;
-
- case IND:
- case IND_OLDGEN:
- // follow chains of indirections, don't evacuate them
- q = ((StgInd*)q)->indirectee;
- *p = q;
- goto loop;
-
- case RET_BCO:
- case RET_SMALL:
- case RET_BIG:
- case RET_DYN:
- case UPDATE_FRAME:
- case STOP_FRAME:
- case CATCH_FRAME:
- case CATCH_STM_FRAME:
- case CATCH_RETRY_FRAME:
- case ATOMICALLY_FRAME:
- // shouldn't see these
- barf("evacuate: stack frame at %p\n", q);
-
- case PAP:
- copy(p,q,pap_sizeW((StgPAP*)q),stp);
- return;
-
- case AP:
- copy(p,q,ap_sizeW((StgAP*)q),stp);
- return;
-
- case AP_STACK:
- copy(p,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
- return;
-
- case EVACUATED:
- /* Already evacuated, just return the forwarding address.
- * HOWEVER: if the requested destination generation (gct->evac_step) is
- * older than the actual generation (because the object was
- * already evacuated to a younger generation) then we have to
- * set the gct->failed_to_evac flag to indicate that we couldn't
- * manage to promote the object to the desired generation.
- */
- /*
- * Optimisation: the check is fairly expensive, but we can often
- * shortcut it if either the required generation is 0, or the
- * current object (the EVACUATED) is in a high enough generation.
- * We know that an EVACUATED always points to an object in the
- * same or an older generation. stp is the lowest step that the
- * current object would be evacuated to, so we only do the full
- * check if stp is too low.
- */
- {
- StgClosure *e = ((StgEvacuated*)q)->evacuee;
- *p = e;
- if (stp < gct->evac_step) { // optimisation
- if (HEAP_ALLOCED(e) && Bdescr((P_)e)->step < gct->evac_step) {
- gct->failed_to_evac = rtsTrue;
- TICK_GC_FAILED_PROMOTION();
- }
- }
- return;
- }
-
- case ARR_WORDS:
- // just copy the block
- copy(p,q,arr_words_sizeW((StgArrWords *)q),stp);
- return;
-
- case MUT_ARR_PTRS_CLEAN:
- case MUT_ARR_PTRS_DIRTY:
- case MUT_ARR_PTRS_FROZEN:
- case MUT_ARR_PTRS_FROZEN0:
- // just copy the block
- copy(p,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
- return;
-
- case TSO:
- {
- StgTSO *tso = (StgTSO *)q;
-
- /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
- */
- if (tso->what_next == ThreadRelocated) {
- q = (StgClosure *)tso->link;
- *p = q;
- goto loop;
- }
-
- /* To evacuate a small TSO, we need to relocate the update frame
- * list it contains.
- */
- {
- StgTSO *new_tso;
- StgPtr r, s;
-
- copyPart(p,(StgClosure *)tso, tso_sizeW(tso), sizeofW(StgTSO), stp);
- new_tso = (StgTSO *)*p;
- move_TSO(tso, new_tso);
- for (r = tso->sp, s = new_tso->sp;
- r < tso->stack+tso->stack_size;) {
- *s++ = *r++;
- }
- return;
- }
- }
-
- case TREC_HEADER:
- copy(p,q,sizeofW(StgTRecHeader),stp);
- return;
-
- case TVAR_WATCH_QUEUE:
- copy(p,q,sizeofW(StgTVarWatchQueue),stp);
- return;
-
- case TVAR:
- copy(p,q,sizeofW(StgTVar),stp);
- return;
-
- case TREC_CHUNK:
- copy(p,q,sizeofW(StgTRecChunk),stp);
- return;
-
- case ATOMIC_INVARIANT:
- copy(p,q,sizeofW(StgAtomicInvariant),stp);
- return;
-
- case INVARIANT_CHECK_QUEUE:
- copy(p,q,sizeofW(StgInvariantCheckQueue),stp);
- return;
-
- default:
- barf("evacuate: strange closure type %d", (int)(info->type));
- }
-
- barf("evacuate");
-}
-