[sgen] Make GC handles table lock-free.
[mono.git] / mono / metadata / boehm-gc.c
1 /*
2  * boehm-gc.c: GC implementation using either the installed or included Boehm GC.
3  *
4  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
5  * Copyright 2004-2011 Novell, Inc (http://www.novell.com)
6  * Copyright 2011-2012 Xamarin, Inc (http://www.xamarin.com)
7  */
8
9 #include "config.h"
10
11 #include <string.h>
12
13 #define GC_I_HIDE_POINTERS
14 #include <mono/metadata/gc-internal.h>
15 #include <mono/metadata/mono-gc.h>
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/class-internals.h>
18 #include <mono/metadata/method-builder.h>
19 #include <mono/metadata/opcodes.h>
20 #include <mono/metadata/domain-internals.h>
21 #include <mono/metadata/metadata-internals.h>
22 #include <mono/metadata/marshal.h>
23 #include <mono/metadata/runtime.h>
24 #include <mono/metadata/sgen-toggleref.h>
25 #include <mono/utils/atomic.h>
26 #include <mono/utils/mono-logger-internal.h>
27 #include <mono/utils/mono-memory-model.h>
28 #include <mono/utils/mono-time.h>
29 #include <mono/utils/mono-threads.h>
30 #include <mono/utils/dtrace.h>
31 #include <mono/utils/gc_wrapper.h>
32 #include <mono/utils/mono-mutex.h>
33 #include <mono/utils/mono-counters.h>
34
35 #if HAVE_BOEHM_GC
36
37 #undef TRUE
38 #undef FALSE
39 #define THREAD_LOCAL_ALLOC 1
40 #include "private/pthread_support.h"
41
42 #if defined(PLATFORM_MACOSX) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
43 void *pthread_get_stackaddr_np(pthread_t);
44 #endif
45
46 #define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
47 /*Boehm max heap cannot be smaller than 16MB*/
48 #define MIN_BOEHM_MAX_HEAP_SIZE_IN_MB 16
49 #define MIN_BOEHM_MAX_HEAP_SIZE (MIN_BOEHM_MAX_HEAP_SIZE_IN_MB << 20)
50
51 static gboolean gc_initialized = FALSE;
52 static mono_mutex_t mono_gc_lock;
53
54 static void*
55 boehm_thread_register (MonoThreadInfo* info, void *baseptr);
56 static void
57 boehm_thread_unregister (MonoThreadInfo *p);
58 static void
59 register_test_toggleref_callback (void);
60
61 #define BOEHM_GC_BIT_FINALIZER_AWARE 1
62 static MonoGCFinalizerCallbacks fin_callbacks;
63
64 static void
65 mono_gc_warning (char *msg, GC_word arg)
66 {
67         mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
68 }
69
70 void
71 mono_gc_base_init (void)
72 {
73         MonoThreadInfoCallbacks cb;
74         const char *env;
75         int dummy;
76
77         if (gc_initialized)
78                 return;
79
80         mono_counters_init ();
81
82         /*
83          * Handle the case when we are called from a thread different from the main thread,
84          * confusing libgc.
85          * FIXME: Move this to libgc where it belongs.
86          *
87          * we used to do this only when running on valgrind,
88          * but it happens also in other setups.
89          */
90 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK) && !defined(__native_client__)
91         {
92                 size_t size;
93                 void *sstart;
94                 pthread_attr_t attr;
95                 pthread_getattr_np (pthread_self (), &attr);
96                 pthread_attr_getstack (&attr, &sstart, &size);
97                 pthread_attr_destroy (&attr); 
98                 /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
99 #ifdef __ia64__
100                 /*
101                  * The calculation above doesn't seem to work on ia64, also we need to set
102                  * GC_register_stackbottom as well, but don't know how.
103                  */
104 #else
105                 /* apparently with some linuxthreads implementations sstart can be NULL,
106                  * fallback to the more imprecise method (bug# 78096).
107                  */
108                 if (sstart) {
109                         GC_stackbottom = (char*)sstart + size;
110                 } else {
111                         int dummy;
112                         gsize stack_bottom = (gsize)&dummy;
113                         stack_bottom += 4095;
114                         stack_bottom &= ~4095;
115                         GC_stackbottom = (char*)stack_bottom;
116                 }
117 #endif
118         }
119 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
120                 GC_stackbottom = (char*)pthread_get_stackaddr_np (pthread_self ());
121 #elif defined(__OpenBSD__)
122 #  include <pthread_np.h>
123         {
124                 stack_t ss;
125                 int rslt;
126
127                 rslt = pthread_stackseg_np(pthread_self(), &ss);
128                 g_assert (rslt == 0);
129
130                 GC_stackbottom = (char*)ss.ss_sp;
131         }
132 #elif defined(__native_client__)
133         /* Do nothing, GC_stackbottom is set correctly in libgc */
134 #else
135         {
136                 int dummy;
137                 gsize stack_bottom = (gsize)&dummy;
138                 stack_bottom += 4095;
139                 stack_bottom &= ~4095;
140                 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
141                 GC_stackbottom = (char*)stack_bottom;
142         }
143 #endif
144
145 #if !defined(PLATFORM_ANDROID)
146         /* If GC_no_dls is set to true, GC_find_limit is not called. This causes a seg fault on Android. */
147         GC_no_dls = TRUE;
148 #endif
149         {
150                 if ((env = g_getenv ("MONO_GC_DEBUG"))) {
151                         char **opts = g_strsplit (env, ",", -1);
152                         for (char **ptr = opts; ptr && *ptr; ptr ++) {
153                                 char *opt = *ptr;
154                                 if (!strcmp (opt, "do-not-finalize")) {
155                                         do_not_finalize = 1;
156                                 } else if (!strcmp (opt, "log-finalizers")) {
157                                         log_finalizers = 1;
158                                 }
159                         }
160                 }
161         }
162
163         GC_init ();
164
165         GC_oom_fn = mono_gc_out_of_memory;
166         GC_set_warn_proc (mono_gc_warning);
167         GC_finalize_on_demand = 1;
168         GC_finalizer_notifier = mono_gc_finalize_notify;
169
170         GC_init_gcj_malloc (5, NULL);
171
172         if ((env = g_getenv ("MONO_GC_PARAMS"))) {
173                 char **ptr, **opts = g_strsplit (env, ",", -1);
174                 for (ptr = opts; *ptr; ++ptr) {
175                         char *opt = *ptr;
176                         if (g_str_has_prefix (opt, "max-heap-size=")) {
177                                 size_t max_heap;
178
179                                 opt = strchr (opt, '=') + 1;
180                                 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &max_heap)) {
181                                         if (max_heap < MIN_BOEHM_MAX_HEAP_SIZE) {
182                                                 fprintf (stderr, "max-heap-size must be at least %dMb.\n", MIN_BOEHM_MAX_HEAP_SIZE_IN_MB);
183                                                 exit (1);
184                                         }
185                                         GC_set_max_heap_size (max_heap);
186                                 } else {
187                                         fprintf (stderr, "max-heap-size must be an integer.\n");
188                                         exit (1);
189                                 }
190                                 continue;
191                         } else if (g_str_has_prefix (opt, "toggleref-test")) {
192                                 register_test_toggleref_callback ();
193                                 continue;
194                         } else {
195                                 /* Could be a parameter for sgen */
196                                 /*
197                                 fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
198                                 fprintf (stderr, "  max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
199                                 exit (1);
200                                 */
201                         }
202                 }
203                 g_strfreev (opts);
204         }
205
206         memset (&cb, 0, sizeof (cb));
207         cb.thread_register = boehm_thread_register;
208         cb.thread_unregister = boehm_thread_unregister;
209         cb.mono_method_is_critical = (gpointer)mono_runtime_is_critical_method;
210
211         mono_threads_init (&cb, sizeof (MonoThreadInfo));
212         mono_mutex_init (&mono_gc_lock);
213
214         mono_thread_info_attach (&dummy);
215
216         mono_gc_enable_events ();
217         gc_initialized = TRUE;
218 }
219
220 void
221 mono_gc_base_cleanup (void)
222 {
223         GC_finalizer_notifier = NULL;
224 }
225
226 /**
227  * mono_gc_collect:
228  * @generation: GC generation identifier
229  *
230  * Perform a garbage collection for the given generation, higher numbers
231  * mean usually older objects. Collecting a high-numbered generation
232  * implies collecting also the lower-numbered generations.
233  * The maximum value for @generation can be retrieved with a call to
234  * mono_gc_max_generation(), so this function is usually called as:
235  *
236  *      mono_gc_collect (mono_gc_max_generation ());
237  */
238 void
239 mono_gc_collect (int generation)
240 {
241 #ifndef DISABLE_PERFCOUNTERS
242         mono_perfcounters->gc_induced++;
243 #endif
244         GC_gcollect ();
245 }
246
247 /**
248  * mono_gc_max_generation:
249  *
250  * Get the maximum generation number used by the current garbage
251  * collector. The value will be 0 for the Boehm collector, 1 or more
252  * for the generational collectors.
253  *
254  * Returns: the maximum generation number.
255  */
256 int
257 mono_gc_max_generation (void)
258 {
259         return 0;
260 }
261
262 /**
263  * mono_gc_get_generation:
264  * @object: a managed object
265  *
266  * Get the garbage collector's generation that @object belongs to.
267  * Use this has a hint only.
268  *
269  * Returns: a garbage collector generation number
270  */
271 int
272 mono_gc_get_generation  (MonoObject *object)
273 {
274         return 0;
275 }
276
277 /**
278  * mono_gc_collection_count:
279  * @generation: a GC generation number
280  *
281  * Get how many times a garbage collection has been performed
282  * for the given @generation number.
283  *
284  * Returns: the number of garbage collections
285  */
286 int
287 mono_gc_collection_count (int generation)
288 {
289         return GC_gc_no;
290 }
291
292 /**
293  * mono_gc_add_memory_pressure:
294  * @value: amount of bytes
295  *
296  * Adjust the garbage collector's view of how many bytes of memory
297  * are indirectly referenced by managed objects (for example unmanaged
298  * memory holding image or other binary data).
299  * This is a hint only to the garbage collector algorithm.
300  * Note that negative amounts of @value will decrease the memory
301  * pressure.
302  */
303 void
304 mono_gc_add_memory_pressure (gint64 value)
305 {
306 }
307
308 /**
309  * mono_gc_get_used_size:
310  *
311  * Get the approximate amount of memory used by managed objects.
312  *
313  * Returns: the amount of memory used in bytes
314  */
315 int64_t
316 mono_gc_get_used_size (void)
317 {
318         return GC_get_heap_size () - GC_get_free_bytes ();
319 }
320
321 /**
322  * mono_gc_get_heap_size:
323  *
324  * Get the amount of memory used by the garbage collector.
325  *
326  * Returns: the size of the heap in bytes
327  */
328 int64_t
329 mono_gc_get_heap_size (void)
330 {
331         return GC_get_heap_size ();
332 }
333
334 gboolean
335 mono_gc_is_gc_thread (void)
336 {
337         return GC_thread_is_registered ();
338 }
339
340 extern int GC_thread_register_foreign (void *base_addr);
341
342 gboolean
343 mono_gc_register_thread (void *baseptr)
344 {
345         return mono_thread_info_attach (baseptr) != NULL;
346 }
347
348 static void*
349 boehm_thread_register (MonoThreadInfo* info, void *baseptr)
350 {
351         if (mono_gc_is_gc_thread())
352                 return info;
353 #if !defined(HOST_WIN32)
354         return GC_thread_register_foreign (baseptr) ? info : NULL;
355 #else
356         return NULL;
357 #endif
358 }
359
360 static void
361 boehm_thread_unregister (MonoThreadInfo *p)
362 {
363         MonoNativeThreadId tid;
364
365         tid = mono_thread_info_get_tid (p);
366
367         if (p->runtime_thread)
368                 mono_threads_add_joinable_thread ((gpointer)tid);
369 }
370
371 gboolean
372 mono_object_is_alive (MonoObject* o)
373 {
374         return GC_is_marked ((gpointer)o);
375 }
376
377 int
378 mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
379 {
380         return 1;
381 }
382
383 static gint64 gc_start_time;
384
385 static void
386 on_gc_notification (GCEventType event)
387 {
388         MonoGCEvent e = (MonoGCEvent)event;
389
390         switch (e) {
391         case MONO_GC_EVENT_PRE_STOP_WORLD:
392                 MONO_GC_WORLD_STOP_BEGIN ();
393                 mono_thread_info_suspend_lock ();
394                 break;
395
396         case MONO_GC_EVENT_POST_STOP_WORLD:
397                 MONO_GC_WORLD_STOP_END ();
398                 break;
399
400         case MONO_GC_EVENT_PRE_START_WORLD:
401                 MONO_GC_WORLD_RESTART_BEGIN (1);
402                 break;
403
404         case MONO_GC_EVENT_POST_START_WORLD:
405                 MONO_GC_WORLD_RESTART_END (1);
406                 mono_thread_info_suspend_unlock ();
407                 break;
408
409         case MONO_GC_EVENT_START:
410                 MONO_GC_BEGIN (1);
411 #ifndef DISABLE_PERFCOUNTERS
412                 if (mono_perfcounters)
413                         mono_perfcounters->gc_collections0++;
414 #endif
415                 gc_stats.major_gc_count ++;
416                 gc_start_time = mono_100ns_ticks ();
417                 break;
418
419         case MONO_GC_EVENT_END:
420                 MONO_GC_END (1);
421 #if defined(ENABLE_DTRACE) && defined(__sun__)
422                 /* This works around a dtrace -G problem on Solaris.
423                    Limit its actual use to when the probe is enabled. */
424                 if (MONO_GC_END_ENABLED ())
425                         sleep(0);
426 #endif
427
428 #ifndef DISABLE_PERFCOUNTERS
429                 if (mono_perfcounters) {
430                         guint64 heap_size = GC_get_heap_size ();
431                         guint64 used_size = heap_size - GC_get_free_bytes ();
432                         mono_perfcounters->gc_total_bytes = used_size;
433                         mono_perfcounters->gc_committed_bytes = heap_size;
434                         mono_perfcounters->gc_reserved_bytes = heap_size;
435                         mono_perfcounters->gc_gen0size = heap_size;
436                 }
437 #endif
438                 gc_stats.major_gc_time += mono_100ns_ticks () - gc_start_time;
439                 mono_trace_message (MONO_TRACE_GC, "gc took %d usecs", (mono_100ns_ticks () - gc_start_time) / 10);
440                 break;
441         default:
442                 break;
443         }
444
445         mono_profiler_gc_event (e, 0);
446 }
447  
448 static void
449 on_gc_heap_resize (size_t new_size)
450 {
451         guint64 heap_size = GC_get_heap_size ();
452 #ifndef DISABLE_PERFCOUNTERS
453         if (mono_perfcounters) {
454                 mono_perfcounters->gc_committed_bytes = heap_size;
455                 mono_perfcounters->gc_reserved_bytes = heap_size;
456                 mono_perfcounters->gc_gen0size = heap_size;
457         }
458 #endif
459         mono_profiler_gc_heap_resize (new_size);
460 }
461
462 void
463 mono_gc_enable_events (void)
464 {
465         GC_notify_event = on_gc_notification;
466         GC_on_heap_resize = on_gc_heap_resize;
467 }
468
469 static gboolean alloc_events = FALSE;
470
471 void
472 mono_gc_enable_alloc_events (void)
473 {
474         alloc_events = TRUE;
475 }
476
477 int
478 mono_gc_register_root (char *start, size_t size, void *descr, MonoGCRootSource source, const char *msg)
479 {
480         /* for some strange reason, they want one extra byte on the end */
481         GC_add_roots (start, start + size + 1);
482
483         return TRUE;
484 }
485
486 void
487 mono_gc_deregister_root (char* addr)
488 {
489 #ifndef HOST_WIN32
490         /* FIXME: libgc doesn't define this work win32 for some reason */
491         /* FIXME: No size info */
492         GC_remove_roots (addr, addr + sizeof (gpointer) + 1);
493 #endif
494 }
495
496 void
497 mono_gc_weak_link_register (volatile gpointer *link_addr, MonoObject *obj, gboolean track)
498 {
499         if (track)
500                 GC_REGISTER_LONG_LINK ((gpointer *)link_addr, obj);
501         else
502                 GC_GENERAL_REGISTER_DISAPPEARING_LINK ((gpointer *)link_addr, obj);
503 }
504
505 void
506 mono_gc_weak_link_unregister (volatile gpointer *link_addr, gboolean track)
507 {
508         if (track)
509                 GC_unregister_long_link ((gpointer *)link_addr);
510         else
511                 GC_unregister_disappearing_link ((gpointer *)link_addr);
512 }
513
514 void
515 mono_gc_ensure_weak_links_accessible (void)
516 {
517 }
518
519 void*
520 mono_gc_make_descr_for_string (gsize *bitmap, int numbits)
521 {
522         return mono_gc_make_descr_from_bitmap (bitmap, numbits);
523 }
524
525 void*
526 mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size)
527 {
528         return mono_gc_make_descr_from_bitmap (bitmap, numbits);
529 }
530
531 void*
532 mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size)
533 {
534         /* libgc has no usable support for arrays... */
535         return GC_NO_DESCRIPTOR;
536 }
537
538 void*
539 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
540 {
541         /* It seems there are issues when the bitmap doesn't fit: play it safe */
542         if (numbits >= 30)
543                 return GC_NO_DESCRIPTOR;
544         else
545                 return (gpointer)GC_make_descriptor ((GC_bitmap)bitmap, numbits);
546 }
547
548 void*
549 mono_gc_make_root_descr_all_refs (int numbits)
550 {
551         return NULL;
552 }
553
554 void*
555 mono_gc_alloc_fixed (size_t size, void *descr, MonoGCRootSource source, const char *msg)
556 {
557         /* To help track down typed allocation bugs */
558         /*
559         static int count;
560         count ++;
561         if (count == atoi (g_getenv ("COUNT2")))
562                 printf ("HIT!\n");
563         if (count > atoi (g_getenv ("COUNT2")))
564                 return GC_MALLOC (size);
565         */
566
567         if (descr)
568                 return GC_MALLOC_EXPLICITLY_TYPED (size, (GC_descr)descr);
569         else
570                 return GC_MALLOC (size);
571 }
572
573 void
574 mono_gc_free_fixed (void* addr)
575 {
576 }
577
578 void *
579 mono_gc_alloc_obj (MonoVTable *vtable, size_t size)
580 {
581         MonoObject *obj;
582
583         if (!vtable->klass->has_references) {
584                 obj = GC_MALLOC_ATOMIC (size);
585
586                 obj->vtable = vtable;
587                 obj->synchronisation = NULL;
588
589                 memset ((char *) obj + sizeof (MonoObject), 0, size - sizeof (MonoObject));
590         } else if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
591                 obj = GC_GCJ_MALLOC (size, vtable);
592         } else {
593                 obj = GC_MALLOC (size);
594
595                 obj->vtable = vtable;
596         }
597
598         if (G_UNLIKELY (alloc_events))
599                 mono_profiler_allocation (obj);
600
601         return obj;
602 }
603
604 void *
605 mono_gc_alloc_vector (MonoVTable *vtable, size_t size, uintptr_t max_length)
606 {
607         MonoArray *obj;
608
609         if (!vtable->klass->has_references) {
610                 obj = GC_MALLOC_ATOMIC (size);
611
612                 obj->obj.vtable = vtable;
613                 obj->obj.synchronisation = NULL;
614
615                 memset ((char *) obj + sizeof (MonoObject), 0, size - sizeof (MonoObject));
616         } else if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
617                 obj = GC_GCJ_MALLOC (size, vtable);
618         } else {
619                 obj = GC_MALLOC (size);
620
621                 obj->obj.vtable = vtable;
622         }
623
624         obj->max_length = max_length;
625
626         if (G_UNLIKELY (alloc_events))
627                 mono_profiler_allocation (&obj->obj);
628
629         return obj;
630 }
631
632 void *
633 mono_gc_alloc_array (MonoVTable *vtable, size_t size, uintptr_t max_length, uintptr_t bounds_size)
634 {
635         MonoArray *obj;
636
637         if (!vtable->klass->has_references) {
638                 obj = GC_MALLOC_ATOMIC (size);
639
640                 obj->obj.vtable = vtable;
641                 obj->obj.synchronisation = NULL;
642
643                 memset ((char *) obj + sizeof (MonoObject), 0, size - sizeof (MonoObject));
644         } else if (vtable->gc_descr != GC_NO_DESCRIPTOR) {
645                 obj = GC_GCJ_MALLOC (size, vtable);
646         } else {
647                 obj = GC_MALLOC (size);
648
649                 obj->obj.vtable = vtable;
650         }
651
652         obj->max_length = max_length;
653
654         if (bounds_size)
655                 obj->bounds = (MonoArrayBounds *) ((char *) obj + size - bounds_size);
656
657         if (G_UNLIKELY (alloc_events))
658                 mono_profiler_allocation (&obj->obj);
659
660         return obj;
661 }
662
663 void *
664 mono_gc_alloc_string (MonoVTable *vtable, size_t size, gint32 len)
665 {
666         MonoString *obj = GC_MALLOC_ATOMIC (size);
667
668         obj->object.vtable = vtable;
669         obj->object.synchronisation = NULL;
670         obj->length = len;
671         obj->chars [len] = 0;
672
673         if (G_UNLIKELY (alloc_events))
674                 mono_profiler_allocation (&obj->object);
675
676         return obj;
677 }
678
679 int
680 mono_gc_invoke_finalizers (void)
681 {
682         /* There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
683          * the 'mem_freed' variable is not initialized when there are no
684          * objects to finalize, which leads to strange behavior later on.
685          * The check is necessary to work around that bug.
686          */
687         if (GC_should_invoke_finalizers ())
688                 return GC_invoke_finalizers ();
689         return 0;
690 }
691
692 gboolean
693 mono_gc_pending_finalizers (void)
694 {
695         return GC_should_invoke_finalizers ();
696 }
697
698 void
699 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
700 {
701         *(void**)field_ptr = value;
702 }
703
704 void
705 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
706 {
707         *(void**)slot_ptr = value;
708 }
709
710 void
711 mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
712 {
713         mono_gc_memmove_aligned (dest_ptr, src_ptr, count * sizeof (gpointer));
714 }
715
716 void
717 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
718 {
719         *(void**)ptr = value;
720 }
721
722 void
723 mono_gc_wbarrier_generic_store_atomic (gpointer ptr, MonoObject *value)
724 {
725         InterlockedWritePointer (ptr, value);
726 }
727
728 void
729 mono_gc_wbarrier_generic_nostore (gpointer ptr)
730 {
731 }
732
733 void
734 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
735 {
736         mono_gc_memmove_atomic (dest, src, count * mono_class_value_size (klass, NULL));
737 }
738
739 void
740 mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
741 {
742         /* do not copy the sync state */
743         mono_gc_memmove_aligned ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
744                         mono_object_class (obj)->instance_size - sizeof (MonoObject));
745 }
746
747 void
748 mono_gc_clear_domain (MonoDomain *domain)
749 {
750 }
751
752 int
753 mono_gc_get_suspend_signal (void)
754 {
755         return GC_get_suspend_signal ();
756 }
757
758 int
759 mono_gc_get_restart_signal (void)
760 {
761         return GC_get_restart_signal ();
762 }
763
764 #if defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
765 extern __thread MONO_TLS_FAST void* GC_thread_tls;
766 #include "metadata-internals.h"
767
768 static int
769 shift_amount (int v)
770 {
771         int i = 0;
772         while (!(v & (1 << i)))
773                 i++;
774         return i;
775 }
776
777 enum {
778         ATYPE_FREEPTR,
779         ATYPE_FREEPTR_FOR_BOX,
780         ATYPE_NORMAL,
781         ATYPE_GCJ,
782         ATYPE_STRING,
783         ATYPE_NUM
784 };
785
786 static MonoMethod*
787 create_allocator (int atype, int tls_key, gboolean slowpath)
788 {
789         int index_var, bytes_var, my_fl_var, my_entry_var;
790         guint32 no_freelist_branch, not_small_enough_branch = 0;
791         guint32 size_overflow_branch = 0;
792         MonoMethodBuilder *mb;
793         MonoMethod *res;
794         MonoMethodSignature *csig;
795         const char *name = NULL;
796         AllocatorWrapperInfo *info;
797
798         if (atype == ATYPE_FREEPTR) {
799                 name = slowpath ? "SlowAllocPtrfree" : "AllocPtrfree";
800         } else if (atype == ATYPE_FREEPTR_FOR_BOX) {
801                 name = slowpath ? "SlowAllocPtrfreeBox" : "AllocPtrfreeBox";
802         } else if (atype == ATYPE_NORMAL) {
803                 name = slowpath ? "SlowAlloc" : "Alloc";
804         } else if (atype == ATYPE_GCJ) {
805                 name = slowpath ? "SlowAllocGcj" : "AllocGcj";
806         } else if (atype == ATYPE_STRING) {
807                 name = slowpath ? "SlowAllocString" : "AllocString";
808         } else {
809                 g_assert_not_reached ();
810         }
811
812         csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
813
814         if (atype == ATYPE_STRING) {
815                 csig->ret = &mono_defaults.string_class->byval_arg;
816                 csig->params [0] = &mono_defaults.int_class->byval_arg;
817                 csig->params [1] = &mono_defaults.int32_class->byval_arg;
818         } else {
819                 csig->ret = &mono_defaults.object_class->byval_arg;
820                 csig->params [0] = &mono_defaults.int_class->byval_arg;
821                 csig->params [1] = &mono_defaults.int32_class->byval_arg;
822         }
823
824         mb = mono_mb_new (mono_defaults.object_class, name, MONO_WRAPPER_ALLOC);
825
826         if (slowpath)
827                 goto always_slowpath;
828
829         bytes_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
830         if (atype == ATYPE_STRING) {
831                 /* a string alloator method takes the args: (vtable, len) */
832                 /* bytes = (offsetof (MonoString, chars) + ((len + 1) * 2)); */
833                 mono_mb_emit_ldarg (mb, 1);
834                 mono_mb_emit_icon (mb, 1);
835                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
836                 mono_mb_emit_icon (mb, 1);
837                 mono_mb_emit_byte (mb, MONO_CEE_SHL);
838                 // sizeof (MonoString) might include padding
839                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, chars));
840                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
841                 mono_mb_emit_stloc (mb, bytes_var);
842         } else {
843                 mono_mb_emit_ldarg (mb, 1);
844                 mono_mb_emit_stloc (mb, bytes_var);
845         }
846
847         /* this is needed for strings/arrays only as the other big types are never allocated with this method */
848         if (atype == ATYPE_STRING) {
849                 /* check for size */
850                 /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
851                 mono_mb_emit_ldloc (mb, bytes_var);
852                 mono_mb_emit_icon (mb, (NFREELISTS-1) * GRANULARITY);
853                 not_small_enough_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
854                 /* check for overflow */
855                 mono_mb_emit_ldloc (mb, bytes_var);
856                 mono_mb_emit_icon (mb, sizeof (MonoString));
857                 size_overflow_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
858         }
859
860         /* int index = INDEX_FROM_BYTES(bytes); */
861         index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
862         
863         mono_mb_emit_ldloc (mb, bytes_var);
864         mono_mb_emit_icon (mb, GRANULARITY - 1);
865         mono_mb_emit_byte (mb, MONO_CEE_ADD);
866         mono_mb_emit_icon (mb, shift_amount (GRANULARITY));
867         mono_mb_emit_byte (mb, MONO_CEE_SHR_UN);
868         mono_mb_emit_icon (mb, shift_amount (sizeof (gpointer)));
869         mono_mb_emit_byte (mb, MONO_CEE_SHL);
870         /* index var is already adjusted into bytes */
871         mono_mb_emit_stloc (mb, index_var);
872
873         my_fl_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
874         my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
875         /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
876         mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
877         mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
878         mono_mb_emit_i4 (mb, tls_key);
879         if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
880                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
881         else if (atype == ATYPE_NORMAL)
882                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, normal_freelists));
883         else if (atype == ATYPE_GCJ)
884                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, gcj_freelists));
885         else
886                 g_assert_not_reached ();
887         mono_mb_emit_byte (mb, MONO_CEE_ADD);
888         mono_mb_emit_ldloc (mb, index_var);
889         mono_mb_emit_byte (mb, MONO_CEE_ADD);
890         mono_mb_emit_stloc (mb, my_fl_var);
891
892         /* my_entry = *my_fl; */
893         mono_mb_emit_ldloc (mb, my_fl_var);
894         mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
895         mono_mb_emit_stloc (mb, my_entry_var);
896
897         /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
898         mono_mb_emit_ldloc (mb, my_entry_var);
899         mono_mb_emit_icon (mb, HBLKSIZE);
900         no_freelist_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
901
902         /* ptr_t next = obj_link(my_entry); *my_fl = next; */
903         mono_mb_emit_ldloc (mb, my_fl_var);
904         mono_mb_emit_ldloc (mb, my_entry_var);
905         mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
906         mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
907
908         /* set the vtable and clear the words in the object */
909         mono_mb_emit_ldloc (mb, my_entry_var);
910         mono_mb_emit_ldarg (mb, 0);
911         mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
912
913         if (atype == ATYPE_FREEPTR) {
914                 int start_var, end_var, start_loop;
915                 /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
916                  */
917                 start_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
918                 end_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
919                 mono_mb_emit_ldloc (mb, my_entry_var);
920                 mono_mb_emit_ldloc (mb, bytes_var);
921                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
922                 mono_mb_emit_stloc (mb, end_var);
923                 mono_mb_emit_ldloc (mb, my_entry_var);
924                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
925                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
926                 mono_mb_emit_stloc (mb, start_var);
927                 /*
928                  * do {
929                  *      *start++ = NULL;
930                  * } while (start < end);
931                  */
932                 start_loop = mono_mb_get_label (mb);
933                 mono_mb_emit_ldloc (mb, start_var);
934                 mono_mb_emit_icon (mb, 0);
935                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
936                 mono_mb_emit_ldloc (mb, start_var);
937                 mono_mb_emit_icon (mb, sizeof (gpointer));
938                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
939                 mono_mb_emit_stloc (mb, start_var);
940
941                 mono_mb_emit_ldloc (mb, start_var);
942                 mono_mb_emit_ldloc (mb, end_var);
943                 mono_mb_emit_byte (mb, MONO_CEE_BLT_UN_S);
944                 mono_mb_emit_byte (mb, start_loop - (mono_mb_get_label (mb) + 1));
945         } else if (atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING) {
946                 /* need to clear just the sync pointer */
947                 mono_mb_emit_ldloc (mb, my_entry_var);
948                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
949                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
950                 mono_mb_emit_icon (mb, 0);
951                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
952         }
953
954         if (atype == ATYPE_STRING) {
955                 /* need to set length and clear the last char */
956                 /* s->length = len; */
957                 mono_mb_emit_ldloc (mb, my_entry_var);
958                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
959                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
960                 mono_mb_emit_ldarg (mb, 1);
961                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
962                 /* s->chars [len] = 0; */
963                 mono_mb_emit_ldloc (mb, my_entry_var);
964                 mono_mb_emit_ldloc (mb, bytes_var);
965                 mono_mb_emit_icon (mb, 2);
966                 mono_mb_emit_byte (mb, MONO_CEE_SUB);
967                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
968                 mono_mb_emit_icon (mb, 0);
969                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
970         }
971
972         /* return my_entry; */
973         mono_mb_emit_ldloc (mb, my_entry_var);
974         mono_mb_emit_byte (mb, MONO_CEE_RET);
975         
976         mono_mb_patch_short_branch (mb, no_freelist_branch);
977         if (not_small_enough_branch > 0)
978                 mono_mb_patch_short_branch (mb, not_small_enough_branch);
979         if (size_overflow_branch > 0)
980                 mono_mb_patch_short_branch (mb, size_overflow_branch);
981
982         /* the slow path: we just call back into the runtime */
983  always_slowpath:
984         if (atype == ATYPE_STRING) {
985                 mono_mb_emit_ldarg (mb, 1);
986                 mono_mb_emit_icall (mb, mono_string_alloc);
987         } else {
988                 mono_mb_emit_ldarg (mb, 0);
989                 mono_mb_emit_icall (mb, mono_object_new_specific);
990         }
991
992         mono_mb_emit_byte (mb, MONO_CEE_RET);
993
994         res = mono_mb_create_method (mb, csig, 8);
995         mono_mb_free (mb);
996         mono_method_get_header (res)->init_locals = FALSE;
997
998         info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
999         info->gc_name = "boehm";
1000         info->alloc_type = atype;
1001         mono_marshal_set_wrapper_info (res, info);
1002
1003         return res;
1004 }
1005
1006 static MonoMethod* alloc_method_cache [ATYPE_NUM];
1007 static MonoMethod* slowpath_alloc_method_cache [ATYPE_NUM];
1008
1009 static G_GNUC_UNUSED gboolean
1010 mono_gc_is_critical_method (MonoMethod *method)
1011 {
1012         int i;
1013
1014         for (i = 0; i < ATYPE_NUM; ++i)
1015                 if (method == alloc_method_cache [i] || method == slowpath_alloc_method_cache [i])
1016                         return TRUE;
1017
1018         return FALSE;
1019 }
1020
1021 /*
1022  * If possible, generate a managed method that can quickly allocate objects in class
1023  * @klass. The method will typically have an thread-local inline allocation sequence.
1024  * The signature of the called method is:
1025  *      object allocate (MonoVTable *vtable)
1026  * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
1027  * keep in sync.
1028  * The thread local alloc logic is taken from libgc/pthread_support.c.
1029  */
1030
1031 MonoMethod*
1032 mono_gc_get_managed_allocator (MonoClass *klass, gboolean for_box, gboolean known_instance_size)
1033 {
1034         int offset = -1;
1035         int atype;
1036         MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
1037
1038         /*g_print ("thread tls: %d\n", offset);*/
1039         if (offset == -1)
1040                 return NULL;
1041         if (!SMALL_ENOUGH (klass->instance_size))
1042                 return NULL;
1043         if (mono_class_has_finalizer (klass) || mono_class_is_marshalbyref (klass) || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
1044                 return NULL;
1045         if (klass->rank)
1046                 return NULL;
1047         if (mono_class_is_open_constructed_type (&klass->byval_arg))
1048                 return NULL;
1049         if (klass->byval_arg.type == MONO_TYPE_STRING) {
1050                 atype = ATYPE_STRING;
1051         } else if (!known_instance_size) {
1052                 return NULL;
1053         } else if (!klass->has_references) {
1054                 if (for_box)
1055                         atype = ATYPE_FREEPTR_FOR_BOX;
1056                 else
1057                         atype = ATYPE_FREEPTR;
1058         } else {
1059                 return NULL;
1060                 /*
1061                  * disabled because we currently do a runtime choice anyway, to
1062                  * deal with multiple appdomains.
1063                 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
1064                         atype = ATYPE_GCJ;
1065                 else
1066                         atype = ATYPE_NORMAL;
1067                 */
1068         }
1069         return mono_gc_get_managed_allocator_by_type (atype, FALSE);
1070 }
1071
1072 MonoMethod*
1073 mono_gc_get_managed_array_allocator (MonoClass *klass)
1074 {
1075         return NULL;
1076 }
1077
1078 /**
1079  * mono_gc_get_managed_allocator_by_type:
1080  *
1081  *   Return a managed allocator method corresponding to allocator type ATYPE.
1082  */
1083 MonoMethod*
1084 mono_gc_get_managed_allocator_by_type (int atype, gboolean slowpath)
1085 {
1086         int offset = -1;
1087         MonoMethod *res;
1088         MonoMethod **cache = slowpath ? slowpath_alloc_method_cache : alloc_method_cache;
1089         MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
1090
1091         mono_tls_key_set_offset (TLS_KEY_BOEHM_GC_THREAD, offset);
1092
1093         res = cache [atype];
1094         if (res)
1095                 return res;
1096
1097         res = create_allocator (atype, TLS_KEY_BOEHM_GC_THREAD, slowpath);
1098         mono_mutex_lock (&mono_gc_lock);
1099         if (cache [atype]) {
1100                 mono_free_method (res);
1101                 res = cache [atype];
1102         } else {
1103                 mono_memory_barrier ();
1104                 cache [atype] = res;
1105         }
1106         mono_mutex_unlock (&mono_gc_lock);
1107         return res;
1108 }
1109
1110 guint32
1111 mono_gc_get_managed_allocator_types (void)
1112 {
1113         return ATYPE_NUM;
1114 }
1115
1116 MonoMethod*
1117 mono_gc_get_write_barrier (void)
1118 {
1119         g_assert_not_reached ();
1120         return NULL;
1121 }
1122
1123 #else
1124
1125 static G_GNUC_UNUSED gboolean
1126 mono_gc_is_critical_method (MonoMethod *method)
1127 {
1128         return FALSE;
1129 }
1130
1131 MonoMethod*
1132 mono_gc_get_managed_allocator (MonoClass *klass, gboolean for_box, gboolean known_instance_size)
1133 {
1134         return NULL;
1135 }
1136
1137 MonoMethod*
1138 mono_gc_get_managed_array_allocator (MonoClass *klass)
1139 {
1140         return NULL;
1141 }
1142
1143 MonoMethod*
1144 mono_gc_get_managed_allocator_by_type (int atype, gboolean slowpath)
1145 {
1146         return NULL;
1147 }
1148
1149 guint32
1150 mono_gc_get_managed_allocator_types (void)
1151 {
1152         return 0;
1153 }
1154
1155 MonoMethod*
1156 mono_gc_get_write_barrier (void)
1157 {
1158         g_assert_not_reached ();
1159         return NULL;
1160 }
1161
1162 #endif
1163
1164 MonoMethod*
1165 mono_gc_get_specific_write_barrier (gboolean is_concurrent)
1166 {
1167         g_assert_not_reached ();
1168         return NULL;
1169 }
1170
1171 int
1172 mono_gc_get_aligned_size_for_allocator (int size)
1173 {
1174         return size;
1175 }
1176
1177 const char *
1178 mono_gc_get_gc_name (void)
1179 {
1180         return "boehm";
1181 }
1182
1183 void*
1184 mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data)
1185 {
1186         return GC_call_with_alloc_lock (func, data);
1187 }
1188
1189 char*
1190 mono_gc_get_description (void)
1191 {
1192         return g_strdup (DEFAULT_GC_NAME);
1193 }
1194
1195 void
1196 mono_gc_set_desktop_mode (void)
1197 {
1198         GC_dont_expand = 1;
1199 }
1200
1201 gboolean
1202 mono_gc_is_moving (void)
1203 {
1204         return FALSE;
1205 }
1206
1207 gboolean
1208 mono_gc_is_disabled (void)
1209 {
1210         if (GC_dont_gc || g_getenv ("GC_DONT_GC"))
1211                 return TRUE;
1212         else
1213                 return FALSE;
1214 }
1215
1216 void
1217 mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
1218 {
1219         g_assert_not_reached ();
1220 }
1221
1222
1223 guint8*
1224 mono_gc_get_card_table (int *shift_bits, gpointer *card_mask)
1225 {
1226         g_assert_not_reached ();
1227         return NULL;
1228 }
1229
1230 gboolean
1231 mono_gc_card_table_nursery_check (void)
1232 {
1233         g_assert_not_reached ();
1234         return TRUE;
1235 }
1236
1237 void*
1238 mono_gc_get_nursery (int *shift_bits, size_t *size)
1239 {
1240         return NULL;
1241 }
1242
1243 void
1244 mono_gc_set_current_thread_appdomain (MonoDomain *domain)
1245 {
1246 }
1247
1248 gboolean
1249 mono_gc_precise_stack_mark_enabled (void)
1250 {
1251         return FALSE;
1252 }
1253
1254 FILE *
1255 mono_gc_get_logfile (void)
1256 {
1257         return NULL;
1258 }
1259
1260 void
1261 mono_gc_conservatively_scan_area (void *start, void *end)
1262 {
1263         g_assert_not_reached ();
1264 }
1265
1266 void *
1267 mono_gc_scan_object (void *obj, void *gc_data)
1268 {
1269         g_assert_not_reached ();
1270         return NULL;
1271 }
1272
1273 gsize*
1274 mono_gc_get_bitmap_for_descr (void *descr, int *numbits)
1275 {
1276         g_assert_not_reached ();
1277         return NULL;
1278 }
1279
1280 void
1281 mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks)
1282 {
1283 }
1284
1285 void
1286 mono_gc_set_stack_end (void *stack_end)
1287 {
1288 }
1289
1290 void mono_gc_set_skip_thread (gboolean value)
1291 {
1292 }
1293
1294 void
1295 mono_gc_register_for_finalization (MonoObject *obj, void *user_data)
1296 {
1297         guint offset = 0;
1298
1299 #ifndef GC_DEBUG
1300         /* This assertion is not valid when GC_DEBUG is defined */
1301         g_assert (GC_base (obj) == (char*)obj - offset);
1302 #endif
1303
1304         GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, user_data, GUINT_TO_POINTER (offset), NULL, NULL);
1305 }
1306
1307 #ifndef HOST_WIN32
1308 int
1309 mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
1310 {
1311         /* it is being replaced by GC_pthread_create on some
1312          * platforms, see libgc/include/gc_pthread_redirects.h */
1313         return pthread_create (new_thread, attr, start_routine, arg);
1314 }
1315 #endif
1316
1317 #ifdef HOST_WIN32
1318 BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reserved)
1319 {
1320         return GC_DllMain (module_handle, reason, reserved);
1321 }
1322 #endif
1323
1324 guint
1325 mono_gc_get_vtable_bits (MonoClass *class)
1326 {
1327         if (fin_callbacks.is_class_finalization_aware) {
1328                 if (fin_callbacks.is_class_finalization_aware (class))
1329                         return BOEHM_GC_BIT_FINALIZER_AWARE;
1330         }
1331         return 0;
1332 }
1333
1334 /*
1335  * mono_gc_register_altstack:
1336  *
1337  *   Register the dimensions of the normal stack and altstack with the collector.
1338  * Currently, STACK/STACK_SIZE is only used when the thread is suspended while it is on an altstack.
1339  */
1340 void
1341 mono_gc_register_altstack (gpointer stack, gint32 stack_size, gpointer altstack, gint32 altstack_size)
1342 {
1343         GC_register_altstack (stack, stack_size, altstack, altstack_size);
1344 }
1345
1346 int
1347 mono_gc_get_los_limit (void)
1348 {
1349         return G_MAXINT;
1350 }
1351
1352 void
1353 mono_gc_set_string_length (MonoString *str, gint32 new_length)
1354 {
1355         mono_unichar2 *new_end = str->chars + new_length;
1356         
1357         /* zero the discarded string. This null-delimits the string and allows 
1358          * the space to be reclaimed by SGen. */
1359          
1360         memset (new_end, 0, (str->length - new_length + 1) * sizeof (mono_unichar2));
1361         str->length = new_length;
1362 }
1363
1364 gboolean
1365 mono_gc_user_markers_supported (void)
1366 {
1367         return FALSE;
1368 }
1369
1370 void *
1371 mono_gc_make_root_descr_user (MonoGCRootMarkFunc marker)
1372 {
1373         g_assert_not_reached ();
1374         return NULL;
1375 }
1376
1377 gboolean
1378 mono_gc_set_allow_synchronous_major (gboolean flag)
1379 {
1380         return flag;
1381 }
1382 /* Toggleref support */
1383
1384 void
1385 mono_gc_toggleref_add (MonoObject *object, mono_bool strong_ref)
1386 {
1387         GC_toggleref_add ((GC_PTR)object, (int)strong_ref);
1388 }
1389
1390 void
1391 mono_gc_toggleref_register_callback (MonoToggleRefStatus (*proccess_toggleref) (MonoObject *obj))
1392 {
1393         GC_toggleref_register_callback ((int (*) (GC_PTR obj)) proccess_toggleref);
1394 }
1395
1396 /* Test support code */
1397
1398 static MonoToggleRefStatus
1399 test_toggleref_callback (MonoObject *obj)
1400 {
1401         static MonoClassField *mono_toggleref_test_field;
1402         int status = MONO_TOGGLE_REF_DROP;
1403
1404         if (!mono_toggleref_test_field) {
1405                 mono_toggleref_test_field = mono_class_get_field_from_name (mono_object_get_class (obj), "__test");
1406                 g_assert (mono_toggleref_test_field);
1407         }
1408
1409         mono_field_get_value (obj, mono_toggleref_test_field, &status);
1410         printf ("toggleref-cb obj %d\n", status);
1411         return status;
1412 }
1413
1414 static void
1415 register_test_toggleref_callback (void)
1416 {
1417         mono_gc_toggleref_register_callback (test_toggleref_callback);
1418 }
1419
1420 static gboolean
1421 is_finalization_aware (MonoObject *obj)
1422 {
1423         MonoVTable *vt = obj->vtable;
1424         return (vt->gc_bits & BOEHM_GC_BIT_FINALIZER_AWARE) == BOEHM_GC_BIT_FINALIZER_AWARE;
1425 }
1426
1427 static void
1428 fin_notifier (MonoObject *obj)
1429 {
1430         if (is_finalization_aware (obj))
1431                 fin_callbacks.object_queued_for_finalization (obj);
1432 }
1433
1434 void
1435 mono_gc_register_finalizer_callbacks (MonoGCFinalizerCallbacks *callbacks)
1436 {
1437         if (callbacks->version != MONO_GC_FINALIZER_EXTENSION_VERSION)
1438                 g_error ("Invalid finalizer callback version. Expected %d but got %d\n", MONO_GC_FINALIZER_EXTENSION_VERSION, callbacks->version);
1439
1440         fin_callbacks = *callbacks;
1441
1442         GC_set_finalizer_notify_proc ((void (*) (GC_PTR))fin_notifier);
1443 }
1444
1445 gboolean
1446 mono_gc_is_null (void)
1447 {
1448         return FALSE;
1449 }
1450
1451 gboolean
1452 mono_gc_object_older_than (MonoObject *object, int generation) {
1453         return FALSE;
1454 }
1455
1456 #endif /* no Boehm GC */