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 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
184 MonoJitInfoTable *table = domain->jit_info_table;
185 gpointer start = ji->code_start;
188 mono_domain_lock (domain);
189 pos = mono_jit_info_table_index (table, start);
190 g_assert (g_array_index (table, gpointer, pos) == ji);
192 g_array_remove_index (table, pos);
193 mono_domain_unlock (domain);
197 ldstr_hash (const char* str)
201 len = mono_metadata_decode_blob_size (str, &str) - 1;
203 /* if len == 0 *str will point to the mark byte */
206 * FIXME: The distribution may not be so nice with lots of
207 * null chars in the string.
209 for (str += 1; str < end; str++)
210 h = (h << 5) - h + *str;
215 ldstr_equal (const char *str1, const char *str2) {
219 len = mono_metadata_decode_blob_size (str1, NULL) - 1;
220 len2 = mono_metadata_decode_blob_size (str2, NULL) - 1;
223 return memcmp (str1, str2, len) == 0;
227 mono_string_equal (MonoString *s1, MonoString *s2)
229 int l1 = mono_string_length (s1);
230 int l2 = mono_string_length (s2);
235 return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0;
239 mono_string_hash (MonoString *s)
241 const guint16 *p = mono_string_chars (s);
242 int i, len = mono_string_length (s);
245 for (i = 0; i < len; i++) {
246 h = (h << 5) - h + *p;
255 domain_finalizer (void *obj, void *data) {
256 g_print ("domain finalized\n");
261 mono_domain_create (void)
266 domain = GC_MALLOC (sizeof (MonoDomain));
267 GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
269 domain = g_new0 (MonoDomain, 1);
271 domain->domain = NULL;
272 domain->setup = NULL;
273 domain->friendly_name = NULL;
274 domain->search_path = NULL;
276 domain->mp = mono_mempool_new ();
277 domain->code_mp = mono_code_manager_new ();
278 domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
279 domain->assemblies = g_hash_table_new (g_str_hash, g_str_equal);
280 domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
281 domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
282 domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
283 domain->jit_code_hash = g_hash_table_new (NULL, NULL);
284 domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
285 domain->jit_info_table = mono_jit_info_table_new ();
286 domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
287 domain->jump_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
288 domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
289 domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
291 InitializeCriticalSection (&domain->lock);
293 EnterCriticalSection (&appdomains_mutex);
294 mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
295 LeaveCriticalSection (&appdomains_mutex);
301 * mono_init_internal:
303 * Creates the initial application domain and initializes the mono_defaults
305 * This function is guaranteed to not run any IL code.
306 * If exe_filename is not NULL, the method will determine the required runtime
307 * from the exe configuration file or the version PE field.
308 * If runtime_version is not NULL, that runtime version will be used.
309 * Either exe_filename or runtime_version must be provided.
311 * Returns: the initial domain.
314 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
316 static MonoDomain *domain = NULL;
318 MonoImageOpenStatus status = MONO_IMAGE_OK;
319 MonoAssemblyName corlib_aname;
322 g_assert_not_reached ();
326 appdomain_thread_id = TlsAlloc ();
327 context_thread_id = TlsAlloc ();
329 InitializeCriticalSection (&appdomains_mutex);
331 mono_metadata_init ();
332 mono_raw_buffer_init ();
334 mono_assemblies_init ();
337 /* FIXME: When should we release this memory? */
338 MONO_GC_REGISTER_ROOT (appdomains_list);
339 appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
341 domain = mono_domain_create ();
342 mono_root_domain = domain;
344 SET_APPDOMAIN (domain);
346 if (exe_filename != NULL) {
347 current_runtime = get_runtime_from_exe (exe_filename);
348 } else if (runtime_version != NULL) {
349 current_runtime = get_runtime_by_version (runtime_version);
352 if (current_runtime == NULL) {
353 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
354 current_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
355 g_print ("Using default runtime: %s\n", current_runtime->runtime_version);
358 /* find the corlib */
359 corlib_aname.name = "mscorlib";
360 ass = mono_assembly_load (&corlib_aname, NULL, &status);
361 if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
363 case MONO_IMAGE_ERROR_ERRNO: {
364 char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", mono_get_framework_version (), "mscorlib.dll", NULL);
365 g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
366 g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
367 g_free (corlib_file);
370 case MONO_IMAGE_IMAGE_INVALID:
371 g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
372 mono_assembly_getrootdir ());
374 case MONO_IMAGE_MISSING_ASSEMBLYREF:
375 g_print ("Missing assembly reference in %s/mscorlib.dll\n",
376 mono_assembly_getrootdir ());
379 /* to suppress compiler warning */
385 mono_defaults.corlib = mono_assembly_get_image (ass);
387 mono_defaults.object_class = mono_class_from_name (
388 mono_defaults.corlib, "System", "Object");
389 g_assert (mono_defaults.object_class != 0);
391 mono_defaults.void_class = mono_class_from_name (
392 mono_defaults.corlib, "System", "Void");
393 g_assert (mono_defaults.void_class != 0);
395 mono_defaults.boolean_class = mono_class_from_name (
396 mono_defaults.corlib, "System", "Boolean");
397 g_assert (mono_defaults.boolean_class != 0);
399 mono_defaults.byte_class = mono_class_from_name (
400 mono_defaults.corlib, "System", "Byte");
401 g_assert (mono_defaults.byte_class != 0);
403 mono_defaults.sbyte_class = mono_class_from_name (
404 mono_defaults.corlib, "System", "SByte");
405 g_assert (mono_defaults.sbyte_class != 0);
407 mono_defaults.int16_class = mono_class_from_name (
408 mono_defaults.corlib, "System", "Int16");
409 g_assert (mono_defaults.int16_class != 0);
411 mono_defaults.uint16_class = mono_class_from_name (
412 mono_defaults.corlib, "System", "UInt16");
413 g_assert (mono_defaults.uint16_class != 0);
415 mono_defaults.int32_class = mono_class_from_name (
416 mono_defaults.corlib, "System", "Int32");
417 g_assert (mono_defaults.int32_class != 0);
419 mono_defaults.uint32_class = mono_class_from_name (
420 mono_defaults.corlib, "System", "UInt32");
421 g_assert (mono_defaults.uint32_class != 0);
423 mono_defaults.uint_class = mono_class_from_name (
424 mono_defaults.corlib, "System", "UIntPtr");
425 g_assert (mono_defaults.uint_class != 0);
427 mono_defaults.int_class = mono_class_from_name (
428 mono_defaults.corlib, "System", "IntPtr");
429 g_assert (mono_defaults.int_class != 0);
431 mono_defaults.int64_class = mono_class_from_name (
432 mono_defaults.corlib, "System", "Int64");
433 g_assert (mono_defaults.int64_class != 0);
435 mono_defaults.uint64_class = mono_class_from_name (
436 mono_defaults.corlib, "System", "UInt64");
437 g_assert (mono_defaults.uint64_class != 0);
439 mono_defaults.single_class = mono_class_from_name (
440 mono_defaults.corlib, "System", "Single");
441 g_assert (mono_defaults.single_class != 0);
443 mono_defaults.double_class = mono_class_from_name (
444 mono_defaults.corlib, "System", "Double");
445 g_assert (mono_defaults.double_class != 0);
447 mono_defaults.char_class = mono_class_from_name (
448 mono_defaults.corlib, "System", "Char");
449 g_assert (mono_defaults.char_class != 0);
451 mono_defaults.string_class = mono_class_from_name (
452 mono_defaults.corlib, "System", "String");
453 g_assert (mono_defaults.string_class != 0);
455 mono_defaults.enum_class = mono_class_from_name (
456 mono_defaults.corlib, "System", "Enum");
457 g_assert (mono_defaults.enum_class != 0);
459 mono_defaults.array_class = mono_class_from_name (
460 mono_defaults.corlib, "System", "Array");
461 g_assert (mono_defaults.array_class != 0);
463 mono_defaults.delegate_class = mono_class_from_name (
464 mono_defaults.corlib, "System", "Delegate");
465 g_assert (mono_defaults.delegate_class != 0 );
467 mono_defaults.multicastdelegate_class = mono_class_from_name (
468 mono_defaults.corlib, "System", "MulticastDelegate");
469 g_assert (mono_defaults.multicastdelegate_class != 0 );
471 mono_defaults.asyncresult_class = mono_class_from_name (
472 mono_defaults.corlib, "System.Runtime.Remoting.Messaging",
474 g_assert (mono_defaults.asyncresult_class != 0 );
476 mono_defaults.waithandle_class = mono_class_from_name (
477 mono_defaults.corlib, "System.Threading", "WaitHandle");
478 g_assert (mono_defaults.waithandle_class != 0 );
480 mono_defaults.typehandle_class = mono_class_from_name (
481 mono_defaults.corlib, "System", "RuntimeTypeHandle");
482 g_assert (mono_defaults.typehandle_class != 0);
484 mono_defaults.methodhandle_class = mono_class_from_name (
485 mono_defaults.corlib, "System", "RuntimeMethodHandle");
486 g_assert (mono_defaults.methodhandle_class != 0);
488 mono_defaults.fieldhandle_class = mono_class_from_name (
489 mono_defaults.corlib, "System", "RuntimeFieldHandle");
490 g_assert (mono_defaults.fieldhandle_class != 0);
492 mono_defaults.monotype_class = mono_class_from_name (
493 mono_defaults.corlib, "System", "MonoType");
494 g_assert (mono_defaults.monotype_class != 0);
496 mono_defaults.exception_class = mono_class_from_name (
497 mono_defaults.corlib, "System", "Exception");
498 g_assert (mono_defaults.exception_class != 0);
500 mono_defaults.threadabortexception_class = mono_class_from_name (
501 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
502 g_assert (mono_defaults.threadabortexception_class != 0);
504 mono_defaults.thread_class = mono_class_from_name (
505 mono_defaults.corlib, "System.Threading", "Thread");
506 g_assert (mono_defaults.thread_class != 0);
508 mono_defaults.appdomain_class = mono_class_from_name (
509 mono_defaults.corlib, "System", "AppDomain");
510 g_assert (mono_defaults.appdomain_class != 0);
512 mono_defaults.transparent_proxy_class = mono_class_from_name (
513 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
514 g_assert (mono_defaults.transparent_proxy_class != 0);
516 mono_defaults.real_proxy_class = mono_class_from_name (
517 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
518 g_assert (mono_defaults.real_proxy_class != 0);
520 mono_defaults.mono_method_message_class = mono_class_from_name (
521 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
522 g_assert (mono_defaults.mono_method_message_class != 0);
524 mono_defaults.field_info_class = mono_class_from_name (
525 mono_defaults.corlib, "System.Reflection", "FieldInfo");
526 g_assert (mono_defaults.field_info_class != 0);
528 mono_defaults.method_info_class = mono_class_from_name (
529 mono_defaults.corlib, "System.Reflection", "MethodInfo");
530 g_assert (mono_defaults.method_info_class != 0);
532 mono_defaults.stringbuilder_class = mono_class_from_name (
533 mono_defaults.corlib, "System.Text", "StringBuilder");
534 g_assert (mono_defaults.stringbuilder_class != 0);
536 mono_defaults.math_class = mono_class_from_name (
537 mono_defaults.corlib, "System", "Math");
538 g_assert (mono_defaults.math_class != 0);
540 mono_defaults.stack_frame_class = mono_class_from_name (
541 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
542 g_assert (mono_defaults.stack_frame_class != 0);
544 mono_defaults.stack_trace_class = mono_class_from_name (
545 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
546 g_assert (mono_defaults.stack_trace_class != 0);
548 mono_defaults.marshal_class = mono_class_from_name (
549 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
550 g_assert (mono_defaults.marshal_class != 0);
552 mono_defaults.iserializeable_class = mono_class_from_name (
553 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
554 g_assert (mono_defaults.iserializeable_class != 0);
556 mono_defaults.serializationinfo_class = mono_class_from_name (
557 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
558 g_assert (mono_defaults.serializationinfo_class != 0);
560 mono_defaults.streamingcontext_class = mono_class_from_name (
561 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
562 g_assert (mono_defaults.streamingcontext_class != 0);
564 mono_defaults.typed_reference_class = mono_class_from_name (
565 mono_defaults.corlib, "System", "TypedReference");
566 g_assert (mono_defaults.typed_reference_class != 0);
568 mono_defaults.argumenthandle_class = mono_class_from_name (
569 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
570 g_assert (mono_defaults.argumenthandle_class != 0);
572 mono_defaults.marshalbyrefobject_class = mono_class_from_name (
573 mono_defaults.corlib, "System", "MarshalByRefObject");
574 g_assert (mono_defaults.marshalbyrefobject_class != 0);
576 mono_defaults.monitor_class = mono_class_from_name (
577 mono_defaults.corlib, "System.Threading", "Monitor");
578 g_assert (mono_defaults.monitor_class != 0);
580 mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
581 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
582 g_assert (mono_defaults.iremotingtypeinfo_class != 0);
584 domain->friendly_name = g_path_get_basename (filename);
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 default runtime version.
597 * Returns: the initial domain.
600 mono_init (const char *domain_name)
602 return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
606 * mono_init_from_assembly:
608 * Creates the initial application domain and initializes the mono_defaults
610 * This function is guaranteed to not run any IL code.
611 * The runtime is initialized using the runtime version required by the
612 * provided executable. The version is determined by looking at the exe
613 * configuration file and the version PE field)
615 * Returns: the initial domain.
618 mono_init_from_assembly (const char *domain_name, const char *filename)
620 return mono_init_internal (domain_name, filename, NULL);
626 * Creates the initial application domain and initializes the mono_defaults
628 * This function is guaranteed to not run any IL code.
629 * The runtime is initialized using the provided rutime version.
631 * Returns: the initial domain.
634 mono_init_version (const char *domain_name, const char *version)
636 return mono_init_internal (domain_name, NULL, version);
640 mono_get_root_domain (void)
642 return mono_root_domain;
648 * Returns the current domain.
653 return GET_APPDOMAIN ();
657 * mono_domain_set_internal:
658 * @domain: the new domain
660 * Sets the current domain to @domain.
663 mono_domain_set_internal (MonoDomain *domain)
665 SET_APPDOMAIN (domain);
666 SET_APPCONTEXT (domain->default_context);
675 copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
677 MonoGHashTable *dest = (MonoGHashTable*)user_data;
679 mono_g_hash_table_insert (dest, key, data);
683 foreach_domain (gpointer key, gpointer data, gpointer user_data)
685 DomainInfo *dom_info = user_data;
687 dom_info->func ((MonoDomain*)data, dom_info->user_data);
691 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
694 MonoGHashTable *copy;
697 * Create a copy of the hashtable to avoid calling the user callback
698 * inside the lock because that could lead to deadlocks.
699 * We can do this because this function is not perf. critical.
701 copy = mono_g_hash_table_new (NULL, NULL);
702 EnterCriticalSection (&appdomains_mutex);
703 mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
704 LeaveCriticalSection (&appdomains_mutex);
706 dom_info.func = func;
707 dom_info.user_data = user_data;
708 mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
710 mono_g_hash_table_destroy (copy);
714 * mono_domain_assembly_open:
715 * @domain: the application domain
716 * @name: file name of the assembly
718 * fixme: maybe we should integrate this with mono_assembly_open ??
721 mono_domain_assembly_open (MonoDomain *domain, const char *name)
725 mono_domain_lock (domain);
726 if ((ass = g_hash_table_lookup (domain->assemblies, name))) {
727 mono_domain_unlock (domain);
730 mono_domain_unlock (domain);
732 if (!(ass = mono_assembly_open (name, NULL)))
739 remove_assembly (gpointer key, gpointer value, gpointer user_data)
741 mono_assembly_close ((MonoAssembly *)value);
745 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
747 g_slist_free (value);
751 mono_domain_free (MonoDomain *domain, gboolean force)
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 g_free (domain->friendly_name);
763 g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
765 mono_g_hash_table_destroy (domain->env);
766 g_hash_table_destroy (domain->assemblies);
767 mono_g_hash_table_destroy (domain->class_vtable_hash);
768 mono_g_hash_table_destroy (domain->proxy_vtable_hash);
769 mono_g_hash_table_destroy (domain->static_data_hash);
770 g_hash_table_destroy (domain->jit_code_hash);
771 mono_g_hash_table_destroy (domain->ldstr_table);
772 mono_jit_info_table_free (domain->jit_info_table);
773 #ifdef DEBUG_DOMAIN_UNLOAD
774 mono_mempool_invalidate (domain->mp);
775 mono_code_manager_invalidate (domain->code_mp);
777 mono_mempool_destroy (domain->mp);
778 mono_code_manager_destroy (domain->code_mp);
780 if (domain->jump_target_hash) {
781 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
782 g_hash_table_destroy (domain->jump_target_hash);
784 mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
785 mono_g_hash_table_destroy (domain->jump_trampoline_hash);
786 g_hash_table_destroy (domain->finalizable_objects_hash);
787 if (domain->special_static_fields)
788 g_hash_table_destroy (domain->special_static_fields);
789 DeleteCriticalSection (&domain->lock);
790 domain->setup = NULL;
792 /* FIXME: anything else required ? */
799 if ((domain == mono_root_domain))
800 mono_root_domain = NULL;
804 * mono_domain_get_id:
806 * Returns the a domain for a specific domain id.
809 mono_domain_get_by_id (gint32 domainid)
813 EnterCriticalSection (&appdomains_mutex);
814 domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
815 LeaveCriticalSection (&appdomains_mutex);
821 mono_domain_get_id (MonoDomain *domain)
823 return domain->domain_id;
827 mono_context_set (MonoAppContext * new_context)
829 SET_APPCONTEXT (new_context);
833 mono_context_get (void)
835 return GET_APPCONTEXT ();
839 mono_get_corlib (void)
841 return mono_defaults.corlib;
845 mono_get_object_class (void)
847 return mono_defaults.object_class;
851 mono_get_byte_class (void)
853 return mono_defaults.byte_class;
857 mono_get_void_class (void)
859 return mono_defaults.void_class;
863 mono_get_boolean_class (void)
865 return mono_defaults.boolean_class;
869 mono_get_sbyte_class (void)
871 return mono_defaults.sbyte_class;
875 mono_get_int16_class (void)
877 return mono_defaults.int16_class;
881 mono_get_uint16_class (void)
883 return mono_defaults.uint16_class;
887 mono_get_int32_class (void)
889 return mono_defaults.int32_class;
893 mono_get_uint32_class (void)
895 return mono_defaults.uint32_class;
899 mono_get_intptr_class (void)
901 return mono_defaults.int_class;
905 mono_get_uintptr_class (void)
907 return mono_defaults.uint_class;
911 mono_get_int64_class (void)
913 return mono_defaults.int64_class;
917 mono_get_uint64_class (void)
919 return mono_defaults.uint64_class;
923 mono_get_single_class (void)
925 return mono_defaults.single_class;
929 mono_get_double_class (void)
931 return mono_defaults.double_class;
935 mono_get_char_class (void)
937 return mono_defaults.char_class;
941 mono_get_string_class (void)
943 return mono_defaults.string_class;
947 mono_get_enum_class (void)
949 return mono_defaults.enum_class;
953 mono_get_array_class (void)
955 return mono_defaults.array_class;
959 mono_get_thread_class (void)
961 return mono_defaults.thread_class;
965 mono_get_exception_class (void)
967 return mono_defaults.exception_class;
971 static char* get_attribute_value (const gchar **attribute_names,
972 const gchar **attribute_values,
973 const char *att_name)
976 for (n=0; attribute_names[n] != NULL; n++) {
977 if (strcmp (attribute_names[n], att_name) == 0)
978 return g_strdup (attribute_values[n]);
983 static void start_element (GMarkupParseContext *context,
984 const gchar *element_name,
985 const gchar **attribute_names,
986 const gchar **attribute_values,
990 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
992 if (strcmp (element_name, "configuration") == 0) {
993 app_config->configuration_count++;
996 if (strcmp (element_name, "startup") == 0) {
997 app_config->startup_count++;
1001 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1004 if (strcmp (element_name, "requiredRuntime") == 0) {
1005 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1006 } else if (strcmp (element_name, "supportedRuntime") == 0) {
1007 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1008 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1012 static void end_element (GMarkupParseContext *context,
1013 const gchar *element_name,
1017 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1019 if (strcmp (element_name, "configuration") == 0) {
1020 app_config->configuration_count--;
1021 } else if (strcmp (element_name, "startup") == 0) {
1022 app_config->startup_count--;
1026 static const GMarkupParser
1035 static AppConfigInfo *
1036 app_config_parse (const char *filename)
1038 AppConfigInfo *app_config;
1039 GMarkupParseContext *context;
1044 if (stat (filename, &buf) != 0)
1047 app_config = g_new0 (AppConfigInfo, 1);
1049 if (!g_file_get_contents (filename, &text, &len, NULL))
1052 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1053 if (g_markup_parse_context_parse (context, text, len, NULL)) {
1054 g_markup_parse_context_end_parse (context, NULL);
1056 g_markup_parse_context_free (context);
1062 app_config_free (AppConfigInfo* app_config)
1065 GSList *list = app_config->supported_runtimes;
1066 while (list != NULL) {
1067 rt = (char*)list->data;
1069 list = g_slist_next (list);
1071 g_slist_free (app_config->supported_runtimes);
1072 g_free (app_config->required_runtime);
1073 g_free (app_config);
1078 get_runtime_by_version (const char *version)
1081 int max = G_N_ELEMENTS (supported_runtimes);
1083 for (n=0; n<max; n++) {
1084 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1085 return &supported_runtimes[n];
1091 get_runtime_from_exe (const char *exe_file)
1093 AppConfigInfo* app_config;
1096 RuntimeInfo* runtime = NULL;
1097 MonoImage *image = NULL;
1099 config_name = g_strconcat (exe_file, ".config", NULL);
1100 app_config = app_config_parse (config_name);
1101 g_free (config_name);
1103 if (app_config != NULL) {
1104 /* Check supportedRuntime elements, if none is supported, fail.
1105 * If there are no such elements, look for a requiredRuntime element.
1107 if (app_config->supported_runtimes != NULL) {
1108 GSList *list = app_config->supported_runtimes;
1109 while (list != NULL && runtime == NULL) {
1110 version = (char*) list->data;
1111 runtime = get_runtime_by_version (version);
1112 list = g_slist_next (list);
1114 app_config_free (app_config);
1118 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1119 if (app_config->required_runtime != NULL) {
1120 runtime = get_runtime_by_version (app_config->required_runtime);
1121 app_config_free (app_config);
1124 app_config_free (app_config);
1127 /* Look for a runtime with the exact version */
1128 image = mono_image_open (exe_file, NULL);
1129 if (image == NULL) {
1130 /* The image is wrong or the file was not found. In this case return
1131 * a default runtime and leave to the initialization method the work of
1132 * reporting the error.
1134 return get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1137 runtime = get_runtime_by_version (image->version);
1143 mono_get_framework_version (void)
1145 return current_runtime->framework_version;
1149 mono_get_runtime_version (void)
1151 return current_runtime->runtime_version;