Import boehm-gc snapshot, taken from
[official-gcc.git] / boehm-gc / include / gc_mark.h
blob508fb2cc3b0e9433f82b4b5d4e5ce5fbef82bc24
1 /*
2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 2001 by Hewlett-Packard Company. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
17 * This contains interfaces to the GC marker that are likely to be useful to
18 * clients that provide detailed heap layout information to the collector.
19 * This interface should not be used by normal C or C++ clients.
20 * It will be useful to runtimes for other languages.
22 * This is an experts-only interface! There are many ways to break the
23 * collector in subtle ways by using this functionality.
25 #ifndef GC_MARK_H
26 #define GC_MARK_H
28 #ifndef GC_H
29 # include "gc.h"
30 #endif
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
36 /* A client supplied mark procedure. Returns new mark stack pointer. */
37 /* Primary effect should be to push new entries on the mark stack. */
38 /* Mark stack pointer values are passed and returned explicitly. */
39 /* Global variables describing mark stack are not necessarily valid. */
40 /* (This usually saves a few cycles by keeping things in registers.) */
41 /* Assumed to scan about GC_PROC_BYTES on average. If it needs to do */
42 /* much more work than that, it should do it in smaller pieces by */
43 /* pushing itself back on the mark stack. */
44 /* Note that it should always do some work (defined as marking some */
45 /* objects) before pushing more than one entry on the mark stack. */
46 /* This is required to ensure termination in the event of mark stack */
47 /* overflows. */
48 /* This procedure is always called with at least one empty entry on the */
49 /* mark stack. */
50 /* Currently we require that mark procedures look for pointers in a */
51 /* subset of the places the conservative marker would. It must be safe */
52 /* to invoke the normal mark procedure instead. */
53 /* WARNING: Such a mark procedure may be invoked on an unused object */
54 /* residing on a free list. Such objects are cleared, except for a */
55 /* free list link field in the first word. Thus mark procedures may */
56 /* not count on the presence of a type descriptor, and must handle this */
57 /* case correctly somehow. */
58 #define GC_PROC_BYTES 100
60 #ifdef GC_BUILD
61 struct GC_ms_entry;
62 #else
63 struct GC_ms_entry { void *opaque; };
64 #endif
65 typedef struct GC_ms_entry * (*GC_mark_proc)(GC_word * /* addr */,
66 struct GC_ms_entry * /* mark_stack_ptr */,
67 struct GC_ms_entry * /* mark_stack_limit */,
68 GC_word /* env */);
70 #define GC_LOG_MAX_MARK_PROCS 6
71 #define GC_MAX_MARK_PROCS (1 << GC_LOG_MAX_MARK_PROCS)
73 /* In a few cases it's necessary to assign statically known indices to */
74 /* certain mark procs. Thus we reserve a few for well known clients. */
75 /* (This is necessary if mark descriptors are compiler generated.) */
76 #define GC_RESERVED_MARK_PROCS 8
77 #define GC_GCJ_RESERVED_MARK_PROC_INDEX 0
79 /* Object descriptors on mark stack or in objects. Low order two */
80 /* bits are tags distinguishing among the following 4 possibilities */
81 /* for the high order 30 bits. */
82 #define GC_DS_TAG_BITS 2
83 #define GC_DS_TAGS ((1 << GC_DS_TAG_BITS) - 1)
84 #define GC_DS_LENGTH 0 /* The entire word is a length in bytes that */
85 /* must be a multiple of 4. */
86 #define GC_DS_BITMAP 1 /* 30 (62) bits are a bitmap describing pointer */
87 /* fields. The msb is 1 if the first word */
88 /* is a pointer. */
89 /* (This unconventional ordering sometimes */
90 /* makes the marker slightly faster.) */
91 /* Zeroes indicate definite nonpointers. Ones */
92 /* indicate possible pointers. */
93 /* Only usable if pointers are word aligned. */
94 #define GC_DS_PROC 2
95 /* The objects referenced by this object can be */
96 /* pushed on the mark stack by invoking */
97 /* PROC(descr). ENV(descr) is passed as the */
98 /* last argument. */
99 #define GC_MAKE_PROC(proc_index, env) \
100 (((((env) << GC_LOG_MAX_MARK_PROCS) \
101 | (proc_index)) << GC_DS_TAG_BITS) | GC_DS_PROC)
102 #define GC_DS_PER_OBJECT 3 /* The real descriptor is at the */
103 /* byte displacement from the beginning of the */
104 /* object given by descr & ~DS_TAGS */
105 /* If the descriptor is negative, the real */
106 /* descriptor is at (*<object_start>) - */
107 /* (descr & ~DS_TAGS) - GC_INDIR_PER_OBJ_BIAS */
108 /* The latter alternative can be used if each */
109 /* object contains a type descriptor in the */
110 /* first word. */
111 /* Note that in multithreaded environments */
112 /* per object descriptors must be located in */
113 /* either the first two or last two words of */
114 /* the object, since only those are guaranteed */
115 /* to be cleared while the allocation lock is */
116 /* held. */
117 #define GC_INDIR_PER_OBJ_BIAS 0x10
119 GC_API void * GC_least_plausible_heap_addr;
120 GC_API void * GC_greatest_plausible_heap_addr;
121 /* Bounds on the heap. Guaranteed valid */
122 /* Likely to include future heap expansion. */
123 /* Hence usually includes not-yet-mapped */
124 /* memory. */
126 /* Handle nested references in a custom mark procedure. */
127 /* Check if obj is a valid object. If so, ensure that it is marked. */
128 /* If it was not previously marked, push its contents onto the mark */
129 /* stack for future scanning. The object will then be scanned using */
130 /* its mark descriptor. */
131 /* Returns the new mark stack pointer. */
132 /* Handles mark stack overflows correctly. */
133 /* Since this marks first, it makes progress even if there are mark */
134 /* stack overflows. */
135 /* Src is the address of the pointer to obj, which is used only */
136 /* for back pointer-based heap debugging. */
137 /* It is strongly recommended that most objects be handled without mark */
138 /* procedures, e.g. with bitmap descriptors, and that mark procedures */
139 /* be reserved for exceptional cases. That will ensure that */
140 /* performance of this call is not extremely performance critical. */
141 /* (Otherwise we would need to inline GC_mark_and_push completely, */
142 /* which would tie the client code to a fixed collector version.) */
143 /* Note that mark procedures should explicitly call FIXUP_POINTER() */
144 /* if required. */
145 GC_API struct GC_ms_entry * GC_CALL GC_mark_and_push(void * /* obj */,
146 struct GC_ms_entry * /* mark_stack_ptr */,
147 struct GC_ms_entry * /* mark_stack_limit */,
148 void ** /* src */);
150 #define GC_MARK_AND_PUSH(obj, msp, lim, src) \
151 ((GC_word)(obj) >= (GC_word)GC_least_plausible_heap_addr && \
152 (GC_word)(obj) <= (GC_word)GC_greatest_plausible_heap_addr ? \
153 GC_mark_and_push(obj, msp, lim, src) : (msp))
155 GC_API size_t GC_debug_header_size;
156 /* The size of the header added to objects allocated through */
157 /* the GC_debug routines. */
158 /* Defined as a variable so that client mark procedures don't */
159 /* need to be recompiled for collector version changes. */
160 #define GC_USR_PTR_FROM_BASE(p) ((void *)((char *)(p) + GC_debug_header_size))
162 /* And some routines to support creation of new "kinds", e.g. with */
163 /* custom mark procedures, by language runtimes. */
164 /* The _inner versions assume the caller holds the allocation lock. */
166 /* Return a new free list array. */
167 GC_API void ** GC_CALL GC_new_free_list(void);
168 GC_API void ** GC_CALL GC_new_free_list_inner(void);
170 /* Return a new kind, as specified. */
171 GC_API unsigned GC_CALL GC_new_kind(void ** /* free_list */,
172 GC_word /* mark_descriptor_template */,
173 int /* add_size_to_descriptor */,
174 int /* clear_new_objects */) GC_ATTR_NONNULL(1);
175 /* The last two parameters must be zero or one. */
176 GC_API unsigned GC_CALL GC_new_kind_inner(void ** /* free_list */,
177 GC_word /* mark_descriptor_template */,
178 int /* add_size_to_descriptor */,
179 int /* clear_new_objects */) GC_ATTR_NONNULL(1);
181 /* Return a new mark procedure identifier, suitable for use as */
182 /* the first argument in GC_MAKE_PROC. */
183 GC_API unsigned GC_CALL GC_new_proc(GC_mark_proc);
184 GC_API unsigned GC_CALL GC_new_proc_inner(GC_mark_proc);
186 /* Allocate an object of a given kind. Note that in multithreaded */
187 /* contexts, this is usually unsafe for kinds that have the descriptor */
188 /* in the object itself, since there is otherwise a window in which */
189 /* the descriptor is not correct. Even in the single-threaded case, */
190 /* we need to be sure that cleared objects on a free list don't */
191 /* cause a GC crash if they are accidentally traced. */
192 GC_API GC_ATTR_MALLOC void * GC_CALL
193 GC_generic_malloc(size_t /* lb */, int /* k */);
195 typedef void (GC_CALLBACK * GC_describe_type_fn)(void * /* p */,
196 char * /* out_buf */);
197 /* A procedure which */
198 /* produces a human-readable */
199 /* description of the "type" of object */
200 /* p into the buffer out_buf of length */
201 /* GC_TYPE_DESCR_LEN. This is used by */
202 /* the debug support when printing */
203 /* objects. */
204 /* These functions should be as robust */
205 /* as possible, though we do avoid */
206 /* invoking them on objects on the */
207 /* global free list. */
208 #define GC_TYPE_DESCR_LEN 40
210 GC_API void GC_CALL GC_register_describe_type_fn(int /* kind */,
211 GC_describe_type_fn);
212 /* Register a describe_type function */
213 /* to be used when printing objects */
214 /* of a particular kind. */
216 /* Clear some of the inaccessible part of the stack. Returns its */
217 /* argument, so it can be used in a tail call position, hence clearing */
218 /* another frame. Argument may be NULL. */
219 GC_API void * GC_CALL GC_clear_stack(void *);
221 /* Set and get the client notifier on collections. The client function */
222 /* is called at the start of every full GC (called with the allocation */
223 /* lock held). May be 0. This is a really tricky interface to use */
224 /* correctly. Unless you really understand the collector internals, */
225 /* the callback should not, directly or indirectly, make any GC_ or */
226 /* potentially blocking calls. In particular, it is not safe to */
227 /* allocate memory using the garbage collector from within the callback */
228 /* function. Both the setter and getter acquire the GC lock. */
229 typedef void (GC_CALLBACK * GC_start_callback_proc)(void);
230 GC_API void GC_CALL GC_set_start_callback(GC_start_callback_proc);
231 GC_API GC_start_callback_proc GC_CALL GC_get_start_callback(void);
233 /* Slow/general mark bit manipulation. The caller must hold the */
234 /* allocation lock. GC_is_marked returns 1 (TRUE) or 0. */
235 GC_API int GC_CALL GC_is_marked(const void *) GC_ATTR_NONNULL(1);
236 GC_API void GC_CALL GC_clear_mark_bit(const void *) GC_ATTR_NONNULL(1);
237 GC_API void GC_CALL GC_set_mark_bit(const void *) GC_ATTR_NONNULL(1);
239 /* Push everything in the given range onto the mark stack. */
240 /* (GC_push_conditional pushes either all or only dirty pages depending */
241 /* on the third argument.) */
242 GC_API void GC_CALL GC_push_all(char * /* bottom */, char * /* top */);
243 GC_API void GC_CALL GC_push_conditional(char * /* bottom */, char * /* top */,
244 int /* bool all */);
246 /* Set and get the client push-other-roots procedure. A client */
247 /* supplied procedure should also call the original procedure. */
248 /* Note that both the setter and getter require some external */
249 /* synchronization to avoid data race. */
250 typedef void (GC_CALLBACK * GC_push_other_roots_proc)(void);
251 GC_API void GC_CALL GC_set_push_other_roots(GC_push_other_roots_proc);
252 GC_API GC_push_other_roots_proc GC_CALL GC_get_push_other_roots(void);
254 #ifdef __cplusplus
255 } /* end of extern "C" */
256 #endif
258 #endif /* GC_MARK_H */