Back to index

plt-scheme  4.2.1
gc_pmark.h
Go to the documentation of this file.
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              && displ != 0) { \
00253            GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); \
00254            goto exit_label; \
00255          } \
00256        } else { \
00257           GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); goto exit_label; \
00258        } \
00259     } else { \
00260         displ -= map_entry; \
00261     } \
00262     GC_ASSERT(displ >= 0 && displ < MARK_BITS_PER_HBLK); \
00263     SET_MARK_BIT_EXIT_IF_SET(hhdr, displ, exit_label); \
00264     GC_STORE_BACK_PTR((ptr_t)source, (ptr_t)HBLKPTR(current) \
00265                                   + WORDS_TO_BYTES(displ)); \
00266     PUSH_OBJ(((word *)(HBLKPTR(current)) + displ), hhdr, \
00267             mark_stack_top, mark_stack_limit) \
00268 }
00269 
00270 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
00271 #   define PUSH_ONE_CHECKED_STACK(p, source) \
00272        GC_mark_and_push_stack(p, (ptr_t)(source))
00273 #else
00274 #   define PUSH_ONE_CHECKED_STACK(p, source) \
00275        GC_mark_and_push_stack(p)
00276 #endif
00277 
00278 /*
00279  * Push a single value onto mark stack. Mark from the object pointed to by p.
00280  * Invoke FIXUP_POINTER(p) before any further processing.
00281  * P is considered valid even if it is an interior pointer.
00282  * Previously marked objects are not pushed.  Hence we make progress even
00283  * if the mark stack overflows.
00284  */
00285 
00286 # if NEED_FIXUP_POINTER
00287     /* Try both the raw version and the fixed up one.   */
00288 #   define GC_PUSH_ONE_STACK(p, source) \
00289       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr    \
00290         && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {     \
00291         PUSH_ONE_CHECKED_STACK(p, source);       \
00292       } \
00293       FIXUP_POINTER(p); \
00294       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr    \
00295         && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {     \
00296         PUSH_ONE_CHECKED_STACK(p, source);       \
00297       }
00298 # else /* !NEED_FIXUP_POINTER */
00299 #   define GC_PUSH_ONE_STACK(p, source) \
00300       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr    \
00301         && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {     \
00302         PUSH_ONE_CHECKED_STACK(p, source);       \
00303       }
00304 # endif
00305 
00306 
00307 /*
00308  * As above, but interior pointer recognition as for
00309  * normal for heap pointers.
00310  */
00311 # define GC_PUSH_ONE_HEAP(p,source) \
00312     FIXUP_POINTER(p); \
00313     if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr      \
00314         && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {     \
00315            GC_mark_stack_top = GC_mark_and_push( \
00316                          (GC_PTR)(p), GC_mark_stack_top, \
00317                          GC_mark_stack_limit, (GC_PTR *)(source)); \
00318     }
00319 
00320 /* Mark starting at mark stack entry top (incl.) down to       */
00321 /* mark stack entry bottom (incl.).  Stop after performing     */
00322 /* about one page worth of work.  Return the new mark stack    */
00323 /* top entry.                                           */
00324 mse * GC_mark_from GC_PROTO((mse * top, mse * bottom, mse *limit));
00325 
00326 #define MARK_FROM_MARK_STACK() \
00327        GC_mark_stack_top = GC_mark_from(GC_mark_stack_top, \
00328                                     GC_mark_stack, \
00329                                     GC_mark_stack + GC_mark_stack_size);
00330 
00331 /*
00332  * Mark from one finalizable object using the specified
00333  * mark proc. May not mark the object pointed to by 
00334  * real_ptr. That is the job of the caller, if appropriate
00335  */
00336 # define GC_MARK_FO(real_ptr, mark_proc) \
00337 { \
00338     (*(mark_proc))(real_ptr); \
00339     while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK(); \
00340     if (GC_mark_state != MS_NONE) { \
00341         GC_set_mark_bit(real_ptr); \
00342         while (!GC_mark_some((ptr_t)0)) {} \
00343     } \
00344 }
00345 
00346 extern GC_bool GC_mark_stack_too_small;
00347                             /* We need a larger mark stack.  May be   */
00348                             /* set by client supplied mark routines.*/
00349 
00350 typedef int mark_state_t;   /* Current state of marking, as follows:*/
00351                             /* Used to remember where we are during */
00352                             /* concurrent marking.                    */
00353 
00354                             /* We say something is dirty if it was    */
00355                             /* written since the last time we  */
00356                             /* retrieved dirty bits.  We say it's     */
00357                             /* grungy if it was marked dirty in the   */
00358                             /* last set of bits we retrieved.  */
00359                             
00360                             /* Invariant I: all roots and marked      */
00361                             /* objects p are either dirty, or point */
00362                             /* to objects q that are either marked    */
00363                             /* or a pointer to q appears in a range   */
00364                             /* on the mark stack.                     */
00365 
00366 # define MS_NONE 0          /* No marking in progress. I holds.       */
00367                             /* Mark stack is empty.                   */
00368 
00369 # define MS_PUSH_RESCUERS 1 /* Rescuing objects are currently  */
00370                             /* being pushed.  I holds, except  */
00371                             /* that grungy roots may point to  */
00372                             /* unmarked objects, as may marked */
00373                             /* grungy objects above scan_ptr.  */
00374 
00375 # define MS_PUSH_UNCOLLECTABLE 2
00376                             /* I holds, except that marked            */
00377                             /* uncollectable objects above scan_ptr */
00378                             /* may point to unmarked objects.  */
00379                             /* Roots may point to unmarked objects    */
00380 
00381 # define MS_ROOTS_PUSHED 3  /* I holds, mark stack may be nonempty  */
00382 
00383 # define MS_PARTIALLY_INVALID 4    /* I may not hold, e.g. because of M.S. */
00384                             /* overflow.  However marked heap  */
00385                             /* objects below scan_ptr point to */
00386                             /* marked or stacked objects.             */
00387 
00388 # define MS_INVALID 5              /* I may not hold.                 */
00389 
00390 extern mark_state_t GC_mark_state;
00391 
00392 #endif  /* GC_PMARK_H */
00393