[runtime] Free info_offsets used by emit_extra_methods
[mono.git] / mono / mini / aot-compiler.c
index 28d254d0ac601e4f0b3e1c18b384b584f4d084ba..cba4adbe28bc23aa3514ad4e2f3ecf990d1990eb 100644 (file)
@@ -52,6 +52,7 @@
 #include <mono/utils/mono-time.h>
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/json.h>
+#include <mono/utils/mono-threads-coop.h>
 
 #include "aot-compiler.h"
 #include "seq-points.h"
@@ -2724,7 +2725,10 @@ find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
                        MonoError error;
                        int typespec = MONO_TOKEN_TYPE_SPEC | (i + 1);
                        MonoClass *klass_key = mono_class_get_and_inflate_typespec_checked (acfg->image, typespec, NULL, &error);
-                       g_assert (mono_error_ok (&error)); /* FIXME error handling */
+                       if (!is_ok (&error)) {
+                               mono_error_cleanup (&error);
+                               continue;
+                       }
                        g_hash_table_insert (acfg->typespec_classes, klass_key, GINT_TO_POINTER (typespec));
                }
        }
@@ -3096,7 +3100,6 @@ encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8
                        encode_klass_ref (acfg, info->d.proxy.klass, p, &p);
                        break;
                }
-               case MONO_WRAPPER_LDFLD_REMOTE:
                case MONO_WRAPPER_STFLD_REMOTE:
                        break;
                case MONO_WRAPPER_ALLOC: {
@@ -3582,7 +3585,9 @@ get_runtime_invoke_sig (MonoMethodSignature *sig)
 
        mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
        m = mono_mb_create_method (mb, sig, 16);
-       return mono_marshal_get_runtime_invoke (m, FALSE);
+       MonoMethod *invoke = mono_marshal_get_runtime_invoke (m, FALSE);
+       mono_mb_free (mb);
+       return invoke;
 }
 
 static MonoMethod*
@@ -4054,7 +4059,7 @@ add_wrappers (MonoAotCompile *acfg)
                        continue;
                }
 
-               if (!acfg->aot_opts.llvm_only && klass->rank && MONO_TYPE_IS_PRIMITIVE (&klass->element_class->byval_arg)) {
+               if (klass->rank && MONO_TYPE_IS_PRIMITIVE (&klass->element_class->byval_arg)) {
                        MonoMethod *m, *wrapper;
 
                        /* Add runtime-invoke wrappers too */
@@ -4063,13 +4068,15 @@ add_wrappers (MonoAotCompile *acfg)
                        g_assert (m);
                        wrapper = mono_marshal_get_array_accessor_wrapper (m);
                        add_extra_method (acfg, wrapper);
-                       add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
+                       if (!acfg->aot_opts.llvm_only)
+                               add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
 
                        m = mono_class_get_method_from_name (klass, "Set", -1);
                        g_assert (m);
                        wrapper = mono_marshal_get_array_accessor_wrapper (m);
                        add_extra_method (acfg, wrapper);
-                       add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
+                       if (!acfg->aot_opts.llvm_only)
+                               add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
                }
        }
 
@@ -4228,6 +4235,7 @@ add_wrappers (MonoAotCompile *acfg)
                                if (export_name)
                                        g_hash_table_insert (acfg->export_names, wrapper, export_name);
                        }
+                       g_free (cattr);
                }
 
                if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
@@ -4652,9 +4660,11 @@ add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method)
                for (j = 0; j < header->num_locals; ++j)
                        if (header->locals [j]->type == MONO_TYPE_GENERICINST)
                                add_generic_class_with_depth (acfg, mono_class_from_mono_type (header->locals [j]), depth + 1, "local");
+               mono_metadata_free_mh (header);
        } else {
                mono_error_cleanup (&error); /* FIXME report the error */
        }
+
 }
 
 /*
@@ -4939,23 +4949,23 @@ add_generic_instances (MonoAotCompile *acfg)
                }
 
                /* object[] accessor wrappers. */
-               {
-                       MonoClass *obj_array_class = mono_array_class_get (mono_defaults.object_class, 1);
+               for (i = 1; i < 4; ++i) {
+                       MonoClass *obj_array_class = mono_array_class_get (mono_defaults.object_class, i);
                        MonoMethod *m;
 
-                       m = mono_class_get_method_from_name (obj_array_class, "Get", 1);
+                       m = mono_class_get_method_from_name (obj_array_class, "Get", i);
                        g_assert (m);
 
                        m = mono_marshal_get_array_accessor_wrapper (m);
                        add_extra_method (acfg, m);
 
-                       m = mono_class_get_method_from_name (obj_array_class, "Address", 1);
+                       m = mono_class_get_method_from_name (obj_array_class, "Address", i);
                        g_assert (m);
 
                        m = mono_marshal_get_array_accessor_wrapper (m);
                        add_extra_method (acfg, m);
 
-                       m = mono_class_get_method_from_name (obj_array_class, "Set", 2);
+                       m = mono_class_get_method_from_name (obj_array_class, "Set", i + 1);
                        g_assert (m);
 
                        m = mono_marshal_get_array_accessor_wrapper (m);
@@ -5390,6 +5400,7 @@ emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, gui
                }
        }
 
+       g_ptr_array_free (patches, TRUE);
        g_free (locs);
 }
 
@@ -5885,6 +5896,8 @@ emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
 
        encode_patch_list (acfg, patches, n_patches, cfg->compile_llvm, first_got_offset, p, &p);
 
+       g_ptr_array_free (patches, TRUE);
+
        acfg->stats.info_size += p - buf;
 
        g_assert (p - buf < buf_size);
@@ -5971,6 +5984,8 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg, gboolean stor
 
                unwind_desc = get_unwind_info_offset (acfg, encoded, encoded_len);
                encode_value (unwind_desc, p, &p);
+
+               g_free (encoded);
        } else {
                encode_value (jinfo->unwind_info, p, &p);
        }
@@ -6509,6 +6524,7 @@ emit_trampoline_full (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info,
 
        encode_patch_list (acfg, patches, patches->len, FALSE, got_offset, p, &p);
        g_assert (p - buf < buf_size);
+       g_ptr_array_free (patches, TRUE);
 
        sprintf (symbol, "%s%s_p", acfg->user_symbol_prefix, name);
 
@@ -6546,6 +6562,8 @@ emit_trampoline_full (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info,
                if (acfg->dwarf)
                        mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
        }
+
+       g_free (buf);
 }
 
 static G_GNUC_UNUSED void
@@ -7029,6 +7047,8 @@ mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
                        opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
                } else if (str_begins_with (arg, "nrgctx-trampolines=")) {
                        opts->nrgctx_trampolines = atoi (arg + strlen ("nrgctx-trampolines="));
+               } else if (str_begins_with (arg, "nrgctx-fetch-trampolines=")) {
+                       opts->nrgctx_fetch_trampolines = atoi (arg + strlen ("nrgctx-fetch-trampolines="));
                } else if (str_begins_with (arg, "nimt-trampolines=")) {
                        opts->nimt_trampolines = atoi (arg + strlen ("nimt-trampolines="));
                } else if (str_begins_with (arg, "ngsharedvt-trampolines=")) {
@@ -7177,7 +7197,6 @@ can_encode_method (MonoAotCompile *acfg, MonoMethod *method)
                        case MONO_WRAPPER_STFLD:
                        case MONO_WRAPPER_LDFLD:
                        case MONO_WRAPPER_LDFLDA:
-                       case MONO_WRAPPER_LDFLD_REMOTE:
                        case MONO_WRAPPER_STFLD_REMOTE:
                        case MONO_WRAPPER_STELEMREF:
                        case MONO_WRAPPER_ISINST:
@@ -7460,7 +7479,6 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                if (acfg->aot_opts.print_skipped_methods)
                        printf ("Skip (disabled): %s\n", mono_method_get_full_name (method));
                InterlockedIncrement (&acfg->stats.ocount);
-               mono_destroy_compile (cfg);
                return;
        }
        cfg->method_index = index;
@@ -7503,7 +7521,6 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                if (acfg->aot_opts.print_skipped_methods)
                        printf ("Skip (abs call): %s\n", mono_method_get_full_name (method));
                InterlockedIncrement (&acfg->stats.abscount);
-               mono_destroy_compile (cfg);
                return;
        }
 
@@ -7532,7 +7549,6 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                if (acfg->aot_opts.print_skipped_methods)
                        printf ("Skip (patches): %s\n", mono_method_get_full_name (method));
                acfg->stats.ocount++;
-               mono_destroy_compile (cfg);
                mono_acfg_unlock (acfg);
                return;
        }
@@ -7709,20 +7725,12 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                        locals [i] = (MonoInst *)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
                        memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
                }
+               mono_metadata_free_mh (header);
                cfg->locals = locals;
        }
 
        /* Free some fields used by cfg to conserve memory */
-       mono_mempool_destroy (cfg->mempool);
-       cfg->mempool = NULL;
-       g_free (cfg->varinfo);
-       cfg->varinfo = NULL;
-       g_free (cfg->vars);
-       cfg->vars = NULL;
-       if (cfg->rs) {
-               mono_regstate_free (cfg->rs);
-               cfg->rs = NULL;
-       }
+       mono_empty_compile (cfg);
 
        //printf ("Compile:           %s\n", mono_method_full_name (method, TRUE));
 
@@ -8813,6 +8821,7 @@ emit_extra_methods (MonoAotCompile *acfg)
                        g_ptr_array_add (table, new_entry);
                }
        }
+       g_free (chain_lengths);
 
        //printf ("MAX: %d\n", max_chain_length);
 
@@ -8842,6 +8851,8 @@ emit_extra_methods (MonoAotCompile *acfg)
        /* Emit the table */
        emit_aot_data (acfg, MONO_AOT_TABLE_EXTRA_METHOD_TABLE, "extra_method_table", buf, p - buf);
 
+       g_free (buf);
+
        /* 
         * Emit a table reverse mapping method indexes to their index in extra_method_info.
         * This is used by mono_aot_find_jit_info ().
@@ -8856,6 +8867,10 @@ emit_extra_methods (MonoAotCompile *acfg)
                encode_int (info_offsets [i], p, &p);
        }
        emit_aot_data (acfg, MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS, "extra_method_info_offsets", buf, p - buf);
+
+       g_free (buf);
+       g_free (info_offsets);
+       g_ptr_array_free (table, TRUE);
 }      
 
 static void
@@ -9032,8 +9047,10 @@ emit_class_name_table (MonoAotCompile *acfg)
                        else
                                encode_int16 (0, p, &p);
                }
+               g_free (entry);
        }
        g_assert (p - buf <= buf_size);
+       g_ptr_array_free (table, TRUE);
 
        emit_aot_data (acfg, MONO_AOT_TABLE_CLASS_NAME, "class_name_table", buf, p - buf);
 }
@@ -9712,6 +9729,7 @@ compile_methods (MonoAotCompile *acfg)
                HANDLE handle;
                gpointer *user_data;
                MonoMethod **methods;
+               MonoThreadParm tp;
 
                methods_len = acfg->methods->len;
 
@@ -9742,7 +9760,10 @@ compile_methods (MonoAotCompile *acfg)
                        user_data [1] = acfg;
                        user_data [2] = frag;
                        
-                       handle = mono_threads_create_thread ((LPTHREAD_START_ROUTINE)compile_thread_main, user_data, 0, 0, NULL);
+                       tp.priority = 0;
+                       tp.stack_size = 0;
+                       tp.creation_flags = 0;
+                       handle = mono_threads_create_thread ((LPTHREAD_START_ROUTINE)compile_thread_main, user_data, &tp, NULL);
                        g_ptr_array_add (threads, handle);
                }
                g_free (methods);
@@ -10038,8 +10059,10 @@ acfg_free (MonoAotCompile *acfg)
        mono_img_writer_destroy (acfg->w);
        for (i = 0; i < acfg->nmethods; ++i)
                if (acfg->cfgs [i])
-                       g_free (acfg->cfgs [i]);
+                       mono_destroy_compile (acfg->cfgs [i]);
+
        g_free (acfg->cfgs);
+
        g_free (acfg->static_linking_symbol);
        g_free (acfg->got_symbol);
        g_free (acfg->plt_symbol);
@@ -10061,7 +10084,7 @@ acfg_free (MonoAotCompile *acfg)
        g_hash_table_destroy (acfg->image_hash);
        g_hash_table_destroy (acfg->unwind_info_offsets);
        g_hash_table_destroy (acfg->method_label_hash);
-       if (!acfg->typespec_classes)
+       if (acfg->typespec_classes)
                g_hash_table_destroy (acfg->typespec_classes);
        g_hash_table_destroy (acfg->export_names);
        g_hash_table_destroy (acfg->plt_entry_debug_sym_cache);