First set of licensing changes
[mono.git] / mono / sgen / sgen-client.h
1 /*
2  * sgen-client.h: SGen client interface.
3  *
4  * Copyright (C) 2014 Xamarin Inc
5  *
6  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7  */
8
9 #include "mono/sgen/sgen-pointer-queue.h"
10
11 /*
12  * Init whatever needs initing.  This is called relatively early in SGen initialization.
13  * Must initialized the small ID for the current thread.
14  */
15 void sgen_client_init (void);
16
17 /*
18  * The slow path for getting an object's size.  We're passing in the vtable because we've
19  * already fetched it.
20  */
21 mword sgen_client_slow_object_get_size (GCVTable vtable, GCObject* o);
22
23 /*
24  * Fill the given range with a dummy object.  If the range is too short to be filled with an
25  * object, null it.  Return `TRUE` if the range was filled with an object, `FALSE` if it was
26  * nulled.
27  */
28 gboolean sgen_client_array_fill_range (char *start, size_t size);
29
30 /*
31  * This is called if the nursery clearing policy at `clear-at-gc`, which is usually only
32  * used for debugging.  If `size` is large enough for the memory to have been filled with a
33  * dummy, object, zero its header.  Note that there might not actually be a header there.
34  */
35 void sgen_client_zero_array_fill_header (void *p, size_t size);
36
37 /*
38  * Return whether the given object is an array fill dummy object.
39  */
40 gboolean sgen_client_object_is_array_fill (GCObject *o);
41
42 /*
43  * Return whether the given finalizable object's finalizer is critical, i.e., needs to run
44  * after all non-critical finalizers have run.
45  */
46 gboolean sgen_client_object_has_critical_finalizer (GCObject *obj);
47
48 /*
49  * Called after an object is enqueued for finalization.  This is a very low-level callback.
50  * It should almost certainly be a NOP.
51  *
52  * FIXME: Can we merge this with `sgen_client_object_has_critical_finalizer()`?
53  */
54 void sgen_client_object_queued_for_finalization (GCObject *obj);
55
56 /*
57  * Run the given object's finalizer.
58  */
59 void sgen_client_run_finalize (GCObject *obj);
60
61 /*
62  * Is called after a collection if there are objects to finalize.  The world is still
63  * stopped.  This will usually notify the finalizer thread that it needs to run.
64  */
65 void sgen_client_finalize_notify (void);
66
67 /*
68  * Returns TRUE if no ephemerons have been marked.  Will be called again if it returned
69  * FALSE.  If ephemerons are not supported, just return TRUE.
70  */
71 gboolean sgen_client_mark_ephemerons (ScanCopyContext ctx);
72
73 /*
74  * Clear ephemeron pairs with unreachable keys.
75  * We pass the copy func so we can figure out if an array was promoted or not.
76  */
77 void sgen_client_clear_unreachable_ephemerons (ScanCopyContext ctx);
78
79 /*
80  * May return NULL.  Must be an aligned pointer.
81  */
82 gpointer sgen_client_default_metadata (void);
83 gpointer sgen_client_metadata_for_object (GCObject *obj);
84
85 /*
86  * No action required.
87  */
88 void sgen_client_gchandle_created (int handle_type, GCObject *obj, guint32 handle);
89 void sgen_client_gchandle_destroyed (int handle_type, guint32 handle);
90 void sgen_client_ensure_weak_gchandles_accessible (void);
91
92 /*
93  * This is called for objects that are larger than one card.  If it's possible to scan only
94  * parts of the object based on which cards are marked, do so and return TRUE.  Otherwise,
95  * return FALSE.
96  */
97 gboolean sgen_client_cardtable_scan_object (GCObject *obj, mword block_obj_size, guint8 *cards, ScanCopyContext ctx);
98
99 /*
100  * Called after nursery objects have been pinned.  No action is necessary.
101  */
102 void sgen_client_nursery_objects_pinned (void **definitely_pinned, int count);
103
104 /*
105  * Called at a semi-random point during minor collections.  No action is necessary.
106  */
107 void sgen_client_collecting_minor (SgenPointerQueue *fin_ready_queue, SgenPointerQueue *critical_fin_queue);
108
109 /*
110  * Called at semi-random points during major collections.  No action is necessary.
111  */
112 void sgen_client_collecting_major_1 (void);
113 void sgen_client_collecting_major_2 (void);
114 void sgen_client_collecting_major_3 (SgenPointerQueue *fin_ready_queue, SgenPointerQueue *critical_fin_queue);
115
116 /*
117  * Called after a LOS object has been pinned.  No action is necessary.
118  */
119 void sgen_client_pinned_los_object (GCObject *obj);
120
121 /*
122  * Called for every degraded allocation.  No action is necessary.
123  */
124 void sgen_client_degraded_allocation (size_t size);
125
126 /*
127  * Called whenever the amount of memory allocated for the managed heap changes.  No action
128  * is necessary.
129  */
130 void sgen_client_total_allocated_heap_changed (size_t allocated_heap_size);
131
132 /*
133  * If the client has registered any internal memory types, this must return a string
134  * describing the given type.  Only used for debugging.
135  */
136 const char* sgen_client_description_for_internal_mem_type (int type);
137
138 /*
139  * Only used for debugging.  `sgen_client_vtable_get_namespace()` may return NULL.
140  */
141 gboolean sgen_client_vtable_is_inited (GCVTable vtable);
142 const char* sgen_client_vtable_get_namespace (GCVTable vtable);
143 const char* sgen_client_vtable_get_name (GCVTable vtable);
144
145 /*
146  * Called before starting collections.  The world is already stopped.  No action is
147  * necessary.
148  */
149 void sgen_client_pre_collection_checks (void);
150
151 /*
152  * Must set the thread's thread info to `info`.  If the thread's small ID was not already
153  * initialized in `sgen_client_init()` (for the main thread, usually), it must be done here.
154  *
155  * `stack_bottom_fallback` is the value passed through via `sgen_thread_register()`.
156  */
157 void sgen_client_thread_register (SgenThreadInfo* info, void *stack_bottom_fallback);
158
159 void sgen_client_thread_unregister (SgenThreadInfo *p);
160
161 /*
162  * Called on each worker thread when it starts up.  Must initialize the thread's small ID.
163  */
164 void sgen_client_thread_register_worker (void);
165
166 /*
167  * The least this function needs to do is scan all registers and thread stacks.  To do this
168  * conservatively, use `sgen_conservatively_pin_objects_from()`.
169  */
170 void sgen_client_scan_thread_data (void *start_nursery, void *end_nursery, gboolean precise, ScanCopyContext ctx);
171
172 /*
173  * Stop and restart the world, i.e., all threads that interact with the managed heap.  For
174  * single-threaded programs this is a nop.
175  */
176 void sgen_client_stop_world (int generation);
177 void sgen_client_restart_world (int generation, GGTimingInfo *timing);
178
179 /*
180  * Must return FALSE.  The bridge is not supported outside of Mono.
181  */
182 gboolean sgen_client_bridge_need_processing (void);
183
184 /*
185  * None of these should ever be called.
186  */
187 void sgen_client_bridge_reset_data (void);
188 void sgen_client_bridge_processing_stw_step (void);
189 void sgen_client_bridge_wait_for_processing (void);
190 void sgen_client_bridge_processing_finish (int generation);
191 gboolean sgen_client_bridge_is_bridge_object (GCObject *obj);
192 void sgen_client_bridge_register_finalized_object (GCObject *object);
193
194 /*
195  * No action is necessary.
196  */
197 void sgen_client_mark_togglerefs (char *start, char *end, ScanCopyContext ctx);
198 void sgen_client_clear_togglerefs (char *start, char *end, ScanCopyContext ctx);
199
200 /*
201  * Called after collections, reporting the amount of time they took.  No action is
202  * necessary.
203  */
204 void sgen_client_log_timing (GGTimingInfo *info, mword last_major_num_sections, mword last_los_memory_usage);
205
206 /*
207  * Called to handle `MONO_GC_PARAMS` and `MONO_GC_DEBUG` options.  The `handle` functions
208  * must return TRUE if they have recognized and processed the option, FALSE otherwise.
209  */
210 gboolean sgen_client_handle_gc_param (const char *opt);
211 void sgen_client_print_gc_params_usage (void);
212 gboolean sgen_client_handle_gc_debug (const char *opt);
213 void sgen_client_print_gc_debug_usage (void);
214
215 /*
216  * Called to obtain an identifier for the current location, such as a method pointer. This
217  * is used for logging the provenances of allocations with the heavy binary protocol.
218  */
219 gpointer sgen_client_get_provenance (void);
220
221 /*
222  * Called by the debugging infrastructure to describe pointers that have an invalid vtable.
223  * Should usually print to `stdout`.
224  */
225 void sgen_client_describe_invalid_pointer (GCObject *ptr);
226
227 /*
228  * These client binary protocol functions are called from the respective binary protocol
229  * functions.  No action is necessary.  We suggest implementing them as inline functions in
230  * the client header file so that no overhead is incurred if they don't actually do
231  * anything.
232  */
233
234 #define TYPE_INT int
235 #define TYPE_LONGLONG long long
236 #define TYPE_SIZE size_t
237 #define TYPE_POINTER gpointer
238 #define TYPE_BOOL gboolean
239
240 #define BEGIN_PROTOCOL_ENTRY0(method) \
241         void sgen_client_ ## method (void);
242 #define BEGIN_PROTOCOL_ENTRY_HEAVY0(method) \
243         void sgen_client_ ## method (void);
244 #define BEGIN_PROTOCOL_ENTRY1(method,t1,f1) \
245         void sgen_client_ ## method (t1 f1);
246 #define BEGIN_PROTOCOL_ENTRY_HEAVY1(method,t1,f1) \
247         void sgen_client_ ## method (t1 f1);
248 #define BEGIN_PROTOCOL_ENTRY2(method,t1,f1,t2,f2) \
249         void sgen_client_ ## method (t1 f1, t2 f2);
250 #define BEGIN_PROTOCOL_ENTRY_HEAVY2(method,t1,f1,t2,f2) \
251         void sgen_client_ ## method (t1 f1, t2 f2);
252 #define BEGIN_PROTOCOL_ENTRY3(method,t1,f1,t2,f2,t3,f3) \
253         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3);
254 #define BEGIN_PROTOCOL_ENTRY_HEAVY3(method,t1,f1,t2,f2,t3,f3) \
255         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3);
256 #define BEGIN_PROTOCOL_ENTRY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
257         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3, t4 f4);
258 #define BEGIN_PROTOCOL_ENTRY_HEAVY4(method,t1,f1,t2,f2,t3,f3,t4,f4) \
259         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3, t4 f4);
260 #define BEGIN_PROTOCOL_ENTRY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
261         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5);
262 #define BEGIN_PROTOCOL_ENTRY_HEAVY5(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5) \
263         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5);
264 #define BEGIN_PROTOCOL_ENTRY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
265         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5, t6 f6);
266 #define BEGIN_PROTOCOL_ENTRY_HEAVY6(method,t1,f1,t2,f2,t3,f3,t4,f4,t5,f5,t6,f6) \
267         void sgen_client_ ## method (t1 f1, t2 f2, t3 f3, t4 f4, t5 f5, t6 f6);
268
269 #define DEFAULT_PRINT()
270 #define CUSTOM_PRINT(_)
271
272 #define IS_ALWAYS_MATCH(_)
273 #define MATCH_INDEX(_)
274 #define IS_VTABLE_MATCH(_)
275
276 #define END_PROTOCOL_ENTRY
277 #define END_PROTOCOL_ENTRY_FLUSH
278 #define END_PROTOCOL_ENTRY_HEAVY
279
280 #include "sgen-protocol-def.h"
281
282 #undef TYPE_INT
283 #undef TYPE_LONGLONG
284 #undef TYPE_SIZE
285 #undef TYPE_POINTER
286 #undef TYPE_BOOL
287
288 #ifdef SGEN_WITHOUT_MONO
289 /*
290  * Get the current thread's thread info.  This will only be called on managed threads.
291  */
292 SgenThreadInfo* mono_thread_info_current (void);
293
294 /*
295  * Get the current thread's small ID.  This will be called on managed and worker threads.
296  */
297 int mono_thread_info_get_small_id (void);
298 #endif