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