Merge pull request #2305 from ludovic-henry/fix-threadpool-36414
[mono.git] / mono / metadata / object.c
index 6a11417dc4d7bd31e6fdecba20ce7f3d87685ca7..a28c6f15d9dc322fa675fdbf0a8e76edb222c38b 100644 (file)
@@ -21,7 +21,7 @@
 #include <mono/metadata/tokentype.h>
 #include <mono/metadata/loader.h>
 #include <mono/metadata/object.h>
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/metadata/exception.h>
 #include <mono/metadata/domain-internals.h>
 #include "mono/metadata/metadata-internals.h"
@@ -36,7 +36,7 @@
 #include "mono/metadata/profiler-private.h"
 #include "mono/metadata/security-manager.h"
 #include "mono/metadata/mono-debug-debugger.h"
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/metadata/verify-internals.h>
 #include <mono/utils/strenc.h>
 #include <mono/utils/mono-counters.h>
@@ -59,25 +59,25 @@ static char *
 mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s, gboolean ignore_error, MonoError *error);
 
 
-#define ldstr_lock() mono_mutex_lock (&ldstr_section)
-#define ldstr_unlock() mono_mutex_unlock (&ldstr_section)
+#define ldstr_lock() mono_os_mutex_lock (&ldstr_section)
+#define ldstr_unlock() mono_os_mutex_unlock (&ldstr_section)
 static mono_mutex_t ldstr_section;
 
 void
-mono_runtime_object_init (MonoObject *this)
+mono_runtime_object_init (MonoObject *this_obj)
 {
        MONO_REQ_GC_UNSAFE_MODE;
 
        MonoMethod *method = NULL;
-       MonoClass *klass = this->vtable->klass;
+       MonoClass *klass = this_obj->vtable->klass;
 
        method = mono_class_get_method_from_name (klass, ".ctor", 0);
        if (!method)
                g_error ("Could not lookup zero argument constructor for class %s", mono_type_get_full_name (klass));
 
        if (method->klass->valuetype)
-               this = mono_object_unbox (this);
-       mono_runtime_invoke (method, this, NULL, NULL);
+               this_obj = (MonoObject *)mono_object_unbox (this_obj);
+       mono_runtime_invoke (method, this_obj, NULL, NULL);
 }
 
 /* The pseudo algorithm for type initialization from the spec
@@ -108,25 +108,23 @@ typedef struct
        MonoNativeThreadId initializing_tid;
        guint32 waiting_count;
        gboolean done;
-       mono_mutex_t initialization_section;
+       MonoCoopMutex initialization_section;
 } TypeInitializationLock;
 
 /* for locking access to type_initialization_hash and blocked_thread_hash */
-static mono_mutex_t type_initialization_section;
+static MonoCoopMutex type_initialization_section;
 
 static inline void
 mono_type_initialization_lock (void)
 {
        /* The critical sections protected by this lock in mono_runtime_class_init_full () can block */
-       MONO_PREPARE_BLOCKING;
-       mono_mutex_lock (&type_initialization_section);
-       MONO_FINISH_BLOCKING;
+       mono_coop_mutex_lock (&type_initialization_section);
 }
 
 static inline void
 mono_type_initialization_unlock (void)
 {
-       mono_mutex_unlock (&type_initialization_section);
+       mono_coop_mutex_unlock (&type_initialization_section);
 }
 
 static void
@@ -134,15 +132,13 @@ mono_type_init_lock (TypeInitializationLock *lock)
 {
        MONO_REQ_GC_NEUTRAL_MODE;
 
-       MONO_TRY_BLOCKING;
-       mono_mutex_lock (&lock->initialization_section);
-       MONO_FINISH_TRY_BLOCKING;
+       mono_coop_mutex_lock (&lock->initialization_section);
 }
 
 static void
 mono_type_init_unlock (TypeInitializationLock *lock)
 {
-       mono_mutex_unlock (&lock->initialization_section);
+       mono_coop_mutex_unlock (&lock->initialization_section);
 }
 
 /* from vtable to lock */
@@ -192,10 +188,10 @@ mono_thread_get_main (void)
 void
 mono_type_initialization_init (void)
 {
-       mono_mutex_init_recursive (&type_initialization_section);
+       mono_coop_mutex_init_recursive (&type_initialization_section);
        type_initialization_hash = g_hash_table_new (NULL, NULL);
        blocked_thread_hash = g_hash_table_new (NULL, NULL);
-       mono_mutex_init_recursive (&ldstr_section);
+       mono_os_mutex_init_recursive (&ldstr_section);
 }
 
 void
@@ -205,11 +201,11 @@ mono_type_initialization_cleanup (void)
        /* This is causing race conditions with
         * mono_release_type_locks
         */
-       mono_mutex_destroy (&type_initialization_section);
+       mono_coop_mutex_destroy (&type_initialization_section);
        g_hash_table_destroy (type_initialization_hash);
        type_initialization_hash = NULL;
 #endif
-       mono_mutex_destroy (&ldstr_section);
+       mono_os_mutex_destroy (&ldstr_section);
        g_hash_table_destroy (blocked_thread_hash);
        blocked_thread_hash = NULL;
 
@@ -241,7 +237,7 @@ get_type_init_exception_for_vtable (MonoVTable *vtable)
        ex = NULL;
        mono_domain_lock (domain);
        if (domain->type_init_exception_hash)
-               ex = mono_g_hash_table_lookup (domain->type_init_exception_hash, klass);
+               ex = (MonoException *)mono_g_hash_table_lookup (domain->type_init_exception_hash, klass);
        mono_domain_unlock (domain);
 
        if (!ex) {
@@ -342,7 +338,7 @@ mono_runtime_class_init_full (MonoVTable *vtable, gboolean raise_exception)
                        mono_raise_exception (get_type_init_exception_for_vtable (vtable));
                return get_type_init_exception_for_vtable (vtable);
        }
-       lock = g_hash_table_lookup (type_initialization_hash, vtable);
+       lock = (TypeInitializationLock *)g_hash_table_lookup (type_initialization_hash, vtable);
        if (lock == NULL) {
                /* This thread will get to do the initialization */
                if (mono_domain_get () != domain) {
@@ -356,8 +352,8 @@ mono_runtime_class_init_full (MonoVTable *vtable, gboolean raise_exception)
                                return mono_get_exception_appdomain_unloaded ();
                        }
                }
-               lock = g_malloc (sizeof(TypeInitializationLock));
-               mono_mutex_init_recursive (&lock->initialization_section);
+               lock = (TypeInitializationLock *)g_malloc (sizeof (TypeInitializationLock));
+               mono_coop_mutex_init_recursive (&lock->initialization_section);
                lock->initializing_tid = tid;
                lock->waiting_count = 1;
                lock->done = FALSE;
@@ -440,7 +436,7 @@ mono_runtime_class_init_full (MonoVTable *vtable, gboolean raise_exception)
                g_hash_table_remove (blocked_thread_hash, GUINT_TO_POINTER (tid));
        --lock->waiting_count;
        if (lock->waiting_count == 0) {
-               mono_mutex_destroy (&lock->initialization_section);
+               mono_coop_mutex_destroy (&lock->initialization_section);
                g_hash_table_remove (type_initialization_hash, vtable);
                g_free (lock);
        }
@@ -477,7 +473,7 @@ gboolean release_type_locks (gpointer key, gpointer value, gpointer user)
                mono_type_init_unlock (lock);
                --lock->waiting_count;
                if (lock->waiting_count == 0) {
-                       mono_mutex_destroy (&lock->initialization_section);
+                       mono_coop_mutex_destroy (&lock->initialization_section);
                        g_free (lock);
                        return TRUE;
                }
@@ -695,7 +691,7 @@ compute_class_bitmap (MonoClass *klass, gsize *bitmap, int size, int offset, int
                max_size = klass->instance_size / sizeof (gpointer);
        if (max_size > size) {
                g_assert (offset <= 0);
-               bitmap = g_malloc0 ((max_size + BITMAP_EL_SIZE - 1) / BITMAP_EL_SIZE * sizeof (gsize));
+               bitmap = (gsize *)g_malloc0 ((max_size + BITMAP_EL_SIZE - 1) / BITMAP_EL_SIZE * sizeof (gsize));
                size = max_size;
        }
 
@@ -1113,7 +1109,7 @@ mono_method_get_imt_slot (MonoMethod *method)
 
        sig = mono_method_signature (method);
        hashes_count = sig->param_count + 4;
-       hashes_start = malloc (hashes_count * sizeof (guint32));
+       hashes_start = (guint32 *)malloc (hashes_count * sizeof (guint32));
        hashes = hashes_start;
 
        if (! MONO_CLASS_IS_INTERFACE (method->klass)) {
@@ -1175,7 +1171,7 @@ add_imt_builder_entry (MonoImtBuilderEntry **imt_builder, MonoMethod *method, gu
                return;
        }
 
-       entry = g_malloc0 (sizeof (MonoImtBuilderEntry));
+       entry = (MonoImtBuilderEntry *)g_malloc0 (sizeof (MonoImtBuilderEntry));
        entry->key = method;
        entry->value.vtable_slot = vtable_slot;
        entry->next = imt_builder [imt_slot];
@@ -1265,7 +1261,7 @@ imt_sort_slot_entries (MonoImtBuilderEntry *entries) {
        MONO_REQ_GC_NEUTRAL_MODE;
 
        int number_of_entries = entries->children + 1;
-       MonoImtBuilderEntry **sorted_array = malloc (sizeof (MonoImtBuilderEntry*) * number_of_entries);
+       MonoImtBuilderEntry **sorted_array = (MonoImtBuilderEntry **)malloc (sizeof (MonoImtBuilderEntry*) * number_of_entries);
        GPtrArray *result = g_ptr_array_new ();
        MonoImtBuilderEntry *current_entry;
        int i;
@@ -1330,7 +1326,7 @@ build_imt_slots (MonoClass *klass, MonoVTable *vt, MonoDomain *domain, gpointer*
        int i;
        GSList *list_item;
        guint32 imt_collisions_bitmap = 0;
-       MonoImtBuilderEntry **imt_builder = calloc (MONO_IMT_SIZE, sizeof (MonoImtBuilderEntry*));
+       MonoImtBuilderEntry **imt_builder = (MonoImtBuilderEntry **)calloc (MONO_IMT_SIZE, sizeof (MonoImtBuilderEntry*));
        int method_count = 0;
        gboolean record_method_count_for_max_collisions = FALSE;
        gboolean has_generic_virtual = FALSE, has_variant_iface = FALSE;
@@ -1381,7 +1377,7 @@ build_imt_slots (MonoClass *klass, MonoVTable *vt, MonoDomain *domain, gpointer*
                int interface_offset = klass->vtable_size;
 
                for (list_item = extra_interfaces; list_item != NULL; list_item=list_item->next) {
-                       MonoClass* iface = list_item->data;
+                       MonoClass* iface = (MonoClass *)list_item->data;
                        int method_slot_in_interface;
                        for (method_slot_in_interface = 0; method_slot_in_interface < iface->method.count; method_slot_in_interface++) {
                                MonoMethod *method = mono_class_get_method_by_index (iface, method_slot_in_interface);
@@ -1533,7 +1529,7 @@ init_thunk_free_lists (MonoDomain *domain)
 
        if (domain->thunk_free_lists)
                return;
-       domain->thunk_free_lists = mono_domain_alloc0 (domain, sizeof (gpointer) * NUM_FREE_LISTS);
+       domain->thunk_free_lists = (MonoThunkFreeList **)mono_domain_alloc0 (domain, sizeof (gpointer) * NUM_FREE_LISTS);
 }
 
 static int
@@ -1606,7 +1602,7 @@ mono_method_alloc_generic_virtual_thunk (MonoDomain *domain, int size)
        }
        generic_virtual_thunks_size += size;
 
-       p = mono_domain_code_reserve (domain, size);
+       p = (guint32 *)mono_domain_code_reserve (domain, size);
        *p = size;
 
        mono_domain_lock (domain);
@@ -1626,7 +1622,7 @@ invalidate_generic_virtual_thunk (MonoDomain *domain, gpointer code)
 {
        MONO_REQ_GC_NEUTRAL_MODE;
 
-       guint32 *p = code;
+       guint32 *p = (guint32 *)code;
        MonoThunkFreeList *l = (MonoThunkFreeList*)(p - 1);
        gboolean found = FALSE;
 
@@ -1696,7 +1692,7 @@ get_generic_virtual_entries (MonoDomain *domain, gpointer *vtable_slot)
        if (!domain->generic_virtual_cases)
                domain->generic_virtual_cases = g_hash_table_new (mono_aligned_addr_hash, NULL);
  
-       list = g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
+       list = (GenericVirtualCase *)g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
  
        entries = NULL;
        for (; list; list = list->next) {
@@ -1753,7 +1749,7 @@ mono_method_add_generic_virtual_invocation (MonoDomain *domain, MonoVTable *vtab
                domain->generic_virtual_cases = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
        /* Check whether the case was already added */
-       list = g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
+       list = (GenericVirtualCase *)g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
        gvc = list;
        while (gvc) {
                if (gvc->method == method)
@@ -1763,11 +1759,11 @@ mono_method_add_generic_virtual_invocation (MonoDomain *domain, MonoVTable *vtab
 
        /* If not found, make a new one */
        if (!gvc) {
-               gvc = mono_domain_alloc (domain, sizeof (GenericVirtualCase));
+               gvc = (GenericVirtualCase *)mono_domain_alloc (domain, sizeof (GenericVirtualCase));
                gvc->method = method;
                gvc->code = code;
                gvc->count = 0;
-               gvc->next = g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
+               gvc->next = (GenericVirtualCase *)g_hash_table_lookup (domain->generic_virtual_cases, vtable_slot);
 
                g_hash_table_insert (domain->generic_virtual_cases, vtable_slot, gvc);
 
@@ -1779,7 +1775,7 @@ mono_method_add_generic_virtual_invocation (MonoDomain *domain, MonoVTable *vtab
        }
 
        if (++gvc->count == THUNK_THRESHOLD) {
-               gpointer *old_thunk = *vtable_slot;
+               gpointer *old_thunk = (void **)*vtable_slot;
                gpointer vtable_trampoline = NULL;
                gpointer imt_trampoline = NULL;
 
@@ -2168,7 +2164,7 @@ mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *klass, gboolean
        if (klass != mono_defaults.monotype_class) {
                /*FIXME check for OOM*/
                vt->type = mono_type_get_object (domain, &klass->byval_arg);
-               if (mono_object_get_class (vt->type) != mono_defaults.monotype_class)
+               if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.monotype_class)
                        /* This is unregistered in
                           unregister_vtable_reflection_type() in
                           domain.c. */
@@ -2207,7 +2203,7 @@ mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *klass, gboolean
                /* this is a bounded memory retention issue: may want to 
                 * handle it differently when we'll have a rcu-like system.
                 */
-               runtime_info = mono_image_alloc0 (klass->image, MONO_SIZEOF_CLASS_RUNTIME_INFO + new_size * sizeof (gpointer));
+               runtime_info = (MonoClassRuntimeInfo *)mono_image_alloc0 (klass->image, MONO_SIZEOF_CLASS_RUNTIME_INFO + new_size * sizeof (gpointer));
                runtime_info->max_domain = new_size - 1;
                /* copy the stuff from the older info */
                if (old_info) {
@@ -2222,7 +2218,7 @@ mono_class_create_runtime_vtable (MonoDomain *domain, MonoClass *klass, gboolean
        if (klass == mono_defaults.monotype_class) {
                /*FIXME check for OOM*/
                vt->type = mono_type_get_object (domain, &klass->byval_arg);
-               if (mono_object_get_class (vt->type) != mono_defaults.monotype_class)
+               if (mono_object_get_class ((MonoObject *)vt->type) != mono_defaults.monotype_class)
                        /* This is unregistered in
                           unregister_vtable_reflection_type() in
                           domain.c. */
@@ -2295,7 +2291,7 @@ mono_class_proxy_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, Mono
                g_assert (mono_error_ok (&error)); /*FIXME do proper error handling*/
                if (ifaces) {
                        for (i = 0; i < ifaces->len; ++i) {
-                               MonoClass *ic = g_ptr_array_index (ifaces, i);
+                               MonoClass *ic = (MonoClass *)g_ptr_array_index (ifaces, i);
                                /*FIXME test for interfaces with variant generic arguments*/
                                if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, ic->interface_id))
                                        continue;       /* interface implemented by the class */
@@ -2354,9 +2350,9 @@ mono_class_proxy_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, Mono
        pvt->max_interface_id = max_interface_id;
        bsize = sizeof (guint8) * (max_interface_id/8 + 1 );
 #ifdef COMPRESSED_INTERFACE_BITMAP
-       bitmap = g_malloc0 (bsize);
+       bitmap = (uint8_t *)g_malloc0 (bsize);
 #else
-       bitmap = mono_domain_alloc0 (domain, bsize);
+       bitmap = (uint8_t *)mono_domain_alloc0 (domain, bsize);
 #endif
 
        for (i = 0; i < klass->interface_offsets_count; ++i) {
@@ -2373,7 +2369,7 @@ mono_class_proxy_vtable (MonoDomain *domain, MonoRemoteClass *remote_class, Mono
 
                /* Create trampolines for the methods of the interfaces */
                for (list_item = extra_interfaces; list_item != NULL; list_item=list_item->next) {
-                       interf = list_item->data;
+                       interf = (MonoClass *)list_item->data;
                        
                        bitmap [interf->interface_id >> 3] |= (1 << (interf->interface_id & 7));
 
@@ -2486,18 +2482,18 @@ create_remote_class_key (MonoRemoteClass *remote_class, MonoClass *extra_class)
        
        if (remote_class == NULL) {
                if (extra_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
-                       key = g_malloc (sizeof(gpointer) * 3);
+                       key = (void **)g_malloc (sizeof(gpointer) * 3);
                        key [0] = GINT_TO_POINTER (2);
                        key [1] = mono_defaults.marshalbyrefobject_class;
                        key [2] = extra_class;
                } else {
-                       key = g_malloc (sizeof(gpointer) * 2);
+                       key = (void **)g_malloc (sizeof(gpointer) * 2);
                        key [0] = GINT_TO_POINTER (1);
                        key [1] = extra_class;
                }
        } else {
                if (extra_class != NULL && (extra_class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
-                       key = g_malloc (sizeof(gpointer) * (remote_class->interface_count + 3));
+                       key = (void **)g_malloc (sizeof(gpointer) * (remote_class->interface_count + 3));
                        key [0] = GINT_TO_POINTER (remote_class->interface_count + 2);
                        key [1] = remote_class->proxy_class;
 
@@ -2513,7 +2509,7 @@ create_remote_class_key (MonoRemoteClass *remote_class, MonoClass *extra_class)
                                key [j] = extra_class;
                } else {
                        // Replace the old class. The interface list is the same
-                       key = g_malloc (sizeof(gpointer) * (remote_class->interface_count + 2));
+                       key = (void **)g_malloc (sizeof(gpointer) * (remote_class->interface_count + 2));
                        key [0] = GINT_TO_POINTER (remote_class->interface_count + 1);
                        key [1] = extra_class != NULL ? extra_class : remote_class->proxy_class;
                        for (i = 0; i < remote_class->interface_count; i++)
@@ -2535,7 +2531,7 @@ copy_remote_class_key (MonoDomain *domain, gpointer *key)
        MONO_REQ_GC_NEUTRAL_MODE
 
        int key_size = (GPOINTER_TO_UINT (key [0]) + 1) * sizeof (gpointer);
-       gpointer *mp_key = mono_domain_alloc (domain, key_size);
+       gpointer *mp_key = (gpointer *)mono_domain_alloc (domain, key_size);
 
        memcpy (mp_key, key, key_size);
 
@@ -2564,7 +2560,7 @@ mono_remote_class (MonoDomain *domain, MonoString *class_name, MonoClass *proxy_
        key = create_remote_class_key (NULL, proxy_class);
        
        mono_domain_lock (domain);
-       rc = g_hash_table_lookup (domain->proxy_vtable_hash, key);
+       rc = (MonoRemoteClass *)g_hash_table_lookup (domain->proxy_vtable_hash, key);
 
        if (rc) {
                g_free (key);
@@ -2584,12 +2580,12 @@ mono_remote_class (MonoDomain *domain, MonoString *class_name, MonoClass *proxy_
        key = mp_key;
 
        if (proxy_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
-               rc = mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*));
+               rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*));
                rc->interface_count = 1;
                rc->interfaces [0] = proxy_class;
                rc->proxy_class = mono_defaults.marshalbyrefobject_class;
        } else {
-               rc = mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS);
+               rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS);
                rc->interface_count = 0;
                rc->proxy_class = proxy_class;
        }
@@ -2620,7 +2616,7 @@ clone_remote_class (MonoDomain *domain, MonoRemoteClass* remote_class, MonoClass
        gpointer* key, *mp_key;
        
        key = create_remote_class_key (remote_class, extra_class);
-       rc = g_hash_table_lookup (domain->proxy_vtable_hash, key);
+       rc = (MonoRemoteClass *)g_hash_table_lookup (domain->proxy_vtable_hash, key);
        if (rc != NULL) {
                g_free (key);
                return rc;
@@ -2632,7 +2628,7 @@ clone_remote_class (MonoDomain *domain, MonoRemoteClass* remote_class, MonoClass
 
        if (extra_class->flags & TYPE_ATTRIBUTE_INTERFACE) {
                int i,j;
-               rc = mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*) * (remote_class->interface_count + 1));
+               rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*) * (remote_class->interface_count + 1));
                rc->proxy_class = remote_class->proxy_class;
                rc->interface_count = remote_class->interface_count + 1;
                
@@ -2647,7 +2643,7 @@ clone_remote_class (MonoDomain *domain, MonoRemoteClass* remote_class, MonoClass
                        rc->interfaces [j] = extra_class;
        } else {
                // Replace the old class. The interface array is the same
-               rc = mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*) * remote_class->interface_count);
+               rc = (MonoRemoteClass *)mono_domain_alloc (domain, MONO_SIZEOF_REMOTE_CLASS + sizeof(MonoClass*) * remote_class->interface_count);
                rc->proxy_class = extra_class;
                rc->interface_count = remote_class->interface_count;
                if (rc->interface_count > 0)
@@ -2733,7 +2729,7 @@ mono_upgrade_remote_class (MonoDomain *domain, MonoObject *proxy_object, MonoCla
 
        if (redo_vtable) {
                tproxy->remote_class = clone_remote_class (domain, remote_class, klass);
-               proxy_object->vtable = mono_remote_class_vtable (domain, tproxy->remote_class, tproxy->rp);
+               proxy_object->vtable = (MonoVTable *)mono_remote_class_vtable (domain, tproxy->remote_class, tproxy->rp);
        }
        
        mono_domain_unlock (domain);
@@ -2954,10 +2950,10 @@ mono_method_get_unmanaged_thunk (MonoMethod *method)
 
        gpointer res;
 
-       MONO_PREPARE_RESET_BLOCKING
+       MONO_PREPARE_RESET_BLOCKING;
        method = mono_marshal_get_thunk_invoke_wrapper (method);
        res = mono_compile_method (method);
-       MONO_FINISH_RESET_BLOCKING
+       MONO_FINISH_RESET_BLOCKING;
 
        return res;
 }
@@ -3027,7 +3023,7 @@ handle_enum:
        case MONO_TYPE_CLASS:
        case MONO_TYPE_OBJECT:
        case MONO_TYPE_ARRAY:
-               mono_gc_wbarrier_generic_store (dest, deref_pointer? *(gpointer*)value: value);
+               mono_gc_wbarrier_generic_store (dest, deref_pointer ? *(MonoObject **)value : (MonoObject *)value);
                return;
        case MONO_TYPE_FNPTR:
        case MONO_TYPE_PTR: {
@@ -3149,7 +3145,7 @@ mono_field_get_addr (MonoObject *obj, MonoVTable *vt, MonoClassField *field)
                        mono_domain_lock (vt->domain);
                        addr = g_hash_table_lookup (vt->domain->special_static_fields, field);
                        mono_domain_unlock (vt->domain);
-                       src = mono_get_special_static_data (GPOINTER_TO_UINT (addr));
+                       src = (guint8 *)mono_get_special_static_data (GPOINTER_TO_UINT (addr));
                } else {
                        src = (guint8*)mono_vtable_get_static_field_data (vt) + field->offset;
                }
@@ -4300,7 +4296,7 @@ mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
        gboolean has_byref_nullables = FALSE;
 
        if (NULL != params) {
-               pa = alloca (sizeof (gpointer) * mono_array_length (params));
+               pa = (void **)alloca (sizeof (gpointer) * mono_array_length (params));
                for (i = 0; i < mono_array_length (params); i++) {
                        MonoType *t = sig->params [i];
 
@@ -4405,7 +4401,7 @@ mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
                        }
 #endif
                        if (method->klass->valuetype)
-                               o = mono_object_unbox (obj);
+                               o = (MonoObject *)mono_object_unbox ((MonoObject *)obj);
                        else
                                o = obj;
                } else if (method->klass->valuetype) {
@@ -4413,7 +4409,7 @@ mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
                }
 
                mono_runtime_invoke (method, o, pa, exc);
-               return obj;
+               return (MonoObject *)obj;
        } else {
                if (mono_class_is_nullable (method->klass)) {
                        MonoObject *nullable;
@@ -4421,7 +4417,7 @@ mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
                        /* Convert the unboxed vtype into a Nullable structure */
                        nullable = mono_object_new (mono_domain_get (), method->klass);
 
-                       mono_nullable_init (mono_object_unbox (nullable), mono_value_box (mono_domain_get (), method->klass->cast_class, obj), method->klass);
+                       mono_nullable_init ((guint8 *)mono_object_unbox (nullable), mono_value_box (mono_domain_get (), method->klass->cast_class, obj), method->klass);
                        obj = mono_object_unbox (nullable);
                }
 
@@ -4517,7 +4513,7 @@ mono_object_new_pinned (MonoDomain *domain, MonoClass *klass)
                return NULL;
 
 #ifdef HAVE_SGEN_GC
-       return mono_gc_alloc_pinned_obj (vtable, mono_class_instance_size (klass));
+       return (MonoObject *)mono_gc_alloc_pinned_obj (vtable, mono_class_instance_size (klass));
 #else
        return mono_object_new_specific (vtable);
 #endif
@@ -4569,7 +4565,7 @@ mono_object_new_alloc_specific (MonoVTable *vtable)
 {
        MONO_REQ_GC_UNSAFE_MODE;
 
-       MonoObject *o = mono_gc_alloc_obj (vtable, vtable->klass->instance_size);
+       MonoObject *o = (MonoObject *)mono_gc_alloc_obj (vtable, vtable->klass->instance_size);
 
        if (G_UNLIKELY (vtable->klass->has_finalize))
                mono_object_register_finalizer (o);
@@ -4582,7 +4578,7 @@ mono_object_new_fast (MonoVTable *vtable)
 {
        MONO_REQ_GC_UNSAFE_MODE;
 
-       return mono_gc_alloc_obj (vtable, vtable->klass->instance_size);
+       return (MonoObject *)mono_gc_alloc_obj (vtable, vtable->klass->instance_size);
 }
 
 /**
@@ -4665,7 +4661,7 @@ mono_object_clone (MonoObject *obj)
        if (obj->vtable->klass->rank)
                return (MonoObject*)mono_array_clone ((MonoArray*)obj);
 
-       o = mono_gc_alloc_obj (obj->vtable, size);
+       o = (MonoObject *)mono_gc_alloc_obj (obj->vtable, size);
 
        /* If the object doesn't contain references this will do a simple memmove. */
        mono_gc_wbarrier_object_copy (o, obj);
@@ -4747,7 +4743,7 @@ mono_array_clone_in_domain (MonoDomain *domain, MonoArray *array)
                return o;
        }
        
-       sizes = alloca (klass->rank * sizeof(intptr_t) * 2);
+       sizes = (uintptr_t *)alloca (klass->rank * sizeof(intptr_t) * 2);
        size = mono_array_element_size (klass);
        for (i = 0; i < klass->rank; ++i) {
                sizes [i] = array->bounds [i].length;
@@ -4815,9 +4811,9 @@ mono_array_calc_byte_len (MonoClass *klass, uintptr_t len, uintptr_t *res)
        if (CHECK_MUL_OVERFLOW_UN (byte_len, len))
                return FALSE;
        byte_len *= len;
-       if (CHECK_ADD_OVERFLOW_UN (byte_len, sizeof (MonoArray)))
+       if (CHECK_ADD_OVERFLOW_UN (byte_len, MONO_SIZEOF_MONO_ARRAY))
                return FALSE;
-       byte_len += sizeof (MonoArray);
+       byte_len += MONO_SIZEOF_MONO_ARRAY;
 
        *res = byte_len;
 
@@ -4887,9 +4883,9 @@ mono_array_new_full (MonoDomain *domain, MonoClass *array_class, uintptr_t *leng
         */
        vtable = mono_class_vtable_full (domain, array_class, TRUE);
        if (bounds_size)
-               o = mono_gc_alloc_array (vtable, byte_len, len, bounds_size);
+               o = (MonoObject *)mono_gc_alloc_array (vtable, byte_len, len, bounds_size);
        else
-               o = mono_gc_alloc_vector (vtable, byte_len, len);
+               o = (MonoObject *)mono_gc_alloc_vector (vtable, byte_len, len);
        array = (MonoArray*)o;
 
        bounds = array->bounds;
@@ -4952,7 +4948,7 @@ mono_array_new_specific (MonoVTable *vtable, uintptr_t n)
                mono_gc_out_of_memory (MONO_ARRAY_MAX_SIZE);
                return NULL;
        }
-       o = mono_gc_alloc_vector (vtable, byte_len, n);
+       o = (MonoObject *)mono_gc_alloc_vector (vtable, byte_len, n);
        ao = (MonoArray*)o;
 
        return ao;
@@ -5041,7 +5037,7 @@ mono_string_new_size (MonoDomain *domain, gint32 len)
        vtable = mono_class_vtable (domain, mono_defaults.string_class);
        g_assert (vtable);
 
-       s = mono_gc_alloc_string (vtable, size, len);
+       s = (MonoString *)mono_gc_alloc_string (vtable, size, len);
 
        return s;
 }
@@ -5161,7 +5157,7 @@ mono_value_box (MonoDomain *domain, MonoClass *klass, gpointer value)
 
        g_assert (klass->valuetype);
        if (mono_class_is_nullable (klass))
-               return mono_nullable_box (value, klass);
+               return mono_nullable_box ((guint8 *)value, klass);
 
        vtable = mono_class_vtable (domain, klass);
        if (!vtable)
@@ -5283,7 +5279,7 @@ mono_object_get_size (MonoObject* o)
                return sizeof (MonoString) + 2 * mono_string_length ((MonoString*) o) + 2;
        } else if (o->vtable->rank) {
                MonoArray *array = (MonoArray*)o;
-               size_t size = sizeof (MonoArray) + mono_array_element_size (klass) * mono_array_length (array);
+               size_t size = MONO_SIZEOF_MONO_ARRAY + mono_array_element_size (klass) * mono_array_length (array);
                if (array->bounds) {
                        size += 3;
                        size &= ~3;
@@ -5430,10 +5426,10 @@ str_lookup (MonoDomain *domain, gpointer user_data)
 {
        MONO_REQ_GC_UNSAFE_MODE;
 
-       LDStrInfo *info = user_data;
+       LDStrInfo *info = (LDStrInfo *)user_data;
        if (info->res || domain == info->orig_domain)
                return;
-       info->res = mono_g_hash_table_lookup (domain->ldstr_table, info->ins);
+       info->res = (MonoString *)mono_g_hash_table_lookup (domain->ldstr_table, info->ins);
 }
 
 #ifdef HAVE_SGEN_GC
@@ -5446,7 +5442,7 @@ mono_string_get_pinned (MonoString *str)
        int size;
        MonoString *news;
        size = sizeof (MonoString) + 2 * (mono_string_length (str) + 1);
-       news = mono_gc_alloc_pinned_obj (((MonoObject*)str)->vtable, size);
+       news = (MonoString *)mono_gc_alloc_pinned_obj (((MonoObject*)str)->vtable, size);
        if (news) {
                memcpy (mono_string_chars (news), mono_string_chars (str), mono_string_length (str) * 2);
                news->length = mono_string_length (str);
@@ -5470,7 +5466,7 @@ mono_string_is_interned_lookup (MonoString *str, int insert)
        domain = ((MonoObject *)str)->vtable->domain;
        ldstr_table = domain->ldstr_table;
        ldstr_lock ();
-       res = mono_g_hash_table_lookup (ldstr_table, str);
+       res = (MonoString *)mono_g_hash_table_lookup (ldstr_table, str);
        if (res) {
                ldstr_unlock ();
                return res;
@@ -5481,7 +5477,7 @@ mono_string_is_interned_lookup (MonoString *str, int insert)
                s = mono_string_get_pinned (str);
                if (s) {
                        ldstr_lock ();
-                       res = mono_g_hash_table_lookup (ldstr_table, str);
+                       res = (MonoString *)mono_g_hash_table_lookup (ldstr_table, str);
                        if (res) {
                                ldstr_unlock ();
                                return res;
@@ -5555,7 +5551,7 @@ mono_ldstr (MonoDomain *domain, MonoImage *image, guint32 idx)
        MONO_REQ_GC_UNSAFE_MODE;
 
        if (image->dynamic) {
-               MonoString *str = mono_lookup_dynamic_token (image, MONO_TOKEN_STRING | idx, NULL);
+               MonoString *str = (MonoString *)mono_lookup_dynamic_token (image, MONO_TOKEN_STRING | idx, NULL);
                return str;
        } else {
                if (!mono_verifier_verify_string_signature (image, idx, NULL))
@@ -5595,7 +5591,7 @@ mono_ldstr_metadata_sig (MonoDomain *domain, const char* sig)
        }
 #endif
        ldstr_lock ();
-       interned = mono_g_hash_table_lookup (domain->ldstr_table, o);
+       interned = (MonoString *)mono_g_hash_table_lookup (domain->ldstr_table, o);
        ldstr_unlock ();
        if (interned)
                return interned; /* o will get garbage collected */
@@ -5603,7 +5599,7 @@ mono_ldstr_metadata_sig (MonoDomain *domain, const char* sig)
        o = mono_string_get_pinned (o);
        if (o) {
                ldstr_lock ();
-               interned = mono_g_hash_table_lookup (domain->ldstr_table, o);
+               interned = (MonoString *)mono_g_hash_table_lookup (domain->ldstr_table, o);
                if (!interned) {
                        mono_g_hash_table_insert (domain->ldstr_table, o, o);
                        interned = o;
@@ -5671,7 +5667,7 @@ mono_string_to_utf8_checked (MonoString *s, MonoError *error)
        /* g_utf16_to_utf8  may not be able to complete the convertion (e.g. NULL values were found, #335488) */
        if (s->length > written) {
                /* allocate the total length and copy the part of the string that has been converted */
-               char *as2 = g_malloc0 (s->length);
+               char *as2 = (char *)g_malloc0 (s->length);
                memcpy (as2, as, written);
                g_free (as);
                as = as2;
@@ -5708,7 +5704,7 @@ mono_string_to_utf8_ignore (MonoString *s)
        /* g_utf16_to_utf8  may not be able to complete the convertion (e.g. NULL values were found, #335488) */
        if (s->length > written) {
                /* allocate the total length and copy the part of the string that has been converted */
-               char *as2 = g_malloc0 (s->length);
+               char *as2 = (char *)g_malloc0 (s->length);
                memcpy (as2, as, written);
                g_free (as);
                as = as2;
@@ -5765,7 +5761,7 @@ mono_string_to_utf16 (MonoString *s)
        if (s == NULL)
                return NULL;
 
-       as = g_malloc ((s->length * 2) + 2);
+       as = (char *)g_malloc ((s->length * 2) + 2);
        as [(s->length * 2)] = '\0';
        as [(s->length * 2) + 1] = '\0';
 
@@ -5884,9 +5880,9 @@ mono_string_to_utf8_internal (MonoMemPool *mp, MonoImage *image, MonoString *s,
 
        len = strlen (r) + 1;
        if (mp)
-               mp_s = mono_mempool_alloc (mp, len);
+               mp_s = (char *)mono_mempool_alloc (mp, len);
        else
-               mp_s = mono_image_alloc (image, len);
+               mp_s = (char *)mono_image_alloc (image, len);
 
        memcpy (mp_s, r, len);
 
@@ -6036,7 +6032,7 @@ mono_runtime_capture_context (MonoDomain *domain)
                domain->capture_context_method = mono_compile_method (method);
        }
 
-       runtime_invoke = domain->capture_context_runtime_invoke;
+       runtime_invoke = (RuntimeInvokeFunction)domain->capture_context_runtime_invoke;
 
        return runtime_invoke (NULL, NULL, NULL, domain->capture_context_method);
 }
@@ -6064,7 +6060,7 @@ mono_async_result_new (MonoDomain *domain, HANDLE handle, MonoObject *state, gpo
                /* note: result may be null if the flow is suppressed */
        }
 
-       res->data = data;
+       res->data = (void **)data;
        MONO_OBJECT_SETREF (res, object_data, object_data);
        MONO_OBJECT_SETREF (res, async_state, state);
        if (handle != NULL)
@@ -6172,7 +6168,7 @@ mono_message_init (MonoDomain *domain,
        for (i = 0, j = 0; i < sig->param_count; i++) {
                if (sig->params [i]->byref) {
                        if (out_args) {
-                               MonoObject* arg = mono_array_get (out_args, gpointer, j);
+                               MonoObject* arg = (MonoObject *)mono_array_get (out_args, gpointer, j);
                                mono_array_setref (this_obj->args, i, arg);
                                j++;
                        }
@@ -6281,7 +6277,7 @@ mono_message_invoke (MonoObject *target, MonoMethodMessage *msg,
        for (i = 0, j = 0; i < sig->param_count; i++) {
                if (sig->params [i]->byref) {
                        MonoObject* arg;
-                       arg = mono_array_get (msg->args, gpointer, i);
+                       arg = (MonoObject *)mono_array_get (msg->args, gpointer, i);
                        mono_array_setref (*out_args, j, arg);
                        j++;
                }
@@ -6447,10 +6443,10 @@ mono_delegate_ctor (MonoObject *this_obj, MonoObject *target, gpointer addr)
 
        g_assert (addr);
 
-       ji = mono_jit_info_table_find (domain, mono_get_addr_from_ftnptr (addr));
+       ji = mono_jit_info_table_find (domain, (char *)mono_get_addr_from_ftnptr (addr));
        /* Shared code */
        if (!ji && domain != mono_get_root_domain ())
-               ji = mono_jit_info_table_find (mono_get_root_domain (), mono_get_addr_from_ftnptr (addr));
+               ji = mono_jit_info_table_find (mono_get_root_domain (), (char *)mono_get_addr_from_ftnptr (addr));
        if (ji) {
                method = mono_jit_info_get_method (ji);
                g_assert (!method->klass->generic_container);
@@ -6546,7 +6542,7 @@ mono_method_return_message_restore (MonoMethod *method, gpointer *params, MonoAr
                        if (j >= out_len)
                                mono_raise_exception (mono_get_exception_execution_engine ("The proxy call returned an incorrect number of output arguments"));
 
-                       arg = mono_array_get (out_args, gpointer, j);
+                       arg = (char *)mono_array_get (out_args, gpointer, j);
                        type = pt->type;
 
                        g_assert (type != MONO_TYPE_VOID);