1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team 1998-2008
5 * Generational garbage collector: evacuation functions
7 * Documentation on the architecture of the Garbage Collector can be
8 * found in the online commentary:
10 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/Rts/Storage/GC
12 * ---------------------------------------------------------------------------*/
14 #include "PosixSource.h"
25 #include "LdvProfile.h"
27 #if defined(PROF_SPIN) && defined(THREADED_RTS) && defined(PARALLEL_GC)
28 StgWord64 evac_collision = 0;
31 #if defined(THREADED_RTS) && !defined(PARALLEL_GC)
32 #define evacuate(p) evacuate1(p)
33 #define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
36 #if !defined(PARALLEL_GC)
37 #define copy_tag_nolock(p, info, src, size, stp, tag) \
38 copy_tag(p, info, src, size, stp, tag)
41 /* Used to avoid long recursion due to selector thunks
43 #define MAX_THUNK_SELECTOR_DEPTH 16
45 static void eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool);
46 STATIC_INLINE void evacuate_large(StgPtr p);
48 /* -----------------------------------------------------------------------------
49 Allocate some space in which to copy an object.
50 -------------------------------------------------------------------------- */
53 alloc_for_copy (nat size, step *stp)
58 /* Find out where we're going, using the handy "to" pointer in
59 * the step of the source object. If it turns out we need to
60 * evacuate to an older generation, adjust it here (see comment
63 if (stp < gct->evac_step) {
64 if (gct->eager_promotion) {
67 gct->failed_to_evac = rtsTrue;
71 ws = &gct->steps[stp->abs_no];
72 // this compiles to a single mem access to stp->abs_no only
74 /* chain a new block onto the to-space for the destination step if
78 ws->todo_free += size;
79 if (ws->todo_free > ws->todo_lim) {
80 to = todo_block_full(size, ws);
82 ASSERT(ws->todo_free >= ws->todo_bd->free && ws->todo_free <= ws->todo_lim);
87 /* -----------------------------------------------------------------------------
89 -------------------------------------------------------------------------- */
91 STATIC_INLINE GNUC_ATTR_HOT void
92 copy_tag(StgClosure **p, const StgInfoTable *info,
93 StgClosure *src, nat size, step *stp, StgWord tag)
98 to = alloc_for_copy(size,stp);
100 TICK_GC_WORDS_COPIED(size);
104 for (i = 1; i < size; i++) { // unroll for small i
108 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
109 // __builtin_prefetch(to + size + 2, 1);
112 #if defined(PARALLEL_GC)
114 const StgInfoTable *new_info;
115 new_info = (const StgInfoTable *)cas((StgPtr)&src->header.info,
116 (W_)info, MK_FORWARDING_PTR(to));
117 if (new_info != info) {
118 #if defined(PROF_SPIN)
121 evacuate(p); // does the failed_to_evac stuff
123 *p = TAG_CLOSURE(tag,(StgClosure*)to);
127 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
128 *p = TAG_CLOSURE(tag,(StgClosure*)to);
132 // We store the size of the just evacuated object in the LDV word so that
133 // the profiler can guess the position of the next object later.
134 SET_EVACUAEE_FOR_LDV(from, size);
138 #if defined(PARALLEL_GC)
140 copy_tag_nolock(StgClosure **p, const StgInfoTable *info,
141 StgClosure *src, nat size, step *stp, StgWord tag)
146 to = alloc_for_copy(size,stp);
147 *p = TAG_CLOSURE(tag,(StgClosure*)to);
148 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
150 TICK_GC_WORDS_COPIED(size);
154 for (i = 1; i < size; i++) { // unroll for small i
158 // if (to+size+2 < bd->start + BLOCK_SIZE_W) {
159 // __builtin_prefetch(to + size + 2, 1);
163 // We store the size of the just evacuated object in the LDV word so that
164 // the profiler can guess the position of the next object later.
165 SET_EVACUAEE_FOR_LDV(from, size);
170 /* Special version of copy() for when we only want to copy the info
171 * pointer of an object, but reserve some padding after it. This is
172 * used to optimise evacuation of BLACKHOLEs.
175 copyPart(StgClosure **p, const StgInfoTable *info, StgClosure *src,
176 nat size_to_reserve, nat size_to_copy, step *stp)
181 to = alloc_for_copy(size_to_reserve, stp);
183 TICK_GC_WORDS_COPIED(size_to_copy);
187 for (i = 1; i < size_to_copy; i++) { // unroll for small i
191 #if defined(PARALLEL_GC)
193 const StgInfoTable *new_info;
194 new_info = (const StgInfoTable *)cas((StgPtr)&src->header.info,
195 (W_)info, MK_FORWARDING_PTR(to));
196 if (new_info != info) {
197 #if defined(PROF_SPIN)
200 evacuate(p); // does the failed_to_evac stuff
203 *p = (StgClosure*)to;
207 src->header.info = (const StgInfoTable *)MK_FORWARDING_PTR(to);
208 *p = (StgClosure*)to;
212 // We store the size of the just evacuated object in the LDV word so that
213 // the profiler can guess the position of the next object later.
214 SET_EVACUAEE_FOR_LDV(from, size_to_reserve);
216 if (size_to_reserve - size_to_copy > 0)
217 LDV_FILL_SLOP(to + size_to_copy, (int)(size_to_reserve - size_to_copy));
224 /* Copy wrappers that don't tag the closure after copying */
225 STATIC_INLINE GNUC_ATTR_HOT void
226 copy(StgClosure **p, const StgInfoTable *info,
227 StgClosure *src, nat size, step *stp)
229 copy_tag(p,info,src,size,stp,0);
232 /* -----------------------------------------------------------------------------
233 Evacuate a large object
235 This just consists of removing the object from the (doubly-linked)
236 step->large_objects list, and linking it on to the (singly-linked)
237 step->new_large_objects list, from where it will be scavenged later.
239 Convention: bd->flags has BF_EVACUATED set for a large object
240 that has been evacuated, or unset otherwise.
241 -------------------------------------------------------------------------- */
244 evacuate_large(StgPtr p)
246 bdescr *bd = Bdescr(p);
251 ACQUIRE_SPIN_LOCK(&stp->sync_large_objects);
253 // already evacuated?
254 if (bd->flags & BF_EVACUATED) {
255 /* Don't forget to set the gct->failed_to_evac flag if we didn't get
256 * the desired destination (see comments in evacuate()).
258 if (stp < gct->evac_step) {
259 gct->failed_to_evac = rtsTrue;
260 TICK_GC_FAILED_PROMOTION();
262 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
266 // remove from large_object list
268 bd->u.back->link = bd->link;
269 } else { // first object in the list
270 stp->large_objects = bd->link;
273 bd->link->u.back = bd->u.back;
276 /* link it on to the evacuated large object list of the destination step
279 if (new_stp < gct->evac_step) {
280 if (gct->eager_promotion) {
281 new_stp = gct->evac_step;
283 gct->failed_to_evac = rtsTrue;
287 ws = &gct->steps[new_stp->abs_no];
289 bd->flags |= BF_EVACUATED;
291 bd->gen_no = new_stp->gen_no;
293 // If this is a block of pinned objects, we don't have to scan
294 // these objects, because they aren't allowed to contain any
295 // pointers. For these blocks, we skip the scavenge stage and put
296 // them straight on the scavenged_large_objects list.
297 if (bd->flags & BF_PINNED) {
298 ASSERT(get_itbl((StgClosure *)p)->type == ARR_WORDS);
299 if (new_stp != stp) { ACQUIRE_SPIN_LOCK(&new_stp->sync_large_objects); }
300 dbl_link_onto(bd, &new_stp->scavenged_large_objects);
301 new_stp->n_scavenged_large_blocks += bd->blocks;
302 if (new_stp != stp) { RELEASE_SPIN_LOCK(&new_stp->sync_large_objects); }
304 bd->link = ws->todo_large_objects;
305 ws->todo_large_objects = bd;
308 RELEASE_SPIN_LOCK(&stp->sync_large_objects);
311 /* ----------------------------------------------------------------------------
314 This is called (eventually) for every live object in the system.
316 The caller to evacuate specifies a desired generation in the
317 gct->evac_step thread-local variable. The following conditions apply to
318 evacuating an object which resides in generation M when we're
319 collecting up to generation N
321 if M >= gct->evac_step
323 else evac to step->to
325 if M < gct->evac_step evac to gct->evac_step, step 0
327 if the object is already evacuated, then we check which generation
330 if M >= gct->evac_step do nothing
331 if M < gct->evac_step set gct->failed_to_evac flag to indicate that we
332 didn't manage to evacuate this object into gct->evac_step.
337 evacuate() is the single most important function performance-wise
338 in the GC. Various things have been tried to speed it up, but as
339 far as I can tell the code generated by gcc 3.2 with -O2 is about
340 as good as it's going to get. We pass the argument to evacuate()
341 in a register using the 'regparm' attribute (see the prototype for
342 evacuate() near the top of this file).
344 Changing evacuate() to take an (StgClosure **) rather than
345 returning the new pointer seems attractive, because we can avoid
346 writing back the pointer when it hasn't changed (eg. for a static
347 object, or an object in a generation > N). However, I tried it and
348 it doesn't help. One reason is that the (StgClosure **) pointer
349 gets spilled to the stack inside evacuate(), resulting in far more
350 extra reads/writes than we save.
351 ------------------------------------------------------------------------- */
353 REGPARM1 GNUC_ATTR_HOT void
354 evacuate(StgClosure **p)
359 const StgInfoTable *info;
365 /* The tag and the pointer are split, to be merged after evacing */
366 tag = GET_CLOSURE_TAG(q);
367 q = UNTAG_CLOSURE(q);
369 ASSERT(LOOKS_LIKE_CLOSURE_PTR(q));
371 if (!HEAP_ALLOCED_GC(q)) {
373 if (!major_gc) return;
376 switch (info->type) {
379 if (info->srt_bitmap != 0) {
380 if (*THUNK_STATIC_LINK((StgClosure *)q) == NULL) {
382 *THUNK_STATIC_LINK((StgClosure *)q) = gct->static_objects;
383 gct->static_objects = (StgClosure *)q;
386 link = (StgPtr)cas((StgPtr)THUNK_STATIC_LINK((StgClosure *)q),
388 (StgWord)gct->static_objects);
390 gct->static_objects = (StgClosure *)q;
398 if (info->srt_bitmap != 0 &&
399 *FUN_STATIC_LINK((StgClosure *)q) == NULL) {
401 *FUN_STATIC_LINK((StgClosure *)q) = gct->static_objects;
402 gct->static_objects = (StgClosure *)q;
405 link = (StgPtr)cas((StgPtr)FUN_STATIC_LINK((StgClosure *)q),
407 (StgWord)gct->static_objects);
409 gct->static_objects = (StgClosure *)q;
416 /* If q->saved_info != NULL, then it's a revertible CAF - it'll be
417 * on the CAF list, so don't do anything with it here (we'll
418 * scavenge it later).
420 if (((StgIndStatic *)q)->saved_info == NULL) {
421 if (*IND_STATIC_LINK((StgClosure *)q) == NULL) {
423 *IND_STATIC_LINK((StgClosure *)q) = gct->static_objects;
424 gct->static_objects = (StgClosure *)q;
427 link = (StgPtr)cas((StgPtr)IND_STATIC_LINK((StgClosure *)q),
429 (StgWord)gct->static_objects);
431 gct->static_objects = (StgClosure *)q;
439 if (*STATIC_LINK(info,(StgClosure *)q) == NULL) {
441 *STATIC_LINK(info,(StgClosure *)q) = gct->static_objects;
442 gct->static_objects = (StgClosure *)q;
445 link = (StgPtr)cas((StgPtr)STATIC_LINK(info,(StgClosure *)q),
447 (StgWord)gct->static_objects);
449 gct->static_objects = (StgClosure *)q;
453 /* I am assuming that static_objects pointers are not
454 * written to other objects, and thus, no need to retag. */
457 case CONSTR_NOCAF_STATIC:
458 /* no need to put these on the static linked list, they don't need
464 barf("evacuate(static): strange closure type %d", (int)(info->type));
470 if ((bd->flags & (BF_LARGE | BF_MARKED | BF_EVACUATED)) != 0) {
472 // pointer into to-space: just return it. It might be a pointer
473 // into a generation that we aren't collecting (> N), or it
474 // might just be a pointer into to-space. The latter doesn't
475 // happen often, but allowing it makes certain things a bit
476 // easier; e.g. scavenging an object is idempotent, so it's OK to
477 // have an object on the mutable list multiple times.
478 if (bd->flags & BF_EVACUATED) {
479 // We aren't copying this object, so we have to check
480 // whether it is already in the target generation. (this is
481 // the write barrier).
482 if (bd->step < gct->evac_step) {
483 gct->failed_to_evac = rtsTrue;
484 TICK_GC_FAILED_PROMOTION();
489 /* evacuate large objects by re-linking them onto a different list.
491 if (bd->flags & BF_LARGE) {
493 if (info->type == TSO &&
494 ((StgTSO *)q)->what_next == ThreadRelocated) {
495 q = (StgClosure *)((StgTSO *)q)->_link;
499 evacuate_large((P_)q);
503 /* If the object is in a step that we're compacting, then we
504 * need to use an alternative evacuate procedure.
506 if (!is_marked((P_)q,bd)) {
508 if (mark_stack_full()) {
509 debugTrace(DEBUG_gc,"mark stack overflowed");
510 mark_stack_overflowed = rtsTrue;
513 push_mark_stack((P_)q);
520 info = q->header.info;
521 if (IS_FORWARDING_PTR(info))
523 /* Already evacuated, just return the forwarding address.
524 * HOWEVER: if the requested destination generation (gct->evac_step) is
525 * older than the actual generation (because the object was
526 * already evacuated to a younger generation) then we have to
527 * set the gct->failed_to_evac flag to indicate that we couldn't
528 * manage to promote the object to the desired generation.
531 * Optimisation: the check is fairly expensive, but we can often
532 * shortcut it if either the required generation is 0, or the
533 * current object (the EVACUATED) is in a high enough generation.
534 * We know that an EVACUATED always points to an object in the
535 * same or an older generation. stp is the lowest step that the
536 * current object would be evacuated to, so we only do the full
537 * check if stp is too low.
539 StgClosure *e = (StgClosure*)UN_FORWARDING_PTR(info);
540 *p = TAG_CLOSURE(tag,e);
541 if (stp < gct->evac_step) { // optimisation
542 if (Bdescr((P_)e)->step < gct->evac_step) {
543 gct->failed_to_evac = rtsTrue;
544 TICK_GC_FAILED_PROMOTION();
550 switch (INFO_PTR_TO_STRUCT(info)->type) {
559 copy(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
564 StgWord w = (StgWord)q->payload[0];
565 if (info == Czh_con_info &&
566 // unsigned, so always true: (StgChar)w >= MIN_CHARLIKE &&
567 (StgChar)w <= MAX_CHARLIKE) {
568 *p = TAG_CLOSURE(tag,
569 (StgClosure *)CHARLIKE_CLOSURE((StgChar)w)
572 else if (info == Izh_con_info &&
573 (StgInt)w >= MIN_INTLIKE && (StgInt)w <= MAX_INTLIKE) {
574 *p = TAG_CLOSURE(tag,
575 (StgClosure *)INTLIKE_CLOSURE((StgInt)w)
579 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
587 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+1,stp,tag);
592 copy(p,info,q,sizeofW(StgThunk)+1,stp);
598 #ifdef NO_PROMOTE_THUNKS
599 if (bd->gen_no == 0 &&
601 bd->step->no == generations[bd->gen_no].n_steps-1) {
605 copy(p,info,q,sizeofW(StgThunk)+2,stp);
613 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
617 copy_tag_nolock(p,info,q,sizeofW(StgHeader)+2,stp,tag);
621 copy(p,info,q,thunk_sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp);
626 case IND_OLDGEN_PERM:
628 copy_tag_nolock(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
633 copy_tag(p,info,q,sizeW_fromITBL(INFO_PTR_TO_STRUCT(info)),stp,tag);
637 copy(p,info,q,bco_sizeW((StgBCO *)q),stp);
642 copyPart(p,info,q,BLACKHOLE_sizeW(),sizeofW(StgHeader),stp);
646 eval_thunk_selector(p, (StgSelector *)q, rtsTrue);
651 // follow chains of indirections, don't evacuate them
652 q = ((StgInd*)q)->indirectee;
663 case CATCH_STM_FRAME:
664 case CATCH_RETRY_FRAME:
665 case ATOMICALLY_FRAME:
666 // shouldn't see these
667 barf("evacuate: stack frame at %p\n", q);
670 copy(p,info,q,pap_sizeW((StgPAP*)q),stp);
674 copy(p,info,q,ap_sizeW((StgAP*)q),stp);
678 copy(p,info,q,ap_stack_sizeW((StgAP_STACK*)q),stp);
682 // just copy the block
683 copy(p,info,q,arr_words_sizeW((StgArrWords *)q),stp);
686 case MUT_ARR_PTRS_CLEAN:
687 case MUT_ARR_PTRS_DIRTY:
688 case MUT_ARR_PTRS_FROZEN:
689 case MUT_ARR_PTRS_FROZEN0:
690 // just copy the block
691 copy(p,info,q,mut_arr_ptrs_sizeW((StgMutArrPtrs *)q),stp);
696 StgTSO *tso = (StgTSO *)q;
698 /* Deal with redirected TSOs (a TSO that's had its stack enlarged).
700 if (tso->what_next == ThreadRelocated) {
701 q = (StgClosure *)tso->_link;
706 /* To evacuate a small TSO, we need to relocate the update frame
714 mine = copyPart(p,info,(StgClosure *)tso, tso_sizeW(tso),
715 sizeofW(StgTSO), stp);
717 new_tso = (StgTSO *)*p;
718 move_TSO(tso, new_tso);
719 for (r = tso->sp, s = new_tso->sp;
720 r < tso->stack+tso->stack_size;) {
729 copy(p,info,q,sizeofW(StgTRecHeader),stp);
732 case TVAR_WATCH_QUEUE:
733 copy(p,info,q,sizeofW(StgTVarWatchQueue),stp);
737 copy(p,info,q,sizeofW(StgTVar),stp);
741 copy(p,info,q,sizeofW(StgTRecChunk),stp);
744 case ATOMIC_INVARIANT:
745 copy(p,info,q,sizeofW(StgAtomicInvariant),stp);
748 case INVARIANT_CHECK_QUEUE:
749 copy(p,info,q,sizeofW(StgInvariantCheckQueue),stp);
753 barf("evacuate: strange closure type %d", (int)(INFO_PTR_TO_STRUCT(info)->type));
759 /* -----------------------------------------------------------------------------
760 Evaluate a THUNK_SELECTOR if possible.
762 p points to a THUNK_SELECTOR that we want to evaluate. The
763 result of "evaluating" it will be evacuated and a pointer to the
764 to-space closure will be returned.
766 If the THUNK_SELECTOR could not be evaluated (its selectee is still
767 a THUNK, for example), then the THUNK_SELECTOR itself will be
769 -------------------------------------------------------------------------- */
771 unchain_thunk_selectors(StgSelector *p, StgClosure *val)
779 ASSERT(p->header.info == &stg_WHITEHOLE_info);
781 ASSERT(p->header.info == &stg_BLACKHOLE_info);
783 // val must be in to-space. Not always: when we recursively
784 // invoke eval_thunk_selector(), the recursive calls will not
785 // evacuate the value (because we want to select on the value,
786 // not evacuate it), so in this case val is in from-space.
787 // ASSERT(!HEAP_ALLOCED_GC(val) || Bdescr((P_)val)->gen_no > N || (Bdescr((P_)val)->flags & BF_EVACUATED));
789 prev = (StgSelector*)((StgClosure *)p)->payload[0];
791 // Update the THUNK_SELECTOR with an indirection to the
792 // value. The value is still in from-space at this stage.
794 // (old note: Why not do upd_evacuee(q,p)? Because we have an
795 // invariant that an EVACUATED closure always points to an
796 // object in the same or an older generation (required by
797 // the short-cut test in the EVACUATED case, below).
798 if ((StgClosure *)p == val) {
799 // must be a loop; just leave a BLACKHOLE in place. This
800 // can happen when we have a chain of selectors that
801 // eventually loops back on itself. We can't leave an
802 // indirection pointing to itself, and we want the program
803 // to deadlock if it ever enters this closure, so
804 // BLACKHOLE is correct.
805 SET_INFO(p, &stg_BLACKHOLE_info);
807 ((StgInd *)p)->indirectee = val;
809 SET_INFO(p, &stg_IND_info);
812 // For the purposes of LDV profiling, we have created an
814 LDV_RECORD_CREATE(p);
821 eval_thunk_selector (StgClosure **q, StgSelector * p, rtsBool evac)
822 // NB. for legacy reasons, p & q are swapped around :(
827 StgClosure *selectee;
828 StgSelector *prev_thunk_selector;
832 prev_thunk_selector = NULL;
833 // this is a chain of THUNK_SELECTORs that we are going to update
834 // to point to the value of the current THUNK_SELECTOR. Each
835 // closure on the chain is a BLACKHOLE, and points to the next in the
836 // chain with payload[0].
840 bd = Bdescr((StgPtr)p);
841 if (HEAP_ALLOCED_GC(p)) {
842 // If the THUNK_SELECTOR is in to-space or in a generation that we
843 // are not collecting, then bale out early. We won't be able to
844 // save any space in any case, and updating with an indirection is
845 // trickier in a non-collected gen: we would have to update the
847 if (bd->flags & BF_EVACUATED) {
848 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
849 *q = (StgClosure *)p;
850 // shortcut, behave as for: if (evac) evacuate(q);
851 if (evac && bd->step < gct->evac_step) {
852 gct->failed_to_evac = rtsTrue;
853 TICK_GC_FAILED_PROMOTION();
857 // we don't update THUNK_SELECTORS in the compacted
858 // generation, because compaction does not remove the INDs
859 // that result, this causes confusion later
860 // (scavenge_mark_stack doesn't deal with IND). BEWARE! This
861 // bit is very tricky to get right. If you make changes
862 // around here, test by compiling stage 3 with +RTS -c -RTS.
863 if (bd->flags & BF_MARKED) {
864 // must call evacuate() to mark this closure if evac==rtsTrue
865 *q = (StgClosure *)p;
866 if (evac) evacuate(q);
867 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
873 // BLACKHOLE the selector thunk, since it is now under evaluation.
874 // This is important to stop us going into an infinite loop if
875 // this selector thunk eventually refers to itself.
876 #if defined(THREADED_RTS)
877 // In threaded mode, we'll use WHITEHOLE to lock the selector
878 // thunk while we evaluate it.
881 info_ptr = xchg((StgPtr)&p->header.info, (W_)&stg_WHITEHOLE_info);
882 } while (info_ptr == (W_)&stg_WHITEHOLE_info);
884 // make sure someone else didn't get here first...
885 if (IS_FORWARDING_PTR(p) ||
886 INFO_PTR_TO_STRUCT(info_ptr)->type != THUNK_SELECTOR) {
887 // v. tricky now. The THUNK_SELECTOR has been evacuated
888 // by another thread, and is now either a forwarding ptr or IND.
889 // We need to extract ourselves from the current situation
890 // as cleanly as possible.
891 // - unlock the closure
892 // - update *q, we may have done *some* evaluation
893 // - if evac, we need to call evacuate(), because we
894 // need the write-barrier stuff.
895 // - undo the chain we've built to point to p.
896 SET_INFO(p, (const StgInfoTable *)info_ptr);
897 *q = (StgClosure *)p;
898 if (evac) evacuate(q);
899 unchain_thunk_selectors(prev_thunk_selector, (StgClosure *)p);
904 // Save the real info pointer (NOTE: not the same as get_itbl()).
905 info_ptr = (StgWord)p->header.info;
906 SET_INFO(p,&stg_BLACKHOLE_info);
909 field = INFO_PTR_TO_STRUCT(info_ptr)->layout.selector_offset;
911 // The selectee might be a constructor closure,
912 // so we untag the pointer.
913 selectee = UNTAG_CLOSURE(p->selectee);
916 // selectee now points to the closure that we're trying to select
917 // a field from. It may or may not be in to-space: we try not to
918 // end up in to-space, but it's impractical to avoid it in
919 // general. The compacting GC scatters to-space pointers in
920 // from-space during marking, for example. We rely on the property
921 // that evacuate() doesn't mind if it gets passed a to-space pointer.
923 info = (StgInfoTable*)selectee->header.info;
925 if (IS_FORWARDING_PTR(info)) {
926 // We don't follow pointers into to-space; the constructor
927 // has already been evacuated, so we won't save any space
928 // leaks by evaluating this selector thunk anyhow.
932 info = INFO_PTR_TO_STRUCT(info);
933 switch (info->type) {
935 goto bale_out; // about to be evacuated by another thread (or a loop).
944 case CONSTR_NOCAF_STATIC:
946 // check that the size is in range
947 ASSERT(field < (StgWord32)(info->layout.payload.ptrs +
948 info->layout.payload.nptrs));
950 // Select the right field from the constructor
951 val = selectee->payload[field];
954 // For the purposes of LDV profiling, we have destroyed
955 // the original selector thunk, p.
956 SET_INFO(p, (StgInfoTable *)info_ptr);
957 LDV_RECORD_DEAD_FILL_SLOP_DYNAMIC((StgClosure *)p);
958 #if defined(THREADED_RTS)
959 SET_INFO(p, &stg_WHITEHOLE_info);
961 SET_INFO(p, &stg_BLACKHOLE_info);
965 // the closure in val is now the "value" of the
966 // THUNK_SELECTOR in p. However, val may itself be a
967 // THUNK_SELECTOR, in which case we want to continue
968 // evaluating until we find the real value, and then
969 // update the whole chain to point to the value.
971 info_ptr = (StgWord)UNTAG_CLOSURE(val)->header.info;
972 if (!IS_FORWARDING_PTR(info_ptr))
974 info = INFO_PTR_TO_STRUCT(info_ptr);
975 switch (info->type) {
979 case IND_OLDGEN_PERM:
981 val = ((StgInd *)val)->indirectee;
984 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
985 prev_thunk_selector = p;
986 p = (StgSelector*)val;
992 ((StgClosure*)p)->payload[0] = (StgClosure *)prev_thunk_selector;
993 prev_thunk_selector = p;
997 // update the other selectors in the chain *before*
998 // evacuating the value. This is necessary in the case
999 // where the value turns out to be one of the selectors
1000 // in the chain (i.e. we have a loop), and evacuating it
1001 // would corrupt the chain.
1002 unchain_thunk_selectors(prev_thunk_selector, val);
1004 // evacuate() cannot recurse through
1005 // eval_thunk_selector(), because we know val is not
1006 // a THUNK_SELECTOR.
1007 if (evac) evacuate(q);
1014 case IND_OLDGEN_PERM:
1016 // Again, we might need to untag a constructor.
1017 selectee = UNTAG_CLOSURE( ((StgInd *)selectee)->indirectee );
1020 case THUNK_SELECTOR:
1024 // recursively evaluate this selector. We don't want to
1025 // recurse indefinitely, so we impose a depth bound.
1026 if (gct->thunk_selector_depth >= MAX_THUNK_SELECTOR_DEPTH) {
1030 gct->thunk_selector_depth++;
1031 // rtsFalse says "don't evacuate the result". It will,
1032 // however, update any THUNK_SELECTORs that are evaluated
1034 eval_thunk_selector(&val, (StgSelector*)selectee, rtsFalse);
1035 gct->thunk_selector_depth--;
1037 // did we actually manage to evaluate it?
1038 if (val == selectee) goto bale_out;
1040 // Of course this pointer might be tagged...
1041 selectee = UNTAG_CLOSURE(val);
1056 // not evaluated yet
1060 barf("eval_thunk_selector: strange selectee %d",
1065 // We didn't manage to evaluate this thunk; restore the old info
1066 // pointer. But don't forget: we still need to evacuate the thunk itself.
1067 SET_INFO(p, (const StgInfoTable *)info_ptr);
1068 // THREADED_RTS: we just unlocked the thunk, so another thread
1069 // might get in and update it. copy() will lock it again and
1070 // check whether it was updated in the meantime.
1071 *q = (StgClosure *)p;
1073 copy(q,(const StgInfoTable *)info_ptr,(StgClosure *)p,THUNK_SELECTOR_sizeW(),bd->step->to);
1075 unchain_thunk_selectors(prev_thunk_selector, *q);