Debian Codegraph @

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