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