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