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.
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 */
48 /* This procedure is always called with at least one empty entry on the */
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 typedef struct GC_ms_entry * (*GC_mark_proc)(GC_word * /* addr */,
61 struct GC_ms_entry * /* mark_stack_ptr */,
62 struct GC_ms_entry * /* mark_stack_limit */,
65 #define GC_LOG_MAX_MARK_PROCS 6
66 #define GC_MAX_MARK_PROCS (1 << GC_LOG_MAX_MARK_PROCS)
68 /* In a few cases it's necessary to assign statically known indices to */
69 /* certain mark procs. Thus we reserve a few for well known clients. */
70 /* (This is necessary if mark descriptors are compiler generated.) */
71 #define GC_RESERVED_MARK_PROCS 8
72 #define GC_GCJ_RESERVED_MARK_PROC_INDEX 0
74 /* Object descriptors on mark stack or in objects. Low order two */
75 /* bits are tags distinguishing among the following 4 possibilities */
76 /* for the high order 30 bits. */
77 #define GC_DS_TAG_BITS 2
78 #define GC_DS_TAGS ((1 << GC_DS_TAG_BITS) - 1)
79 #define GC_DS_LENGTH 0 /* The entire word is a length in bytes that */
80 /* must be a multiple of 4. */
81 #define GC_DS_BITMAP 1 /* 30 (62) bits are a bitmap describing pointer */
82 /* fields. The msb is 1 if the first word */
84 /* (This unconventional ordering sometimes */
85 /* makes the marker slightly faster.) */
86 /* Zeroes indicate definite nonpointers. Ones */
87 /* indicate possible pointers. */
88 /* Only usable if pointers are word aligned. */
90 /* The objects referenced by this object can be */
91 /* pushed on the mark stack by invoking */
92 /* PROC(descr). ENV(descr) is passed as the */
94 #define GC_MAKE_PROC(proc_index, env) \
95 (((((env) << GC_LOG_MAX_MARK_PROCS) \
96 | (proc_index)) << GC_DS_TAG_BITS) | GC_DS_PROC)
97 #define GC_DS_PER_OBJECT 3 /* The real descriptor is at the */
98 /* byte displacement from the beginning of the */
99 /* object given by descr & ~DS_TAGS */
100 /* If the descriptor is negative, the real */
101 /* descriptor is at (*<object_start>) - */
102 /* (descr & ~DS_TAGS) - GC_INDIR_PER_OBJ_BIAS */
103 /* The latter alternative can be used if each */
104 /* object contains a type descriptor in the */
106 /* Note that in multithreaded environments */
107 /* per object descriptors must be located in */
108 /* either the first two or last two words of */
109 /* the object, since only those are guaranteed */
110 /* to be cleared while the allocation lock is */
112 #define GC_INDIR_PER_OBJ_BIAS 0x10
114 GC_API void * GC_least_plausible_heap_addr;
115 GC_API void * GC_greatest_plausible_heap_addr;
116 /* Bounds on the heap. Guaranteed valid */
117 /* Likely to include future heap expansion. */
118 /* Hence usually includes not-yet-mapped */
121 /* Handle nested references in a custom mark procedure. */
122 /* Check if obj is a valid object. If so, ensure that it is marked. */
123 /* If it was not previously marked, push its contents onto the mark */
124 /* stack for future scanning. The object will then be scanned using */
125 /* its mark descriptor. */
126 /* Returns the new mark stack pointer. */
127 /* Handles mark stack overflows correctly. */
128 /* Since this marks first, it makes progress even if there are mark */
129 /* stack overflows. */
130 /* Src is the address of the pointer to obj, which is used only */
131 /* for back pointer-based heap debugging. */
132 /* It is strongly recommended that most objects be handled without mark */
133 /* procedures, e.g. with bitmap descriptors, and that mark procedures */
134 /* be reserved for exceptional cases. That will ensure that */
135 /* performance of this call is not extremely performance critical. */
136 /* (Otherwise we would need to inline GC_mark_and_push completely, */
137 /* which would tie the client code to a fixed collector version.) */
138 /* Note that mark procedures should explicitly call FIXUP_POINTER() */
140 GC_API struct GC_ms_entry * GC_CALL GC_mark_and_push(void * /* obj */,
141 struct GC_ms_entry * /* mark_stack_ptr */,
142 struct GC_ms_entry * /* mark_stack_limit */,
145 #define GC_MARK_AND_PUSH(obj, msp, lim, src) \
146 ((GC_word)(obj) >= (GC_word)GC_least_plausible_heap_addr && \
147 (GC_word)(obj) <= (GC_word)GC_greatest_plausible_heap_addr ? \
148 GC_mark_and_push(obj, msp, lim, src) : (msp))
150 GC_API size_t GC_debug_header_size;
151 /* The size of the header added to objects allocated through */
152 /* the GC_debug routines. */
153 /* Defined as a variable so that client mark procedures don't */
154 /* need to be recompiled for collector version changes. */
155 #define GC_USR_PTR_FROM_BASE(p) ((void *)((char *)(p) + GC_debug_header_size))
157 /* And some routines to support creation of new "kinds", e.g. with */
158 /* custom mark procedures, by language runtimes. */
159 /* The _inner versions assume the caller holds the allocation lock. */
161 /* Return a new free list array. */
162 GC_API void ** GC_CALL GC_new_free_list(void);
163 GC_API void ** GC_CALL GC_new_free_list_inner(void);
165 /* Return a new kind, as specified. */
166 GC_API unsigned GC_CALL GC_new_kind(void ** /* free_list */,
167 GC_word /* mark_descriptor_template */,
168 int /* add_size_to_descriptor */,
169 int /* clear_new_objects */);
170 /* The last two parameters must be zero or one. */
171 GC_API unsigned GC_CALL GC_new_kind_inner(void ** /* free_list */,
172 GC_word /* mark_descriptor_template */,
173 int /* add_size_to_descriptor */,
174 int /* clear_new_objects */);
176 /* Return a new mark procedure identifier, suitable for use as */
177 /* the first argument in GC_MAKE_PROC. */
178 GC_API unsigned GC_CALL GC_new_proc(GC_mark_proc);
179 GC_API unsigned GC_CALL GC_new_proc_inner(GC_mark_proc);
181 /* Allocate an object of a given kind. Note that in multithreaded */
182 /* contexts, this is usually unsafe for kinds that have the descriptor */
183 /* in the object itself, since there is otherwise a window in which */
184 /* the descriptor is not correct. Even in the single-threaded case, */
185 /* we need to be sure that cleared objects on a free list don't */
186 /* cause a GC crash if they are accidentally traced. */
187 GC_API void * GC_CALL GC_generic_malloc(size_t /* lb */, int /* k */);
189 typedef void (GC_CALLBACK * GC_describe_type_fn)(void * /* p */,
190 char * /* out_buf */);
191 /* A procedure which */
192 /* produces a human-readable */
193 /* description of the "type" of object */
194 /* p into the buffer out_buf of length */
195 /* GC_TYPE_DESCR_LEN. This is used by */
196 /* the debug support when printing */
198 /* These functions should be as robust */
199 /* as possible, though we do avoid */
200 /* invoking them on objects on the */
201 /* global free list. */
202 #define GC_TYPE_DESCR_LEN 40
204 GC_API void GC_CALL GC_register_describe_type_fn(int /* kind */,
205 GC_describe_type_fn);
206 /* Register a describe_type function */
207 /* to be used when printing objects */
208 /* of a particular kind. */
210 /* Clear some of the inaccessible part of the stack. Returns its */
211 /* argument, so it can be used in a tail call position, hence clearing */
212 /* another frame. Argument may be NULL. */
213 GC_API void * GC_CALL GC_clear_stack(void *);
215 /* Set and get the client notifier on collections. The client function */
216 /* is called at the start of every full GC (called with the allocation */
217 /* lock held). May be 0. This is a really tricky interface to use */
218 /* correctly. Unless you really understand the collector internals, */
219 /* the callback should not, directly or indirectly, make any GC_ or */
220 /* potentially blocking calls. In particular, it is not safe to */
221 /* allocate memory using the garbage collector from within the callback */
222 /* function. Both the setter and getter acquire the GC lock. */
223 typedef void (GC_CALLBACK * GC_start_callback_proc)(void);
224 GC_API void GC_CALL GC_set_start_callback(GC_start_callback_proc);
225 GC_API GC_start_callback_proc GC_CALL GC_get_start_callback(void);
228 } /* end of extern "C" */
231 #endif /* GC_MARK_H */