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 static guint32 appdomain_thread_id = -1;
31 static guint32 context_thread_id = -1;
33 static gint32 appdomain_id_counter = 0;
35 static MonoGHashTable * appdomains_list = NULL;
37 static CRITICAL_SECTION appdomains_mutex;
39 static MonoDomain *mono_root_domain = NULL;
41 /* RuntimeInfo: Contains information about versions supported by this runtime */
43 const char* runtime_version;
44 const char* framework_version;
47 /* AppConfigInfo: Information about runtime versions supported by an
51 GSList *supported_runtimes;
52 char *required_runtime;
53 int configuration_count;
57 static RuntimeInfo *current_runtime = NULL;
59 /* This is the list of runtime versions supported by this JIT.
61 static RuntimeInfo supported_runtimes[] = {
62 {"v1.0.3705", "1.0"}, {"v1.1.4322", "1.0"}, {"v2.0.40607","2.0"}
65 /* The stable runtime version */
66 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
69 get_runtime_from_exe (const char *exe_file);
72 get_runtime_by_version (const char *version);
74 static MonoJitInfoTable *
75 mono_jit_info_table_new (void)
77 return g_array_new (FALSE, FALSE, sizeof (gpointer));
81 mono_jit_info_table_free (MonoJitInfoTable *table)
83 g_array_free (table, TRUE);
87 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
89 int left = 0, right = table->len;
91 while (left < right) {
92 int pos = (left + right) / 2;
93 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
94 char *start = ji->code_start;
95 char *end = start + ji->code_size;
109 mono_jit_info_table_find (MonoDomain *domain, char *addr)
111 MonoJitInfoTable *table = domain->jit_info_table;
114 mono_domain_lock (domain);
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)
128 mono_domain_unlock (domain);
132 mono_domain_unlock (domain);
134 /* maybe it is shared code, so we also search in the root domain */
135 if (domain != mono_root_domain)
136 return mono_jit_info_table_find (mono_root_domain, addr);
142 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
144 MonoJitInfoTable *table = domain->jit_info_table;
145 gpointer start = ji->code_start;
148 mono_domain_lock (domain);
149 pos = mono_jit_info_table_index (table, start);
151 g_array_insert_val (table, pos, ji);
152 mono_domain_unlock (domain);
156 ldstr_hash (const char* str)
160 len = mono_metadata_decode_blob_size (str, &str) - 1;
162 /* if len == 0 *str will point to the mark byte */
165 * FIXME: The distribution may not be so nice with lots of
166 * null chars in the string.
168 for (str += 1; str < end; str++)
169 h = (h << 5) - h + *str;
174 ldstr_equal (const char *str1, const char *str2) {
178 len = mono_metadata_decode_blob_size (str1, NULL) - 1;
179 len2 = mono_metadata_decode_blob_size (str2, NULL) - 1;
182 return memcmp (str1, str2, len) == 0;
186 mono_string_equal (MonoString *s1, MonoString *s2)
188 int l1 = mono_string_length (s1);
189 int l2 = mono_string_length (s2);
194 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0;
198 mono_string_hash (MonoString *s)
200 const guint16 *p = mono_string_chars (s);
201 int i, len = mono_string_length (s);
204 for (i = 0; i < len; i++) {
205 h = (h << 5) - h + *p;
214 domain_finalizer (void *obj, void *data) {
215 g_print ("domain finalized\n");
220 mono_domain_create (void)
225 domain = GC_MALLOC (sizeof (MonoDomain));
226 GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
228 domain = g_new0 (MonoDomain, 1);
230 domain->domain = NULL;
231 domain->setup = NULL;
232 domain->friendly_name = NULL;
233 domain->search_path = NULL;
235 domain->mp = mono_mempool_new ();
236 domain->code_mp = mono_code_manager_new ();
237 domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
238 domain->assemblies = g_hash_table_new (g_str_hash, g_str_equal);
239 domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
240 domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
241 domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
242 domain->jit_code_hash = g_hash_table_new (NULL, NULL);
243 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
244 domain->jit_info_table = mono_jit_info_table_new ();
245 domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
246 domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
247 domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
249 InitializeCriticalSection (&domain->lock);
251 EnterCriticalSection (&appdomains_mutex);
252 mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
253 LeaveCriticalSection (&appdomains_mutex);
259 * mono_init_internal:
261 * Creates the initial application domain and initializes the mono_defaults
263 * This function is guaranteed to not run any IL code.
264 * If exe_filename is not NULL, the method will determine the required runtime
265 * from the exe configuration file or the version PE field.
266 * If runtime_version is not NULL, that runtime version will be used.
267 * Either exe_filename or runtime_version must be provided.
269 * Returns: the initial domain.
272 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
274 static MonoDomain *domain = NULL;
276 MonoImageOpenStatus status = MONO_IMAGE_OK;
277 MonoAssemblyName corlib_aname;
280 g_assert_not_reached ();
282 appdomain_thread_id = TlsAlloc ();
283 context_thread_id = TlsAlloc ();
285 InitializeCriticalSection (&appdomains_mutex);
287 mono_metadata_init ();
288 mono_raw_buffer_init ();
290 mono_assemblies_init ();
293 /* FIXME: When should we release this memory? */
294 MONO_GC_REGISTER_ROOT (appdomains_list);
295 appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
297 domain = mono_domain_create ();
298 mono_root_domain = domain;
300 TlsSetValue (appdomain_thread_id, domain);
302 if (exe_filename != NULL) {
303 current_runtime = get_runtime_from_exe (exe_filename);
304 } else if (runtime_version != NULL) {
305 current_runtime = get_runtime_by_version (runtime_version);
308 if (current_runtime == NULL) {
309 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
310 current_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
311 g_print ("Using default runtime: %s\n", current_runtime->runtime_version);
314 /* find the corlib */
315 corlib_aname.name = "mscorlib";
316 ass = mono_assembly_load (&corlib_aname, NULL, &status);
317 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
319 case MONO_IMAGE_ERROR_ERRNO: {
320 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", mono_get_framework_version (), "mscorlib.dll", NULL);
321 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
322 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
323 g_free (corlib_file);
326 case MONO_IMAGE_IMAGE_INVALID:
327 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
328 mono_assembly_getrootdir ());
330 case MONO_IMAGE_MISSING_ASSEMBLYREF:
331 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
332 mono_assembly_getrootdir ());
335 /* to suppress compiler warning */
341 mono_defaults.corlib = mono_assembly_get_image (ass);
343 mono_defaults.object_class = mono_class_from_name (
344 mono_defaults.corlib, "System", "Object");
345 g_assert (mono_defaults.object_class != 0);
347 mono_defaults.void_class = mono_class_from_name (
348 mono_defaults.corlib, "System", "Void");
349 g_assert (mono_defaults.void_class != 0);
351 mono_defaults.boolean_class = mono_class_from_name (
352 mono_defaults.corlib, "System", "Boolean");
353 g_assert (mono_defaults.boolean_class != 0);
355 mono_defaults.byte_class = mono_class_from_name (
356 mono_defaults.corlib, "System", "Byte");
357 g_assert (mono_defaults.byte_class != 0);
359 mono_defaults.sbyte_class = mono_class_from_name (
360 mono_defaults.corlib, "System", "SByte");
361 g_assert (mono_defaults.sbyte_class != 0);
363 mono_defaults.int16_class = mono_class_from_name (
364 mono_defaults.corlib, "System", "Int16");
365 g_assert (mono_defaults.int16_class != 0);
367 mono_defaults.uint16_class = mono_class_from_name (
368 mono_defaults.corlib, "System", "UInt16");
369 g_assert (mono_defaults.uint16_class != 0);
371 mono_defaults.int32_class = mono_class_from_name (
372 mono_defaults.corlib, "System", "Int32");
373 g_assert (mono_defaults.int32_class != 0);
375 mono_defaults.uint32_class = mono_class_from_name (
376 mono_defaults.corlib, "System", "UInt32");
377 g_assert (mono_defaults.uint32_class != 0);
379 mono_defaults.uint_class = mono_class_from_name (
380 mono_defaults.corlib, "System", "UIntPtr");
381 g_assert (mono_defaults.uint_class != 0);
383 mono_defaults.int_class = mono_class_from_name (
384 mono_defaults.corlib, "System", "IntPtr");
385 g_assert (mono_defaults.int_class != 0);
387 mono_defaults.int64_class = mono_class_from_name (
388 mono_defaults.corlib, "System", "Int64");
389 g_assert (mono_defaults.int64_class != 0);
391 mono_defaults.uint64_class = mono_class_from_name (
392 mono_defaults.corlib, "System", "UInt64");
393 g_assert (mono_defaults.uint64_class != 0);
395 mono_defaults.single_class = mono_class_from_name (
396 mono_defaults.corlib, "System", "Single");
397 g_assert (mono_defaults.single_class != 0);
399 mono_defaults.double_class = mono_class_from_name (
400 mono_defaults.corlib, "System", "Double");
401 g_assert (mono_defaults.double_class != 0);
403 mono_defaults.char_class = mono_class_from_name (
404 mono_defaults.corlib, "System", "Char");
405 g_assert (mono_defaults.char_class != 0);
407 mono_defaults.string_class = mono_class_from_name (
408 mono_defaults.corlib, "System", "String");
409 g_assert (mono_defaults.string_class != 0);
411 mono_defaults.enum_class = mono_class_from_name (
412 mono_defaults.corlib, "System", "Enum");
413 g_assert (mono_defaults.enum_class != 0);
415 mono_defaults.array_class = mono_class_from_name (
416 mono_defaults.corlib, "System", "Array");
417 g_assert (mono_defaults.array_class != 0);
419 mono_defaults.delegate_class = mono_class_from_name (
420 mono_defaults.corlib, "System", "Delegate");
421 g_assert (mono_defaults.delegate_class != 0 );
423 mono_defaults.multicastdelegate_class = mono_class_from_name (
424 mono_defaults.corlib, "System", "MulticastDelegate");
425 g_assert (mono_defaults.multicastdelegate_class != 0 );
427 mono_defaults.asyncresult_class = mono_class_from_name (
428 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
430 g_assert (mono_defaults.asyncresult_class != 0 );
432 mono_defaults.waithandle_class = mono_class_from_name (
433 mono_defaults.corlib, "System.Threading", "WaitHandle");
434 g_assert (mono_defaults.waithandle_class != 0 );
436 mono_defaults.typehandle_class = mono_class_from_name (
437 mono_defaults.corlib, "System", "RuntimeTypeHandle");
438 g_assert (mono_defaults.typehandle_class != 0);
440 mono_defaults.methodhandle_class = mono_class_from_name (
441 mono_defaults.corlib, "System", "RuntimeMethodHandle");
442 g_assert (mono_defaults.methodhandle_class != 0);
444 mono_defaults.fieldhandle_class = mono_class_from_name (
445 mono_defaults.corlib, "System", "RuntimeFieldHandle");
446 g_assert (mono_defaults.fieldhandle_class != 0);
448 mono_defaults.monotype_class = mono_class_from_name (
449 mono_defaults.corlib, "System", "MonoType");
450 g_assert (mono_defaults.monotype_class != 0);
452 mono_defaults.exception_class = mono_class_from_name (
453 mono_defaults.corlib, "System", "Exception");
454 g_assert (mono_defaults.exception_class != 0);
456 mono_defaults.threadabortexception_class = mono_class_from_name (
457 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
458 g_assert (mono_defaults.threadabortexception_class != 0);
460 mono_defaults.thread_class = mono_class_from_name (
461 mono_defaults.corlib, "System.Threading", "Thread");
462 g_assert (mono_defaults.thread_class != 0);
464 mono_defaults.appdomain_class = mono_class_from_name (
465 mono_defaults.corlib, "System", "AppDomain");
466 g_assert (mono_defaults.appdomain_class != 0);
468 mono_defaults.transparent_proxy_class = mono_class_from_name (
469 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
470 g_assert (mono_defaults.transparent_proxy_class != 0);
472 mono_defaults.real_proxy_class = mono_class_from_name (
473 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
474 g_assert (mono_defaults.real_proxy_class != 0);
476 mono_defaults.mono_method_message_class = mono_class_from_name (
477 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
478 g_assert (mono_defaults.mono_method_message_class != 0);
480 mono_defaults.field_info_class = mono_class_from_name (
481 mono_defaults.corlib, "System.Reflection", "FieldInfo");
482 g_assert (mono_defaults.field_info_class != 0);
484 mono_defaults.method_info_class = mono_class_from_name (
485 mono_defaults.corlib, "System.Reflection", "MethodInfo");
486 g_assert (mono_defaults.method_info_class != 0);
488 mono_defaults.stringbuilder_class = mono_class_from_name (
489 mono_defaults.corlib, "System.Text", "StringBuilder");
490 g_assert (mono_defaults.stringbuilder_class != 0);
492 mono_defaults.math_class = mono_class_from_name (
493 mono_defaults.corlib, "System", "Math");
494 g_assert (mono_defaults.math_class != 0);
496 mono_defaults.stack_frame_class = mono_class_from_name (
497 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
498 g_assert (mono_defaults.stack_frame_class != 0);
500 mono_defaults.stack_trace_class = mono_class_from_name (
501 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
502 g_assert (mono_defaults.stack_trace_class != 0);
504 mono_defaults.marshal_class = mono_class_from_name (
505 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
506 g_assert (mono_defaults.marshal_class != 0);
508 mono_defaults.iserializeable_class = mono_class_from_name (
509 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
510 g_assert (mono_defaults.iserializeable_class != 0);
512 mono_defaults.serializationinfo_class = mono_class_from_name (
513 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
514 g_assert (mono_defaults.serializationinfo_class != 0);
516 mono_defaults.streamingcontext_class = mono_class_from_name (
517 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
518 g_assert (mono_defaults.streamingcontext_class != 0);
520 mono_defaults.typed_reference_class = mono_class_from_name (
521 mono_defaults.corlib, "System", "TypedReference");
522 g_assert (mono_defaults.typed_reference_class != 0);
524 mono_defaults.argumenthandle_class = mono_class_from_name (
525 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
526 g_assert (mono_defaults.argumenthandle_class != 0);
528 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
529 mono_defaults.corlib, "System", "MarshalByRefObject");
530 g_assert (mono_defaults.marshalbyrefobject_class != 0);
532 mono_defaults.monitor_class = mono_class_from_name (
533 mono_defaults.corlib, "System.Threading", "Monitor");
534 g_assert (mono_defaults.monitor_class != 0);
536 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
537 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
538 g_assert (mono_defaults.iremotingtypeinfo_class != 0);
540 domain->friendly_name = g_path_get_basename (filename);
548 * Creates the initial application domain and initializes the mono_defaults
550 * This function is guaranteed to not run any IL code.
551 * The runtime is initialized using the default runtime version.
553 * Returns: the initial domain.
556 mono_init (const char *domain_name)
558 return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
562 * mono_init_from_assembly:
564 * Creates the initial application domain and initializes the mono_defaults
566 * This function is guaranteed to not run any IL code.
567 * The runtime is initialized using the runtime version required by the
568 * provided executable. The version is determined by looking at the exe
569 * configuration file and the version PE field)
571 * Returns: the initial domain.
574 mono_init_from_assembly (const char *domain_name, const char *filename)
576 return mono_init_internal (domain_name, filename, NULL);
582 * Creates the initial application domain and initializes the mono_defaults
584 * This function is guaranteed to not run any IL code.
585 * The runtime is initialized using the provided rutime version.
587 * Returns: the initial domain.
590 mono_init_version (const char *domain_name, const char *version)
592 return mono_init_internal (domain_name, NULL, version);
596 mono_get_root_domain (void)
598 return mono_root_domain;
604 * Returns the current domain.
609 return ((MonoDomain *)TlsGetValue (appdomain_thread_id));
613 * mono_domain_set_internal:
614 * @domain: the new domain
616 * Sets the current domain to @domain.
619 mono_domain_set_internal (MonoDomain *domain)
621 TlsSetValue (appdomain_thread_id, domain);
622 TlsSetValue (context_thread_id, domain->default_context);
631 copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
633 MonoGHashTable *dest = (MonoGHashTable*)user_data;
635 mono_g_hash_table_insert (dest, key, data);
639 foreach_domain (gpointer key, gpointer data, gpointer user_data)
641 DomainInfo *dom_info = user_data;
643 dom_info->func ((MonoDomain*)data, dom_info->user_data);
647 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
650 MonoGHashTable *copy;
653 * Create a copy of the hashtable to avoid calling the user callback
654 * inside the lock because that could lead to deadlocks.
655 * We can do this because this function is not perf. critical.
657 copy = mono_g_hash_table_new (NULL, NULL);
658 EnterCriticalSection (&appdomains_mutex);
659 mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
660 LeaveCriticalSection (&appdomains_mutex);
662 dom_info.func = func;
663 dom_info.user_data = user_data;
664 mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
666 mono_g_hash_table_destroy (copy);
670 * mono_domain_assembly_open:
671 * @domain: the application domain
672 * @name: file name of the assembly
674 * fixme: maybe we should integrate this with mono_assembly_open ??
677 mono_domain_assembly_open (MonoDomain *domain, const char *name)
681 mono_domain_lock (domain);
682 if ((ass = g_hash_table_lookup (domain->assemblies, name))) {
683 mono_domain_unlock (domain);
686 mono_domain_unlock (domain);
688 if (!(ass = mono_assembly_open (name, NULL)))
695 remove_assembly (gpointer key, gpointer value, gpointer user_data)
697 mono_assembly_close ((MonoAssembly *)value);
701 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
703 g_slist_free (value);
707 mono_domain_free (MonoDomain *domain, gboolean force)
709 if ((domain == mono_root_domain) && !force) {
710 g_warning ("cant unload root domain");
714 EnterCriticalSection (&appdomains_mutex);
715 mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
716 LeaveCriticalSection (&appdomains_mutex);
718 g_free (domain->friendly_name);
719 g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
721 mono_g_hash_table_destroy (domain->env);
722 g_hash_table_destroy (domain->assemblies);
723 mono_g_hash_table_destroy (domain->class_vtable_hash);
724 mono_g_hash_table_destroy (domain->proxy_vtable_hash);
725 mono_g_hash_table_destroy (domain->static_data_hash);
726 g_hash_table_destroy (domain->jit_code_hash);
727 mono_g_hash_table_destroy (domain->ldstr_table);
728 mono_jit_info_table_free (domain->jit_info_table);
729 #ifdef DEBUG_DOMAIN_UNLOAD
730 mono_mempool_invalidate (domain->mp);
731 mono_code_manager_invalidate (domain->code_mp);
733 mono_mempool_destroy (domain->mp);
734 mono_code_manager_destroy (domain->code_mp);
736 if (domain->jump_target_hash) {
737 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
738 g_hash_table_destroy (domain->jump_target_hash);
740 mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
741 g_hash_table_destroy (domain->finalizable_objects_hash);
742 if (domain->special_static_fields)
743 g_hash_table_destroy (domain->special_static_fields);
744 DeleteCriticalSection (&domain->lock);
745 domain->setup = NULL;
747 /* FIXME: anything else required ? */
754 if ((domain == mono_root_domain))
755 mono_root_domain = NULL;
759 * mono_domain_get_id:
761 * Returns the a domain for a specific domain id.
764 mono_domain_get_by_id (gint32 domainid)
768 EnterCriticalSection (&appdomains_mutex);
769 domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
770 LeaveCriticalSection (&appdomains_mutex);
776 mono_domain_get_id (MonoDomain *domain)
778 return domain->domain_id;
782 mono_context_set (MonoAppContext * new_context)
784 TlsSetValue (context_thread_id, new_context);
788 mono_context_get (void)
790 return ((MonoAppContext *)TlsGetValue (context_thread_id));
794 mono_get_corlib (void)
796 return mono_defaults.corlib;
800 mono_get_object_class (void)
802 return mono_defaults.object_class;
806 mono_get_byte_class (void)
808 return mono_defaults.byte_class;
812 mono_get_void_class (void)
814 return mono_defaults.void_class;
818 mono_get_boolean_class (void)
820 return mono_defaults.boolean_class;
824 mono_get_sbyte_class (void)
826 return mono_defaults.sbyte_class;
830 mono_get_int16_class (void)
832 return mono_defaults.int16_class;
836 mono_get_uint16_class (void)
838 return mono_defaults.uint16_class;
842 mono_get_int32_class (void)
844 return mono_defaults.int32_class;
848 mono_get_uint32_class (void)
850 return mono_defaults.uint32_class;
854 mono_get_intptr_class (void)
856 return mono_defaults.int_class;
860 mono_get_uintptr_class (void)
862 return mono_defaults.uint_class;
866 mono_get_int64_class (void)
868 return mono_defaults.int64_class;
872 mono_get_uint64_class (void)
874 return mono_defaults.uint64_class;
878 mono_get_single_class (void)
880 return mono_defaults.single_class;
884 mono_get_double_class (void)
886 return mono_defaults.double_class;
890 mono_get_char_class (void)
892 return mono_defaults.char_class;
896 mono_get_string_class (void)
898 return mono_defaults.string_class;
902 mono_get_enum_class (void)
904 return mono_defaults.enum_class;
908 mono_get_array_class (void)
910 return mono_defaults.array_class;
914 mono_get_thread_class (void)
916 return mono_defaults.thread_class;
920 mono_get_exception_class (void)
922 return mono_defaults.exception_class;
926 static char* get_attribute_value (const gchar **attribute_names,
927 const gchar **attribute_values,
928 const char *att_name)
931 for (n=0; attribute_names[n] != NULL; n++) {
932 if (strcmp (attribute_names[n], att_name) == 0)
933 return g_strdup (attribute_values[n]);
938 static void start_element (GMarkupParseContext *context,
939 const gchar *element_name,
940 const gchar **attribute_names,
941 const gchar **attribute_values,
945 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
947 if (strcmp (element_name, "configuration") == 0) {
948 app_config->configuration_count++;
951 if (strcmp (element_name, "startup") == 0) {
952 app_config->startup_count++;
956 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
959 if (strcmp (element_name, "requiredRuntime") == 0) {
960 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
961 } else if (strcmp (element_name, "supportedRuntime") == 0) {
962 char *version = get_attribute_value (attribute_names, attribute_values, "version");
963 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
967 static void end_element (GMarkupParseContext *context,
968 const gchar *element_name,
972 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
974 if (strcmp (element_name, "configuration") == 0) {
975 app_config->configuration_count--;
976 } else if (strcmp (element_name, "startup") == 0) {
977 app_config->startup_count--;
981 static const GMarkupParser
990 static AppConfigInfo *
991 app_config_parse (const char *filename)
993 AppConfigInfo *app_config;
994 GMarkupParseContext *context;
999 if (stat (filename, &buf) != 0)
1002 app_config = g_new0 (AppConfigInfo, 1);
1004 if (!g_file_get_contents (filename, &text, &len, NULL))
1007 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1008 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1009 g_markup_parse_context_end_parse (context, NULL);
1011 g_markup_parse_context_free (context);
1017 app_config_free (AppConfigInfo* app_config)
1020 GSList *list = app_config->supported_runtimes;
1021 while (list != NULL) {
1022 rt = (char*)list->data;
1024 list = g_slist_next (list);
1026 g_slist_free (app_config->supported_runtimes);
1027 g_free (app_config->required_runtime);
1028 g_free (app_config);
1033 get_runtime_by_version (const char *version)
1036 int max = G_N_ELEMENTS (supported_runtimes);
1038 for (n=0; n<max; n++) {
1039 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1040 return &supported_runtimes[n];
1046 get_runtime_from_exe (const char *exe_file)
1048 AppConfigInfo* app_config;
1051 RuntimeInfo* runtime = NULL;
1052 MonoImage *image = NULL;
1054 config_name = g_strconcat (exe_file, ".config", NULL);
1055 app_config = app_config_parse (config_name);
1056 g_free (config_name);
1058 if (app_config != NULL) {
1059 /* Check supportedRuntime elements, if none is supported, fail.
1060 * If there are no such elements, look for a requiredRuntime element.
1062 if (app_config->supported_runtimes != NULL) {
1063 GSList *list = app_config->supported_runtimes;
1064 while (list != NULL && runtime == NULL) {
1065 version = (char*) list->data;
1066 runtime = get_runtime_by_version (version);
1067 list = g_slist_next (list);
1069 app_config_free (app_config);
1073 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1074 if (app_config->required_runtime != NULL) {
1075 runtime = get_runtime_by_version (app_config->required_runtime);
1076 app_config_free (app_config);
1079 app_config_free (app_config);
1082 /* Look for a runtime with the exact version */
1083 image = mono_image_open (exe_file, NULL);
1084 if (image == NULL) {
1085 /* The image is wrong or the file was not found. In this case return
1086 * a default runtime and leave to the initialization method the work of
1087 * reporting the error.
1089 return get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1092 runtime = get_runtime_by_version (image->version);
1098 mono_get_framework_version (void)
1100 return current_runtime->framework_version;
1104 mono_get_runtime_version (void)
1106 return current_runtime->runtime_version;