2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 2001 by Hewlett-Packard Company. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
16 /* Private declarations of GC marker data structures and macros */
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
28 # if defined(KEEP_BACK_PTRS) || defined(PRINT_BLACK_LIST)
32 # include "../gc_mark.h"
38 /* The real declarations of the following is in gc_priv.h, so that */
39 /* we can avoid scanning the following table. */
41 extern mark_proc GC_mark_procs[MAX_MARK_PROCS];
45 * Mark descriptor stuff that should remain private for now, mostly
46 * because it's hard to export WORDSZ without including gcconfig.h.
48 # define BITMAP_BITS (WORDSZ - GC_DS_TAG_BITS)
49 # define PROC(descr) \
50 (GC_mark_procs[((descr) >> GC_DS_TAG_BITS) & (GC_MAX_MARK_PROCS-1)])
52 ((descr) >> (GC_DS_TAG_BITS + GC_LOG_MAX_MARK_PROCS))
54 (((word)1 << (WORDSZ - GC_DS_TAG_BITS - GC_LOG_MAX_MARK_PROCS)) - 1)
57 extern word GC_n_mark_procs;
59 /* Number of mark stack entries to discard on overflow. */
60 #define GC_MARK_STACK_DISCARDS (INITIAL_MARK_STACK_SIZE/8)
62 typedef struct GC_ms_entry {
63 GC_word * mse_start; /* First word of object */
64 GC_word mse_descr; /* Descriptor; low order two bits are tags, */
65 /* identifying the upper 30 bits as one of the */
69 extern word GC_mark_stack_size;
71 extern mse * GC_mark_stack_limit;
74 extern mse * VOLATILE GC_mark_stack_top;
76 extern mse * GC_mark_stack_top;
79 extern mse * GC_mark_stack;
83 * Allow multiple threads to participate in the marking process.
84 * This works roughly as follows:
85 * The main mark stack never shrinks, but it can grow.
87 * The initiating threads holds the GC lock, and sets GC_help_wanted.
90 * 1) update helper_count (while holding mark_lock.)
91 * 2) allocate a local mark stack
93 * 3) Steal a global mark stack entry by atomically replacing
94 * its descriptor with 0.
95 * 4) Copy it to the local stack.
96 * 5) Mark on the local stack until it is empty, or
97 * it may be profitable to copy it back.
98 * 6) If necessary, copy local stack to global one,
100 * 7) Stop when the global mark stack is empty.
101 * 8) decrement helper_count (holding mark_lock).
103 * This is an experiment to see if we can do something along the lines
104 * of the University of Tokyo SGC in a less intrusive, though probably
105 * also less performant, way.
107 void GC_do_parallel_mark();
108 /* inititate parallel marking. */
110 extern GC_bool GC_help_wanted; /* Protected by mark lock */
111 extern unsigned GC_helper_count; /* Number of running helpers. */
112 /* Protected by mark lock */
113 extern unsigned GC_active_count; /* Number of active helpers. */
114 /* Protected by mark lock */
115 /* May increase and decrease */
116 /* within each mark cycle. But */
117 /* once it returns to 0, it */
118 /* stays zero for the cycle. */
119 /* GC_mark_stack_top is also protected by mark lock. */
120 extern mse * VOLATILE GC_first_nonempty;
121 /* Lowest entry on mark stack */
122 /* that may be nonempty. */
123 /* Updated only by initiating */
126 * GC_notify_all_marker() is used when GC_help_wanted is first set,
127 * when the last helper becomes inactive,
128 * when something is added to the global mark stack, and just after
129 * GC_mark_no is incremented.
130 * This could be split into multiple CVs (and probably should be to
131 * scale to really large numbers of processors.)
133 #endif /* PARALLEL_MARK */
135 /* Return a pointer to within 1st page of object. */
136 /* Set *new_hdr_p to corr. hdr. */
138 ptr_t GC_find_start(ptr_t current, hdr *hhdr, hdr **new_hdr_p);
140 ptr_t GC_find_start();
143 mse * GC_signal_mark_stack_overflow GC_PROTO((mse *msp));
146 # define ADD_TO_ATOMIC(sz) GC_atomic_in_use += (sz)
147 # define ADD_TO_COMPOSITE(sz) GC_composite_in_use += (sz)
149 # define ADD_TO_ATOMIC(sz)
150 # define ADD_TO_COMPOSITE(sz)
153 /* Push the object obj with corresponding heap block header hhdr onto */
154 /* the mark stack. */
155 # define PUSH_OBJ(obj, hhdr, mark_stack_top, mark_stack_limit) \
157 register word _descr = (hhdr) -> hb_descr; \
160 ADD_TO_ATOMIC((hhdr) -> hb_sz); \
162 ADD_TO_COMPOSITE((hhdr) -> hb_sz); \
164 if (mark_stack_top >= mark_stack_limit) { \
165 mark_stack_top = GC_signal_mark_stack_overflow(mark_stack_top); \
167 mark_stack_top -> mse_start = (obj); \
168 mark_stack_top -> mse_descr = _descr; \
172 /* Push the contents of current onto the mark stack if it is a valid */
173 /* ptr to a currently unmarked object. Mark it. */
174 /* If we assumed a standard-conforming compiler, we could probably */
175 /* generate the exit_label transparently. */
176 # define PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
177 source, exit_label) \
180 ptr_t my_current = current; \
182 GET_HDR(my_current, my_hhdr); \
183 if (IS_FORWARDING_ADDR_OR_NIL(my_hhdr)) { \
184 hdr * new_hdr = GC_invalid_header; \
185 my_current = GC_find_start(my_current, my_hhdr, &new_hdr); \
188 PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
189 source, exit_label, my_hhdr); \
193 /* As above, but use header cache for header lookup. */
194 # define HC_PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
195 source, exit_label) \
198 ptr_t my_current = current; \
200 HC_GET_HDR(my_current, my_hhdr, source); \
201 PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
202 source, exit_label, my_hhdr); \
206 /* Set mark bit, exit if it was already set. */
208 # ifdef USE_MARK_BYTES
209 /* Unlike the mark bit case, there is a race here, and we may set */
210 /* the bit twice in the concurrent case. This can result in the */
211 /* object being pushed twice. But that's only a performance issue. */
212 # define SET_MARK_BIT_EXIT_IF_SET(hhdr,displ,exit_label) \
214 register VOLATILE char * mark_byte_addr = \
215 hhdr -> hb_marks + ((displ) >> 1); \
216 register char mark_byte = *mark_byte_addr; \
218 if (mark_byte) goto exit_label; \
219 *mark_byte_addr = 1; \
222 # define SET_MARK_BIT_EXIT_IF_SET(hhdr,displ,exit_label) \
224 register word * mark_word_addr = hhdr -> hb_marks + divWORDSZ(displ); \
226 OR_WORD_EXIT_IF_SET(mark_word_addr, (word)1 << modWORDSZ(displ), \
229 # endif /* USE_MARK_BYTES */
231 /* If the mark bit corresponding to current is not set, set it, and */
232 /* push the contents of the object on the mark stack. For a small */
233 /* object we assume that current is the (possibly interior) pointer */
234 /* to the object. For large objects we assume that current points */
235 /* to somewhere inside the first page of the object. If */
236 /* GC_all_interior_pointers is set, it may have been previously */
237 /* adjusted to make that true. */
238 # define PUSH_CONTENTS_HDR(current, mark_stack_top, mark_stack_limit, \
239 source, exit_label, hhdr) \
241 int displ; /* Displacement in block; first bytes, then words */ \
244 displ = HBLKDISPL(current); \
245 map_entry = MAP_ENTRY((hhdr -> hb_map), displ); \
246 displ = BYTES_TO_WORDS(displ); \
247 if (map_entry > CPP_MAX_OFFSET) { \
248 if (map_entry == OFFSET_TOO_BIG) { \
249 map_entry = displ % (hhdr -> hb_sz); \
250 displ -= map_entry; \
251 if (displ + (hhdr -> hb_sz) > BYTES_TO_WORDS(HBLKSIZE) \
253 GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); \
257 GC_ADD_TO_BLACK_LIST_NORMAL((word)current, source); goto exit_label; \
260 displ -= map_entry; \
262 GC_ASSERT(displ >= 0 && displ < MARK_BITS_PER_HBLK); \
263 SET_MARK_BIT_EXIT_IF_SET(hhdr, displ, exit_label); \
264 GC_STORE_BACK_PTR((ptr_t)source, (ptr_t)HBLKPTR(current) \
265 + WORDS_TO_BYTES(displ)); \
266 PUSH_OBJ(((word *)(HBLKPTR(current)) + displ), hhdr, \
267 mark_stack_top, mark_stack_limit) \
270 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
271 # define PUSH_ONE_CHECKED_STACK(p, source) \
272 GC_mark_and_push_stack(p, (ptr_t)(source))
274 # define PUSH_ONE_CHECKED_STACK(p, source) \
275 GC_mark_and_push_stack(p)
279 * Push a single value onto mark stack. Mark from the object pointed to by p.
280 * Invoke FIXUP_POINTER(p) before any further processing.
281 * P is considered valid even if it is an interior pointer.
282 * Previously marked objects are not pushed. Hence we make progress even
283 * if the mark stack overflows.
286 # if NEED_FIXUP_POINTER
287 /* Try both the raw version and the fixed up one. */
288 # define GC_PUSH_ONE_STACK(p, source) \
289 if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr \
290 && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) { \
291 PUSH_ONE_CHECKED_STACK(p, source); \
294 if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr \
295 && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) { \
296 PUSH_ONE_CHECKED_STACK(p, source); \
298 # else /* !NEED_FIXUP_POINTER */
299 # define GC_PUSH_ONE_STACK(p, source) \
300 if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr \
301 && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) { \
302 PUSH_ONE_CHECKED_STACK(p, source); \
308 * As above, but interior pointer recognition as for
309 * normal for heap pointers.
311 # define GC_PUSH_ONE_HEAP(p,source) \
313 if ((ptr_t)(p) >= (ptr_t)GC_least_plausible_heap_addr \
314 && (ptr_t)(p) < (ptr_t)GC_greatest_plausible_heap_addr) { \
315 GC_mark_stack_top = GC_mark_and_push( \
316 (GC_PTR)(p), GC_mark_stack_top, \
317 GC_mark_stack_limit, (GC_PTR *)(source)); \
320 /* Mark starting at mark stack entry top (incl.) down to */
321 /* mark stack entry bottom (incl.). Stop after performing */
322 /* about one page worth of work. Return the new mark stack */
324 mse * GC_mark_from GC_PROTO((mse * top, mse * bottom, mse *limit));
326 #define MARK_FROM_MARK_STACK() \
327 GC_mark_stack_top = GC_mark_from(GC_mark_stack_top, \
329 GC_mark_stack + GC_mark_stack_size);
332 * Mark from one finalizable object using the specified
333 * mark proc. May not mark the object pointed to by
334 * real_ptr. That is the job of the caller, if appropriate
336 # define GC_MARK_FO(real_ptr, mark_proc) \
338 (*(mark_proc))(real_ptr); \
339 while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK(); \
340 if (GC_mark_state != MS_NONE) { \
341 GC_set_mark_bit(real_ptr); \
342 while (!GC_mark_some((ptr_t)0)) {} \
346 extern GC_bool GC_mark_stack_too_small;
347 /* We need a larger mark stack. May be */
348 /* set by client supplied mark routines.*/
350 typedef int mark_state_t; /* Current state of marking, as follows:*/
351 /* Used to remember where we are during */
352 /* concurrent marking. */
354 /* We say something is dirty if it was */
355 /* written since the last time we */
356 /* retrieved dirty bits. We say it's */
357 /* grungy if it was marked dirty in the */
358 /* last set of bits we retrieved. */
360 /* Invariant I: all roots and marked */
361 /* objects p are either dirty, or point */
362 /* to objects q that are either marked */
363 /* or a pointer to q appears in a range */
364 /* on the mark stack. */
366 # define MS_NONE 0 /* No marking in progress. I holds. */
367 /* Mark stack is empty. */
369 # define MS_PUSH_RESCUERS 1 /* Rescuing objects are currently */
370 /* being pushed. I holds, except */
371 /* that grungy roots may point to */
372 /* unmarked objects, as may marked */
373 /* grungy objects above scan_ptr. */
375 # define MS_PUSH_UNCOLLECTABLE 2
376 /* I holds, except that marked */
377 /* uncollectable objects above scan_ptr */
378 /* may point to unmarked objects. */
379 /* Roots may point to unmarked objects */
381 # define MS_ROOTS_PUSHED 3 /* I holds, mark stack may be nonempty */
383 # define MS_PARTIALLY_INVALID 4 /* I may not hold, e.g. because of M.S. */
384 /* overflow. However marked heap */
385 /* objects below scan_ptr point to */
386 /* marked or stacked objects. */
388 # define MS_INVALID 5 /* I may not hold. */
390 extern mark_state_t GC_mark_state;
392 #endif /* GC_PMARK_H */