Naveen Neel / shedskin
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gc_pmark.h Source File

gc_pmark.h

00001 /*
00002  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
00003  * Copyright (c) 2001 by Hewlett-Packard Company. All rights reserved.
00004  *
00005  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00006  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00007  *
00008  * Permission is hereby granted to use or copy this program
00009  * for any purpose,  provided the above notices are retained on all copies.
00010  * Permission to modify the code and to distribute modified code is granted,
00011  * provided the above notices are retained, and a notice that the code was
00012  * modified is included with the above copyright notice.
00013  *
00014  */
00015 
00016 /* Private declarations of GC marker data structures and macros */
00017 
00018 /*
00019  * Declarations of mark stack.  Needed by marker and client supplied mark
00020  * routines.  Transitively include gc_priv.h.
00021  * (Note that gc_priv.h should not be included before this, since this
00022  * includes dbg_mlc.h, which wants to include gc_priv.h AFTER defining
00023  * I_HIDE_POINTERS.)
00024  */
00025 #ifndef GC_PMARK_H
00026 # define GC_PMARK_H
00027 
00028 # if defined(KEEP_BACK_PTRS) || defined(PRINT_BLACK_LIST)
00029 #   include "dbg_mlc.h"
00030 # endif
00031 # ifndef GC_MARK_H
00032 #   include "../gc_mark.h"
00033 # endif
00034 # ifndef GC_PRIVATE_H
00035 #   include "gc_priv.h"
00036 # endif
00037 
00038 /* The real declarations of the following is in gc_priv.h, so that  */
00039 /* we can avoid scanning the following table.               */
00040 /*
00041 extern mark_proc GC_mark_procs[MAX_MARK_PROCS];
00042 */
00043 
00044 /*
00045  * Mark descriptor stuff that should remain private for now, mostly
00046  * because it's hard to export WORDSZ without including gcconfig.h.
00047  */
00048 # define BITMAP_BITS (WORDSZ - GC_DS_TAG_BITS)
00049 # define PROC(descr) \
00050     (GC_mark_procs[((descr) >> GC_DS_TAG_BITS) & (GC_MAX_MARK_PROCS-1)])
00051 # define ENV(descr) \
00052     ((descr) >> (GC_DS_TAG_BITS + GC_LOG_MAX_MARK_PROCS))
00053 # define MAX_ENV \
00054     (((word)1 << (WORDSZ - GC_DS_TAG_BITS - GC_LOG_MAX_MARK_PROCS)) - 1)
00055 
00056 
00057 extern word GC_n_mark_procs;
00058 
00059 /* Number of mark stack entries to discard on overflow. */
00060 #define GC_MARK_STACK_DISCARDS (INITIAL_MARK_STACK_SIZE/8)
00061 
00062 typedef struct GC_ms_entry {
00063     GC_word * mse_start;   /* First word of object */
00064     GC_word mse_descr;  /* Descriptor; low order two bits are tags, */
00065                 /* identifying the upper 30 bits as one of the  */
00066                 /* following:                   */
00067 } mse;
00068 
00069 extern word GC_mark_stack_size;
00070 
00071 extern mse * GC_mark_stack_limit;
00072 
00073 #ifdef PARALLEL_MARK
00074   extern mse * VOLATILE GC_mark_stack_top;
00075 #else
00076   extern mse * GC_mark_stack_top;
00077 #endif
00078 
00079 extern mse * GC_mark_stack;
00080 
00081 #ifdef PARALLEL_MARK
00082     /*
00083      * Allow multiple threads to participate in the marking process.
00084      * This works roughly as follows:
00085      *  The main mark stack never shrinks, but it can grow.
00086      *
00087      *  The initiating threads holds the GC lock, and sets GC_help_wanted.
00088      *  
00089      *  Other threads:
00090      *     1) update helper_count (while holding mark_lock.)
00091      *     2) allocate a local mark stack
00092      *     repeatedly:
00093      *      3) Steal a global mark stack entry by atomically replacing
00094      *         its descriptor with 0.
00095      *      4) Copy it to the local stack.
00096      *          5) Mark on the local stack until it is empty, or
00097      *         it may be profitable to copy it back.
00098      *          6) If necessary, copy local stack to global one,
00099      *         holding mark lock.
00100      *    7) Stop when the global mark stack is empty.
00101      *    8) decrement helper_count (holding mark_lock).
00102      *
00103      * This is an experiment to see if we can do something along the lines
00104      * of the University of Tokyo SGC in a less intrusive, though probably
00105      * also less performant, way.
00106      */
00107     void GC_do_parallel_mark();
00108         /* inititate parallel marking.  */
00109 
00110     extern GC_bool GC_help_wanted;  /* Protected by mark lock   */
00111     extern unsigned GC_helper_count;    /* Number of running helpers.   */
00112                     /* Protected by mark lock   */
00113     extern unsigned GC_active_count;    /* Number of active helpers.    */
00114                     /* Protected by mark lock   */
00115                     /* May increase and decrease    */
00116                     /* within each mark cycle.  But */
00117                     /* once it returns to 0, it */
00118                     /* stays zero for the cycle.    */
00119     /* GC_mark_stack_top is also protected by mark lock.    */
00120     extern mse * VOLATILE GC_first_nonempty;
00121                     /* Lowest entry on mark stack   */
00122                     /* that may be nonempty.    */
00123                     /* Updated only by initiating   */
00124                     /* thread.          */
00125     /*
00126      * GC_notify_all_marker() is used when GC_help_wanted is first set,
00127      * when the last helper becomes inactive,
00128      * when something is added to the global mark stack, and just after
00129      * GC_mark_no is incremented.
00130      * This could be split into multiple CVs (and probably should be to
00131      * scale to really large numbers of processors.)
00132      */
00133 #endif /* PARALLEL_MARK */
00134 
00135 /* Return a pointer to within 1st page of object.   */
00136 /* Set *new_hdr_p to corr. hdr.             */
00137 #ifdef __STDC__
00138   ptr_t GC_find_start(ptr_t current, hdr *hhdr, hdr **new_hdr_p);
00139 #else
00140   ptr_t GC_find_start();
00141 #endif
00142 
00143 mse * GC_signal_mark_stack_overflow GC_PROTO((mse *msp));
00144 
00145 # ifdef GATHERSTATS
00146 #   define ADD_TO_ATOMIC(sz) GC_atomic_in_use += (sz)
00147 #   define ADD_TO_COMPOSITE(sz) GC_composite_in_use += (sz)
00148 # else
00149 #   define ADD_TO_ATOMIC(sz)
00150 #   define ADD_TO_COMPOSITE(sz)
00151 # endif
00152 
00153 /* Push the object obj with corresponding heap block header hhdr onto   */
00154 /* the mark stack.                          */
00155 # define PUSH_OBJ(obj, hhdr, mark_stack_top, mark_stack_limit) \
00156 { \
00157     register word _descr = (hhdr) -> hb_descr; \
00158         \
00159     if (_descr == 0) { \
00160         ADD_TO_ATOMIC((hhdr) -> hb_sz); \
00161     } else { \
00162         ADD_TO_COMPOSITE((hhdr) -> hb_sz); \
00163         mark_stack_top++; \
00164         if (mark_stack_top >= mark_stack_limit) { \
00165           mark_stack_top = GC_signal_mark_stack_overflow(mark_stack_top); \
00166         } \
00167         mark_stack_top -> mse_start = (obj); \
00168         mark_stack_top -> mse_descr = _descr; \
00169     } \
00170 }
00171 
00172 /* Push the contents of current onto the mark stack if it is a valid    */
00173 /* ptr to a currently unmarked object.  Mark it.            */
00174 /* If we assumed a standard-conforming compiler, we could probably  */
00175 /* generate the exit_label transparently.               */
00176 # define PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
00177                source, exit_label) \
00178 { \
00179     hdr * my_hhdr; \
00180     ptr_t my_current = current; \
00181  \
00182     GET_HDR(my_current, my_hhdr); \
00183     if (IS_FORWARDING_ADDR_OR_NIL(my_hhdr)) { \
00184      hdr * new_hdr = GC_invalid_header; \
00185          my_current = GC_find_start(my_current, my_hhdr, &new_hdr); \
00186          my_hhdr = new_hdr; \
00187     } \
00188     PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
00189           source, exit_label, my_hhdr); \
00190 exit_label: ; \
00191 }
00192 
00193 /* As above, but use header cache for header lookup.    */
00194 # define HC_PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
00195                source, exit_label) \
00196 { \
00197     hdr * my_hhdr; \
00198     ptr_t my_current = current; \
00199  \
00200     HC_GET_HDR(my_current, my_hhdr, source); \
00201     PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
00202           source, exit_label, my_hhdr); \
00203 exit_label: ; \
00204 }
00205 
00206 /* Set mark bit, exit if it was already set.    */
00207 
00208 # ifdef USE_MARK_BYTES
00209     /* Unlike the mark bit case, there is a race here, and we may set   */
00210     /* the bit twice in the concurrent case.  This can result in the    */
00211     /* object being pushed twice.  But that's only a performance issue. */
00212 #   define SET_MARK_BIT_EXIT_IF_SET(hhdr,displ,exit_label) \
00213     { \
00214         register VOLATILE char * mark_byte_addr = \
00215                 hhdr -> hb_marks + ((displ) >> 1); \
00216         register char mark_byte = *mark_byte_addr; \
00217           \
00218     if (mark_byte) goto exit_label; \
00219     *mark_byte_addr = 1;  \
00220     } 
00221 # else
00222 #   define SET_MARK_BIT_EXIT_IF_SET(hhdr,displ,exit_label) \
00223     { \
00224         register word * mark_word_addr = hhdr -> hb_marks + divWORDSZ(displ); \
00225           \
00226         OR_WORD_EXIT_IF_SET(mark_word_addr, (word)1 << modWORDSZ(displ), \
00227                 exit_label); \
00228     } 
00229 # endif /* USE_MARK_BYTES */
00230 
00231 /* If the mark bit corresponding to current is not set, set it, and     */
00232 /* push the contents of the object on the mark stack.  For a small  */
00233 /* object we assume that current is the (possibly interior) pointer */
00234 /* to the object.  For large objects we assume that current points  */
00235 /* to somewhere inside the first page of the object.  If        */
00236 /* GC_all_interior_pointers is set, it may have been previously     */
00237 /* adjusted to make that true.                      */
00238 # define PUSH_CONTENTS_HDR(current, mark_stack_top, mark_stack_limit, \
00239                    source, exit_label, hhdr) \
00240 { \
00241     int displ;  /* Displacement in block; first bytes, then words */ \
00242     int map_entry; \
00243     \
00244     displ = HBLKDISPL(current); \
00245     map_entry = MAP_ENTRY((hhdr -> hb_map), displ); \
00246     displ = BYTES_TO_WORDS(displ); \
00247     if (map_entry > CPP_MAX_OFFSET) { \
00248     if (map_entry == OFFSET_TOO_BIG) { \
00249       map_entry = displ % (hhdr -> hb_sz); \
00250       displ -= map_entry; \
00251       if (displ + (hhdr -> hb_sz) > BYTES_TO_WORDS(HBLKSIZE)) { \
00252         GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); \
00253         goto exit_label; \
00254       } \
00255     } else { \
00256           GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); goto exit_label; \
00257     } \
00258     } else { \
00259         displ -= map_entry; \
00260     } \
00261     GC_ASSERT(displ >= 0 && displ < MARK_BITS_PER_HBLK); \
00262     SET_MARK_BIT_EXIT_IF_SET(hhdr, displ, exit_label); \
00263     GC_STORE_BACK_PTR((ptr_t)source, (ptr_t)HBLKPTR(current) \
00264                       + WORDS_TO_BYTES(displ)); \
00265     PUSH_OBJ(((word *)(HBLKPTR(current)) + displ), hhdr, \
00266              mark_stack_top, mark_stack_limit) \
00267 }
00268 
00269 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
00270 #   define PUSH_ONE_CHECKED_STACK(p, source) \
00271     GC_mark_and_push_stack(p, (ptr_t)(source))
00272 #else
00273 #   define PUSH_ONE_CHECKED_STACK(p, source) \
00274     GC_mark_and_push_stack(p)
00275 #endif
00276 
00277 /*
00278  * Push a single value onto mark stack. Mark from the object pointed to by p.
00279  * Invoke FIXUP_POINTER(p) before any further processing.
00280  * P is considered valid even if it is an interior pointer.
00281  * Previously marked objects are not pushed.  Hence we make progress even
00282  * if the mark stack overflows.
00283  */
00284 
00285 # if NEED_FIXUP_POINTER
00286     /* Try both the raw version and the fixed up one.   */
00287 #   define GC_PUSH_ONE_STACK(p, source) \
00288       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr     \
00289      && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {  \
00290      PUSH_ONE_CHECKED_STACK(p, source); \
00291       } \
00292       FIXUP_POINTER(p); \
00293       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr     \
00294      && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {  \
00295      PUSH_ONE_CHECKED_STACK(p, source); \
00296       }
00297 # else /* !NEED_FIXUP_POINTER */
00298 #   define GC_PUSH_ONE_STACK(p, source) \
00299       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr     \
00300      && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {  \
00301      PUSH_ONE_CHECKED_STACK(p, source); \
00302       }
00303 # endif
00304 
00305 
00306 /*
00307  * As above, but interior pointer recognition as for
00308  * normal for heap pointers.
00309  */
00310 # define GC_PUSH_ONE_HEAP(p,source) \
00311     FIXUP_POINTER(p); \
00312     if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr   \
00313      && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {  \
00314         GC_mark_stack_top = GC_mark_and_push( \
00315                 (GC_PTR)(p), GC_mark_stack_top, \
00316                 GC_mark_stack_limit, (GC_PTR *)(source)); \
00317     }
00318 
00319 /* Mark starting at mark stack entry top (incl.) down to    */
00320 /* mark stack entry bottom (incl.).  Stop after performing  */
00321 /* about one page worth of work.  Return the new mark stack */
00322 /* top entry.                           */
00323 mse * GC_mark_from GC_PROTO((mse * top, mse * bottom, mse *limit));
00324 
00325 #define MARK_FROM_MARK_STACK() \
00326     GC_mark_stack_top = GC_mark_from(GC_mark_stack_top, \
00327                      GC_mark_stack, \
00328                      GC_mark_stack + GC_mark_stack_size);
00329 
00330 /*
00331  * Mark from one finalizable object using the specified
00332  * mark proc. May not mark the object pointed to by 
00333  * real_ptr. That is the job of the caller, if appropriate
00334  */
00335 # define GC_MARK_FO(real_ptr, mark_proc) \
00336 { \
00337     (*(mark_proc))(real_ptr); \
00338     while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK(); \
00339     if (GC_mark_state != MS_NONE) { \
00340         GC_set_mark_bit(real_ptr); \
00341         while (!GC_mark_some((ptr_t)0)) {} \
00342     } \
00343 }
00344 
00345 extern GC_bool GC_mark_stack_too_small;
00346                 /* We need a larger mark stack.  May be */
00347                 /* set by client supplied mark routines.*/
00348 
00349 typedef int mark_state_t;   /* Current state of marking, as follows:*/
00350                 /* Used to remember where we are during */
00351                 /* concurrent marking.          */
00352 
00353                 /* We say something is dirty if it was  */
00354                 /* written since the last time we   */
00355                 /* retrieved dirty bits.  We say it's   */
00356                 /* grungy if it was marked dirty in the */
00357                 /* last set of bits we retrieved.   */
00358                 
00359                 /* Invariant I: all roots and marked    */
00360                 /* objects p are either dirty, or point */
00361                 /* to objects q that are either marked  */
00362                 /* or a pointer to q appears in a range */
00363                 /* on the mark stack.           */
00364 
00365 # define MS_NONE 0      /* No marking in progress. I holds. */
00366                 /* Mark stack is empty.         */
00367 
00368 # define MS_PUSH_RESCUERS 1 /* Rescuing objects are currently   */
00369                 /* being pushed.  I holds, except   */
00370                 /* that grungy roots may point to   */
00371                 /* unmarked objects, as may marked  */
00372                 /* grungy objects above scan_ptr.   */
00373 
00374 # define MS_PUSH_UNCOLLECTABLE 2
00375                 /* I holds, except that marked      */
00376                 /* uncollectable objects above scan_ptr */
00377                 /* may point to unmarked objects.   */
00378                 /* Roots may point to unmarked objects  */
00379 
00380 # define MS_ROOTS_PUSHED 3  /* I holds, mark stack may be nonempty  */
00381 
00382 # define MS_PARTIALLY_INVALID 4 /* I may not hold, e.g. because of M.S. */
00383                 /* overflow.  However marked heap   */
00384                 /* objects below scan_ptr point to  */
00385                 /* marked or stacked objects.       */
00386 
00387 # define MS_INVALID 5       /* I may not hold.          */
00388 
00389 extern mark_state_t GC_mark_state;
00390 
00391 #endif  /* GC_PMARK_H */
00392