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