3 * domain.c: MonoDomain functions
6 * Dietmar Maurer (dietmar@ximian.com)
9 * (C) 2001 Ximian, Inc.
17 #include <mono/os/gc_wrapper.h>
19 #include <mono/metadata/object.h>
20 #include <mono/metadata/domain-internals.h>
21 #include <mono/metadata/class-internals.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/exception.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/rawbuffer.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/mono-debug-debugger.h>
29 /* #define DEBUG_DOMAIN_UNLOAD */
31 /* we need to use both the Tls* functions and __thread because
32 * the gc needs to see all the appcontext
34 static guint32 context_thread_id = -1;
35 static guint32 appdomain_thread_id = -1;
38 static __thread MonoDomain * tls_appdomain;
39 static __thread MonoAppContext * tls_appcontext;
40 #define GET_APPDOMAIN() tls_appdomain
41 #define SET_APPDOMAIN(x) do { \
43 TlsSetValue (appdomain_thread_id, x); \
46 #define GET_APPCONTEXT() tls_appcontext
47 #define SET_APPCONTEXT(x) do { \
49 TlsSetValue (context_thread_id, x); \
53 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
54 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
56 #define GET_APPCONTEXT() ((MonoAppContext *)TlsGetValue (context_thread_id))
57 #define SET_APPCONTEXT(x) TlsSetValue (context_thread_id, x);
60 static gint32 appdomain_id_counter = 0;
62 static MonoGHashTable * appdomains_list = NULL;
64 static CRITICAL_SECTION appdomains_mutex;
66 static MonoDomain *mono_root_domain = NULL;
68 /* RuntimeInfo: Contains information about versions supported by this runtime */
70 const char* runtime_version;
71 const char* framework_version;
74 /* AppConfigInfo: Information about runtime versions supported by an
78 GSList *supported_runtimes;
79 char *required_runtime;
80 int configuration_count;
84 static RuntimeInfo *current_runtime = NULL;
86 /* This is the list of runtime versions supported by this JIT.
88 static RuntimeInfo supported_runtimes[] = {
89 {"v1.0.3705", "1.0"}, {"v1.1.4322", "1.0"}, {"v2.0.40607","2.0"}
92 /* The stable runtime version */
93 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
96 get_runtime_from_exe (const char *exe_file);
99 get_runtime_by_version (const char *version);
102 mono_domain_get_tls_key (void)
104 return appdomain_thread_id;
107 static MonoJitInfoTable *
108 mono_jit_info_table_new (void)
110 return g_array_new (FALSE, FALSE, sizeof (gpointer));
114 mono_jit_info_table_free (MonoJitInfoTable *table)
116 g_array_free (table, TRUE);
120 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
122 int left = 0, right = table->len;
124 while (left < right) {
125 int pos = (left + right) / 2;
126 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
127 char *start = ji->code_start;
128 char *end = start + ji->code_size;
132 else if (addr >= end)
142 mono_jit_info_table_find (MonoDomain *domain, char *addr)
144 MonoJitInfoTable *table = domain->jit_info_table;
147 mono_domain_lock (domain);
150 while (left < right) {
151 int pos = (left + right) / 2;
152 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
153 char *start = ji->code_start;
154 char *end = start + ji->code_size;
158 else if (addr >= end)
161 mono_domain_unlock (domain);
165 mono_domain_unlock (domain);
167 /* maybe it is shared code, so we also search in the root domain */
168 if (domain != mono_root_domain)
169 return mono_jit_info_table_find (mono_root_domain, addr);
175 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
177 MonoJitInfoTable *table = domain->jit_info_table;
178 gpointer start = ji->code_start;
181 mono_domain_lock (domain);
182 pos = mono_jit_info_table_index (table, start);
184 g_array_insert_val (table, pos, ji);
185 mono_domain_unlock (domain);
189 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
191 MonoJitInfoTable *table = domain->jit_info_table;
192 gpointer start = ji->code_start;
195 mono_domain_lock (domain);
196 pos = mono_jit_info_table_index (table, start);
197 g_assert (g_array_index (table, gpointer, pos) == ji);
199 g_array_remove_index (table, pos);
200 mono_domain_unlock (domain);
204 mono_string_equal (MonoString *s1, MonoString *s2)
206 int l1 = mono_string_length (s1);
207 int l2 = mono_string_length (s2);
214 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1 * 2) == 0;
218 mono_string_hash (MonoString *s)
220 const guint16 *p = mono_string_chars (s);
221 int i, len = mono_string_length (s);
224 for (i = 0; i < len; i++) {
225 h = (h << 5) - h + *p;
234 domain_finalizer (void *obj, void *data) {
235 /*g_print ("domain finalized\n");*/
240 mono_domain_create (void)
245 domain = GC_MALLOC (sizeof (MonoDomain));
246 GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
248 domain = g_new0 (MonoDomain, 1);
250 domain->domain = NULL;
251 domain->setup = NULL;
252 domain->friendly_name = NULL;
253 domain->search_path = NULL;
255 domain->mp = mono_mempool_new ();
256 domain->code_mp = mono_code_manager_new ();
257 domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
258 domain->assemblies_by_name = g_hash_table_new (g_str_hash, g_str_equal);
259 domain->assemblies = NULL;
260 domain->class_vtable_hash = g_hash_table_new (NULL, NULL);
261 domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
262 domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
263 domain->jit_code_hash = g_hash_table_new (NULL, NULL);
264 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
265 domain->jit_info_table = mono_jit_info_table_new ();
266 domain->class_init_trampoline_hash = g_hash_table_new (NULL, NULL);
267 domain->jump_trampoline_hash = g_hash_table_new (NULL, NULL);
268 domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
269 domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
271 InitializeCriticalSection (&domain->lock);
273 EnterCriticalSection (&appdomains_mutex);
274 mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
275 LeaveCriticalSection (&appdomains_mutex);
281 * mono_init_internal:
283 * Creates the initial application domain and initializes the mono_defaults
285 * This function is guaranteed to not run any IL code.
286 * If exe_filename is not NULL, the method will determine the required runtime
287 * from the exe configuration file or the version PE field.
288 * If runtime_version is not NULL, that runtime version will be used.
289 * Either exe_filename or runtime_version must be provided.
291 * Returns: the initial domain.
294 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
296 static MonoDomain *domain = NULL;
298 MonoImageOpenStatus status = MONO_IMAGE_OK;
299 MonoAssemblyName corlib_aname;
302 g_assert_not_reached ();
306 appdomain_thread_id = TlsAlloc ();
307 context_thread_id = TlsAlloc ();
309 InitializeCriticalSection (&appdomains_mutex);
311 mono_metadata_init ();
312 mono_raw_buffer_init ();
314 mono_assemblies_init ();
317 /* FIXME: When should we release this memory? */
318 MONO_GC_REGISTER_ROOT (appdomains_list);
319 appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
321 domain = mono_domain_create ();
322 mono_root_domain = domain;
324 SET_APPDOMAIN (domain);
326 if (exe_filename != NULL) {
327 current_runtime = get_runtime_from_exe (exe_filename);
328 } else if (runtime_version != NULL) {
329 current_runtime = get_runtime_by_version (runtime_version);
332 if (current_runtime == NULL) {
333 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
334 current_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
335 g_print ("Using default runtime: %s\n", current_runtime->runtime_version);
338 /* find the corlib */
339 corlib_aname.name = "mscorlib";
340 ass = mono_assembly_load (&corlib_aname, NULL, &status);
341 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
343 case MONO_IMAGE_ERROR_ERRNO: {
344 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", mono_get_framework_version (), "mscorlib.dll", NULL);
345 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
346 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
347 g_free (corlib_file);
350 case MONO_IMAGE_IMAGE_INVALID:
351 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
352 mono_assembly_getrootdir ());
354 case MONO_IMAGE_MISSING_ASSEMBLYREF:
355 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
356 mono_assembly_getrootdir ());
359 /* to suppress compiler warning */
365 mono_defaults.corlib = mono_assembly_get_image (ass);
367 mono_defaults.object_class = mono_class_from_name (
368 mono_defaults.corlib, "System", "Object");
369 g_assert (mono_defaults.object_class != 0);
371 mono_defaults.void_class = mono_class_from_name (
372 mono_defaults.corlib, "System", "Void");
373 g_assert (mono_defaults.void_class != 0);
375 mono_defaults.boolean_class = mono_class_from_name (
376 mono_defaults.corlib, "System", "Boolean");
377 g_assert (mono_defaults.boolean_class != 0);
379 mono_defaults.byte_class = mono_class_from_name (
380 mono_defaults.corlib, "System", "Byte");
381 g_assert (mono_defaults.byte_class != 0);
383 mono_defaults.sbyte_class = mono_class_from_name (
384 mono_defaults.corlib, "System", "SByte");
385 g_assert (mono_defaults.sbyte_class != 0);
387 mono_defaults.int16_class = mono_class_from_name (
388 mono_defaults.corlib, "System", "Int16");
389 g_assert (mono_defaults.int16_class != 0);
391 mono_defaults.uint16_class = mono_class_from_name (
392 mono_defaults.corlib, "System", "UInt16");
393 g_assert (mono_defaults.uint16_class != 0);
395 mono_defaults.int32_class = mono_class_from_name (
396 mono_defaults.corlib, "System", "Int32");
397 g_assert (mono_defaults.int32_class != 0);
399 mono_defaults.uint32_class = mono_class_from_name (
400 mono_defaults.corlib, "System", "UInt32");
401 g_assert (mono_defaults.uint32_class != 0);
403 mono_defaults.uint_class = mono_class_from_name (
404 mono_defaults.corlib, "System", "UIntPtr");
405 g_assert (mono_defaults.uint_class != 0);
407 mono_defaults.int_class = mono_class_from_name (
408 mono_defaults.corlib, "System", "IntPtr");
409 g_assert (mono_defaults.int_class != 0);
411 mono_defaults.int64_class = mono_class_from_name (
412 mono_defaults.corlib, "System", "Int64");
413 g_assert (mono_defaults.int64_class != 0);
415 mono_defaults.uint64_class = mono_class_from_name (
416 mono_defaults.corlib, "System", "UInt64");
417 g_assert (mono_defaults.uint64_class != 0);
419 mono_defaults.single_class = mono_class_from_name (
420 mono_defaults.corlib, "System", "Single");
421 g_assert (mono_defaults.single_class != 0);
423 mono_defaults.double_class = mono_class_from_name (
424 mono_defaults.corlib, "System", "Double");
425 g_assert (mono_defaults.double_class != 0);
427 mono_defaults.char_class = mono_class_from_name (
428 mono_defaults.corlib, "System", "Char");
429 g_assert (mono_defaults.char_class != 0);
431 mono_defaults.string_class = mono_class_from_name (
432 mono_defaults.corlib, "System", "String");
433 g_assert (mono_defaults.string_class != 0);
435 mono_defaults.enum_class = mono_class_from_name (
436 mono_defaults.corlib, "System", "Enum");
437 g_assert (mono_defaults.enum_class != 0);
439 mono_defaults.array_class = mono_class_from_name (
440 mono_defaults.corlib, "System", "Array");
441 g_assert (mono_defaults.array_class != 0);
443 mono_defaults.delegate_class = mono_class_from_name (
444 mono_defaults.corlib, "System", "Delegate");
445 g_assert (mono_defaults.delegate_class != 0 );
447 mono_defaults.multicastdelegate_class = mono_class_from_name (
448 mono_defaults.corlib, "System", "MulticastDelegate");
449 g_assert (mono_defaults.multicastdelegate_class != 0 );
451 mono_defaults.asyncresult_class = mono_class_from_name (
452 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
454 g_assert (mono_defaults.asyncresult_class != 0 );
456 mono_defaults.waithandle_class = mono_class_from_name (
457 mono_defaults.corlib, "System.Threading", "WaitHandle");
458 g_assert (mono_defaults.waithandle_class != 0 );
460 mono_defaults.typehandle_class = mono_class_from_name (
461 mono_defaults.corlib, "System", "RuntimeTypeHandle");
462 g_assert (mono_defaults.typehandle_class != 0);
464 mono_defaults.methodhandle_class = mono_class_from_name (
465 mono_defaults.corlib, "System", "RuntimeMethodHandle");
466 g_assert (mono_defaults.methodhandle_class != 0);
468 mono_defaults.fieldhandle_class = mono_class_from_name (
469 mono_defaults.corlib, "System", "RuntimeFieldHandle");
470 g_assert (mono_defaults.fieldhandle_class != 0);
472 mono_defaults.monotype_class = mono_class_from_name (
473 mono_defaults.corlib, "System", "MonoType");
474 g_assert (mono_defaults.monotype_class != 0);
476 mono_defaults.exception_class = mono_class_from_name (
477 mono_defaults.corlib, "System", "Exception");
478 g_assert (mono_defaults.exception_class != 0);
480 mono_defaults.threadabortexception_class = mono_class_from_name (
481 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
482 g_assert (mono_defaults.threadabortexception_class != 0);
484 mono_defaults.thread_class = mono_class_from_name (
485 mono_defaults.corlib, "System.Threading", "Thread");
486 g_assert (mono_defaults.thread_class != 0);
488 mono_defaults.appdomain_class = mono_class_from_name (
489 mono_defaults.corlib, "System", "AppDomain");
490 g_assert (mono_defaults.appdomain_class != 0);
492 mono_defaults.transparent_proxy_class = mono_class_from_name (
493 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
494 g_assert (mono_defaults.transparent_proxy_class != 0);
496 mono_defaults.real_proxy_class = mono_class_from_name (
497 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
498 g_assert (mono_defaults.real_proxy_class != 0);
500 mono_defaults.mono_method_message_class = mono_class_from_name (
501 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
502 g_assert (mono_defaults.mono_method_message_class != 0);
504 mono_defaults.field_info_class = mono_class_from_name (
505 mono_defaults.corlib, "System.Reflection", "FieldInfo");
506 g_assert (mono_defaults.field_info_class != 0);
508 mono_defaults.method_info_class = mono_class_from_name (
509 mono_defaults.corlib, "System.Reflection", "MethodInfo");
510 g_assert (mono_defaults.method_info_class != 0);
512 mono_defaults.stringbuilder_class = mono_class_from_name (
513 mono_defaults.corlib, "System.Text", "StringBuilder");
514 g_assert (mono_defaults.stringbuilder_class != 0);
516 mono_defaults.math_class = mono_class_from_name (
517 mono_defaults.corlib, "System", "Math");
518 g_assert (mono_defaults.math_class != 0);
520 mono_defaults.stack_frame_class = mono_class_from_name (
521 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
522 g_assert (mono_defaults.stack_frame_class != 0);
524 mono_defaults.stack_trace_class = mono_class_from_name (
525 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
526 g_assert (mono_defaults.stack_trace_class != 0);
528 mono_defaults.marshal_class = mono_class_from_name (
529 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
530 g_assert (mono_defaults.marshal_class != 0);
532 mono_defaults.iserializeable_class = mono_class_from_name (
533 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
534 g_assert (mono_defaults.iserializeable_class != 0);
536 mono_defaults.serializationinfo_class = mono_class_from_name (
537 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
538 g_assert (mono_defaults.serializationinfo_class != 0);
540 mono_defaults.streamingcontext_class = mono_class_from_name (
541 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
542 g_assert (mono_defaults.streamingcontext_class != 0);
544 mono_defaults.typed_reference_class = mono_class_from_name (
545 mono_defaults.corlib, "System", "TypedReference");
546 g_assert (mono_defaults.typed_reference_class != 0);
548 mono_defaults.argumenthandle_class = mono_class_from_name (
549 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
550 g_assert (mono_defaults.argumenthandle_class != 0);
552 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
553 mono_defaults.corlib, "System", "MarshalByRefObject");
554 g_assert (mono_defaults.marshalbyrefobject_class != 0);
556 mono_defaults.monitor_class = mono_class_from_name (
557 mono_defaults.corlib, "System.Threading", "Monitor");
558 g_assert (mono_defaults.monitor_class != 0);
560 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
561 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
562 g_assert (mono_defaults.iremotingtypeinfo_class != 0);
564 mono_defaults.runtimesecurityframe_class = mono_class_from_name (
565 mono_defaults.corlib, "System.Security", "RuntimeSecurityFrame");
566 g_assert (mono_defaults.runtimesecurityframe_class != 0);
568 domain->friendly_name = g_path_get_basename (filename);
576 * Creates the initial application domain and initializes the mono_defaults
578 * This function is guaranteed to not run any IL code.
579 * The runtime is initialized using the default runtime version.
581 * Returns: the initial domain.
584 mono_init (const char *domain_name)
586 return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
590 * mono_init_from_assembly:
592 * Creates the initial application domain and initializes the mono_defaults
594 * This function is guaranteed to not run any IL code.
595 * The runtime is initialized using the runtime version required by the
596 * provided executable. The version is determined by looking at the exe
597 * configuration file and the version PE field)
599 * Returns: the initial domain.
602 mono_init_from_assembly (const char *domain_name, const char *filename)
604 return mono_init_internal (domain_name, filename, NULL);
610 * Creates the initial application domain and initializes the mono_defaults
612 * This function is guaranteed to not run any IL code.
613 * The runtime is initialized using the provided rutime version.
615 * Returns: the initial domain.
618 mono_init_version (const char *domain_name, const char *version)
620 return mono_init_internal (domain_name, NULL, version);
624 mono_get_root_domain (void)
626 return mono_root_domain;
632 * Returns: the current domain.
637 return GET_APPDOMAIN ();
641 * mono_domain_set_internal:
642 * @domain: the new domain
644 * Sets the current domain to @domain.
647 mono_domain_set_internal (MonoDomain *domain)
649 SET_APPDOMAIN (domain);
650 SET_APPCONTEXT (domain->default_context);
659 copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
661 MonoGHashTable *dest = (MonoGHashTable*)user_data;
663 mono_g_hash_table_insert (dest, key, data);
667 foreach_domain (gpointer key, gpointer data, gpointer user_data)
669 DomainInfo *dom_info = user_data;
671 dom_info->func ((MonoDomain*)data, dom_info->user_data);
675 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
678 MonoGHashTable *copy;
681 * Create a copy of the hashtable to avoid calling the user callback
682 * inside the lock because that could lead to deadlocks.
683 * We can do this because this function is not perf. critical.
685 copy = mono_g_hash_table_new (NULL, NULL);
686 EnterCriticalSection (&appdomains_mutex);
687 mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
688 LeaveCriticalSection (&appdomains_mutex);
690 dom_info.func = func;
691 dom_info.user_data = user_data;
692 mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
694 mono_g_hash_table_destroy (copy);
698 * mono_domain_assembly_open:
699 * @domain: the application domain
700 * @name: file name of the assembly
702 * fixme: maybe we should integrate this with mono_assembly_open ??
705 mono_domain_assembly_open (MonoDomain *domain, const char *name)
709 mono_domain_lock (domain);
710 if ((ass = g_hash_table_lookup (domain->assemblies_by_name, name))) {
711 mono_domain_unlock (domain);
714 mono_domain_unlock (domain);
716 if (!(ass = mono_assembly_open (name, NULL)))
723 remove_assembly (gpointer key, gpointer value, gpointer user_data)
725 mono_assembly_close ((MonoAssembly *)value);
729 add_assembly (gpointer key, gpointer value, gpointer user_data)
731 g_hash_table_insert ((GHashTable*)user_data, value, value);
735 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
737 MonoJitDynamicMethodInfo *di = value;
738 mono_code_manager_destroy (di->code_mp);
743 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
745 g_slist_free (value);
749 mono_domain_free (MonoDomain *domain, gboolean force)
751 GHashTable *unique_assemblies;
753 if ((domain == mono_root_domain) && !force) {
754 g_warning ("cant unload root domain");
758 EnterCriticalSection (&appdomains_mutex);
759 mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
760 LeaveCriticalSection (&appdomains_mutex);
762 /* FIXME: free delegate_hash_table when it's used */
763 if (domain->search_path) {
764 g_strfreev (domain->search_path);
765 domain->search_path = NULL;
767 domain->create_proxy_for_type_method = NULL;
768 domain->private_invoke_method = NULL;
769 domain->default_context = NULL;
770 domain->out_of_memory_ex = NULL;
771 domain->null_reference_ex = NULL;
772 domain->stack_overflow_ex = NULL;
773 domain->entry_assembly = NULL;
774 g_free (domain->friendly_name);
775 domain->friendly_name = NULL;
776 /* some assemblies are in domain->assemblies_by_name and some only in domain->assemblies:
777 * collect them in unique_assemblies so we don't free them twice.
779 unique_assemblies = g_hash_table_new (NULL, NULL);
780 g_hash_table_foreach (domain->assemblies_by_name, add_assembly, unique_assemblies);
781 for (tmp = domain->assemblies; tmp; tmp = tmp->next)
782 g_hash_table_insert (unique_assemblies, tmp->data, tmp->data);
783 g_hash_table_foreach (unique_assemblies, remove_assembly, NULL);
784 g_hash_table_destroy (unique_assemblies);
785 g_hash_table_destroy (domain->assemblies_by_name);
786 domain->assemblies_by_name = NULL;
787 g_list_free (domain->assemblies);
788 domain->assemblies = NULL;
790 mono_g_hash_table_destroy (domain->env);
792 g_hash_table_destroy (domain->class_vtable_hash);
793 domain->class_vtable_hash = NULL;
794 mono_g_hash_table_destroy (domain->proxy_vtable_hash);
795 domain->proxy_vtable_hash = NULL;
796 mono_g_hash_table_destroy (domain->static_data_hash);
797 domain->static_data_hash = NULL;
798 g_hash_table_destroy (domain->jit_code_hash);
799 domain->jit_code_hash = NULL;
800 if (domain->dynamic_code_hash) {
801 g_hash_table_foreach (domain->dynamic_code_hash, dynamic_method_info_free, NULL);
802 g_hash_table_destroy (domain->dynamic_code_hash);
803 domain->dynamic_code_hash = NULL;
805 mono_g_hash_table_destroy (domain->ldstr_table);
806 domain->ldstr_table = NULL;
807 mono_jit_info_table_free (domain->jit_info_table);
808 domain->jit_info_table = NULL;
809 #ifdef DEBUG_DOMAIN_UNLOAD
810 mono_mempool_invalidate (domain->mp);
811 mono_code_manager_invalidate (domain->code_mp);
813 mono_mempool_destroy (domain->mp);
815 mono_code_manager_destroy (domain->code_mp);
816 domain->code_mp = NULL;
818 if (domain->jump_target_hash) {
819 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
820 g_hash_table_destroy (domain->jump_target_hash);
821 domain->jump_target_hash = NULL;
823 if (domain->type_hash) {
824 mono_g_hash_table_destroy (domain->type_hash);
825 domain->type_hash = NULL;
827 if (domain->refobject_hash) {
828 mono_g_hash_table_destroy (domain->refobject_hash);
829 domain->refobject_hash = NULL;
831 g_hash_table_destroy (domain->class_init_trampoline_hash);
832 domain->class_init_trampoline_hash = NULL;
833 g_hash_table_destroy (domain->jump_trampoline_hash);
834 domain->jump_trampoline_hash = NULL;
835 g_hash_table_destroy (domain->finalizable_objects_hash);
836 domain->finalizable_objects_hash = NULL;
837 if (domain->special_static_fields) {
838 g_hash_table_destroy (domain->special_static_fields);
839 domain->special_static_fields = NULL;
841 DeleteCriticalSection (&domain->lock);
842 domain->setup = NULL;
844 /* FIXME: anything else required ? */
851 if ((domain == mono_root_domain))
852 mono_root_domain = NULL;
856 * mono_domain_get_id:
858 * Returns: the a domain for a specific domain id.
861 mono_domain_get_by_id (gint32 domainid)
865 EnterCriticalSection (&appdomains_mutex);
866 domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
867 LeaveCriticalSection (&appdomains_mutex);
873 mono_domain_get_id (MonoDomain *domain)
875 return domain->domain_id;
879 mono_context_set (MonoAppContext * new_context)
881 SET_APPCONTEXT (new_context);
885 mono_context_get (void)
887 return GET_APPCONTEXT ();
891 mono_get_corlib (void)
893 return mono_defaults.corlib;
897 mono_get_object_class (void)
899 return mono_defaults.object_class;
903 mono_get_byte_class (void)
905 return mono_defaults.byte_class;
909 mono_get_void_class (void)
911 return mono_defaults.void_class;
915 mono_get_boolean_class (void)
917 return mono_defaults.boolean_class;
921 mono_get_sbyte_class (void)
923 return mono_defaults.sbyte_class;
927 mono_get_int16_class (void)
929 return mono_defaults.int16_class;
933 mono_get_uint16_class (void)
935 return mono_defaults.uint16_class;
939 mono_get_int32_class (void)
941 return mono_defaults.int32_class;
945 mono_get_uint32_class (void)
947 return mono_defaults.uint32_class;
951 mono_get_intptr_class (void)
953 return mono_defaults.int_class;
957 mono_get_uintptr_class (void)
959 return mono_defaults.uint_class;
963 mono_get_int64_class (void)
965 return mono_defaults.int64_class;
969 mono_get_uint64_class (void)
971 return mono_defaults.uint64_class;
975 mono_get_single_class (void)
977 return mono_defaults.single_class;
981 mono_get_double_class (void)
983 return mono_defaults.double_class;
987 mono_get_char_class (void)
989 return mono_defaults.char_class;
993 mono_get_string_class (void)
995 return mono_defaults.string_class;
999 mono_get_enum_class (void)
1001 return mono_defaults.enum_class;
1005 mono_get_array_class (void)
1007 return mono_defaults.array_class;
1011 mono_get_thread_class (void)
1013 return mono_defaults.thread_class;
1017 mono_get_exception_class (void)
1019 return mono_defaults.exception_class;
1023 static char* get_attribute_value (const gchar **attribute_names,
1024 const gchar **attribute_values,
1025 const char *att_name)
1028 for (n=0; attribute_names[n] != NULL; n++) {
1029 if (strcmp (attribute_names[n], att_name) == 0)
1030 return g_strdup (attribute_values[n]);
1035 static void start_element (GMarkupParseContext *context,
1036 const gchar *element_name,
1037 const gchar **attribute_names,
1038 const gchar **attribute_values,
1042 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1044 if (strcmp (element_name, "configuration") == 0) {
1045 app_config->configuration_count++;
1048 if (strcmp (element_name, "startup") == 0) {
1049 app_config->startup_count++;
1053 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1056 if (strcmp (element_name, "requiredRuntime") == 0) {
1057 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1058 } else if (strcmp (element_name, "supportedRuntime") == 0) {
1059 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1060 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1064 static void end_element (GMarkupParseContext *context,
1065 const gchar *element_name,
1069 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1071 if (strcmp (element_name, "configuration") == 0) {
1072 app_config->configuration_count--;
1073 } else if (strcmp (element_name, "startup") == 0) {
1074 app_config->startup_count--;
1078 static const GMarkupParser
1087 static AppConfigInfo *
1088 app_config_parse (const char *filename)
1090 AppConfigInfo *app_config;
1091 GMarkupParseContext *context;
1096 if (stat (filename, &buf) != 0)
1099 app_config = g_new0 (AppConfigInfo, 1);
1101 if (!g_file_get_contents (filename, &text, &len, NULL))
1104 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1105 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1106 g_markup_parse_context_end_parse (context, NULL);
1108 g_markup_parse_context_free (context);
1114 app_config_free (AppConfigInfo* app_config)
1117 GSList *list = app_config->supported_runtimes;
1118 while (list != NULL) {
1119 rt = (char*)list->data;
1121 list = g_slist_next (list);
1123 g_slist_free (app_config->supported_runtimes);
1124 g_free (app_config->required_runtime);
1125 g_free (app_config);
1130 get_runtime_by_version (const char *version)
1133 int max = G_N_ELEMENTS (supported_runtimes);
1135 for (n=0; n<max; n++) {
1136 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1137 return &supported_runtimes[n];
1143 get_runtime_from_exe (const char *exe_file)
1145 AppConfigInfo* app_config;
1148 RuntimeInfo* runtime = NULL;
1149 MonoImage *image = NULL;
1151 config_name = g_strconcat (exe_file, ".config", NULL);
1152 app_config = app_config_parse (config_name);
1153 g_free (config_name);
1155 if (app_config != NULL) {
1156 /* Check supportedRuntime elements, if none is supported, fail.
1157 * If there are no such elements, look for a requiredRuntime element.
1159 if (app_config->supported_runtimes != NULL) {
1160 GSList *list = app_config->supported_runtimes;
1161 while (list != NULL && runtime == NULL) {
1162 version = (char*) list->data;
1163 runtime = get_runtime_by_version (version);
1164 list = g_slist_next (list);
1166 app_config_free (app_config);
1170 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1171 if (app_config->required_runtime != NULL) {
1172 runtime = get_runtime_by_version (app_config->required_runtime);
1173 app_config_free (app_config);
1176 app_config_free (app_config);
1179 /* Look for a runtime with the exact version */
1180 image = mono_image_open (exe_file, NULL);
1181 if (image == NULL) {
1182 /* The image is wrong or the file was not found. In this case return
1183 * a default runtime and leave to the initialization method the work of
1184 * reporting the error.
1186 return get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1189 runtime = get_runtime_by_version (image->version);
1195 mono_get_framework_version (void)
1197 return current_runtime->framework_version;
1201 mono_get_runtime_version (void)
1203 return current_runtime->runtime_version;
1207 mono_debugger_check_runtime_version (const char *filename)
1211 rinfo = get_runtime_from_exe (filename);
1213 return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);
1215 if (rinfo != current_runtime)
1216 return g_strdup_printf ("The Mono Debugger is currently using the `%s' runtime, but "
1217 "the assembly `%s' requires version `%s'", current_runtime->runtime_version,
1218 filename, rinfo->runtime_version);