* system.h (malloc, realloc, calloc, strdup, bzero, bcmp, rindex):
[official-gcc.git] / boehm-gc / gc_mark.h
blob295423a62a19f7321185a332eec13a6fad1bd57f
1 /*
2 * Copyright (c) 1991-1994, 2000 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
14 /* Boehm, November 7, 1994 4:56 pm PST */
17 * Declarations of mark stack. Needed by marker and client supplied mark
18 * routines. To be included after gc_priv.h.
20 #ifndef GC_MARK_H
21 # define GC_MARK_H
23 # ifdef KEEP_BACK_PTRS
24 # include "dbg_mlc.h"
25 # endif
27 /* A client supplied mark procedure. Returns new mark stack pointer. */
28 /* Primary effect should be to push new entries on the mark stack. */
29 /* Mark stack pointer values are passed and returned explicitly. */
30 /* Global variables decribing mark stack are not necessarily valid. */
31 /* (This usually saves a few cycles by keeping things in registers.) */
32 /* Assumed to scan about PROC_BYTES on average. If it needs to do */
33 /* much more work than that, it should do it in smaller pieces by */
34 /* pushing itself back on the mark stack. */
35 /* Note that it should always do some work (defined as marking some */
36 /* objects) before pushing more than one entry on the mark stack. */
37 /* This is required to ensure termination in the event of mark stack */
38 /* overflows. */
39 /* This procedure is always called with at least one empty entry on the */
40 /* mark stack. */
41 /* Currently we require that mark procedures look for pointers in a */
42 /* subset of the places the conservative marker would. It must be safe */
43 /* to invoke the normal mark procedure instead. */
44 # define PROC_BYTES 100
45 /* The real declarations of the following are in gc_priv.h, so that */
46 /* we can avoid scanning the following table. */
48 typedef struct ms_entry * (*mark_proc)( word * addr,
49 struct ms_entry *mark_stack_ptr,
50 struct ms_entry *mark_stack_limit,
51 word env );
53 # define LOG_MAX_MARK_PROCS 6
54 # define MAX_MARK_PROCS (1 << LOG_MAX_MARK_PROCS)
55 extern mark_proc GC_mark_procs[MAX_MARK_PROCS];
58 extern word GC_n_mark_procs;
60 /* In a few cases it's necessary to assign statically known indices to */
61 /* certain mark procs. Thus we reserve a few for well known clients. */
62 /* (This is necessary if mark descriptors are compiler generated.) */
63 #define GC_RESERVED_MARK_PROCS 8
64 # define GCJ_RESERVED_MARK_PROC_INDEX 0
66 /* Object descriptors on mark stack or in objects. Low order two */
67 /* bits are tags distinguishing among the following 4 possibilities */
68 /* for the high order 30 bits. */
69 #define DS_TAG_BITS 2
70 #define DS_TAGS ((1 << DS_TAG_BITS) - 1)
71 #define DS_LENGTH 0 /* The entire word is a length in bytes that */
72 /* must be a multiple of 4. */
73 #define DS_BITMAP 1 /* 30 bits are a bitmap describing pointer */
74 /* fields. The msb is 1 iff the first word */
75 /* is a pointer. */
76 /* (This unconventional ordering sometimes */
77 /* makes the marker slightly faster.) */
78 /* Zeroes indicate definite nonpointers. Ones */
79 /* indicate possible pointers. */
80 /* Only usable if pointers are word aligned. */
81 # define BITMAP_BITS (WORDSZ - DS_TAG_BITS)
82 #define DS_PROC 2
83 /* The objects referenced by this object can be */
84 /* pushed on the mark stack by invoking */
85 /* PROC(descr). ENV(descr) is passed as the */
86 /* last argument. */
87 # define PROC(descr) \
88 (GC_mark_procs[((descr) >> DS_TAG_BITS) & (MAX_MARK_PROCS-1)])
89 # define ENV(descr) \
90 ((descr) >> (DS_TAG_BITS + LOG_MAX_MARK_PROCS))
91 # define MAX_ENV \
92 (((word)1 << (WORDSZ - DS_TAG_BITS - LOG_MAX_MARK_PROCS)) - 1)
93 # define MAKE_PROC(proc_index, env) \
94 (((((env) << LOG_MAX_MARK_PROCS) | (proc_index)) << DS_TAG_BITS) \
95 | DS_PROC)
96 #define DS_PER_OBJECT 3 /* The real descriptor is at the */
97 /* byte displacement from the beginning of the */
98 /* object given by descr & ~DS_TAGS */
99 /* If the descriptor is negative, the real */
100 /* descriptor is at (*<object_start>) - */
101 /* (descr & ~DS_TAGS) - INDIR_PER_OBJ_BIAS */
102 /* The latter alternative can be used if each */
103 /* object contains a type descriptor in the */
104 /* first word. */
105 #define INDIR_PER_OBJ_BIAS 0x10
107 typedef struct ms_entry {
108 word * mse_start; /* First word of object */
109 word mse_descr; /* Descriptor; low order two bits are tags, */
110 /* identifying the upper 30 bits as one of the */
111 /* following: */
112 } mse;
114 extern word GC_mark_stack_size;
116 extern mse * GC_mark_stack_top;
118 extern mse * GC_mark_stack;
120 #ifdef PRINT_BLACK_LIST
121 ptr_t GC_find_start(ptr_t, hdr*, word);
122 #else
123 ptr_t GC_find_start(ptr_t, hdr*);
124 #endif
126 mse * GC_signal_mark_stack_overflow(mse *);
128 # ifdef GATHERSTATS
129 # define ADD_TO_ATOMIC(sz) GC_atomic_in_use += (sz)
130 # define ADD_TO_COMPOSITE(sz) GC_composite_in_use += (sz)
131 # else
132 # define ADD_TO_ATOMIC(sz)
133 # define ADD_TO_COMPOSITE(sz)
134 # endif
136 /* Push the object obj with corresponding heap block header hhdr onto */
137 /* the mark stack. */
138 # define PUSH_OBJ(obj, hhdr, mark_stack_top, mark_stack_limit) \
140 register word _descr = (hhdr) -> hb_descr; \
142 if (_descr == 0) { \
143 ADD_TO_ATOMIC((hhdr) -> hb_sz); \
144 } else { \
145 ADD_TO_COMPOSITE((hhdr) -> hb_sz); \
146 mark_stack_top++; \
147 if (mark_stack_top >= mark_stack_limit) { \
148 mark_stack_top = GC_signal_mark_stack_overflow(mark_stack_top); \
150 mark_stack_top -> mse_start = (obj); \
151 mark_stack_top -> mse_descr = _descr; \
155 #ifdef PRINT_BLACK_LIST
156 # define GC_FIND_START(current, hhdr, source) \
157 GC_find_start(current, hhdr, source)
158 #else
159 # define GC_FIND_START(current, hhdr, source) \
160 GC_find_start(current, hhdr)
161 #endif
163 /* Push the contents of current onto the mark stack if it is a valid */
164 /* ptr to a currently unmarked object. Mark it. */
165 /* If we assumed a standard-conforming compiler, we could probably */
166 /* generate the exit_label transparently. */
167 # define PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
168 source, exit_label) \
170 hdr * my_hhdr; \
171 ptr_t my_current = current; \
173 GET_HDR(my_current, my_hhdr); \
174 if (IS_FORWARDING_ADDR_OR_NIL(my_hhdr)) { \
175 my_current = GC_FIND_START(my_current, my_hhdr, (word)source); \
176 if (my_current == 0) goto exit_label; \
177 my_hhdr = GC_find_header(my_current); \
179 PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
180 source, exit_label, my_hhdr); \
181 exit_label: ; \
184 /* As above, but use header cache for header lookup. */
185 # define HC_PUSH_CONTENTS(current, mark_stack_top, mark_stack_limit, \
186 source, exit_label) \
188 hdr * my_hhdr; \
189 ptr_t my_current = current; \
191 HC_GET_HDR(my_current, my_hhdr, source); \
192 PUSH_CONTENTS_HDR(my_current, mark_stack_top, mark_stack_limit, \
193 source, exit_label, my_hhdr); \
194 exit_label: ; \
197 /* As above, but deal with two pointers in interleaved fashion. */
198 # define HC_PUSH_CONTENTS2(current1, current2, mark_stack_top, \
199 mark_stack_limit, \
200 source1, source2, exit_label1, exit_label2) \
202 hdr * hhdr1; \
203 ptr_t my_current1 = current1; \
204 hdr * hhdr2; \
205 ptr_t my_current2 = current2; \
207 HC_GET_HDR2(my_current1, hhdr1, source1, my_current2, hhdr2, source2); \
208 PUSH_CONTENTS_HDR(my_current1, mark_stack_top, mark_stack_limit, \
209 source1, exit_label1, hhdr1); \
210 exit_label1: ; \
211 if (0 != hhdr2) { \
212 PUSH_CONTENTS_HDR(my_current2, mark_stack_top, mark_stack_limit, \
213 source2, exit_label2, hhdr2); \
215 exit_label2: ; \
218 # define PUSH_CONTENTS_HDR(current, mark_stack_top, mark_stack_limit, \
219 source, exit_label, hhdr) \
221 int displ; /* Displacement in block; first bytes, then words */ \
222 map_entry_type map_entry; \
224 displ = HBLKDISPL(current); \
225 map_entry = MAP_ENTRY((hhdr -> hb_map), displ); \
226 if (map_entry == OBJ_INVALID) { \
227 GC_ADD_TO_BLACK_LIST_NORMAL(current, source); goto exit_label; \
229 displ = BYTES_TO_WORDS(displ); \
230 displ -= map_entry; \
233 register word * mark_word_addr = hhdr -> hb_marks + divWORDSZ(displ); \
234 register word mark_word = *mark_word_addr; \
235 register word mark_bit = (word)1 << modWORDSZ(displ); \
237 if (mark_word & mark_bit) { \
238 /* Mark bit is already set */ \
239 goto exit_label; \
241 GC_STORE_BACK_PTR((ptr_t)source, (ptr_t)HBLKPTR(current) \
242 + WORDS_TO_BYTES(displ)); \
243 *mark_word_addr = mark_word | mark_bit; \
245 PUSH_OBJ(((word *)(HBLKPTR(current)) + displ), hhdr, \
246 mark_stack_top, mark_stack_limit) \
249 #if defined(PRINT_BLACK_LIST) || defined(KEEP_BACK_PTRS)
250 # define PUSH_ONE_CHECKED(p, ip, source) \
251 GC_push_one_checked(p, ip, (ptr_t)(source))
252 #else
253 # define PUSH_ONE_CHECKED(p, ip, source) \
254 GC_push_one_checked(p, ip)
255 #endif
258 * Push a single value onto mark stack. Mark from the object pointed to by p.
259 * P is considered valid even if it is an interior pointer.
260 * Previously marked objects are not pushed. Hence we make progress even
261 * if the mark stack overflows.
263 # define GC_PUSH_ONE_STACK(p, source) \
264 if ((ptr_t)(p) >= GC_least_plausible_heap_addr \
265 && (ptr_t)(p) < GC_greatest_plausible_heap_addr) { \
266 PUSH_ONE_CHECKED(p, TRUE, source); \
270 * As above, but interior pointer recognition as for
271 * normal for heap pointers.
273 # ifdef ALL_INTERIOR_POINTERS
274 # define AIP TRUE
275 # else
276 # define AIP FALSE
277 # endif
278 # define GC_PUSH_ONE_HEAP(p,source) \
279 if ((ptr_t)(p) >= GC_least_plausible_heap_addr \
280 && (ptr_t)(p) < GC_greatest_plausible_heap_addr) { \
281 PUSH_ONE_CHECKED(p,AIP,source); \
285 * Mark from one finalizable object using the specified
286 * mark proc. May not mark the object pointed to by
287 * real_ptr. That is the job of the caller, if appropriate
289 # define GC_MARK_FO(real_ptr, mark_proc) \
291 (*(mark_proc))(real_ptr); \
292 while (!GC_mark_stack_empty()) GC_mark_from_mark_stack(); \
293 if (GC_mark_state != MS_NONE) { \
294 GC_set_mark_bit(real_ptr); \
295 while (!GC_mark_some((ptr_t)0)); \
299 extern GC_bool GC_mark_stack_too_small;
300 /* We need a larger mark stack. May be */
301 /* set by client supplied mark routines.*/
303 typedef int mark_state_t; /* Current state of marking, as follows:*/
304 /* Used to remember where we are during */
305 /* concurrent marking. */
307 /* We say something is dirty if it was */
308 /* written since the last time we */
309 /* retrieved dirty bits. We say it's */
310 /* grungy if it was marked dirty in the */
311 /* last set of bits we retrieved. */
313 /* Invariant I: all roots and marked */
314 /* objects p are either dirty, or point */
315 /* to objects q that are either marked */
316 /* or a pointer to q appears in a range */
317 /* on the mark stack. */
319 # define MS_NONE 0 /* No marking in progress. I holds. */
320 /* Mark stack is empty. */
322 # define MS_PUSH_RESCUERS 1 /* Rescuing objects are currently */
323 /* being pushed. I holds, except */
324 /* that grungy roots may point to */
325 /* unmarked objects, as may marked */
326 /* grungy objects above scan_ptr. */
328 # define MS_PUSH_UNCOLLECTABLE 2
329 /* I holds, except that marked */
330 /* uncollectable objects above scan_ptr */
331 /* may point to unmarked objects. */
332 /* Roots may point to unmarked objects */
334 # define MS_ROOTS_PUSHED 3 /* I holds, mark stack may be nonempty */
336 # define MS_PARTIALLY_INVALID 4 /* I may not hold, e.g. because of M.S. */
337 /* overflow. However marked heap */
338 /* objects below scan_ptr point to */
339 /* marked or stacked objects. */
341 # define MS_INVALID 5 /* I may not hold. */
343 extern mark_state_t GC_mark_state;
345 #endif /* GC_MARK_H */