2007-09-25 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / boehm-gc.c
index 29559e7ef276c917f4339bdee5d616dd8ddaff39..635b732b81c4ca671075e9e2640ffcc3ea41a17e 100644 (file)
@@ -9,10 +9,20 @@
 #include <mono/metadata/mono-gc.h>
 #include <mono/metadata/gc-internal.h>
 #include <mono/metadata/profiler-private.h>
+#include <mono/metadata/class-internals.h>
+#include <mono/metadata/marshal.h>
+#include <mono/metadata/opcodes.h>
 #include <mono/utils/mono-logger.h>
 
 #if HAVE_BOEHM_GC
 
+#ifdef USE_INCLUDED_LIBGC
+#undef TRUE
+#undef FALSE
+#define THREAD_LOCAL_ALLOC 1
+#include "private/pthread_support.h"
+#endif
+
 static void
 mono_gc_warning (char *msg, GC_word arg)
 {
@@ -114,8 +124,6 @@ mono_gc_register_thread (void *baseptr)
 #endif
 }
 
-extern int GC_is_marked (void *p);
-
 gboolean
 mono_object_is_alive (MonoObject* o)
 {
@@ -250,5 +258,345 @@ mono_gc_wbarrier_object (MonoObject *object)
 {
 }
 
+#if defined(USE_INCLUDED_LIBGC) && defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
+extern __thread MONO_TLS_FAST void* GC_thread_tls;
+#include "metadata-internals.h"
+
+static int
+shift_amount (int v)
+{
+       int i = 0;
+       while (!(v & (1 << i)))
+               i++;
+       return i;
+}
+
+enum {
+       ATYPE_FREEPTR,
+       ATYPE_FREEPTR_FOR_BOX,
+       ATYPE_NORMAL,
+       ATYPE_GCJ,
+       ATYPE_STRING,
+       ATYPE_NUM
+};
+
+static MonoMethod*
+create_allocator (int atype, int offset)
+{
+       int index_var, bytes_var, my_fl_var, my_entry_var;
+       guint32 no_freelist_branch, not_small_enough_branch = 0;
+       guint32 size_overflow_branch = 0;
+       MonoMethodBuilder *mb;
+       MonoMethod *res;
+       MonoMethodSignature *csig;
+
+       if (atype == ATYPE_STRING) {
+               csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
+               csig->ret = &mono_defaults.string_class->byval_arg;
+               csig->params [0] = &mono_defaults.int_class->byval_arg;
+               csig->params [1] = &mono_defaults.int32_class->byval_arg;
+       } else {
+               csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
+               csig->ret = &mono_defaults.object_class->byval_arg;
+               csig->params [0] = &mono_defaults.int_class->byval_arg;
+       }
+
+       mb = mono_mb_new (mono_defaults.object_class, "Alloc", MONO_WRAPPER_ALLOC);
+       bytes_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
+       if (atype == ATYPE_STRING) {
+               /* a string alloator method takes the args: (vtable, len) */
+               /* bytes = (sizeof (MonoString) + ((len + 1) * 2)); */
+               mono_mb_emit_ldarg (mb, 1);
+               mono_mb_emit_icon (mb, 1);
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_icon (mb, 1);
+               mono_mb_emit_byte (mb, MONO_CEE_SHL);
+               mono_mb_emit_icon (mb, sizeof (MonoString));
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_stloc (mb, bytes_var);
+       } else {
+               /* bytes = vtable->klass->instance_size */
+               mono_mb_emit_ldarg (mb, 0);
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoVTable, klass));
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoClass, instance_size));
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               /* FIXME: assert instance_size stays a 4 byte integer */
+               mono_mb_emit_byte (mb, MONO_CEE_LDIND_U4);
+               mono_mb_emit_stloc (mb, bytes_var);
+       }
+
+       /* this is needed for strings/arrays only as the other big types are never allocated with this method */
+       if (atype == ATYPE_STRING) {
+               /* check for size */
+               /* if (!SMALL_ENOUGH (bytes)) jump slow_path;*/
+               mono_mb_emit_ldloc (mb, bytes_var);
+               mono_mb_emit_icon (mb, (NFREELISTS-1) * GRANULARITY);
+               not_small_enough_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BGT_UN_S);
+               /* check for overflow */
+               mono_mb_emit_ldloc (mb, bytes_var);
+               mono_mb_emit_icon (mb, sizeof (MonoString));
+               size_overflow_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLE_UN_S);
+       }
+
+       /* int index = INDEX_FROM_BYTES(bytes); */
+       index_var = mono_mb_add_local (mb, &mono_defaults.int32_class->byval_arg);
+       
+       mono_mb_emit_ldloc (mb, bytes_var);
+       mono_mb_emit_icon (mb, GRANULARITY - 1);
+       mono_mb_emit_byte (mb, MONO_CEE_ADD);
+       mono_mb_emit_icon (mb, shift_amount (GRANULARITY));
+       mono_mb_emit_byte (mb, MONO_CEE_SHR_UN);
+       mono_mb_emit_icon (mb, shift_amount (sizeof (gpointer)));
+       mono_mb_emit_byte (mb, MONO_CEE_SHL);
+       /* index var is already adjusted into bytes */
+       mono_mb_emit_stloc (mb, index_var);
+
+       my_fl_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+       my_entry_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+       /* my_fl = ((GC_thread)tsd) -> ptrfree_freelists + index; */
+       mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+       mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
+       mono_mb_emit_i4 (mb, offset);
+       if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
+       else if (atype == ATYPE_NORMAL)
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, normal_freelists));
+       else if (atype == ATYPE_GCJ)
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, gcj_freelists));
+       else
+               g_assert_not_reached ();
+       mono_mb_emit_byte (mb, MONO_CEE_ADD);
+       mono_mb_emit_ldloc (mb, index_var);
+       mono_mb_emit_byte (mb, MONO_CEE_ADD);
+       mono_mb_emit_stloc (mb, my_fl_var);
+
+       /* my_entry = *my_fl; */
+       mono_mb_emit_ldloc (mb, my_fl_var);
+       mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
+       mono_mb_emit_stloc (mb, my_entry_var);
+
+       /* if (EXPECT((word)my_entry >= HBLKSIZE, 1)) { */
+       mono_mb_emit_ldloc (mb, my_entry_var);
+       mono_mb_emit_icon (mb, HBLKSIZE);
+       no_freelist_branch = mono_mb_emit_short_branch (mb, MONO_CEE_BLT_UN_S);
+
+       /* ptr_t next = obj_link(my_entry); *my_fl = next; */
+       mono_mb_emit_ldloc (mb, my_fl_var);
+       mono_mb_emit_ldloc (mb, my_entry_var);
+       mono_mb_emit_byte (mb, MONO_CEE_LDIND_I);
+       mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
+
+       /* set the vtable and clear the words in the object */
+       mono_mb_emit_ldloc (mb, my_entry_var);
+       mono_mb_emit_ldarg (mb, 0);
+       mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
+
+       if (atype == ATYPE_FREEPTR) {
+               int start_var, end_var, start_loop;
+               /* end = my_entry + bytes; start = my_entry + sizeof (gpointer);
+                */
+               start_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+               end_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+               mono_mb_emit_ldloc (mb, my_entry_var);
+               mono_mb_emit_ldloc (mb, bytes_var);
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_stloc (mb, end_var);
+               mono_mb_emit_ldloc (mb, my_entry_var);
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_stloc (mb, start_var);
+               /*
+                * do {
+                *      *start++ = NULL;
+                * } while (start < end);
+                */
+               start_loop = mono_mb_get_label (mb);
+               mono_mb_emit_ldloc (mb, start_var);
+               mono_mb_emit_icon (mb, 0);
+               mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
+               mono_mb_emit_ldloc (mb, start_var);
+               mono_mb_emit_icon (mb, sizeof (gpointer));
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_stloc (mb, start_var);
+
+               mono_mb_emit_ldloc (mb, start_var);
+               mono_mb_emit_ldloc (mb, end_var);
+               mono_mb_emit_byte (mb, MONO_CEE_BLT_UN_S);
+               mono_mb_emit_byte (mb, start_loop - (mono_mb_get_label (mb) + 1));
+       } else if (atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING) {
+               /* need to clear just the sync pointer */
+               mono_mb_emit_ldloc (mb, my_entry_var);
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoObject, synchronisation));
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_icon (mb, 0);
+               mono_mb_emit_byte (mb, MONO_CEE_STIND_I);
+       }
+
+       if (atype == ATYPE_STRING) {
+               /* need to set length and clear the last char */
+               /* s->length = len; */
+               mono_mb_emit_ldloc (mb, my_entry_var);
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (MonoString, length));
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_ldarg (mb, 1);
+               mono_mb_emit_byte (mb, MONO_CEE_STIND_I4);
+               /* s->chars [len] = 0; */
+               mono_mb_emit_ldloc (mb, my_entry_var);
+               mono_mb_emit_ldloc (mb, bytes_var);
+               mono_mb_emit_icon (mb, 2);
+               mono_mb_emit_byte (mb, MONO_CEE_SUB);
+               mono_mb_emit_byte (mb, MONO_CEE_ADD);
+               mono_mb_emit_icon (mb, 0);
+               mono_mb_emit_byte (mb, MONO_CEE_STIND_I2);
+       }
+
+       /* return my_entry; */
+       mono_mb_emit_ldloc (mb, my_entry_var);
+       mono_mb_emit_byte (mb, MONO_CEE_RET);
+       
+       mono_mb_patch_short_branch (mb, no_freelist_branch);
+       if (not_small_enough_branch > 0)
+               mono_mb_patch_short_branch (mb, not_small_enough_branch);
+       if (size_overflow_branch > 0)
+               mono_mb_patch_short_branch (mb, size_overflow_branch);
+       /* the slow path: we just call back into the runtime */
+       if (atype == ATYPE_STRING) {
+               mono_mb_emit_ldarg (mb, 1);
+               mono_mb_emit_icall (mb, mono_string_alloc);
+       } else {
+               mono_mb_emit_ldarg (mb, 0);
+               mono_mb_emit_icall (mb, mono_object_new_specific);
+       }
+
+       mono_mb_emit_byte (mb, MONO_CEE_RET);
+
+       res = mono_mb_create_method (mb, csig, 8);
+       mono_mb_free (mb);
+       mono_method_get_header (res)->init_locals = FALSE;
+       return res;
+}
+
+static MonoMethod* alloc_method_cache [ATYPE_NUM];
+#define GC_NO_DESCRIPTOR ((gpointer)(0 | GC_DS_LENGTH))
+
+/*
+ * If possible, generate a managed method that can quickly allocate objects in class
+ * @klass. The method will typically have an thread-local inline allocation sequence.
+ * The signature of the called method is:
+ *     object allocate (MonoVTable *vtable)
+ * Some of the logic here is similar to mono_class_get_allocation_ftn () i object.c,
+ * keep in sync.
+ * The thread local alloc logic is taken from libgc/pthread_support.c.
+ */
+
+MonoMethod*
+mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
+{
+       int offset = -1;
+       int atype;
+       MonoClass *klass = vtable->klass;
+       MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
+
+       /*g_print ("thread tls: %d\n", offset);*/
+       if (offset == -1)
+               return NULL;
+       if (!SMALL_ENOUGH (klass->instance_size))
+               return NULL;
+       if (klass->has_finalize || klass->marshalbyref || (mono_profiler_get_events () & MONO_PROFILE_ALLOCATIONS))
+               return NULL;
+       if (klass->rank)
+               return NULL;
+       if (klass->byval_arg.type == MONO_TYPE_STRING) {
+               atype = ATYPE_STRING;
+#ifdef __x86_64__
+               return NULL;
+#endif
+       } else if (!klass->has_references) {
+               if (for_box)
+                       atype = ATYPE_FREEPTR_FOR_BOX;
+               else
+                       atype = ATYPE_FREEPTR;
+       } else {
+               return NULL;
+               /*
+                * disabled because we currently do a runtime choice anyway, to
+                * deal with multiple appdomains.
+               if (vtable->gc_descr != GC_NO_DESCRIPTOR)
+                       atype = ATYPE_GCJ;
+               else
+                       atype = ATYPE_NORMAL;
+               */
+       }
+       return mono_gc_get_managed_allocator_by_type (atype);
+}
+
+/**
+ * mono_gc_get_managed_allocator_id:
+ *
+ *   Return a type for the managed allocator method MANAGED_ALLOC which can later be passed
+ * to mono_gc_get_managed_allocator_by_type () to get back this allocator method. This can be
+ * used by the AOT code to encode references to managed allocator methods.
+ */
+int
+mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
+{
+       int i;
+
+       mono_loader_lock ();
+       for (i = 0; i < ATYPE_NUM; ++i) {
+               if (alloc_method_cache [i] == managed_alloc) {
+                       mono_loader_unlock ();
+                       return i;
+               }
+       }
+       mono_loader_unlock ();
+
+       return -1;
+}
+
+/**
+ * mono_gc_get_managed_allocator_by_type:
+ *
+ *   Return a managed allocator method corresponding to allocator type ATYPE.
+ */
+MonoMethod*
+mono_gc_get_managed_allocator_by_type (int atype)
+{
+       int offset = -1;
+       MonoMethod *res;
+       MONO_THREAD_VAR_OFFSET (GC_thread_tls, offset);
+
+       mono_loader_lock ();
+       res = alloc_method_cache [atype];
+       if (!res)
+               res = alloc_method_cache [atype] = create_allocator (atype, offset);
+       mono_loader_unlock ();
+       return res;
+}
+
+#else
+
+MonoMethod*
+mono_gc_get_managed_allocator (MonoVTable *vtable, gboolean for_box)
+{
+       return NULL;
+}
+
+int
+mono_gc_get_managed_allocator_type (MonoMethod *managed_alloc)
+{
+       return -1;
+}
+
+MonoMethod*
+mono_gc_get_managed_allocator_by_type (int atype)
+{
+       return NULL;
+}
+
+#endif
+
 #endif /* no Boehm GC */