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);
101 static MonoJitInfoTable *
102 mono_jit_info_table_new (void)
104 return g_array_new (FALSE, FALSE, sizeof (gpointer));
108 mono_jit_info_table_free (MonoJitInfoTable *table)
110 g_array_free (table, TRUE);
114 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
116 int left = 0, right = table->len;
118 while (left < right) {
119 int pos = (left + right) / 2;
120 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
121 char *start = ji->code_start;
122 char *end = start + ji->code_size;
126 else if (addr >= end)
136 mono_jit_info_table_find (MonoDomain *domain, char *addr)
138 MonoJitInfoTable *table = domain->jit_info_table;
141 mono_domain_lock (domain);
144 while (left < right) {
145 int pos = (left + right) / 2;
146 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
147 char *start = ji->code_start;
148 char *end = start + ji->code_size;
152 else if (addr >= end)
155 mono_domain_unlock (domain);
159 mono_domain_unlock (domain);
161 /* maybe it is shared code, so we also search in the root domain */
162 if (domain != mono_root_domain)
163 return mono_jit_info_table_find (mono_root_domain, addr);
169 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
171 MonoJitInfoTable *table = domain->jit_info_table;
172 gpointer start = ji->code_start;
175 mono_domain_lock (domain);
176 pos = mono_jit_info_table_index (table, start);
178 g_array_insert_val (table, pos, ji);
179 mono_domain_unlock (domain);
183 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
185 MonoJitInfoTable *table = domain->jit_info_table;
186 gpointer start = ji->code_start;
189 mono_domain_lock (domain);
190 pos = mono_jit_info_table_index (table, start);
191 g_assert (g_array_index (table, gpointer, pos) == ji);
193 g_array_remove_index (table, pos);
194 mono_domain_unlock (domain);
198 ldstr_hash (const char* str)
202 len = mono_metadata_decode_blob_size (str, &str) - 1;
204 /* if len == 0 *str will point to the mark byte */
207 * FIXME: The distribution may not be so nice with lots of
208 * null chars in the string.
210 for (str += 1; str < end; str++)
211 h = (h << 5) - h + *str;
216 ldstr_equal (const char *str1, const char *str2) {
220 len = mono_metadata_decode_blob_size (str1, NULL) - 1;
221 len2 = mono_metadata_decode_blob_size (str2, NULL) - 1;
224 return memcmp (str1, str2, len) == 0;
228 mono_string_equal (MonoString *s1, MonoString *s2)
230 int l1 = mono_string_length (s1);
231 int l2 = mono_string_length (s2);
236 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0;
240 mono_string_hash (MonoString *s)
242 const guint16 *p = mono_string_chars (s);
243 int i, len = mono_string_length (s);
246 for (i = 0; i < len; i++) {
247 h = (h << 5) - h + *p;
256 domain_finalizer (void *obj, void *data) {
257 g_print ("domain finalized\n");
262 mono_domain_create (void)
267 domain = GC_MALLOC (sizeof (MonoDomain));
268 GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
270 domain = g_new0 (MonoDomain, 1);
272 domain->domain = NULL;
273 domain->setup = NULL;
274 domain->friendly_name = NULL;
275 domain->search_path = NULL;
277 domain->mp = mono_mempool_new ();
278 domain->code_mp = mono_code_manager_new ();
279 domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
280 domain->assemblies = g_hash_table_new (g_str_hash, g_str_equal);
281 domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
282 domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
283 domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
284 domain->jit_code_hash = g_hash_table_new (NULL, NULL);
285 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
286 domain->jit_info_table = mono_jit_info_table_new ();
287 domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
288 domain->jump_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
289 domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
290 domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
292 InitializeCriticalSection (&domain->lock);
294 EnterCriticalSection (&appdomains_mutex);
295 mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
296 LeaveCriticalSection (&appdomains_mutex);
302 * mono_init_internal:
304 * Creates the initial application domain and initializes the mono_defaults
306 * This function is guaranteed to not run any IL code.
307 * If exe_filename is not NULL, the method will determine the required runtime
308 * from the exe configuration file or the version PE field.
309 * If runtime_version is not NULL, that runtime version will be used.
310 * Either exe_filename or runtime_version must be provided.
312 * Returns: the initial domain.
315 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
317 static MonoDomain *domain = NULL;
319 MonoImageOpenStatus status = MONO_IMAGE_OK;
320 MonoAssemblyName corlib_aname;
323 g_assert_not_reached ();
327 appdomain_thread_id = TlsAlloc ();
328 context_thread_id = TlsAlloc ();
330 InitializeCriticalSection (&appdomains_mutex);
332 mono_metadata_init ();
333 mono_raw_buffer_init ();
335 mono_assemblies_init ();
338 /* FIXME: When should we release this memory? */
339 MONO_GC_REGISTER_ROOT (appdomains_list);
340 appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
342 domain = mono_domain_create ();
343 mono_root_domain = domain;
345 SET_APPDOMAIN (domain);
347 if (exe_filename != NULL) {
348 current_runtime = get_runtime_from_exe (exe_filename);
349 } else if (runtime_version != NULL) {
350 current_runtime = get_runtime_by_version (runtime_version);
353 if (current_runtime == NULL) {
354 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
355 current_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
356 g_print ("Using default runtime: %s\n", current_runtime->runtime_version);
359 /* find the corlib */
360 corlib_aname.name = "mscorlib";
361 ass = mono_assembly_load (&corlib_aname, NULL, &status);
362 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
364 case MONO_IMAGE_ERROR_ERRNO: {
365 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", mono_get_framework_version (), "mscorlib.dll", NULL);
366 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
367 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
368 g_free (corlib_file);
371 case MONO_IMAGE_IMAGE_INVALID:
372 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
373 mono_assembly_getrootdir ());
375 case MONO_IMAGE_MISSING_ASSEMBLYREF:
376 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
377 mono_assembly_getrootdir ());
380 /* to suppress compiler warning */
386 mono_defaults.corlib = mono_assembly_get_image (ass);
388 mono_defaults.object_class = mono_class_from_name (
389 mono_defaults.corlib, "System", "Object");
390 g_assert (mono_defaults.object_class != 0);
392 mono_defaults.void_class = mono_class_from_name (
393 mono_defaults.corlib, "System", "Void");
394 g_assert (mono_defaults.void_class != 0);
396 mono_defaults.boolean_class = mono_class_from_name (
397 mono_defaults.corlib, "System", "Boolean");
398 g_assert (mono_defaults.boolean_class != 0);
400 mono_defaults.byte_class = mono_class_from_name (
401 mono_defaults.corlib, "System", "Byte");
402 g_assert (mono_defaults.byte_class != 0);
404 mono_defaults.sbyte_class = mono_class_from_name (
405 mono_defaults.corlib, "System", "SByte");
406 g_assert (mono_defaults.sbyte_class != 0);
408 mono_defaults.int16_class = mono_class_from_name (
409 mono_defaults.corlib, "System", "Int16");
410 g_assert (mono_defaults.int16_class != 0);
412 mono_defaults.uint16_class = mono_class_from_name (
413 mono_defaults.corlib, "System", "UInt16");
414 g_assert (mono_defaults.uint16_class != 0);
416 mono_defaults.int32_class = mono_class_from_name (
417 mono_defaults.corlib, "System", "Int32");
418 g_assert (mono_defaults.int32_class != 0);
420 mono_defaults.uint32_class = mono_class_from_name (
421 mono_defaults.corlib, "System", "UInt32");
422 g_assert (mono_defaults.uint32_class != 0);
424 mono_defaults.uint_class = mono_class_from_name (
425 mono_defaults.corlib, "System", "UIntPtr");
426 g_assert (mono_defaults.uint_class != 0);
428 mono_defaults.int_class = mono_class_from_name (
429 mono_defaults.corlib, "System", "IntPtr");
430 g_assert (mono_defaults.int_class != 0);
432 mono_defaults.int64_class = mono_class_from_name (
433 mono_defaults.corlib, "System", "Int64");
434 g_assert (mono_defaults.int64_class != 0);
436 mono_defaults.uint64_class = mono_class_from_name (
437 mono_defaults.corlib, "System", "UInt64");
438 g_assert (mono_defaults.uint64_class != 0);
440 mono_defaults.single_class = mono_class_from_name (
441 mono_defaults.corlib, "System", "Single");
442 g_assert (mono_defaults.single_class != 0);
444 mono_defaults.double_class = mono_class_from_name (
445 mono_defaults.corlib, "System", "Double");
446 g_assert (mono_defaults.double_class != 0);
448 mono_defaults.char_class = mono_class_from_name (
449 mono_defaults.corlib, "System", "Char");
450 g_assert (mono_defaults.char_class != 0);
452 mono_defaults.string_class = mono_class_from_name (
453 mono_defaults.corlib, "System", "String");
454 g_assert (mono_defaults.string_class != 0);
456 mono_defaults.enum_class = mono_class_from_name (
457 mono_defaults.corlib, "System", "Enum");
458 g_assert (mono_defaults.enum_class != 0);
460 mono_defaults.array_class = mono_class_from_name (
461 mono_defaults.corlib, "System", "Array");
462 g_assert (mono_defaults.array_class != 0);
464 mono_defaults.delegate_class = mono_class_from_name (
465 mono_defaults.corlib, "System", "Delegate");
466 g_assert (mono_defaults.delegate_class != 0 );
468 mono_defaults.multicastdelegate_class = mono_class_from_name (
469 mono_defaults.corlib, "System", "MulticastDelegate");
470 g_assert (mono_defaults.multicastdelegate_class != 0 );
472 mono_defaults.asyncresult_class = mono_class_from_name (
473 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
475 g_assert (mono_defaults.asyncresult_class != 0 );
477 mono_defaults.waithandle_class = mono_class_from_name (
478 mono_defaults.corlib, "System.Threading", "WaitHandle");
479 g_assert (mono_defaults.waithandle_class != 0 );
481 mono_defaults.typehandle_class = mono_class_from_name (
482 mono_defaults.corlib, "System", "RuntimeTypeHandle");
483 g_assert (mono_defaults.typehandle_class != 0);
485 mono_defaults.methodhandle_class = mono_class_from_name (
486 mono_defaults.corlib, "System", "RuntimeMethodHandle");
487 g_assert (mono_defaults.methodhandle_class != 0);
489 mono_defaults.fieldhandle_class = mono_class_from_name (
490 mono_defaults.corlib, "System", "RuntimeFieldHandle");
491 g_assert (mono_defaults.fieldhandle_class != 0);
493 mono_defaults.monotype_class = mono_class_from_name (
494 mono_defaults.corlib, "System", "MonoType");
495 g_assert (mono_defaults.monotype_class != 0);
497 mono_defaults.exception_class = mono_class_from_name (
498 mono_defaults.corlib, "System", "Exception");
499 g_assert (mono_defaults.exception_class != 0);
501 mono_defaults.threadabortexception_class = mono_class_from_name (
502 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
503 g_assert (mono_defaults.threadabortexception_class != 0);
505 mono_defaults.thread_class = mono_class_from_name (
506 mono_defaults.corlib, "System.Threading", "Thread");
507 g_assert (mono_defaults.thread_class != 0);
509 mono_defaults.appdomain_class = mono_class_from_name (
510 mono_defaults.corlib, "System", "AppDomain");
511 g_assert (mono_defaults.appdomain_class != 0);
513 mono_defaults.transparent_proxy_class = mono_class_from_name (
514 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
515 g_assert (mono_defaults.transparent_proxy_class != 0);
517 mono_defaults.real_proxy_class = mono_class_from_name (
518 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
519 g_assert (mono_defaults.real_proxy_class != 0);
521 mono_defaults.mono_method_message_class = mono_class_from_name (
522 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
523 g_assert (mono_defaults.mono_method_message_class != 0);
525 mono_defaults.field_info_class = mono_class_from_name (
526 mono_defaults.corlib, "System.Reflection", "FieldInfo");
527 g_assert (mono_defaults.field_info_class != 0);
529 mono_defaults.method_info_class = mono_class_from_name (
530 mono_defaults.corlib, "System.Reflection", "MethodInfo");
531 g_assert (mono_defaults.method_info_class != 0);
533 mono_defaults.stringbuilder_class = mono_class_from_name (
534 mono_defaults.corlib, "System.Text", "StringBuilder");
535 g_assert (mono_defaults.stringbuilder_class != 0);
537 mono_defaults.math_class = mono_class_from_name (
538 mono_defaults.corlib, "System", "Math");
539 g_assert (mono_defaults.math_class != 0);
541 mono_defaults.stack_frame_class = mono_class_from_name (
542 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
543 g_assert (mono_defaults.stack_frame_class != 0);
545 mono_defaults.stack_trace_class = mono_class_from_name (
546 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
547 g_assert (mono_defaults.stack_trace_class != 0);
549 mono_defaults.marshal_class = mono_class_from_name (
550 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
551 g_assert (mono_defaults.marshal_class != 0);
553 mono_defaults.iserializeable_class = mono_class_from_name (
554 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
555 g_assert (mono_defaults.iserializeable_class != 0);
557 mono_defaults.serializationinfo_class = mono_class_from_name (
558 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
559 g_assert (mono_defaults.serializationinfo_class != 0);
561 mono_defaults.streamingcontext_class = mono_class_from_name (
562 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
563 g_assert (mono_defaults.streamingcontext_class != 0);
565 mono_defaults.typed_reference_class = mono_class_from_name (
566 mono_defaults.corlib, "System", "TypedReference");
567 g_assert (mono_defaults.typed_reference_class != 0);
569 mono_defaults.argumenthandle_class = mono_class_from_name (
570 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
571 g_assert (mono_defaults.argumenthandle_class != 0);
573 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
574 mono_defaults.corlib, "System", "MarshalByRefObject");
575 g_assert (mono_defaults.marshalbyrefobject_class != 0);
577 mono_defaults.monitor_class = mono_class_from_name (
578 mono_defaults.corlib, "System.Threading", "Monitor");
579 g_assert (mono_defaults.monitor_class != 0);
581 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
582 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
583 g_assert (mono_defaults.iremotingtypeinfo_class != 0);
585 domain->friendly_name = g_path_get_basename (filename);
593 * Creates the initial application domain and initializes the mono_defaults
595 * This function is guaranteed to not run any IL code.
596 * The runtime is initialized using the default runtime version.
598 * Returns: the initial domain.
601 mono_init (const char *domain_name)
603 return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
607 * mono_init_from_assembly:
609 * Creates the initial application domain and initializes the mono_defaults
611 * This function is guaranteed to not run any IL code.
612 * The runtime is initialized using the runtime version required by the
613 * provided executable. The version is determined by looking at the exe
614 * configuration file and the version PE field)
616 * Returns: the initial domain.
619 mono_init_from_assembly (const char *domain_name, const char *filename)
621 return mono_init_internal (domain_name, filename, NULL);
627 * Creates the initial application domain and initializes the mono_defaults
629 * This function is guaranteed to not run any IL code.
630 * The runtime is initialized using the provided rutime version.
632 * Returns: the initial domain.
635 mono_init_version (const char *domain_name, const char *version)
637 return mono_init_internal (domain_name, NULL, version);
641 mono_get_root_domain (void)
643 return mono_root_domain;
649 * Returns the current domain.
654 return GET_APPDOMAIN ();
658 * mono_domain_set_internal:
659 * @domain: the new domain
661 * Sets the current domain to @domain.
664 mono_domain_set_internal (MonoDomain *domain)
666 SET_APPDOMAIN (domain);
667 SET_APPCONTEXT (domain->default_context);
676 copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
678 MonoGHashTable *dest = (MonoGHashTable*)user_data;
680 mono_g_hash_table_insert (dest, key, data);
684 foreach_domain (gpointer key, gpointer data, gpointer user_data)
686 DomainInfo *dom_info = user_data;
688 dom_info->func ((MonoDomain*)data, dom_info->user_data);
692 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
695 MonoGHashTable *copy;
698 * Create a copy of the hashtable to avoid calling the user callback
699 * inside the lock because that could lead to deadlocks.
700 * We can do this because this function is not perf. critical.
702 copy = mono_g_hash_table_new (NULL, NULL);
703 EnterCriticalSection (&appdomains_mutex);
704 mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
705 LeaveCriticalSection (&appdomains_mutex);
707 dom_info.func = func;
708 dom_info.user_data = user_data;
709 mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
711 mono_g_hash_table_destroy (copy);
715 * mono_domain_assembly_open:
716 * @domain: the application domain
717 * @name: file name of the assembly
719 * fixme: maybe we should integrate this with mono_assembly_open ??
722 mono_domain_assembly_open (MonoDomain *domain, const char *name)
726 mono_domain_lock (domain);
727 if ((ass = g_hash_table_lookup (domain->assemblies, name))) {
728 mono_domain_unlock (domain);
731 mono_domain_unlock (domain);
733 if (!(ass = mono_assembly_open (name, NULL)))
740 remove_assembly (gpointer key, gpointer value, gpointer user_data)
742 mono_assembly_close ((MonoAssembly *)value);
746 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
748 g_slist_free (value);
752 mono_domain_free (MonoDomain *domain, gboolean force)
754 if ((domain == mono_root_domain) && !force) {
755 g_warning ("cant unload root domain");
759 EnterCriticalSection (&appdomains_mutex);
760 mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
761 LeaveCriticalSection (&appdomains_mutex);
763 g_free (domain->friendly_name);
764 g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
766 mono_g_hash_table_destroy (domain->env);
767 g_hash_table_destroy (domain->assemblies);
768 mono_g_hash_table_destroy (domain->class_vtable_hash);
769 mono_g_hash_table_destroy (domain->proxy_vtable_hash);
770 mono_g_hash_table_destroy (domain->static_data_hash);
771 g_hash_table_destroy (domain->jit_code_hash);
772 mono_g_hash_table_destroy (domain->ldstr_table);
773 mono_jit_info_table_free (domain->jit_info_table);
774 #ifdef DEBUG_DOMAIN_UNLOAD
775 mono_mempool_invalidate (domain->mp);
776 mono_code_manager_invalidate (domain->code_mp);
778 mono_mempool_destroy (domain->mp);
779 mono_code_manager_destroy (domain->code_mp);
781 if (domain->jump_target_hash) {
782 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
783 g_hash_table_destroy (domain->jump_target_hash);
785 mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
786 mono_g_hash_table_destroy (domain->jump_trampoline_hash);
787 g_hash_table_destroy (domain->finalizable_objects_hash);
788 if (domain->special_static_fields)
789 g_hash_table_destroy (domain->special_static_fields);
790 DeleteCriticalSection (&domain->lock);
791 domain->setup = NULL;
793 /* FIXME: anything else required ? */
800 if ((domain == mono_root_domain))
801 mono_root_domain = NULL;
805 * mono_domain_get_id:
807 * Returns the a domain for a specific domain id.
810 mono_domain_get_by_id (gint32 domainid)
814 EnterCriticalSection (&appdomains_mutex);
815 domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
816 LeaveCriticalSection (&appdomains_mutex);
822 mono_domain_get_id (MonoDomain *domain)
824 return domain->domain_id;
828 mono_context_set (MonoAppContext * new_context)
830 SET_APPCONTEXT (new_context);
834 mono_context_get (void)
836 return GET_APPCONTEXT ();
840 mono_get_corlib (void)
842 return mono_defaults.corlib;
846 mono_get_object_class (void)
848 return mono_defaults.object_class;
852 mono_get_byte_class (void)
854 return mono_defaults.byte_class;
858 mono_get_void_class (void)
860 return mono_defaults.void_class;
864 mono_get_boolean_class (void)
866 return mono_defaults.boolean_class;
870 mono_get_sbyte_class (void)
872 return mono_defaults.sbyte_class;
876 mono_get_int16_class (void)
878 return mono_defaults.int16_class;
882 mono_get_uint16_class (void)
884 return mono_defaults.uint16_class;
888 mono_get_int32_class (void)
890 return mono_defaults.int32_class;
894 mono_get_uint32_class (void)
896 return mono_defaults.uint32_class;
900 mono_get_intptr_class (void)
902 return mono_defaults.int_class;
906 mono_get_uintptr_class (void)
908 return mono_defaults.uint_class;
912 mono_get_int64_class (void)
914 return mono_defaults.int64_class;
918 mono_get_uint64_class (void)
920 return mono_defaults.uint64_class;
924 mono_get_single_class (void)
926 return mono_defaults.single_class;
930 mono_get_double_class (void)
932 return mono_defaults.double_class;
936 mono_get_char_class (void)
938 return mono_defaults.char_class;
942 mono_get_string_class (void)
944 return mono_defaults.string_class;
948 mono_get_enum_class (void)
950 return mono_defaults.enum_class;
954 mono_get_array_class (void)
956 return mono_defaults.array_class;
960 mono_get_thread_class (void)
962 return mono_defaults.thread_class;
966 mono_get_exception_class (void)
968 return mono_defaults.exception_class;
972 static char* get_attribute_value (const gchar **attribute_names,
973 const gchar **attribute_values,
974 const char *att_name)
977 for (n=0; attribute_names[n] != NULL; n++) {
978 if (strcmp (attribute_names[n], att_name) == 0)
979 return g_strdup (attribute_values[n]);
984 static void start_element (GMarkupParseContext *context,
985 const gchar *element_name,
986 const gchar **attribute_names,
987 const gchar **attribute_values,
991 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
993 if (strcmp (element_name, "configuration") == 0) {
994 app_config->configuration_count++;
997 if (strcmp (element_name, "startup") == 0) {
998 app_config->startup_count++;
1002 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1005 if (strcmp (element_name, "requiredRuntime") == 0) {
1006 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1007 } else if (strcmp (element_name, "supportedRuntime") == 0) {
1008 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1009 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1013 static void end_element (GMarkupParseContext *context,
1014 const gchar *element_name,
1018 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1020 if (strcmp (element_name, "configuration") == 0) {
1021 app_config->configuration_count--;
1022 } else if (strcmp (element_name, "startup") == 0) {
1023 app_config->startup_count--;
1027 static const GMarkupParser
1036 static AppConfigInfo *
1037 app_config_parse (const char *filename)
1039 AppConfigInfo *app_config;
1040 GMarkupParseContext *context;
1045 if (stat (filename, &buf) != 0)
1048 app_config = g_new0 (AppConfigInfo, 1);
1050 if (!g_file_get_contents (filename, &text, &len, NULL))
1053 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1054 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1055 g_markup_parse_context_end_parse (context, NULL);
1057 g_markup_parse_context_free (context);
1063 app_config_free (AppConfigInfo* app_config)
1066 GSList *list = app_config->supported_runtimes;
1067 while (list != NULL) {
1068 rt = (char*)list->data;
1070 list = g_slist_next (list);
1072 g_slist_free (app_config->supported_runtimes);
1073 g_free (app_config->required_runtime);
1074 g_free (app_config);
1079 get_runtime_by_version (const char *version)
1082 int max = G_N_ELEMENTS (supported_runtimes);
1084 for (n=0; n<max; n++) {
1085 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1086 return &supported_runtimes[n];
1092 get_runtime_from_exe (const char *exe_file)
1094 AppConfigInfo* app_config;
1097 RuntimeInfo* runtime = NULL;
1098 MonoImage *image = NULL;
1100 config_name = g_strconcat (exe_file, ".config", NULL);
1101 app_config = app_config_parse (config_name);
1102 g_free (config_name);
1104 if (app_config != NULL) {
1105 /* Check supportedRuntime elements, if none is supported, fail.
1106 * If there are no such elements, look for a requiredRuntime element.
1108 if (app_config->supported_runtimes != NULL) {
1109 GSList *list = app_config->supported_runtimes;
1110 while (list != NULL && runtime == NULL) {
1111 version = (char*) list->data;
1112 runtime = get_runtime_by_version (version);
1113 list = g_slist_next (list);
1115 app_config_free (app_config);
1119 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1120 if (app_config->required_runtime != NULL) {
1121 runtime = get_runtime_by_version (app_config->required_runtime);
1122 app_config_free (app_config);
1125 app_config_free (app_config);
1128 /* Look for a runtime with the exact version */
1129 image = mono_image_open (exe_file, NULL);
1130 if (image == NULL) {
1131 /* The image is wrong or the file was not found. In this case return
1132 * a default runtime and leave to the initialization method the work of
1133 * reporting the error.
1135 return get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1138 runtime = get_runtime_by_version (image->version);
1144 mono_get_framework_version (void)
1146 return current_runtime->framework_version;
1150 mono_get_runtime_version (void)
1152 return current_runtime->runtime_version;
1156 mono_debugger_check_runtime_version (const char *filename)
1160 rinfo = get_runtime_from_exe (filename);
1162 return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);
1164 if (rinfo != current_runtime)
1165 return g_strdup_printf ("The Mono Debugger is currently using the `%s' runtime, but "
1166 "the assembly `%s' requires version `%s'", current_runtime->runtime_version,
1167 filename, rinfo->runtime_version);