OSDN Git Service

* backgraph.c, gc_priv.h (GC_traverse_back_graph,
[pf3gnuchains/gcc-fork.git] / boehm-gc / include / private / gc_pmark.h
1 /*
2  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
3  * Copyright (c) 2001 by Hewlett-Packard Company. All rights reserved.
4  *
5  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
7  *
8  * Permission is hereby granted to use or copy this program
9  * for any purpose,  provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  *
14  */
15
16 /* Private declarations of GC marker data structures and macros */
17
18 /*
19  * Declarations of mark stack.  Needed by marker and client supplied mark
20  * routines.  Transitively include gc_priv.h.
21  * (Note that gc_priv.h should not be included before this, since this
22  * includes dbg_mlc.h, which wants to include gc_priv.h AFTER defining
23  * I_HIDE_POINTERS.)
24  */
25 #ifndef GC_PMARK_H
26 # define GC_PMARK_H
27
28 # include "gc.h"        /* For configuration */
29
30 # if defined(KEEP_BACK_PTRS) || defined(PRINT_BLACK_LIST)
31 #   include "dbg_mlc.h"
32 # endif
33 # ifndef GC_MARK_H
34 #   include "../gc_mark.h"
35 # endif
36 # ifndef GC_PRIVATE_H
37 #   include "gc_priv.h"
38 # endif
39
40 /* The real declarations of the following is in gc_priv.h, so that      */
41 /* we can avoid scanning the following table.                           */
42 /*
43 extern mark_proc GC_mark_procs[MAX_MARK_PROCS];
44 */
45
46 /*
47  * Mark descriptor stuff that should remain private for now, mostly
48  * because it's hard to export WORDSZ without including gcconfig.h.
49  */
50 # define BITMAP_BITS (WORDSZ - GC_DS_TAG_BITS)
51 # define PROC(descr) \
52         (GC_mark_procs[((descr) >> GC_DS_TAG_BITS) & (GC_MAX_MARK_PROCS-1)])
53 # define ENV(descr) \
54         ((descr) >> (GC_DS_TAG_BITS + GC_LOG_MAX_MARK_PROCS))
55 # define MAX_ENV \
56         (((word)1 << (WORDSZ - GC_DS_TAG_BITS - GC_LOG_MAX_MARK_PROCS)) - 1)
57
58
59 extern word GC_n_mark_procs;
60
61 /* Number of mark stack entries to discard on overflow. */
62 #define GC_MARK_STACK_DISCARDS (INITIAL_MARK_STACK_SIZE/8)
63
64 typedef struct GC_ms_entry {
65     GC_word * mse_start;   /* First word of object */
66     GC_word mse_descr;  /* Descriptor; low order two bits are tags,     */
67                         /* identifying the upper 30 bits as one of the  */
68                         /* following:                                   */
69 } mse;
70
71 extern word GC_mark_stack_size;
72
73 extern mse * GC_mark_stack_limit;
74
75 #ifdef PARALLEL_MARK
76   extern mse * VOLATILE GC_mark_stack_top;
77 #else
78   extern mse * GC_mark_stack_top;
79 #endif
80
81 extern mse * GC_mark_stack;
82
83 #ifdef PARALLEL_MARK
84     /*
85      * Allow multiple threads to participate in the marking process.
86      * This works roughly as follows:
87      *  The main mark stack never shrinks, but it can grow.
88      *
89      *  The initiating threads holds the GC lock, and sets GC_help_wanted.
90      *  
91      *  Other threads:
92      *     1) update helper_count (while holding mark_lock.)
93      *     2) allocate a local mark stack
94      *     repeatedly:
95      *          3) Steal a global mark stack entry by atomically replacing
96      *             its descriptor with 0.
97      *          4) Copy it to the local stack.
98      *          5) Mark on the local stack until it is empty, or
99      *             it may be profitable to copy it back.
100      *          6) If necessary, copy local stack to global one,
101      *             holding mark lock.
102      *    7) Stop when the global mark stack is empty.
103      *    8) decrement helper_count (holding mark_lock).
104      *
105      * This is an experiment to see if we can do something along the lines
106      * of the University of Tokyo SGC in a less intrusive, though probably
107      * also less performant, way.
108      */
109     void GC_do_parallel_mark();
110                 /* inititate parallel marking.  */
111
112     extern GC_bool GC_help_wanted;      /* Protected by mark lock       */
113     extern unsigned GC_helper_count;    /* Number of running helpers.   */
114                                         /* Protected by mark lock       */
115     extern unsigned GC_active_count;    /* Number of active helpers.    */
116                                         /* Protected by mark lock       */
117                                         /* May increase and decrease    */
118                                         /* within each mark cycle.  But */
119                                         /* once it returns to 0, it     */
120                                         /* stays zero for the cycle.    */
121     /* GC_mark_stack_top is also protected by mark lock.        */
122     extern mse * VOLATILE GC_first_nonempty;
123                                         /* Lowest entry on mark stack   */
124                                         /* that may be nonempty.        */
125                                         /* Updated only by initiating   */
126                                         /* thread.                      */
127     /*
128      * GC_notify_all_marker() is used when GC_help_wanted is first set,
129      * when the last helper becomes inactive,
130      * when something is added to the global mark stack, and just after
131      * GC_mark_no is incremented.
132      * This could be split into multiple CVs (and probably should be to
133      * scale to really large numbers of processors.)
134      */
135 #endif /* PARALLEL_MARK */
136
137 /* Return a pointer to within 1st page of object.       */
138 /* Set *new_hdr_p to corr. hdr.                         */
139 #ifdef __STDC__
140   ptr_t GC_find_start(ptr_t current, hdr *hhdr, hdr **new_hdr_p);
141 #else
142   ptr_t GC_find_start();
143 #endif
144
145 mse * GC_signal_mark_stack_overflow GC_PROTO((mse *msp));
146
147 # ifdef GATHERSTATS
148 #   define ADD_TO_ATOMIC(sz) GC_atomic_in_use += (sz)
149 #   define ADD_TO_COMPOSITE(sz) GC_composite_in_use += (sz)
150 # else
151 #   define ADD_TO_ATOMIC(sz)
152 #   define ADD_TO_COMPOSITE(sz)
153 # endif
154
155 /* Push the object obj with corresponding heap block header hhdr onto   */
156 /* the mark stack.                                                      */
157 # define PUSH_OBJ(obj, hhdr, mark_stack_top, mark_stack_limit) \
158 { \
159     register word _descr = (hhdr) -> hb_descr; \
160         \
161     if (_descr == 0) { \
162         ADD_TO_ATOMIC((hhdr) -> hb_sz); \
163     } else { \
164         ADD_TO_COMPOSITE((hhdr) -> hb_sz); \
165         mark_stack_top++; \
166         if (mark_stack_top >= mark_stack_limit) { \
167           mark_stack_top = GC_signal_mark_stack_overflow(mark_stack_top); \
168         } \
169         mark_stack_top -> mse_start = (obj); \
170         mark_stack_top -> mse_descr = _descr; \
171     } \
172 }
173
174 /* Push the contents of current onto the mark stack if it is a valid    */
175 /* ptr to a currently unmarked object.  Mark it.                        */
176 /* If we assumed a standard-conforming compiler, we could probably      */
177 /* generate the exit_label transparently.                               */
178 # define PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
179                        source, exit_label) \
180 { \
181     hdr * my_hhdr; \
182     ptr_t my_current = current; \
183  \
184     GET_HDR(my_current, my_hhdr); \
185     if (IS_FORWARDING_ADDR_OR_NIL(my_hhdr)) { \
186          hdr * new_hdr = GC_invalid_header; \
187          my_current = GC_find_start(my_current, my_hhdr, &new_hdr); \
188          my_hhdr = new_hdr; \
189     } \
190     PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
191                   source, exit_label, my_hhdr); \
192 exit_label: ; \
193 }
194
195 /* As above, but use header cache for header lookup.    */
196 # define HC_PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
197                        source, exit_label) \
198 { \
199     hdr * my_hhdr; \
200     ptr_t my_current = current; \
201  \
202     HC_GET_HDR(my_current, my_hhdr, source); \
203     PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
204                   source, exit_label, my_hhdr); \
205 exit_label: ; \
206 }
207
208 /* Set mark bit, exit if it was already set.    */
209
210 # ifdef USE_MARK_BYTES
211     /* Unlike the mark bit case, there is a race here, and we may set   */
212     /* the bit twice in the concurrent case.  This can result in the    */
213     /* object being pushed twice.  But that's only a performance issue. */
214 #   define SET_MARK_BIT_EXIT_IF_SET(hhdr,displ,exit_label) \
215     { \
216         register VOLATILE char * mark_byte_addr = \
217                                 hhdr -> hb_marks + ((displ) >> 1); \
218         register char mark_byte = *mark_byte_addr; \
219           \
220         if (mark_byte) goto exit_label; \
221         *mark_byte_addr = 1;  \
222     } 
223 # else
224 #   define SET_MARK_BIT_EXIT_IF_SET(hhdr,displ,exit_label) \
225     { \
226         register word * mark_word_addr = hhdr -> hb_marks + divWORDSZ(displ); \
227           \
228         OR_WORD_EXIT_IF_SET(mark_word_addr, (word)1 << modWORDSZ(displ), \
229                             exit_label); \
230     } 
231 # endif /* USE_MARK_BYTES */
232
233 /* If the mark bit corresponding to current is not set, set it, and     */
234 /* push the contents of the object on the mark stack.  For a small      */
235 /* object we assume that current is the (possibly interior) pointer     */
236 /* to the object.  For large objects we assume that current points      */
237 /* to somewhere inside the first page of the object.  If                */
238 /* GC_all_interior_pointers is set, it may have been previously         */
239 /* adjusted to make that true.                                          */
240 # define PUSH_CONTENTS_HDR(current, mark_stack_top, mark_stack_limit, \
241                            source, exit_label, hhdr) \
242 { \
243     int displ;  /* Displacement in block; first bytes, then words */ \
244     int map_entry; \
245     \
246     displ = HBLKDISPL(current); \
247     map_entry = MAP_ENTRY((hhdr -> hb_map), displ); \
248     displ = BYTES_TO_WORDS(displ); \
249     if (map_entry > CPP_MAX_OFFSET) { \
250         if (map_entry == OFFSET_TOO_BIG) { \
251           map_entry = displ % (hhdr -> hb_sz); \
252           displ -= map_entry; \
253           if (displ + (hhdr -> hb_sz) > BYTES_TO_WORDS(HBLKSIZE)) { \
254             GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); \
255             goto exit_label; \
256           } \
257         } else { \
258           GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); goto exit_label; \
259         } \
260     } else { \
261         displ -= map_entry; \
262     } \
263     GC_ASSERT(displ >= 0 && displ < MARK_BITS_PER_HBLK); \
264     SET_MARK_BIT_EXIT_IF_SET(hhdr, displ, exit_label); \
265     GC_STORE_BACK_PTR((ptr_t)source, (ptr_t)HBLKPTR(current) \
266                                       + WORDS_TO_BYTES(displ)); \
267     PUSH_OBJ(((word *)(HBLKPTR(current)) + displ), hhdr, \
268              mark_stack_top, mark_stack_limit) \
269 }
270
271 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
272 #   define PUSH_ONE_CHECKED_STACK(p, source) \
273         GC_mark_and_push_stack(p, (ptr_t)(source))
274 #else
275 #   define PUSH_ONE_CHECKED_STACK(p, source) \
276         GC_mark_and_push_stack(p)
277 #endif
278
279 /*
280  * Push a single value onto mark stack. Mark from the object pointed to by p.
281  * Invoke FIXUP_POINTER(p) before any further processing.
282  * P is considered valid even if it is an interior pointer.
283  * Previously marked objects are not pushed.  Hence we make progress even
284  * if the mark stack overflows.
285  */
286
287 # if NEED_FIXUP_POINTER
288     /* Try both the raw version and the fixed up one.   */
289 #   define GC_PUSH_ONE_STACK(p, source) \
290       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr     \
291          && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {      \
292          PUSH_ONE_CHECKED_STACK(p, source);     \
293       } \
294       FIXUP_POINTER(p); \
295       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr     \
296          && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {      \
297          PUSH_ONE_CHECKED_STACK(p, source);     \
298       }
299 # else /* !NEED_FIXUP_POINTER */
300 #   define GC_PUSH_ONE_STACK(p, source) \
301       if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr     \
302          && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {      \
303          PUSH_ONE_CHECKED_STACK(p, source);     \
304       }
305 # endif
306
307
308 /*
309  * As above, but interior pointer recognition as for
310  * normal for heap pointers.
311  */
312 # define GC_PUSH_ONE_HEAP(p,source) \
313     FIXUP_POINTER(p); \
314     if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr       \
315          && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) {      \
316             GC_mark_stack_top = GC_mark_and_push( \
317                             (GC_PTR)(p), GC_mark_stack_top, \
318                             GC_mark_stack_limit, (GC_PTR *)(source)); \
319     }
320
321 /* Mark starting at mark stack entry top (incl.) down to        */
322 /* mark stack entry bottom (incl.).  Stop after performing      */
323 /* about one page worth of work.  Return the new mark stack     */
324 /* top entry.                                                   */
325 mse * GC_mark_from GC_PROTO((mse * top, mse * bottom, mse *limit));
326
327 #define MARK_FROM_MARK_STACK() \
328         GC_mark_stack_top = GC_mark_from(GC_mark_stack_top, \
329                                          GC_mark_stack, \
330                                          GC_mark_stack + GC_mark_stack_size);
331
332 /*
333  * Mark from one finalizable object using the specified
334  * mark proc. May not mark the object pointed to by 
335  * real_ptr. That is the job of the caller, if appropriate
336  */
337 # define GC_MARK_FO(real_ptr, mark_proc) \
338 { \
339     (*(mark_proc))(real_ptr); \
340     while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK(); \
341     if (GC_mark_state != MS_NONE) { \
342         GC_set_mark_bit(real_ptr); \
343         while (!GC_mark_some((ptr_t)0)) {} \
344     } \
345 }
346
347 extern GC_bool GC_mark_stack_too_small;
348                                 /* We need a larger mark stack.  May be */
349                                 /* set by client supplied mark routines.*/
350
351 typedef int mark_state_t;       /* Current state of marking, as follows:*/
352                                 /* Used to remember where we are during */
353                                 /* concurrent marking.                  */
354
355                                 /* We say something is dirty if it was  */
356                                 /* written since the last time we       */
357                                 /* retrieved dirty bits.  We say it's   */
358                                 /* grungy if it was marked dirty in the */
359                                 /* last set of bits we retrieved.       */
360                                 
361                                 /* Invariant I: all roots and marked    */
362                                 /* objects p are either dirty, or point */
363                                 /* to objects q that are either marked  */
364                                 /* or a pointer to q appears in a range */
365                                 /* on the mark stack.                   */
366
367 # define MS_NONE 0              /* No marking in progress. I holds.     */
368                                 /* Mark stack is empty.                 */
369
370 # define MS_PUSH_RESCUERS 1     /* Rescuing objects are currently       */
371                                 /* being pushed.  I holds, except       */
372                                 /* that grungy roots may point to       */
373                                 /* unmarked objects, as may marked      */
374                                 /* grungy objects above scan_ptr.       */
375
376 # define MS_PUSH_UNCOLLECTABLE 2
377                                 /* I holds, except that marked          */
378                                 /* uncollectable objects above scan_ptr */
379                                 /* may point to unmarked objects.       */
380                                 /* Roots may point to unmarked objects  */
381
382 # define MS_ROOTS_PUSHED 3      /* I holds, mark stack may be nonempty  */
383
384 # define MS_PARTIALLY_INVALID 4 /* I may not hold, e.g. because of M.S. */
385                                 /* overflow.  However marked heap       */
386                                 /* objects below scan_ptr point to      */
387                                 /* marked or stacked objects.           */
388
389 # define MS_INVALID 5           /* I may not hold.                      */
390
391 extern mark_state_t GC_mark_state;
392
393 #endif  /* GC_PMARK_H */
394