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>
28 /* #define DEBUG_DOMAIN_UNLOAD */
30 /* we need to use both the Tls* functions and __thread because
31 * the gc needs to see all the appcontext
33 static guint32 context_thread_id = -1;
34 static guint32 appdomain_thread_id = -1;
37 static __thread MonoDomain * tls_appdomain;
38 static __thread MonoAppContext * tls_appcontext;
39 #define GET_APPDOMAIN() tls_appdomain
40 #define SET_APPDOMAIN(x) do { \
42 TlsSetValue (appdomain_thread_id, x); \
45 #define GET_APPCONTEXT() tls_appcontext
46 #define SET_APPCONTEXT(x) do { \
48 TlsSetValue (context_thread_id, x); \
52 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
53 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
55 #define GET_APPCONTEXT() ((MonoAppContext *)TlsGetValue (context_thread_id))
56 #define SET_APPCONTEXT(x) TlsSetValue (context_thread_id, x);
59 static gint32 appdomain_id_counter = 0;
61 static MonoGHashTable * appdomains_list = NULL;
63 static CRITICAL_SECTION appdomains_mutex;
65 static MonoDomain *mono_root_domain = NULL;
67 /* RuntimeInfo: Contains information about versions supported by this runtime */
69 const char* runtime_version;
70 const char* framework_version;
73 /* AppConfigInfo: Information about runtime versions supported by an
77 GSList *supported_runtimes;
78 char *required_runtime;
79 int configuration_count;
83 static RuntimeInfo *current_runtime = NULL;
85 /* This is the list of runtime versions supported by this JIT.
87 static RuntimeInfo supported_runtimes[] = {
88 {"v1.0.3705", "1.0"}, {"v1.1.4322", "1.0"}, {"v2.0.40607","2.0"}
91 /* The stable runtime version */
92 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
95 get_runtime_from_exe (const char *exe_file);
98 get_runtime_by_version (const char *version);
100 static MonoJitInfoTable *
101 mono_jit_info_table_new (void)
103 return g_array_new (FALSE, FALSE, sizeof (gpointer));
107 mono_jit_info_table_free (MonoJitInfoTable *table)
109 g_array_free (table, TRUE);
113 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
115 int left = 0, right = table->len;
117 while (left < right) {
118 int pos = (left + right) / 2;
119 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
120 char *start = ji->code_start;
121 char *end = start + ji->code_size;
125 else if (addr >= end)
135 mono_jit_info_table_find (MonoDomain *domain, char *addr)
137 MonoJitInfoTable *table = domain->jit_info_table;
140 mono_domain_lock (domain);
143 while (left < right) {
144 int pos = (left + right) / 2;
145 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
146 char *start = ji->code_start;
147 char *end = start + ji->code_size;
151 else if (addr >= end)
154 mono_domain_unlock (domain);
158 mono_domain_unlock (domain);
160 /* maybe it is shared code, so we also search in the root domain */
161 if (domain != mono_root_domain)
162 return mono_jit_info_table_find (mono_root_domain, addr);
168 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
170 MonoJitInfoTable *table = domain->jit_info_table;
171 gpointer start = ji->code_start;
174 mono_domain_lock (domain);
175 pos = mono_jit_info_table_index (table, start);
177 g_array_insert_val (table, pos, ji);
178 mono_domain_unlock (domain);
182 ldstr_hash (const char* str)
186 len = mono_metadata_decode_blob_size (str, &str) - 1;
188 /* if len == 0 *str will point to the mark byte */
191 * FIXME: The distribution may not be so nice with lots of
192 * null chars in the string.
194 for (str += 1; str < end; str++)
195 h = (h << 5) - h + *str;
200 ldstr_equal (const char *str1, const char *str2) {
204 len = mono_metadata_decode_blob_size (str1, NULL) - 1;
205 len2 = mono_metadata_decode_blob_size (str2, NULL) - 1;
208 return memcmp (str1, str2, len) == 0;
212 mono_string_equal (MonoString *s1, MonoString *s2)
214 int l1 = mono_string_length (s1);
215 int l2 = mono_string_length (s2);
220 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0;
224 mono_string_hash (MonoString *s)
226 const guint16 *p = mono_string_chars (s);
227 int i, len = mono_string_length (s);
230 for (i = 0; i < len; i++) {
231 h = (h << 5) - h + *p;
240 domain_finalizer (void *obj, void *data) {
241 g_print ("domain finalized\n");
246 mono_domain_create (void)
251 domain = GC_MALLOC (sizeof (MonoDomain));
252 GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
254 domain = g_new0 (MonoDomain, 1);
256 domain->domain = NULL;
257 domain->setup = NULL;
258 domain->friendly_name = NULL;
259 domain->search_path = NULL;
261 domain->mp = mono_mempool_new ();
262 domain->code_mp = mono_code_manager_new ();
263 domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
264 domain->assemblies = g_hash_table_new (g_str_hash, g_str_equal);
265 domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
266 domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
267 domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
268 domain->jit_code_hash = g_hash_table_new (NULL, NULL);
269 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
270 domain->jit_info_table = mono_jit_info_table_new ();
271 domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
272 domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
273 domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
275 InitializeCriticalSection (&domain->lock);
277 EnterCriticalSection (&appdomains_mutex);
278 mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
279 LeaveCriticalSection (&appdomains_mutex);
285 * mono_init_internal:
287 * Creates the initial application domain and initializes the mono_defaults
289 * This function is guaranteed to not run any IL code.
290 * If exe_filename is not NULL, the method will determine the required runtime
291 * from the exe configuration file or the version PE field.
292 * If runtime_version is not NULL, that runtime version will be used.
293 * Either exe_filename or runtime_version must be provided.
295 * Returns: the initial domain.
298 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
300 static MonoDomain *domain = NULL;
302 MonoImageOpenStatus status = MONO_IMAGE_OK;
303 MonoAssemblyName corlib_aname;
306 g_assert_not_reached ();
310 appdomain_thread_id = TlsAlloc ();
311 context_thread_id = TlsAlloc ();
313 InitializeCriticalSection (&appdomains_mutex);
315 mono_metadata_init ();
316 mono_raw_buffer_init ();
318 mono_assemblies_init ();
321 /* FIXME: When should we release this memory? */
322 MONO_GC_REGISTER_ROOT (appdomains_list);
323 appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
325 domain = mono_domain_create ();
326 mono_root_domain = domain;
328 SET_APPDOMAIN (domain);
330 if (exe_filename != NULL) {
331 current_runtime = get_runtime_from_exe (exe_filename);
332 } else if (runtime_version != NULL) {
333 current_runtime = get_runtime_by_version (runtime_version);
336 if (current_runtime == NULL) {
337 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
338 current_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
339 g_print ("Using default runtime: %s\n", current_runtime->runtime_version);
342 /* find the corlib */
343 corlib_aname.name = "mscorlib";
344 ass = mono_assembly_load (&corlib_aname, NULL, &status);
345 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
347 case MONO_IMAGE_ERROR_ERRNO: {
348 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", mono_get_framework_version (), "mscorlib.dll", NULL);
349 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
350 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
351 g_free (corlib_file);
354 case MONO_IMAGE_IMAGE_INVALID:
355 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
356 mono_assembly_getrootdir ());
358 case MONO_IMAGE_MISSING_ASSEMBLYREF:
359 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
360 mono_assembly_getrootdir ());
363 /* to suppress compiler warning */
369 mono_defaults.corlib = mono_assembly_get_image (ass);
371 mono_defaults.object_class = mono_class_from_name (
372 mono_defaults.corlib, "System", "Object");
373 g_assert (mono_defaults.object_class != 0);
375 mono_defaults.void_class = mono_class_from_name (
376 mono_defaults.corlib, "System", "Void");
377 g_assert (mono_defaults.void_class != 0);
379 mono_defaults.boolean_class = mono_class_from_name (
380 mono_defaults.corlib, "System", "Boolean");
381 g_assert (mono_defaults.boolean_class != 0);
383 mono_defaults.byte_class = mono_class_from_name (
384 mono_defaults.corlib, "System", "Byte");
385 g_assert (mono_defaults.byte_class != 0);
387 mono_defaults.sbyte_class = mono_class_from_name (
388 mono_defaults.corlib, "System", "SByte");
389 g_assert (mono_defaults.sbyte_class != 0);
391 mono_defaults.int16_class = mono_class_from_name (
392 mono_defaults.corlib, "System", "Int16");
393 g_assert (mono_defaults.int16_class != 0);
395 mono_defaults.uint16_class = mono_class_from_name (
396 mono_defaults.corlib, "System", "UInt16");
397 g_assert (mono_defaults.uint16_class != 0);
399 mono_defaults.int32_class = mono_class_from_name (
400 mono_defaults.corlib, "System", "Int32");
401 g_assert (mono_defaults.int32_class != 0);
403 mono_defaults.uint32_class = mono_class_from_name (
404 mono_defaults.corlib, "System", "UInt32");
405 g_assert (mono_defaults.uint32_class != 0);
407 mono_defaults.uint_class = mono_class_from_name (
408 mono_defaults.corlib, "System", "UIntPtr");
409 g_assert (mono_defaults.uint_class != 0);
411 mono_defaults.int_class = mono_class_from_name (
412 mono_defaults.corlib, "System", "IntPtr");
413 g_assert (mono_defaults.int_class != 0);
415 mono_defaults.int64_class = mono_class_from_name (
416 mono_defaults.corlib, "System", "Int64");
417 g_assert (mono_defaults.int64_class != 0);
419 mono_defaults.uint64_class = mono_class_from_name (
420 mono_defaults.corlib, "System", "UInt64");
421 g_assert (mono_defaults.uint64_class != 0);
423 mono_defaults.single_class = mono_class_from_name (
424 mono_defaults.corlib, "System", "Single");
425 g_assert (mono_defaults.single_class != 0);
427 mono_defaults.double_class = mono_class_from_name (
428 mono_defaults.corlib, "System", "Double");
429 g_assert (mono_defaults.double_class != 0);
431 mono_defaults.char_class = mono_class_from_name (
432 mono_defaults.corlib, "System", "Char");
433 g_assert (mono_defaults.char_class != 0);
435 mono_defaults.string_class = mono_class_from_name (
436 mono_defaults.corlib, "System", "String");
437 g_assert (mono_defaults.string_class != 0);
439 mono_defaults.enum_class = mono_class_from_name (
440 mono_defaults.corlib, "System", "Enum");
441 g_assert (mono_defaults.enum_class != 0);
443 mono_defaults.array_class = mono_class_from_name (
444 mono_defaults.corlib, "System", "Array");
445 g_assert (mono_defaults.array_class != 0);
447 mono_defaults.delegate_class = mono_class_from_name (
448 mono_defaults.corlib, "System", "Delegate");
449 g_assert (mono_defaults.delegate_class != 0 );
451 mono_defaults.multicastdelegate_class = mono_class_from_name (
452 mono_defaults.corlib, "System", "MulticastDelegate");
453 g_assert (mono_defaults.multicastdelegate_class != 0 );
455 mono_defaults.asyncresult_class = mono_class_from_name (
456 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
458 g_assert (mono_defaults.asyncresult_class != 0 );
460 mono_defaults.waithandle_class = mono_class_from_name (
461 mono_defaults.corlib, "System.Threading", "WaitHandle");
462 g_assert (mono_defaults.waithandle_class != 0 );
464 mono_defaults.typehandle_class = mono_class_from_name (
465 mono_defaults.corlib, "System", "RuntimeTypeHandle");
466 g_assert (mono_defaults.typehandle_class != 0);
468 mono_defaults.methodhandle_class = mono_class_from_name (
469 mono_defaults.corlib, "System", "RuntimeMethodHandle");
470 g_assert (mono_defaults.methodhandle_class != 0);
472 mono_defaults.fieldhandle_class = mono_class_from_name (
473 mono_defaults.corlib, "System", "RuntimeFieldHandle");
474 g_assert (mono_defaults.fieldhandle_class != 0);
476 mono_defaults.monotype_class = mono_class_from_name (
477 mono_defaults.corlib, "System", "MonoType");
478 g_assert (mono_defaults.monotype_class != 0);
480 mono_defaults.exception_class = mono_class_from_name (
481 mono_defaults.corlib, "System", "Exception");
482 g_assert (mono_defaults.exception_class != 0);
484 mono_defaults.threadabortexception_class = mono_class_from_name (
485 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
486 g_assert (mono_defaults.threadabortexception_class != 0);
488 mono_defaults.thread_class = mono_class_from_name (
489 mono_defaults.corlib, "System.Threading", "Thread");
490 g_assert (mono_defaults.thread_class != 0);
492 mono_defaults.appdomain_class = mono_class_from_name (
493 mono_defaults.corlib, "System", "AppDomain");
494 g_assert (mono_defaults.appdomain_class != 0);
496 mono_defaults.transparent_proxy_class = mono_class_from_name (
497 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
498 g_assert (mono_defaults.transparent_proxy_class != 0);
500 mono_defaults.real_proxy_class = mono_class_from_name (
501 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
502 g_assert (mono_defaults.real_proxy_class != 0);
504 mono_defaults.mono_method_message_class = mono_class_from_name (
505 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
506 g_assert (mono_defaults.mono_method_message_class != 0);
508 mono_defaults.field_info_class = mono_class_from_name (
509 mono_defaults.corlib, "System.Reflection", "FieldInfo");
510 g_assert (mono_defaults.field_info_class != 0);
512 mono_defaults.method_info_class = mono_class_from_name (
513 mono_defaults.corlib, "System.Reflection", "MethodInfo");
514 g_assert (mono_defaults.method_info_class != 0);
516 mono_defaults.stringbuilder_class = mono_class_from_name (
517 mono_defaults.corlib, "System.Text", "StringBuilder");
518 g_assert (mono_defaults.stringbuilder_class != 0);
520 mono_defaults.math_class = mono_class_from_name (
521 mono_defaults.corlib, "System", "Math");
522 g_assert (mono_defaults.math_class != 0);
524 mono_defaults.stack_frame_class = mono_class_from_name (
525 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
526 g_assert (mono_defaults.stack_frame_class != 0);
528 mono_defaults.stack_trace_class = mono_class_from_name (
529 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
530 g_assert (mono_defaults.stack_trace_class != 0);
532 mono_defaults.marshal_class = mono_class_from_name (
533 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
534 g_assert (mono_defaults.marshal_class != 0);
536 mono_defaults.iserializeable_class = mono_class_from_name (
537 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
538 g_assert (mono_defaults.iserializeable_class != 0);
540 mono_defaults.serializationinfo_class = mono_class_from_name (
541 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
542 g_assert (mono_defaults.serializationinfo_class != 0);
544 mono_defaults.streamingcontext_class = mono_class_from_name (
545 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
546 g_assert (mono_defaults.streamingcontext_class != 0);
548 mono_defaults.typed_reference_class = mono_class_from_name (
549 mono_defaults.corlib, "System", "TypedReference");
550 g_assert (mono_defaults.typed_reference_class != 0);
552 mono_defaults.argumenthandle_class = mono_class_from_name (
553 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
554 g_assert (mono_defaults.argumenthandle_class != 0);
556 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
557 mono_defaults.corlib, "System", "MarshalByRefObject");
558 g_assert (mono_defaults.marshalbyrefobject_class != 0);
560 mono_defaults.monitor_class = mono_class_from_name (
561 mono_defaults.corlib, "System.Threading", "Monitor");
562 g_assert (mono_defaults.monitor_class != 0);
564 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
565 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
566 g_assert (mono_defaults.iremotingtypeinfo_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, 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 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
731 g_slist_free (value);
735 mono_domain_free (MonoDomain *domain, gboolean force)
737 if ((domain == mono_root_domain) && !force) {
738 g_warning ("cant unload root domain");
742 EnterCriticalSection (&appdomains_mutex);
743 mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
744 LeaveCriticalSection (&appdomains_mutex);
746 g_free (domain->friendly_name);
747 g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
749 mono_g_hash_table_destroy (domain->env);
750 g_hash_table_destroy (domain->assemblies);
751 mono_g_hash_table_destroy (domain->class_vtable_hash);
752 mono_g_hash_table_destroy (domain->proxy_vtable_hash);
753 mono_g_hash_table_destroy (domain->static_data_hash);
754 g_hash_table_destroy (domain->jit_code_hash);
755 mono_g_hash_table_destroy (domain->ldstr_table);
756 mono_jit_info_table_free (domain->jit_info_table);
757 #ifdef DEBUG_DOMAIN_UNLOAD
758 mono_mempool_invalidate (domain->mp);
759 mono_code_manager_invalidate (domain->code_mp);
761 mono_mempool_destroy (domain->mp);
762 mono_code_manager_destroy (domain->code_mp);
764 if (domain->jump_target_hash) {
765 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
766 g_hash_table_destroy (domain->jump_target_hash);
768 mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
769 g_hash_table_destroy (domain->finalizable_objects_hash);
770 if (domain->special_static_fields)
771 g_hash_table_destroy (domain->special_static_fields);
772 DeleteCriticalSection (&domain->lock);
773 domain->setup = NULL;
775 /* FIXME: anything else required ? */
782 if ((domain == mono_root_domain))
783 mono_root_domain = NULL;
787 * mono_domain_get_id:
789 * Returns the a domain for a specific domain id.
792 mono_domain_get_by_id (gint32 domainid)
796 EnterCriticalSection (&appdomains_mutex);
797 domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
798 LeaveCriticalSection (&appdomains_mutex);
804 mono_domain_get_id (MonoDomain *domain)
806 return domain->domain_id;
810 mono_context_set (MonoAppContext * new_context)
812 SET_APPCONTEXT (new_context);
816 mono_context_get (void)
818 return GET_APPCONTEXT ();
822 mono_get_corlib (void)
824 return mono_defaults.corlib;
828 mono_get_object_class (void)
830 return mono_defaults.object_class;
834 mono_get_byte_class (void)
836 return mono_defaults.byte_class;
840 mono_get_void_class (void)
842 return mono_defaults.void_class;
846 mono_get_boolean_class (void)
848 return mono_defaults.boolean_class;
852 mono_get_sbyte_class (void)
854 return mono_defaults.sbyte_class;
858 mono_get_int16_class (void)
860 return mono_defaults.int16_class;
864 mono_get_uint16_class (void)
866 return mono_defaults.uint16_class;
870 mono_get_int32_class (void)
872 return mono_defaults.int32_class;
876 mono_get_uint32_class (void)
878 return mono_defaults.uint32_class;
882 mono_get_intptr_class (void)
884 return mono_defaults.int_class;
888 mono_get_uintptr_class (void)
890 return mono_defaults.uint_class;
894 mono_get_int64_class (void)
896 return mono_defaults.int64_class;
900 mono_get_uint64_class (void)
902 return mono_defaults.uint64_class;
906 mono_get_single_class (void)
908 return mono_defaults.single_class;
912 mono_get_double_class (void)
914 return mono_defaults.double_class;
918 mono_get_char_class (void)
920 return mono_defaults.char_class;
924 mono_get_string_class (void)
926 return mono_defaults.string_class;
930 mono_get_enum_class (void)
932 return mono_defaults.enum_class;
936 mono_get_array_class (void)
938 return mono_defaults.array_class;
942 mono_get_thread_class (void)
944 return mono_defaults.thread_class;
948 mono_get_exception_class (void)
950 return mono_defaults.exception_class;
954 static char* get_attribute_value (const gchar **attribute_names,
955 const gchar **attribute_values,
956 const char *att_name)
959 for (n=0; attribute_names[n] != NULL; n++) {
960 if (strcmp (attribute_names[n], att_name) == 0)
961 return g_strdup (attribute_values[n]);
966 static void start_element (GMarkupParseContext *context,
967 const gchar *element_name,
968 const gchar **attribute_names,
969 const gchar **attribute_values,
973 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
975 if (strcmp (element_name, "configuration") == 0) {
976 app_config->configuration_count++;
979 if (strcmp (element_name, "startup") == 0) {
980 app_config->startup_count++;
984 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
987 if (strcmp (element_name, "requiredRuntime") == 0) {
988 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
989 } else if (strcmp (element_name, "supportedRuntime") == 0) {
990 char *version = get_attribute_value (attribute_names, attribute_values, "version");
991 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
995 static void end_element (GMarkupParseContext *context,
996 const gchar *element_name,
1000 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1002 if (strcmp (element_name, "configuration") == 0) {
1003 app_config->configuration_count--;
1004 } else if (strcmp (element_name, "startup") == 0) {
1005 app_config->startup_count--;
1009 static const GMarkupParser
1018 static AppConfigInfo *
1019 app_config_parse (const char *filename)
1021 AppConfigInfo *app_config;
1022 GMarkupParseContext *context;
1027 if (stat (filename, &buf) != 0)
1030 app_config = g_new0 (AppConfigInfo, 1);
1032 if (!g_file_get_contents (filename, &text, &len, NULL))
1035 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1036 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1037 g_markup_parse_context_end_parse (context, NULL);
1039 g_markup_parse_context_free (context);
1045 app_config_free (AppConfigInfo* app_config)
1048 GSList *list = app_config->supported_runtimes;
1049 while (list != NULL) {
1050 rt = (char*)list->data;
1052 list = g_slist_next (list);
1054 g_slist_free (app_config->supported_runtimes);
1055 g_free (app_config->required_runtime);
1056 g_free (app_config);
1061 get_runtime_by_version (const char *version)
1064 int max = G_N_ELEMENTS (supported_runtimes);
1066 for (n=0; n<max; n++) {
1067 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1068 return &supported_runtimes[n];
1074 get_runtime_from_exe (const char *exe_file)
1076 AppConfigInfo* app_config;
1079 RuntimeInfo* runtime = NULL;
1080 MonoImage *image = NULL;
1082 config_name = g_strconcat (exe_file, ".config", NULL);
1083 app_config = app_config_parse (config_name);
1084 g_free (config_name);
1086 if (app_config != NULL) {
1087 /* Check supportedRuntime elements, if none is supported, fail.
1088 * If there are no such elements, look for a requiredRuntime element.
1090 if (app_config->supported_runtimes != NULL) {
1091 GSList *list = app_config->supported_runtimes;
1092 while (list != NULL && runtime == NULL) {
1093 version = (char*) list->data;
1094 runtime = get_runtime_by_version (version);
1095 list = g_slist_next (list);
1097 app_config_free (app_config);
1101 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1102 if (app_config->required_runtime != NULL) {
1103 runtime = get_runtime_by_version (app_config->required_runtime);
1104 app_config_free (app_config);
1107 app_config_free (app_config);
1110 /* Look for a runtime with the exact version */
1111 image = mono_image_open (exe_file, NULL);
1112 if (image == NULL) {
1113 /* The image is wrong or the file was not found. In this case return
1114 * a default runtime and leave to the initialization method the work of
1115 * reporting the error.
1117 return get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1120 runtime = get_runtime_by_version (image->version);
1126 mono_get_framework_version (void)
1128 return current_runtime->framework_version;
1132 mono_get_runtime_version (void)
1134 return current_runtime->runtime_version;