Debian Codegraph @

gc/include/private/gc_pmark.h

raw
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 */
22 #ifndef GC_PMARK_H
23 #define GC_PMARK_H
24
25 #if defined(HAVE_CONFIG_H) && !defined(GC_PRIVATE_H)
26 /* When gc_pmark.h is included from gc_priv.h, some of macros might */
27 /* be undefined in gcconfig.h, so skip config.h in this case. */
28 # include "config.h"
29 #endif
30
31 #ifndef GC_BUILD
32 # define GC_BUILD
33 #endif
34
35 #if (defined(__linux__) || defined(__GLIBC__) || defined(__GNU__)) \
36 && !defined(_GNU_SOURCE) && defined(GC_PTHREADS) \
37 && !defined(GC_NO_PTHREAD_SIGMASK)
38 # define _GNU_SOURCE 1
39 #endif
40
41 #if defined(KEEP_BACK_PTRS) || defined(PRINT_BLACK_LIST)
42 # include "dbg_mlc.h"
43 #endif
44
45 #include "../gc_mark.h"
46 #include "gc_priv.h"
47
48 EXTERN_C_BEGIN
49
50 /* The real declarations of the following is in gc_priv.h, so that */
51 /* we can avoid scanning the following table. */
52 /*
53 mark_proc GC_mark_procs[MAX_MARK_PROCS];
54 */
55
56 #ifndef MARK_DESCR_OFFSET
57 # define MARK_DESCR_OFFSET sizeof(word)
58 #endif
59
60 /*
61 * Mark descriptor stuff that should remain private for now, mostly
62 * because it's hard to export WORDSZ without including gcconfig.h.
63 */
64 #define BITMAP_BITS (WORDSZ - GC_DS_TAG_BITS)
65 #define PROC(descr) \
66 (GC_mark_procs[((descr) >> GC_DS_TAG_BITS) & (GC_MAX_MARK_PROCS-1)])
67 #define ENV(descr) \
68 ((descr) >> (GC_DS_TAG_BITS + GC_LOG_MAX_MARK_PROCS))
69 #define MAX_ENV \
70 (((word)1 << (WORDSZ - GC_DS_TAG_BITS - GC_LOG_MAX_MARK_PROCS)) - 1)
71
72 GC_EXTERN unsigned GC_n_mark_procs;
73
74 /* Number of mark stack entries to discard on overflow. */
75 #define GC_MARK_STACK_DISCARDS (INITIAL_MARK_STACK_SIZE/8)
76
77 #ifdef PARALLEL_MARK
78 /*
79 * Allow multiple threads to participate in the marking process.
80 * This works roughly as follows:
81 * The main mark stack never shrinks, but it can grow.
82 *
83 * The initiating threads holds the GC lock, and sets GC_help_wanted.
84 *
85 * Other threads:
86 * 1) update helper_count (while holding the mark lock).
87 * 2) allocate a local mark stack
88 * repeatedly:
89 * 3) Steal a global mark stack entry by atomically replacing
90 * its descriptor with 0.
91 * 4) Copy it to the local stack.
92 * 5) Mark on the local stack until it is empty, or
93 * it may be profitable to copy it back.
94 * 6) If necessary, copy local stack to global one,
95 * holding mark lock.
96 * 7) Stop when the global mark stack is empty.
97 * 8) decrement helper_count (holding the mark lock).
98 *
99 * This is an experiment to see if we can do something along the lines
100 * of the University of Tokyo SGC in a less intrusive, though probably
101 * also less performant, way.
102 */
103
104 /* GC_mark_stack_top is protected by mark lock. */
105
106 /*
107 * GC_notify_all_marker() is used when GC_help_wanted is first set,
108 * when the last helper becomes inactive,
109 * when something is added to the global mark stack, and just after
110 * GC_mark_no is incremented.
111 * This could be split into multiple CVs (and probably should be to
112 * scale to really large numbers of processors.)
113 */
114 #endif /* PARALLEL_MARK */
115
116 GC_INNER mse * GC_signal_mark_stack_overflow(mse *msp);
117
118 /* Push the object obj with corresponding heap block header hhdr onto */
119 /* the mark stack. Returns the updated mark_stack_top value. */
120 GC_INLINE mse * GC_push_obj(ptr_t obj, hdr * hhdr, mse * mark_stack_top,
121 mse * mark_stack_limit)
122 {
123 word descr = hhdr -> hb_descr;
124
125 GC_ASSERT(!HBLK_IS_FREE(hhdr));
126 if (descr != 0) {
127 mark_stack_top++;
128 if ((word)mark_stack_top >= (word)mark_stack_limit) {
129 mark_stack_top = GC_signal_mark_stack_overflow(mark_stack_top);
130 }
131 mark_stack_top -> mse_start = obj;
132 mark_stack_top -> mse_descr.w = descr;
133 }
134 return mark_stack_top;
135 }
136
137 /* Push the contents of current onto the mark stack if it is a valid */
138 /* ptr to a currently unmarked object. Mark it. */
139 #define PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, source) \
140 do { \
141 hdr * my_hhdr; \
142 HC_GET_HDR(current, my_hhdr, source); /* contains "break" */ \
143 mark_stack_top = GC_push_contents_hdr(current, mark_stack_top, \
144 mark_stack_limit, \
145 source, my_hhdr, TRUE); \
146 } while (0)
147
148 /* Set mark bit, exit (using "break" statement) if it is already set. */
149 #ifdef USE_MARK_BYTES
150 # if defined(PARALLEL_MARK) && defined(AO_HAVE_char_store) \
151 && !defined(BASE_ATOMIC_OPS_EMULATED)
152 /* There is a race here, and we may set the bit twice in the */
153 /* concurrent case. This can result in the object being pushed */
154 /* twice. But that is only a performance issue. */
155 # define SET_MARK_BIT_EXIT_IF_SET(hhdr, bit_no) \
156 { /* cannot use do-while(0) here */ \
157 volatile unsigned char * mark_byte_addr = \
158 (unsigned char *)(hhdr)->hb_marks + (bit_no); \
159 /* Unordered atomic load and store are sufficient here. */ \
160 if (AO_char_load(mark_byte_addr) != 0) \
161 break; /* go to the enclosing loop end */ \
162 AO_char_store(mark_byte_addr, 1); \
163 }
164 # else
165 # define SET_MARK_BIT_EXIT_IF_SET(hhdr, bit_no) \
166 { /* cannot use do-while(0) here */ \
167 char * mark_byte_addr = (char *)(hhdr)->hb_marks + (bit_no); \
168 if (*mark_byte_addr != 0) break; /* go to the enclosing loop end */ \
169 *mark_byte_addr = 1; \
170 }
171 # endif /* !PARALLEL_MARK */
172 #else
173 # ifdef PARALLEL_MARK
174 /* This is used only if we explicitly set USE_MARK_BITS. */
175 /* The following may fail to exit even if the bit was already set. */
176 /* For our uses, that's benign: */
177 # ifdef THREAD_SANITIZER
178 # define OR_WORD_EXIT_IF_SET(addr, bits) \
179 { /* cannot use do-while(0) here */ \
180 if (!((word)AO_load((volatile AO_t *)(addr)) & (bits))) { \
181 /* Atomic load is just to avoid TSan false positive. */ \
182 AO_or((volatile AO_t *)(addr), (AO_t)(bits)); \
183 } else { \
184 break; /* go to the enclosing loop end */ \
185 } \
186 }
187 # else
188 # define OR_WORD_EXIT_IF_SET(addr, bits) \
189 { /* cannot use do-while(0) here */ \
190 if (!(*(addr) & (bits))) { \
191 AO_or((volatile AO_t *)(addr), (AO_t)(bits)); \
192 } else { \
193 break; /* go to the enclosing loop end */ \
194 } \
195 }
196 # endif /* !THREAD_SANITIZER */
197 # else
198 # define OR_WORD_EXIT_IF_SET(addr, bits) \
199 { /* cannot use do-while(0) here */ \
200 word old = *(addr); \
201 word my_bits = (bits); \
202 if ((old & my_bits) != 0) \
203 break; /* go to the enclosing loop end */ \
204 *(addr) = old | my_bits; \
205 }
206 # endif /* !PARALLEL_MARK */
207 # define SET_MARK_BIT_EXIT_IF_SET(hhdr, bit_no) \
208 { /* cannot use do-while(0) here */ \
209 word * mark_word_addr = (hhdr)->hb_marks + divWORDSZ(bit_no); \
210 OR_WORD_EXIT_IF_SET(mark_word_addr, \
211 (word)1 << modWORDSZ(bit_no)); /* contains "break" */ \
212 }
213 #endif /* !USE_MARK_BYTES */
214
215 #ifdef PARALLEL_MARK
216 # define INCR_MARKS(hhdr) \
217 AO_store(&hhdr->hb_n_marks, AO_load(&hhdr->hb_n_marks) + 1)
218 #else
219 # define INCR_MARKS(hhdr) (void)(++hhdr->hb_n_marks)
220 #endif
221
222 #ifdef ENABLE_TRACE
223 # define TRACE(source, cmd) \
224 if (GC_trace_addr != 0 && (ptr_t)(source) == GC_trace_addr) cmd
225 # define TRACE_TARGET(target, cmd) \
226 if (GC_trace_addr != NULL && GC_is_heap_ptr(GC_trace_addr) \
227 && (target) == *(ptr_t *)GC_trace_addr) cmd
228 #else
229 # define TRACE(source, cmd)
230 # define TRACE_TARGET(source, cmd)
231 #endif
232
233 #if defined(I386) && defined(__GNUC__) && !defined(NACL)
234 # define LONG_MULT(hprod, lprod, x, y) \
235 do { \
236 __asm__ __volatile__("mull %2" : "=a"(lprod), "=d"(hprod) \
237 : "r"(y), "0"(x)); \
238 } while (0)
239 #else
240 # if defined(__int64) && !defined(__GNUC__) && !defined(CPPCHECK)
241 # define ULONG_MULT_T unsigned __int64
242 # else
243 # define ULONG_MULT_T unsigned long long
244 # endif
245 # define LONG_MULT(hprod, lprod, x, y) \
246 do { \
247 ULONG_MULT_T prod = (ULONG_MULT_T)(x) * (ULONG_MULT_T)(y); \
248 GC_STATIC_ASSERT(sizeof(x) + sizeof(y) <= sizeof(prod)); \
249 hprod = (unsigned32)(prod >> 32); \
250 lprod = (unsigned32)prod; \
251 } while (0)
252 #endif /* !I386 */
253
254 /* If the mark bit corresponding to current is not set, set it, and */
255 /* push the contents of the object on the mark stack. Current points */
256 /* to the beginning of the object. We rely on the fact that the */
257 /* preceding header calculation will succeed for a pointer past the */
258 /* first page of an object, only if it is in fact a valid pointer */
259 /* to the object. Thus we can omit the otherwise necessary tests */
260 /* here. Note in particular that the "displ" value is the displacement */
261 /* from the beginning of the heap block, which may itself be in the */
262 /* interior of a large object. */
263 GC_INLINE mse * GC_push_contents_hdr(ptr_t current, mse * mark_stack_top,
264 mse * mark_stack_limit, ptr_t source,
265 hdr * hhdr, GC_bool do_offset_check)
266 {
267 do {
268 size_t displ = HBLKDISPL(current); /* Displacement in block; in bytes. */
269 /* displ is always within range. If current doesn't point to the */
270 /* first block, then we are in the all_interior_pointers case, and */
271 /* it is safe to use any displacement value. */
272 ptr_t base = current;
273 # ifdef MARK_BIT_PER_GRANULE
274 size_t gran_displ = BYTES_TO_GRANULES(displ);
275 size_t gran_offset = hhdr -> hb_map[gran_displ];
276 size_t byte_offset = displ & (GRANULE_BYTES - 1);
277
278 /* The following always fails for large block references. */
279 if (EXPECT((gran_offset | byte_offset) != 0, FALSE))
280 # else
281 unsigned32 gran_displ; /* high_prod */
282 unsigned32 inv_sz = hhdr -> hb_inv_sz;
283 # endif /* MARK_BIT_PER_OBJ */
284
285 {
286 # ifdef MARK_BIT_PER_GRANULE
287 if ((hhdr -> hb_flags & LARGE_BLOCK) != 0)
288 # else
289 if (EXPECT(inv_sz == LARGE_INV_SZ, FALSE))
290 # endif /* MARK_BIT_PER_OBJ */
291 {
292 /* gran_offset is bogus. */
293 size_t obj_displ;
294
295 base = (ptr_t)hhdr->hb_block;
296 obj_displ = current - base;
297 if (obj_displ != displ) {
298 GC_ASSERT(obj_displ < hhdr -> hb_sz);
299 /* Must be in all_interior_pointer case, not first block */
300 /* already did validity check on cache miss. */
301 } else if (do_offset_check && !GC_valid_offsets[obj_displ]) {
302 GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
303 break;
304 }
305 GC_ASSERT(hhdr -> hb_sz > HBLKSIZE
306 || hhdr -> hb_block == HBLKPTR(current));
307 GC_ASSERT((word)hhdr->hb_block <= (word)current);
308 gran_displ = 0;
309 } else {
310 # ifdef MARK_BIT_PER_GRANULE
311 size_t obj_displ = GRANULES_TO_BYTES(gran_offset) + byte_offset;
312 # else
313 unsigned32 low_prod;
314
315 LONG_MULT(gran_displ, low_prod, (unsigned32)displ, inv_sz);
316 if ((low_prod >> 16) != 0)
317 # endif /* MARK_BIT_PER_OBJ */
318 {
319 # if defined(MARK_BIT_PER_OBJ) \
320 && !defined(MARK_BIT_PER_GRANULE) /* for cppcheck */
321 size_t obj_displ;
322
323 /* Accurate enough if HBLKSIZE <= 2**15. */
324 GC_STATIC_ASSERT(HBLKSIZE <= (1 << 15));
325 obj_displ = (((low_prod >> 16) + 1) * (size_t)hhdr->hb_sz) >> 16;
326 # endif
327 if (do_offset_check && !GC_valid_offsets[obj_displ]) {
328 GC_ADD_TO_BLACK_LIST_NORMAL(current, source);
329 break;
330 }
331 # ifdef MARK_BIT_PER_GRANULE
332 gran_displ -= gran_offset;
333 # endif
334 base -= obj_displ;
335 }
336 }
337 }
338 # ifdef MARK_BIT_PER_GRANULE
339 GC_ASSERT(hhdr == GC_find_header(base));
340 GC_ASSERT(gran_displ % BYTES_TO_GRANULES(hhdr -> hb_sz) == 0);
341 # else
342 /* May get here for pointer to start of block not at the */
343 /* beginning of object. If so, it is valid, and we are fine. */
344 GC_ASSERT(gran_displ <= HBLK_OBJS(hhdr -> hb_sz));
345 # endif /* MARK_BIT_PER_OBJ */
346 TRACE(source, GC_log_printf("GC #%lu: passed validity tests\n",
347 (unsigned long)GC_gc_no));
348 SET_MARK_BIT_EXIT_IF_SET(hhdr, gran_displ); /* contains "break" */
349 TRACE(source, GC_log_printf("GC #%lu: previously unmarked\n",
350 (unsigned long)GC_gc_no));
351 TRACE_TARGET(base, GC_log_printf("GC #%lu: marking %p from %p instead\n",
352 (unsigned long)GC_gc_no, (void *)base,
353 (void *)source));
354 INCR_MARKS(hhdr);
355 GC_STORE_BACK_PTR(source, base);
356 mark_stack_top = GC_push_obj(base, hhdr, mark_stack_top,
357 mark_stack_limit);
358 } while (0);
359 return mark_stack_top;
360 }
361
362 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
363 # define PUSH_ONE_CHECKED_STACK(p, source) \
364 GC_mark_and_push_stack((ptr_t)(p), (ptr_t)(source))
365 #else
366 # define PUSH_ONE_CHECKED_STACK(p, source) \
367 GC_mark_and_push_stack((ptr_t)(p))
368 #endif
369
370 /*
371 * Push a single value onto mark stack. Mark from the object pointed to by p.
372 * Invoke FIXUP_POINTER() before any further processing.
373 * P is considered valid even if it is an interior pointer.
374 * Previously marked objects are not pushed. Hence we make progress even
375 * if the mark stack overflows.
376 */
377
378 #ifdef NEED_FIXUP_POINTER
379 /* Try both the raw version and the fixed up one. */
380 # define GC_PUSH_ONE_STACK(p, source) \
381 do { \
382 word pp = (word)(p); \
383 \
384 if ((word)(p) >= (word)GC_least_plausible_heap_addr \
385 && (word)(p) < (word)GC_greatest_plausible_heap_addr) { \
386 PUSH_ONE_CHECKED_STACK(p, source); \
387 } \
388 FIXUP_POINTER(pp); \
389 if (pp >= (word)GC_least_plausible_heap_addr \
390 && pp < (word)GC_greatest_plausible_heap_addr) { \
391 PUSH_ONE_CHECKED_STACK(pp, source); \
392 } \
393 } while (0)
394 #else /* !NEED_FIXUP_POINTER */
395 # define GC_PUSH_ONE_STACK(p, source) \
396 do { \
397 if ((word)(p) >= (word)GC_least_plausible_heap_addr \
398 && (word)(p) < (word)GC_greatest_plausible_heap_addr) { \
399 PUSH_ONE_CHECKED_STACK(p, source); \
400 } \
401 } while (0)
402 #endif
403
404 /* As above, but interior pointer recognition as for normal heap pointers. */
405 #define GC_PUSH_ONE_HEAP(p,source,mark_stack_top) \
406 do { \
407 FIXUP_POINTER(p); \
408 if ((word)(p) >= (word)GC_least_plausible_heap_addr \
409 && (word)(p) < (word)GC_greatest_plausible_heap_addr) \
410 mark_stack_top = GC_mark_and_push((void *)(p), mark_stack_top, \
411 GC_mark_stack_limit, (void * *)(source)); \
412 } while (0)
413
414 /* Mark starting at mark stack entry top (incl.) down to */
415 /* mark stack entry bottom (incl.). Stop after performing */
416 /* about one page worth of work. Return the new mark stack */
417 /* top entry. */
418 GC_INNER mse * GC_mark_from(mse * top, mse * bottom, mse *limit);
419
420 #define MARK_FROM_MARK_STACK() \
421 GC_mark_stack_top = GC_mark_from(GC_mark_stack_top, \
422 GC_mark_stack, \
423 GC_mark_stack + GC_mark_stack_size);
424
425 #define GC_mark_stack_empty() ((word)GC_mark_stack_top < (word)GC_mark_stack)
426
427 /*
428 * Mark from one finalizable object using the specified
429 * mark proc. May not mark the object pointed to by
430 * real_ptr. That is the job of the caller, if appropriate.
431 * Note that this is called with the mutator running, but
432 * with us holding the allocation lock. This is safe only if the
433 * mutator needs the allocation lock to reveal hidden pointers.
434 * FIXME: Why do we need the GC_mark_state test below?
435 */
436 #define GC_MARK_FO(real_ptr, mark_proc) \
437 do { \
438 (*(mark_proc))(real_ptr); \
439 while (!GC_mark_stack_empty()) MARK_FROM_MARK_STACK(); \
440 if (GC_mark_state != MS_NONE) { \
441 GC_set_mark_bit(real_ptr); \
442 while (!GC_mark_some((ptr_t)0)) { /* empty */ } \
443 } \
444 } while (0)
445
446 /* Current state of marking, as follows.*/
447
448 /* We say something is dirty if it was */
449 /* written since the last time we */
450 /* retrieved dirty bits. We say it's */
451 /* grungy if it was marked dirty in the */
452 /* last set of bits we retrieved. */
453
454 /* Invariant "I": all roots and marked */
455 /* objects p are either dirty, or point */
456 /* to objects q that are either marked */
457 /* or a pointer to q appears in a range */
458 /* on the mark stack. */
459
460 #define MS_NONE 0 /* No marking in progress. "I" holds. */
461 /* Mark stack is empty. */
462
463 #define MS_PUSH_RESCUERS 1 /* Rescuing objects are currently */
464 /* being pushed. "I" holds, except */
465 /* that grungy roots may point to */
466 /* unmarked objects, as may marked */
467 /* grungy objects above GC_scan_ptr. */
468
469 #define MS_PUSH_UNCOLLECTABLE 2 /* "I" holds, except that marked */
470 /* uncollectible objects above */
471 /* GC_scan_ptr may point to unmarked */
472 /* objects. Roots may point to */
473 /* unmarked objects. */
474
475 #define MS_ROOTS_PUSHED 3 /* "I" holds, mark stack may be nonempty. */
476
477 #define MS_PARTIALLY_INVALID 4 /* "I" may not hold, e.g. because of */
478 /* the mark stack overflow. However, */
479 /* marked heap objects below */
480 /* GC_scan_ptr point to marked or */
481 /* stacked objects. */
482
483 #define MS_INVALID 5 /* "I" may not hold. */
484
485 EXTERN_C_END
486
487 #endif /* GC_PMARK_H */