7de44839a7978ae1411d2c8213bd7f202f8d4058
[mono.git] / mono / metadata / boehm-gc.c
1 /*
2  * boehm-gc.c: GC implementation using either the installed or included Boehm GC.
3  *
4  */
5
6 #include "config.h"
7 #define GC_I_HIDE_POINTERS
8 #include <mono/os/gc_wrapper.h>
9 #include <mono/metadata/mono-gc.h>
10 #include <mono/metadata/gc-internal.h>
11 #include <mono/metadata/profiler-private.h>
12 #include <mono/metadata/class-internals.h>
13 #include <mono/metadata/method-builder.h>
14 #include <mono/metadata/opcodes.h>
15 #include <mono/utils/mono-logger.h>
16
17 #if HAVE_BOEHM_GC
18
19 #ifdef USE_INCLUDED_LIBGC
20 #undef TRUE
21 #undef FALSE
22 #define THREAD_LOCAL_ALLOC 1
23 #include "private/pthread_support.h"
24 #endif
25
26 #define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
27
28 static gboolean gc_initialized = FALSE;
29
30 static void
31 mono_gc_warning (char *msg, GC_word arg)
32 {
33         mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
34 }
35
36 void
37 mono_gc_base_init (void)
38 {
39         if (gc_initialized)
40                 return;
41
42         /*
43          * Handle the case when we are called from a thread different from the main thread,
44          * confusing libgc.
45          * FIXME: Move this to libgc where it belongs.
46          *
47          * we used to do this only when running on valgrind,
48          * but it happens also in other setups.
49          */
50 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
51         {
52                 size_t size;
53                 void *sstart;
54                 pthread_attr_t attr;
55                 pthread_getattr_np (pthread_self (), &attr);
56                 pthread_attr_getstack (&attr, &sstart, &size);
57                 pthread_attr_destroy (&attr); 
58                 /*g_print ("stackbottom pth is: %p\n", (char*)sstart + size);*/
59 #ifdef __ia64__
60                 /*
61                  * The calculation above doesn't seem to work on ia64, also we need to set
62                  * GC_register_stackbottom as well, but don't know how.
63                  */
64 #else
65                 /* apparently with some linuxthreads implementations sstart can be NULL,
66                  * fallback to the more imprecise method (bug# 78096).
67                  */
68                 if (sstart) {
69                         GC_stackbottom = (char*)sstart + size;
70                 } else {
71                         int dummy;
72                         gsize stack_bottom = (gsize)&dummy;
73                         stack_bottom += 4095;
74                         stack_bottom &= ~4095;
75                         GC_stackbottom = (char*)stack_bottom;
76                 }
77 #endif
78         }
79 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
80                 GC_stackbottom = (char*)pthread_get_stackaddr_np (pthread_self ());
81 #else
82         {
83                 int dummy;
84                 gsize stack_bottom = (gsize)&dummy;
85                 stack_bottom += 4095;
86                 stack_bottom &= ~4095;
87                 /*g_print ("stackbottom is: %p\n", (char*)stack_bottom);*/
88                 GC_stackbottom = (char*)stack_bottom;
89         }
90 #endif
91
92         GC_init ();
93         GC_no_dls = TRUE;
94         GC_oom_fn = mono_gc_out_of_memory;
95         GC_set_warn_proc (mono_gc_warning);
96         GC_finalize_on_demand = 1;
97         GC_finalizer_notifier = mono_gc_finalize_notify;
98
99 #ifdef HAVE_GC_GCJ_MALLOC
100         GC_init_gcj_malloc (5, NULL);
101 #endif
102
103         gc_initialized = TRUE;
104 }
105
106 void
107 mono_gc_collect (int generation)
108 {
109         GC_gcollect ();
110 }
111
112 int
113 mono_gc_max_generation (void)
114 {
115         return 0;
116 }
117
118 int
119 mono_gc_get_generation  (MonoObject *object)
120 {
121         return 0;
122 }
123
124 int
125 mono_gc_collection_count (int generation)
126 {
127         return GC_gc_no;
128 }
129
130 void
131 mono_gc_add_memory_pressure (gint64 value)
132 {
133 }
134
135 gint64
136 mono_gc_get_used_size (void)
137 {
138         return GC_get_heap_size () - GC_get_free_bytes ();
139 }
140
141 gint64
142 mono_gc_get_heap_size (void)
143 {
144         return GC_get_heap_size ();
145 }
146
147 void
148 mono_gc_disable (void)
149 {
150 #ifdef HAVE_GC_ENABLE
151         GC_disable ();
152 #else
153         g_assert_not_reached ();
154 #endif
155 }
156
157 void
158 mono_gc_enable (void)
159 {
160 #ifdef HAVE_GC_ENABLE
161         GC_enable ();
162 #else
163         g_assert_not_reached ();
164 #endif
165 }
166
167 gboolean
168 mono_gc_is_gc_thread (void)
169 {
170 #ifdef USE_INCLUDED_LIBGC
171         return GC_thread_is_registered ();
172 #else
173         return TRUE;
174 #endif
175 }
176
177 extern int GC_thread_register_foreign (void *base_addr);
178
179 gboolean
180 mono_gc_register_thread (void *baseptr)
181 {
182         if (mono_gc_is_gc_thread())
183                 return TRUE;
184 #if defined(USE_INCLUDED_LIBGC) && !defined(PLATFORM_WIN32)
185         return GC_thread_register_foreign (baseptr);
186 #else
187         return FALSE;
188 #endif
189 }
190
191 gboolean
192 mono_object_is_alive (MonoObject* o)
193 {
194 #ifdef USE_INCLUDED_LIBGC
195         return GC_is_marked ((gpointer)o);
196 #else
197         return TRUE;
198 #endif
199 }
200
201 #ifdef USE_INCLUDED_LIBGC
202
203 static void
204 on_gc_notification (GCEventType event)
205 {
206         mono_profiler_gc_event ((MonoGCEvent) event, 0);
207 }
208  
209 static void
210 on_gc_heap_resize (size_t new_size)
211 {
212         mono_profiler_gc_heap_resize (new_size);
213 }
214
215 void
216 mono_gc_enable_events (void)
217 {
218         GC_notify_event = on_gc_notification;
219         GC_on_heap_resize = on_gc_heap_resize;
220 }
221
222 #else
223
224 void
225 mono_gc_enable_events (void)
226 {
227 }
228
229 #endif
230
231 int
232 mono_gc_register_root (char *start, size_t size, void *descr)
233 {
234         /* for some strange reason, they want one extra byte on the end */
235         GC_add_roots (start, start + size + 1);
236
237         return TRUE;
238 }
239
240 void
241 mono_gc_weak_link_add (void **link_addr, MonoObject *obj)
242 {
243         /* libgc requires that we use HIDE_POINTER... */
244         *link_addr = (void*)HIDE_POINTER (obj);
245         GC_GENERAL_REGISTER_DISAPPEARING_LINK (link_addr, obj);
246 }
247
248 void
249 mono_gc_weak_link_remove (void **link_addr)
250 {
251         GC_unregister_disappearing_link (link_addr);
252         *link_addr = NULL;
253 }
254
255 MonoObject*
256 mono_gc_weak_link_get (void **link_addr)
257 {
258         MonoObject *obj = REVEAL_POINTER (*link_addr);
259         if (obj == (MonoObject *) -1)
260                 return NULL;
261         return obj;
262 }
263
264 void*
265 mono_gc_make_descr_for_string (gsize *bitmap, int numbits)
266 {
267         return mono_gc_make_descr_from_bitmap (bitmap, numbits);
268 }
269
270 void*
271 mono_gc_make_descr_for_object (gsize *bitmap, int numbits, size_t obj_size)
272 {
273         return mono_gc_make_descr_from_bitmap (bitmap, numbits);
274 }
275
276 void*
277 mono_gc_make_descr_for_array (int vector, gsize *elem_bitmap, int numbits, size_t elem_size)
278 {
279         /* libgc has no usable support for arrays... */
280         return GC_NO_DESCRIPTOR;
281 }
282
283 void*
284 mono_gc_make_descr_from_bitmap (gsize *bitmap, int numbits)
285 {
286 #ifdef HAVE_GC_GCJ_MALLOC
287         /* It seems there are issues when the bitmap doesn't fit: play it safe */
288         if (numbits >= 30)
289                 return GC_NO_DESCRIPTOR;
290         else
291                 return (gpointer)GC_make_descriptor ((GC_bitmap)bitmap, numbits);
292 #else
293         return NULL;
294 #endif
295 }
296
297 void*
298 mono_gc_alloc_fixed (size_t size, void *descr)
299 {
300         return GC_MALLOC (size);
301 }
302
303 void
304 mono_gc_free_fixed (void* addr)
305 {
306 }
307
308 int
309 mono_gc_invoke_finalizers (void)
310 {
311         /* There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
312          * the 'mem_freed' variable is not initialized when there are no
313          * objects to finalize, which leads to strange behavior later on.
314          * The check is necessary to work around that bug.
315          */
316         if (GC_should_invoke_finalizers ())
317                 return GC_invoke_finalizers ();
318         return 0;
319 }
320
321 gboolean
322 mono_gc_pending_finalizers (void)
323 {
324         return GC_should_invoke_finalizers ();
325 }
326
327 void
328 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
329 {
330         *(void**)field_ptr = value;
331 }
332
333 void
334 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
335 {
336         *(void**)slot_ptr = value;
337 }
338
339 void
340 mono_gc_wbarrier_arrayref_copy (MonoArray *arr, gpointer slot_ptr, int count)
341 {
342         /* no need to do anything */
343 }
344
345 void
346 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
347 {
348         *(void**)ptr = value;
349 }
350
351 void
352 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
353 {
354 }
355
356 void
357 mono_gc_wbarrier_object (MonoObject *object)
358 {
359 }
360
361 #if defined(USE_INCLUDED_LIBGC) && defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
362 extern __thread MONO_TLS_FAST void* GC_thread_tls;
363 #include "metadata-internals.h"
364
365 static int
366 shift_amount (int v)
367 {
368         int i = 0;
369         while (!(v & (1 << i)))
370                 i++;
371         return i;
372 }
373
374 enum {
375         ATYPE_FREEPTR,
376         ATYPE_FREEPTR_FOR_BOX,
377         ATYPE_NORMAL,
378         ATYPE_GCJ,
379         ATYPE_STRING,
380         ATYPE_NUM
381 };
382
383 static MonoMethod*
384 create_allocator (int atype, int offset)
385 {
386         int index_var, bytes_var, my_fl_var, my_entry_var;
387         guint32 no_freelist_branch, not_small_enough_branch = 0;
388         guint32 size_overflow_branch = 0;
389         MonoMethodBuilder *mb;
390         MonoMethod *res;
391         MonoMethodSignature *csig;
392
393         if (atype == ATYPE_STRING) {
394                 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
395                 csig->ret = &mono_defaults.string_class->byval_arg;
396                 csig->params [0] = &mono_defaults.int_class->byval_arg;
397                 csig->params [1] = &mono_defaults.int32_class->byval_arg;
398         } else {
399                 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
400                 csig->ret = &mono_defaults.object_class->byval_arg;
401                 csig->params [0] = &mono_defaults.int_class->byval_arg;
402         }
403
404         mb = mono_mb_new (mono_defaults.object_class, "Alloc", MONO_WRAPPER_ALLOC);
405         bytes_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
406         if (atype == ATYPE_STRING) {
407                 /* a string alloator method takes the args: (vtable, len) */
408                 /* bytes = (sizeof (MonoString) + ((len + 1) * 2)); */
409                 mono_mb_emit_ldarg (mb, 1);
410                 mono_mb_emit_icon (mb, 1);
411                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
412                 mono_mb_emit_icon (mb, 1);
413                 mono_mb_emit_byte (mb, MONO_CEE_SHL);
414                 // sizeof (MonoString) might include padding
415                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, chars));
416                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
417                 mono_mb_emit_stloc (mb, bytes_var);
418         } else {
419                 /* bytes = vtable->klass->instance_size */
420                 mono_mb_emit_ldarg (mb, 0);
421                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
422                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
423                 mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
424                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
425                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
426                 /* FIXME: assert instance_size stays a 4 byte integer */
427                 mono_mb_emit_byte (mb, MONO_CEE_LDIND_U4);
428                 mono_mb_emit_stloc (mb, bytes_var);
429         }
430
431         /* this is needed for strings/arrays only as the other big types are never allocated with this method */
432         if (atype == ATYPE_STRING) {
433                 /* check for size */
434                 /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
435                 mono_mb_emit_ldloc (mb, bytes_var);
436                 mono_mb_emit_icon (mb, (NFREELISTS-1) * GRANULARITY);
437                 not_small_enough_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
438                 /* check for overflow */
439                 mono_mb_emit_ldloc (mb, bytes_var);
440                 mono_mb_emit_icon (mb, sizeof (MonoString));
441                 size_overflow_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
442         }
443
444         /* int index = INDEX_FROM_BYTES(bytes); */
445         index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
446         
447         mono_mb_emit_ldloc (mb, bytes_var);
448         mono_mb_emit_icon (mb, GRANULARITY - 1);
449         mono_mb_emit_byte (mb, MONO_CEE_ADD);
450         mono_mb_emit_icon (mb, shift_amount (GRANULARITY));
451         mono_mb_emit_byte (mb, MONO_CEE_SHR_UN);
452         mono_mb_emit_icon (mb, shift_amount (sizeof (gpointer)));
453         mono_mb_emit_byte (mb, MONO_CEE_SHL);
454         /* index var is already adjusted into bytes */
455         mono_mb_emit_stloc (mb, index_var);
456
457         my_fl_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
458         my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
459         /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
460         mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
461         mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
462         mono_mb_emit_i4 (mb, offset);
463         if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
464                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
465         else if (atype == ATYPE_NORMAL)
466                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, normal_freelists));
467         else if (atype == ATYPE_GCJ)
468                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, gcj_freelists));
469         else
470                 g_assert_not_reached ();
471         mono_mb_emit_byte (mb, MONO_CEE_ADD);
472         mono_mb_emit_ldloc (mb, index_var);
473         mono_mb_emit_byte (mb, MONO_CEE_ADD);
474         mono_mb_emit_stloc (mb, my_fl_var);
475
476         /* my_entry = *my_fl; */
477         mono_mb_emit_ldloc (mb, my_fl_var);
478         mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
479         mono_mb_emit_stloc (mb, my_entry_var);
480
481         /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
482         mono_mb_emit_ldloc (mb, my_entry_var);
483         mono_mb_emit_icon (mb, HBLKSIZE);
484         no_freelist_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
485
486         /* ptr_t next = obj_link(my_entry); *my_fl = next; */
487         mono_mb_emit_ldloc (mb, my_fl_var);
488         mono_mb_emit_ldloc (mb, my_entry_var);
489         mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
490         mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
491
492         /* set the vtable and clear the words in the object */
493         mono_mb_emit_ldloc (mb, my_entry_var);
494         mono_mb_emit_ldarg (mb, 0);
495         mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
496
497         if (atype == ATYPE_FREEPTR) {
498                 int start_var, end_var, start_loop;
499                 /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
500                  */
501                 start_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
502                 end_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
503                 mono_mb_emit_ldloc (mb, my_entry_var);
504                 mono_mb_emit_ldloc (mb, bytes_var);
505                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
506                 mono_mb_emit_stloc (mb, end_var);
507                 mono_mb_emit_ldloc (mb, my_entry_var);
508                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
509                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
510                 mono_mb_emit_stloc (mb, start_var);
511                 /*
512                  * do {
513                  *      *start++ = NULL;
514                  * } while (start < end);
515                  */
516                 start_loop = mono_mb_get_label (mb);
517                 mono_mb_emit_ldloc (mb, start_var);
518                 mono_mb_emit_icon (mb, 0);
519                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
520                 mono_mb_emit_ldloc (mb, start_var);
521                 mono_mb_emit_icon (mb, sizeof (gpointer));
522                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
523                 mono_mb_emit_stloc (mb, start_var);
524
525                 mono_mb_emit_ldloc (mb, start_var);
526                 mono_mb_emit_ldloc (mb, end_var);
527                 mono_mb_emit_byte (mb, MONO_CEE_BLT_UN_S);
528                 mono_mb_emit_byte (mb, start_loop - (mono_mb_get_label (mb) + 1));
529         } else if (atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING) {
530                 /* need to clear just the sync pointer */
531                 mono_mb_emit_ldloc (mb, my_entry_var);
532                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
533                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
534                 mono_mb_emit_icon (mb, 0);
535                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
536         }
537
538         if (atype == ATYPE_STRING) {
539                 /* need to set length and clear the last char */
540                 /* s->length = len; */
541                 mono_mb_emit_ldloc (mb, my_entry_var);
542                 mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
543                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
544                 mono_mb_emit_ldarg (mb, 1);
545                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
546                 /* s->chars [len] = 0; */
547                 mono_mb_emit_ldloc (mb, my_entry_var);
548                 mono_mb_emit_ldloc (mb, bytes_var);
549                 mono_mb_emit_icon (mb, 2);
550                 mono_mb_emit_byte (mb, MONO_CEE_SUB);
551                 mono_mb_emit_byte (mb, MONO_CEE_ADD);
552                 mono_mb_emit_icon (mb, 0);
553                 mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
554         }
555
556         /* return my_entry; */
557         mono_mb_emit_ldloc (mb, my_entry_var);
558         mono_mb_emit_byte (mb, MONO_CEE_RET);
559         
560         mono_mb_patch_short_branch (mb, no_freelist_branch);
561         if (not_small_enough_branch > 0)
562                 mono_mb_patch_short_branch (mb, not_small_enough_branch);
563         if (size_overflow_branch > 0)
564                 mono_mb_patch_short_branch (mb, size_overflow_branch);
565         /* the slow path: we just call back into the runtime */
566         if (atype == ATYPE_STRING) {
567                 mono_mb_emit_ldarg (mb, 1);
568                 mono_mb_emit_icall (mb, mono_string_alloc);
569         } else {
570                 mono_mb_emit_ldarg (mb, 0);
571                 mono_mb_emit_icall (mb, mono_object_new_specific);
572         }
573
574         mono_mb_emit_byte (mb, MONO_CEE_RET);
575
576         res = mono_mb_create_method (mb, csig, 8);
577         mono_mb_free (mb);
578         mono_method_get_header (res)->init_locals = FALSE;
579         return res;
580 }
581
582 static MonoMethod* alloc_method_cache [ATYPE_NUM];
583
584 /*
585  * If possible, generate a managed method that can quickly allocate objects in class
586  * @klass. The method will typically have an thread-local inline allocation sequence.
587  * The signature of the called method is:
588  *      object allocate (MonoVTable *vtable)
589  * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
590  * keep in sync.
591  * The thread local alloc logic is taken from libgc/pthread_support.c.
592  */
593
594 MonoMethod*
595 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
596 {
597         int offset = -1;
598         int atype;
599         MonoClass *klass = vtable->klass;
600         MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
601
602         /*g_print ("thread tls: %d\n", offset);*/
603         if (offset == -1)
604                 return NULL;
605         if (!SMALL_ENOUGH (klass->instance_size))
606                 return NULL;
607         if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
608                 return NULL;
609         if (klass->rank)
610                 return NULL;
611         if (klass->byval_arg.type == MONO_TYPE_STRING) {
612                 atype = ATYPE_STRING;
613         } else if (!klass->has_references) {
614                 if (for_box)
615                         atype = ATYPE_FREEPTR_FOR_BOX;
616                 else
617                         atype = ATYPE_FREEPTR;
618         } else {
619                 return NULL;
620                 /*
621                  * disabled because we currently do a runtime choice anyway, to
622                  * deal with multiple appdomains.
623                 if (vtable->gc_descr != GC_NO_DESCRIPTOR)
624                         atype = ATYPE_GCJ;
625                 else
626                         atype = ATYPE_NORMAL;
627                 */
628         }
629         return mono_gc_get_managed_allocator_by_type (atype);
630 }
631
632 /**
633  * mono_gc_get_managed_allocator_id:
634  *
635  *   Return a type for the managed allocator method MANAGED_ALLOC which can later be passed
636  * to mono_gc_get_managed_allocator_by_type () to get back this allocator method. This can be
637  * used by the AOT code to encode references to managed allocator methods.
638  */
639 int
640 mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
641 {
642         int i;
643
644         mono_loader_lock ();
645         for (i = 0; i < ATYPE_NUM; ++i) {
646                 if (alloc_method_cache [i] == managed_alloc) {
647                         mono_loader_unlock ();
648                         return i;
649                 }
650         }
651         mono_loader_unlock ();
652
653         return -1;
654 }
655
656 /**
657  * mono_gc_get_managed_allocator_by_type:
658  *
659  *   Return a managed allocator method corresponding to allocator type ATYPE.
660  */
661 MonoMethod*
662 mono_gc_get_managed_allocator_by_type (int atype)
663 {
664         int offset = -1;
665         MonoMethod *res;
666         MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
667
668         mono_loader_lock ();
669         res = alloc_method_cache [atype];
670         if (!res)
671                 res = alloc_method_cache [atype] = create_allocator (atype, offset);
672         mono_loader_unlock ();
673         return res;
674 }
675
676 #else
677
678 MonoMethod*
679 mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
680 {
681         return NULL;
682 }
683
684 int
685 mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
686 {
687         return -1;
688 }
689
690 MonoMethod*
691 mono_gc_get_managed_allocator_by_type (int atype)
692 {
693         return NULL;
694 }
695
696 #endif
697
698 #endif /* no Boehm GC */
699