2005-09-26 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mono / metadata / domain.c
1
2 /*
3  * domain.c: MonoDomain functions
4  *
5  * Author:
6  *      Dietmar Maurer (dietmar@ximian.com)
7  *      Patrik Torstensson
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <string.h>
15 #include <sys/stat.h>
16
17 #include <mono/os/gc_wrapper.h>
18
19 #include <mono/utils/mono-compiler.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/object-internals.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/assembly.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/cil-coff.h>
27 #include <mono/metadata/rawbuffer.h>
28 #include <mono/metadata/metadata-internals.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/metadata/mono-debug-debugger.h>
31 #include <mono/metadata/appdomain.h>
32 #include <metadata/threads.h>
33
34 /* #define DEBUG_DOMAIN_UNLOAD */
35
36 /* we need to use both the Tls* functions and __thread because
37  * some archs may generate faster jit code with one meachanism
38  * or the other (we used to do it because tls slots were GC-tracked,
39  * but we can't depend on this).
40  */
41 static guint32 appdomain_thread_id = -1;
42  
43 #ifdef HAVE_KW_THREAD
44 static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
45 #define GET_APPDOMAIN() tls_appdomain
46 #define SET_APPDOMAIN(x) do { \
47         tls_appdomain = x; \
48         TlsSetValue (appdomain_thread_id, x); \
49 } while (FALSE)
50
51 #else
52
53 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
54 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
55
56 #endif
57
58 #define GET_APPCONTEXT() (mono_thread_current ()->current_appcontext)
59 #define SET_APPCONTEXT(x) mono_thread_current ()->current_appcontext = (x)
60
61 static guint16 appdomain_list_size = 0;
62 static guint16 appdomain_next = 0;
63 static MonoDomain **appdomains_list = NULL;
64
65 static CRITICAL_SECTION appdomains_mutex;
66
67 static MonoDomain *mono_root_domain = NULL;
68
69 /* AppConfigInfo: Information about runtime versions supported by an 
70  * aplication.
71  */
72 typedef struct {
73         GSList *supported_runtimes;
74         char *required_runtime;
75         int configuration_count;
76         int startup_count;
77 } AppConfigInfo;
78
79 /*
80  * AotModuleInfo: Contains information about AOT modules.
81  */
82 typedef struct {
83         MonoImage *image;
84         gpointer start, end;
85 } AotModuleInfo;
86
87 static const MonoRuntimeInfo *current_runtime = NULL;
88
89 static MonoJitInfoFindInAot jit_info_find_in_aot_func = NULL;
90
91 /*
92  * Contains information about AOT loaded code.
93  */
94 static MonoJitInfoTable *aot_modules = NULL;
95
96 /* This is the list of runtime versions supported by this JIT.
97  */
98 static const MonoRuntimeInfo supported_runtimes[] = {
99         {"v1.0.3705", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
100         {"v1.1.4322", "1.0", { {1,0,5000,0}, {7,0,5000,0} }     },
101         {"v2.0.50215","2.0", { {2,0,0,0},    {8,0,0,0} }        },
102         {"v2.0.50727","2.0", { {2,0,0,0},    {8,0,0,0} }        }
103 };
104
105
106 /* The stable runtime version */
107 #define DEFAULT_RUNTIME_VERSION "v1.1.4322"
108
109 static void
110 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes);
111
112 static const MonoRuntimeInfo*
113 get_runtime_by_version (const char *version);
114
115 static MonoImage*
116 mono_jit_info_find_aot_module (guint8* addr);
117
118 guint32
119 mono_domain_get_tls_key (void)
120 {
121         return appdomain_thread_id;
122 }
123
124 gint32
125 mono_domain_get_tls_offset (void)
126 {
127         int offset = -1;
128         MONO_THREAD_VAR_OFFSET (tls_appdomain, offset);
129 /*      __asm ("jmp 1f; .section writetext, \"awx\"; 1: movl $tls_appdomain@ntpoff, %0; jmp 2f; .previous; 2:" 
130                 : "=r" (offset));*/
131         return offset;
132 }
133
134 static MonoJitInfoTable *
135 mono_jit_info_table_new (void)
136 {
137         return g_array_new (FALSE, FALSE, sizeof (gpointer));
138 }
139
140 static void
141 mono_jit_info_table_free (MonoJitInfoTable *table)
142 {
143         g_array_free (table, TRUE);
144 }
145
146 static int
147 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
148 {
149         int left = 0, right = table->len;
150
151         while (left < right) {
152                 int pos = (left + right) / 2;
153                 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
154                 char *start = ji->code_start;
155                 char *end = start + ji->code_size;
156
157                 if (addr < start)
158                         right = pos;
159                 else if (addr >= end) 
160                         left = pos + 1;
161                 else
162                         return pos;
163         }
164
165         return left;
166 }
167
168 MonoJitInfo *
169 mono_jit_info_table_find (MonoDomain *domain, char *addr)
170 {
171         MonoJitInfoTable *table = domain->jit_info_table;
172         MonoJitInfo *ji;
173         guint left = 0, right;
174
175         mono_domain_lock (domain);
176
177         right = table->len;
178         while (left < right) {
179                 guint pos = (left + right) / 2;
180                 ji = g_array_index (table, gpointer, pos);
181
182                 if (addr < (char*)ji->code_start)
183                         right = pos;
184                 else if (addr >= (char*)ji->code_start + ji->code_size) 
185                         left = pos + 1;
186                 else {
187                         mono_domain_unlock (domain);
188                         return ji;
189                 }
190         }
191         mono_domain_unlock (domain);
192
193         /* maybe it is shared code, so we also search in the root domain */
194         ji = NULL;
195         if (domain != mono_root_domain)
196                 ji = mono_jit_info_table_find (mono_root_domain, addr);
197
198         if (ji == NULL) {
199                 /* Maybe its an AOT module */
200                 MonoImage *image = mono_jit_info_find_aot_module (addr);
201                 if (image)
202                         ji = jit_info_find_in_aot_func (domain, image, addr);
203         }
204         
205         return ji;
206 }
207
208 void
209 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
210 {
211         MonoJitInfoTable *table = domain->jit_info_table;
212         gpointer start = ji->code_start;
213         int pos;
214
215         mono_domain_lock (domain);
216         pos = mono_jit_info_table_index (table, start);
217
218         g_array_insert_val (table, pos, ji);
219         mono_domain_unlock (domain);
220 }
221
222 void
223 mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji)
224 {
225         MonoJitInfoTable *table = domain->jit_info_table;
226         gpointer start = ji->code_start;
227         int pos;
228
229         mono_domain_lock (domain);
230         pos = mono_jit_info_table_index (table, start);
231         if (g_array_index (table, gpointer, pos) != ji) {
232                 MonoJitInfo *ji2 = g_array_index (table, gpointer, pos);
233                 g_assert (ji == ji2);
234         }
235         g_assert (g_array_index (table, gpointer, pos) == ji);
236
237         g_array_remove_index (table, pos);
238         mono_domain_unlock (domain);
239 }       
240
241 static int
242 aot_info_table_index (MonoJitInfoTable *table, char *addr)
243 {
244         int left = 0, right = table->len;
245
246         while (left < right) {
247                 int pos = (left + right) / 2;
248                 AotModuleInfo *ainfo = g_array_index (table, gpointer, pos);
249                 char *start = ainfo->start;
250                 char *end = ainfo->end;
251
252                 if (addr < start)
253                         right = pos;
254                 else if (addr >= end) 
255                         left = pos + 1;
256                 else
257                         return pos;
258         }
259
260         return left;
261 }
262
263 void
264 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end)
265 {
266         AotModuleInfo *ainfo = g_new0 (AotModuleInfo, 1);
267         int pos;
268
269         ainfo->image = image;
270         ainfo->start = start;
271         ainfo->end = end;
272
273         EnterCriticalSection (&appdomains_mutex);
274
275         if (!aot_modules)
276                 aot_modules = mono_jit_info_table_new ();
277
278         pos = aot_info_table_index (aot_modules, start);
279
280         g_array_insert_val (aot_modules, pos, ainfo);
281
282         LeaveCriticalSection (&appdomains_mutex);
283 }
284
285 static MonoImage*
286 mono_jit_info_find_aot_module (guint8* addr)
287 {
288         guint left = 0, right;
289
290         if (!aot_modules)
291                 return NULL;
292
293         EnterCriticalSection (&appdomains_mutex);
294
295         right = aot_modules->len;
296         while (left < right) {
297                 guint pos = (left + right) / 2;
298                 AotModuleInfo *ai = g_array_index (aot_modules, gpointer, pos);
299
300                 if (addr < (guint8*)ai->start)
301                         right = pos;
302                 else if (addr >= (guint8*)ai->end)
303                         left = pos + 1;
304                 else {
305                         LeaveCriticalSection (&appdomains_mutex);
306                         return ai->image;
307                 }
308         }
309
310         LeaveCriticalSection (&appdomains_mutex);
311
312         return NULL;
313 }
314
315 void
316 mono_install_jit_info_find_in_aot (MonoJitInfoFindInAot func)
317 {
318         jit_info_find_in_aot_func = func;
319 }
320
321 gboolean
322 mono_string_equal (MonoString *s1, MonoString *s2)
323 {
324         int l1 = mono_string_length (s1);
325         int l2 = mono_string_length (s2);
326
327         if (s1 == s2)
328                 return TRUE;
329         if (l1 != l2)
330                 return FALSE;
331
332         return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1 * 2) == 0; 
333 }
334
335 guint
336 mono_string_hash (MonoString *s)
337 {
338         const guint16 *p = mono_string_chars (s);
339         int i, len = mono_string_length (s);
340         guint h = 0;
341
342         for (i = 0; i < len; i++) {
343                 h = (h << 5) - h + *p;
344                 p++;
345         }
346
347         return h;       
348 }
349
350 static gboolean
351 mono_ptrarray_equal (gpointer *s1, gpointer *s2)
352 {
353         int len = GPOINTER_TO_INT (s1 [0]);
354         if (len != GPOINTER_TO_INT (s2 [0]))
355                 return FALSE;
356
357         return memcmp (s1 + 1, s2 + 1, len * sizeof(gpointer)) == 0; 
358 }
359
360 static guint
361 mono_ptrarray_hash (gpointer *s)
362 {
363         int i;
364         int len = GPOINTER_TO_INT (s [0]);
365         guint hash = 0;
366         
367         for (i = 1; i < len; i++)
368                 hash += GPOINTER_TO_UINT (s [i]);
369
370         return hash;    
371 }
372
373 /*
374  * Allocate an id for domain and set domain->domain_id.
375  * LOCKING: must be called while holding appdomains_mutex.
376  * We try to assign low numbers to the domain, so it can be used
377  * as an index in data tables to lookup domain-specific info
378  * with minimal memory overhead. We also try not to reuse the
379  * same id too quickly (to help debugging).
380  */
381 static int
382 domain_id_alloc (MonoDomain *domain)
383 {
384         int id = -1, i;
385         if (!appdomains_list) {
386                 appdomain_list_size = 2;
387                 appdomains_list = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
388         }
389         for (i = appdomain_next; i < appdomain_list_size; ++i) {
390                 if (!appdomains_list [i]) {
391                         id = i;
392                         break;
393                 }
394         }
395         if (id == -1) {
396                 for (i = 0; i < appdomain_next; ++i) {
397                         if (!appdomains_list [i]) {
398                                 id = i;
399                                 break;
400                         }
401                 }
402         }
403         if (id == -1) {
404                 MonoDomain **new_list;
405                 int new_size = appdomain_list_size * 2;
406                 if (new_size >= (1 << 16))
407                         g_assert_not_reached ();
408                 id = appdomain_list_size;
409                 new_list = mono_gc_alloc_fixed (new_size * sizeof (void*), NULL);
410                 memcpy (new_list, appdomains_list, appdomain_list_size * sizeof (void*));
411                 mono_gc_free_fixed (appdomains_list);
412                 appdomains_list = new_list;
413                 appdomain_list_size = new_size;
414         }
415         domain->domain_id = id;
416         appdomains_list [id] = domain;
417         appdomain_next++;
418         if (appdomain_next > appdomain_list_size)
419                 appdomain_next = 0;
420         return id;
421 }
422
423 MonoDomain *
424 mono_domain_create (void)
425 {
426         MonoDomain *domain;
427
428         domain = mono_gc_alloc_fixed (sizeof (MonoDomain), NULL);
429         domain->domain = NULL;
430         domain->setup = NULL;
431         domain->friendly_name = NULL;
432         domain->search_path = NULL;
433
434         domain->mp = mono_mempool_new ();
435         domain->code_mp = mono_code_manager_new ();
436         domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
437         domain->domain_assemblies = NULL;
438         domain->class_vtable_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
439         domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_ptrarray_hash, (GCompareFunc)mono_ptrarray_equal);
440         domain->static_data_hash = mono_g_hash_table_new (mono_aligned_addr_hash, NULL);
441         domain->jit_code_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
442         domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
443         domain->jit_info_table = mono_jit_info_table_new ();
444         domain->class_init_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
445         domain->jump_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
446         domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
447         domain->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
448         domain->delegate_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
449
450         InitializeCriticalSection (&domain->lock);
451         InitializeCriticalSection (&domain->assemblies_lock);
452
453         EnterCriticalSection (&appdomains_mutex);
454         domain_id_alloc (domain);
455         LeaveCriticalSection (&appdomains_mutex);
456
457         return domain;
458 }
459
460 /**
461  * mono_init_internal:
462  * 
463  * Creates the initial application domain and initializes the mono_defaults
464  * structure.
465  * This function is guaranteed to not run any IL code.
466  * If exe_filename is not NULL, the method will determine the required runtime
467  * from the exe configuration file or the version PE field.
468  * If runtime_version is not NULL, that runtime version will be used.
469  * Either exe_filename or runtime_version must be provided.
470  *
471  * Returns: the initial domain.
472  */
473 static MonoDomain *
474 mono_init_internal (const char *filename, const char *exe_filename, const char *runtime_version)
475 {
476         static MonoDomain *domain = NULL;
477         MonoAssembly *ass = NULL;
478         MonoImageOpenStatus status = MONO_IMAGE_OK;
479         const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
480         int n;
481
482         if (domain)
483                 g_assert_not_reached ();
484
485         MONO_GC_PRE_INIT ();
486
487         appdomain_thread_id = TlsAlloc ();
488
489         InitializeCriticalSection (&appdomains_mutex);
490
491         mono_metadata_init ();
492         mono_raw_buffer_init ();
493         mono_images_init ();
494         mono_assemblies_init ();
495         mono_loader_init ();
496
497         /* FIXME: When should we release this memory? */
498         MONO_GC_REGISTER_ROOT (appdomains_list);
499
500         domain = mono_domain_create ();
501         mono_root_domain = domain;
502
503         SET_APPDOMAIN (domain);
504         
505         /* Get a list of runtimes supported by the exe */
506         if (exe_filename != NULL) {
507                 get_runtimes_from_exe (exe_filename, runtimes);
508         } else if (runtime_version != NULL) {
509                 runtimes [0] = get_runtime_by_version (runtime_version);
510                 runtimes [1] = NULL;
511         }
512
513         if (runtimes [0] == NULL) {
514                 const MonoRuntimeInfo *default_runtime = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
515                 runtimes [0] = default_runtime;
516                 runtimes [1] = NULL;
517                 g_print ("WARNING: The runtime version supported by this application is unavailable.\n");
518                 g_print ("Using default runtime: %s\n", default_runtime->runtime_version);
519         }
520
521         /* The selected runtime will be the first one for which there is a mscrolib.dll */
522         for (n = 0; runtimes [n] != NULL && ass == NULL; n++) {
523                 current_runtime = runtimes [n];
524                 ass = mono_assembly_load_corlib (current_runtime, &status);
525                 if (status != MONO_IMAGE_OK && status != MONO_IMAGE_ERROR_ERRNO)
526                         break;
527         }
528
529         if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
530                 switch (status){
531                 case MONO_IMAGE_ERROR_ERRNO: {
532                         char *corlib_file = g_build_filename (mono_assembly_getrootdir (), "mono", current_runtime->framework_version, "mscorlib.dll", NULL);
533                         g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
534                         g_print ("It should have been installed in the `%s' directory.\n", corlib_file);
535                         g_free (corlib_file);
536                         break;
537                 }
538                 case MONO_IMAGE_IMAGE_INVALID:
539                         g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
540                                  mono_assembly_getrootdir ());
541                         break;
542                 case MONO_IMAGE_MISSING_ASSEMBLYREF:
543                         g_print ("Missing assembly reference in %s/mscorlib.dll\n",
544                                  mono_assembly_getrootdir ());
545                         break;
546                 case MONO_IMAGE_OK:
547                         /* to suppress compiler warning */
548                         break;
549                 }
550                 
551                 exit (1);
552         }
553         mono_defaults.corlib = mono_assembly_get_image (ass);
554
555         mono_defaults.object_class = mono_class_from_name (
556                 mono_defaults.corlib, "System", "Object");
557         g_assert (mono_defaults.object_class != 0);
558
559         mono_defaults.void_class = mono_class_from_name (
560                 mono_defaults.corlib, "System", "Void");
561         g_assert (mono_defaults.void_class != 0);
562
563         mono_defaults.boolean_class = mono_class_from_name (
564                 mono_defaults.corlib, "System", "Boolean");
565         g_assert (mono_defaults.boolean_class != 0);
566
567         mono_defaults.byte_class = mono_class_from_name (
568                 mono_defaults.corlib, "System", "Byte");
569         g_assert (mono_defaults.byte_class != 0);
570
571         mono_defaults.sbyte_class = mono_class_from_name (
572                 mono_defaults.corlib, "System", "SByte");
573         g_assert (mono_defaults.sbyte_class != 0);
574
575         mono_defaults.int16_class = mono_class_from_name (
576                 mono_defaults.corlib, "System", "Int16");
577         g_assert (mono_defaults.int16_class != 0);
578
579         mono_defaults.uint16_class = mono_class_from_name (
580                 mono_defaults.corlib, "System", "UInt16");
581         g_assert (mono_defaults.uint16_class != 0);
582
583         mono_defaults.int32_class = mono_class_from_name (
584                 mono_defaults.corlib, "System", "Int32");
585         g_assert (mono_defaults.int32_class != 0);
586
587         mono_defaults.uint32_class = mono_class_from_name (
588                 mono_defaults.corlib, "System", "UInt32");
589         g_assert (mono_defaults.uint32_class != 0);
590
591         mono_defaults.uint_class = mono_class_from_name (
592                 mono_defaults.corlib, "System", "UIntPtr");
593         g_assert (mono_defaults.uint_class != 0);
594
595         mono_defaults.int_class = mono_class_from_name (
596                 mono_defaults.corlib, "System", "IntPtr");
597         g_assert (mono_defaults.int_class != 0);
598
599         mono_defaults.int64_class = mono_class_from_name (
600                 mono_defaults.corlib, "System", "Int64");
601         g_assert (mono_defaults.int64_class != 0);
602
603         mono_defaults.uint64_class = mono_class_from_name (
604                 mono_defaults.corlib, "System", "UInt64");
605         g_assert (mono_defaults.uint64_class != 0);
606
607         mono_defaults.single_class = mono_class_from_name (
608                 mono_defaults.corlib, "System", "Single");
609         g_assert (mono_defaults.single_class != 0);
610
611         mono_defaults.double_class = mono_class_from_name (
612                 mono_defaults.corlib, "System", "Double");
613         g_assert (mono_defaults.double_class != 0);
614
615         mono_defaults.char_class = mono_class_from_name (
616                 mono_defaults.corlib, "System", "Char");
617         g_assert (mono_defaults.char_class != 0);
618
619         mono_defaults.string_class = mono_class_from_name (
620                 mono_defaults.corlib, "System", "String");
621         g_assert (mono_defaults.string_class != 0);
622
623         mono_defaults.enum_class = mono_class_from_name (
624                 mono_defaults.corlib, "System", "Enum");
625         g_assert (mono_defaults.enum_class != 0);
626
627         mono_defaults.array_class = mono_class_from_name (
628                 mono_defaults.corlib, "System", "Array");
629         g_assert (mono_defaults.array_class != 0);
630
631         mono_defaults.delegate_class = mono_class_from_name (
632                 mono_defaults.corlib, "System", "Delegate");
633         g_assert (mono_defaults.delegate_class != 0 );
634
635         mono_defaults.multicastdelegate_class = mono_class_from_name (
636                 mono_defaults.corlib, "System", "MulticastDelegate");
637         g_assert (mono_defaults.multicastdelegate_class != 0 );
638
639         mono_defaults.asyncresult_class = mono_class_from_name (
640                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", 
641                 "AsyncResult");
642         g_assert (mono_defaults.asyncresult_class != 0 );
643
644         mono_defaults.waithandle_class = mono_class_from_name (
645                 mono_defaults.corlib, "System.Threading", "WaitHandle");
646         g_assert (mono_defaults.waithandle_class != 0 );
647
648         mono_defaults.typehandle_class = mono_class_from_name (
649                 mono_defaults.corlib, "System", "RuntimeTypeHandle");
650         g_assert (mono_defaults.typehandle_class != 0);
651
652         mono_defaults.methodhandle_class = mono_class_from_name (
653                 mono_defaults.corlib, "System", "RuntimeMethodHandle");
654         g_assert (mono_defaults.methodhandle_class != 0);
655
656         mono_defaults.fieldhandle_class = mono_class_from_name (
657                 mono_defaults.corlib, "System", "RuntimeFieldHandle");
658         g_assert (mono_defaults.fieldhandle_class != 0);
659
660         mono_defaults.monotype_class = mono_class_from_name (
661                 mono_defaults.corlib, "System", "MonoType");
662         g_assert (mono_defaults.monotype_class != 0);
663
664         mono_defaults.exception_class = mono_class_from_name (
665                 mono_defaults.corlib, "System", "Exception");
666         g_assert (mono_defaults.exception_class != 0);
667
668         mono_defaults.threadabortexception_class = mono_class_from_name (
669                 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
670         g_assert (mono_defaults.threadabortexception_class != 0);
671
672         mono_defaults.thread_class = mono_class_from_name (
673                 mono_defaults.corlib, "System.Threading", "Thread");
674         g_assert (mono_defaults.thread_class != 0);
675
676         mono_defaults.appdomain_class = mono_class_from_name (
677                 mono_defaults.corlib, "System", "AppDomain");
678         g_assert (mono_defaults.appdomain_class != 0);
679
680         mono_defaults.transparent_proxy_class = mono_class_from_name (
681                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
682         g_assert (mono_defaults.transparent_proxy_class != 0);
683
684         mono_defaults.real_proxy_class = mono_class_from_name (
685                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
686         g_assert (mono_defaults.real_proxy_class != 0);
687
688         mono_defaults.mono_method_message_class = mono_class_from_name (
689                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
690         g_assert (mono_defaults.mono_method_message_class != 0);
691
692         mono_defaults.field_info_class = mono_class_from_name (
693                 mono_defaults.corlib, "System.Reflection", "FieldInfo");
694         g_assert (mono_defaults.field_info_class != 0);
695
696         mono_defaults.method_info_class = mono_class_from_name (
697                 mono_defaults.corlib, "System.Reflection", "MethodInfo");
698         g_assert (mono_defaults.method_info_class != 0);
699
700         mono_defaults.stringbuilder_class = mono_class_from_name (
701                 mono_defaults.corlib, "System.Text", "StringBuilder");
702         g_assert (mono_defaults.stringbuilder_class != 0);
703
704         mono_defaults.math_class = mono_class_from_name (
705                 mono_defaults.corlib, "System", "Math");
706         g_assert (mono_defaults.math_class != 0);
707
708         mono_defaults.stack_frame_class = mono_class_from_name (
709                 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
710         g_assert (mono_defaults.stack_frame_class != 0);
711
712         mono_defaults.stack_trace_class = mono_class_from_name (
713                 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
714         g_assert (mono_defaults.stack_trace_class != 0);
715
716         mono_defaults.marshal_class = mono_class_from_name (
717                 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
718         g_assert (mono_defaults.marshal_class != 0);
719
720         mono_defaults.iserializeable_class = mono_class_from_name (
721                 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
722         g_assert (mono_defaults.iserializeable_class != 0);
723
724         mono_defaults.serializationinfo_class = mono_class_from_name (
725                 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
726         g_assert (mono_defaults.serializationinfo_class != 0);
727
728         mono_defaults.streamingcontext_class = mono_class_from_name (
729                 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
730         g_assert (mono_defaults.streamingcontext_class != 0);
731
732         mono_defaults.typed_reference_class =  mono_class_from_name (
733                 mono_defaults.corlib, "System", "TypedReference");
734         g_assert (mono_defaults.typed_reference_class != 0);
735
736         mono_defaults.argumenthandle_class =  mono_class_from_name (
737                 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
738         g_assert (mono_defaults.argumenthandle_class != 0);
739
740         mono_defaults.marshalbyrefobject_class =  mono_class_from_name (
741                 mono_defaults.corlib, "System", "MarshalByRefObject");
742         g_assert (mono_defaults.marshalbyrefobject_class != 0);
743
744         mono_defaults.monitor_class =  mono_class_from_name (
745                 mono_defaults.corlib, "System.Threading", "Monitor");
746         g_assert (mono_defaults.monitor_class != 0);
747
748         mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
749                 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
750         g_assert (mono_defaults.iremotingtypeinfo_class != 0);
751
752         mono_defaults.runtimesecurityframe_class = mono_class_from_name (
753                 mono_defaults.corlib, "System.Security", "RuntimeSecurityFrame");
754
755         mono_defaults.executioncontext_class = mono_class_from_name (
756                 mono_defaults.corlib, "System.Threading", "ExecutionContext");
757
758         /*
759          * Note that mono_defaults.generic_array_class is only non-NULL if we're
760          * using the 2.0 corlib.
761          */
762         mono_class_init (mono_defaults.array_class);
763         mono_defaults.generic_array_class = mono_class_from_name (
764                 mono_defaults.corlib, "System", "Array/InternalArray`1");
765
766         domain->friendly_name = g_path_get_basename (filename);
767
768         return domain;
769 }
770
771 /**
772  * mono_init:
773  * 
774  * Creates the initial application domain and initializes the mono_defaults
775  * structure.
776  * This function is guaranteed to not run any IL code.
777  * The runtime is initialized using the default runtime version.
778  *
779  * Returns: the initial domain.
780  */
781 MonoDomain *
782 mono_init (const char *domain_name)
783 {
784         return mono_init_internal (domain_name, NULL, DEFAULT_RUNTIME_VERSION);
785 }
786
787 /**
788  * mono_init_from_assembly:
789  * 
790  * Creates the initial application domain and initializes the mono_defaults
791  * structure.
792  * This function is guaranteed to not run any IL code.
793  * The runtime is initialized using the runtime version required by the
794  * provided executable. The version is determined by looking at the exe 
795  * configuration file and the version PE field)
796  *
797  * Returns: the initial domain.
798  */
799 MonoDomain *
800 mono_init_from_assembly (const char *domain_name, const char *filename)
801 {
802         return mono_init_internal (domain_name, filename, NULL);
803 }
804
805 /**
806  * mono_init_version:
807  * 
808  * Creates the initial application domain and initializes the mono_defaults
809  * structure.
810  * This function is guaranteed to not run any IL code.
811  * The runtime is initialized using the provided rutime version.
812  *
813  * Returns: the initial domain.
814  */
815 MonoDomain *
816 mono_init_version (const char *domain_name, const char *version)
817 {
818         return mono_init_internal (domain_name, NULL, version);
819 }
820
821 MonoDomain*
822 mono_get_root_domain (void)
823 {
824         return mono_root_domain;
825 }
826
827 /**
828  * mono_domain_get:
829  *
830  * Returns: the current domain.
831  */
832 MonoDomain *
833 mono_domain_get ()
834 {
835         return GET_APPDOMAIN ();
836 }
837
838 /**
839  * mono_domain_set_internal:
840  * @domain: the new domain
841  *
842  * Sets the current domain to @domain.
843  */
844 void
845 mono_domain_set_internal (MonoDomain *domain)
846 {
847         SET_APPDOMAIN (domain);
848         SET_APPCONTEXT (domain->default_context);
849 }
850
851 void
852 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
853 {
854         int i, size;
855         MonoDomain **copy;
856
857         /*
858          * Create a copy of the data to avoid calling the user callback
859          * inside the lock because that could lead to deadlocks.
860          * We can do this because this function is not perf. critical.
861          */
862         EnterCriticalSection (&appdomains_mutex);
863         size = appdomain_list_size;
864         copy = mono_gc_alloc_fixed (appdomain_list_size * sizeof (void*), NULL);
865         memcpy (copy, appdomains_list, appdomain_list_size * sizeof (void*));
866         LeaveCriticalSection (&appdomains_mutex);
867
868         for (i = 0; i < size; ++i) {
869                 if (copy [i])
870                         func (copy [i], user_data);
871         }
872
873         mono_gc_free_fixed (copy);
874 }
875
876 /**
877  * mono_domain_assembly_open:
878  * @domain: the application domain
879  * @name: file name of the assembly
880  *
881  * fixme: maybe we should integrate this with mono_assembly_open ??
882  */
883 MonoAssembly *
884 mono_domain_assembly_open (MonoDomain *domain, const char *name)
885 {
886         MonoAssembly *ass;
887         GSList *tmp;
888
889         mono_domain_assemblies_lock (domain);
890         for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
891                 ass = tmp->data;
892                 if (strcmp (name, ass->aname.name) == 0) {
893                         mono_domain_assemblies_unlock (domain);
894                         return ass;
895                 }
896         }
897         mono_domain_assemblies_unlock (domain);
898
899         if (!(ass = mono_assembly_open (name, NULL)))
900                 return NULL;
901
902         return ass;
903 }
904
905 static void
906 dynamic_method_info_free (gpointer key, gpointer value, gpointer user_data)
907 {
908         MonoJitDynamicMethodInfo *di = value;
909         mono_code_manager_destroy (di->code_mp);
910         g_free (di);
911 }
912
913 static void
914 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
915 {
916         g_slist_free (value);
917 }
918
919 void
920 mono_domain_free (MonoDomain *domain, gboolean force)
921 {
922         GSList *tmp;
923         if ((domain == mono_root_domain) && !force) {
924                 g_warning ("cant unload root domain");
925                 return;
926         }
927
928         EnterCriticalSection (&appdomains_mutex);
929         appdomains_list [domain->domain_id] = NULL;
930         LeaveCriticalSection (&appdomains_mutex);
931
932         /* FIXME: free delegate_hash_table when it's used */
933         if (domain->search_path) {
934                 g_strfreev (domain->search_path);
935                 domain->search_path = NULL;
936         }
937         domain->create_proxy_for_type_method = NULL;
938         domain->private_invoke_method = NULL;
939         domain->default_context = NULL;
940         domain->out_of_memory_ex = NULL;
941         domain->null_reference_ex = NULL;
942         domain->stack_overflow_ex = NULL;
943         domain->entry_assembly = NULL;
944         g_free (domain->friendly_name);
945         domain->friendly_name = NULL;
946         for (tmp = domain->domain_assemblies; tmp; tmp = tmp->next) {
947                 MonoAssembly *ass = tmp->data;
948                 /*g_print ("Unloading domain %p, assembly %s, refcount: %d\n", domain, ass->aname.name, ass->ref_count);*/
949                 mono_assembly_close (ass);
950         }
951         g_slist_free (domain->domain_assemblies);
952         domain->domain_assemblies = NULL;
953
954         mono_g_hash_table_destroy (domain->env);
955         domain->env = NULL;
956         g_hash_table_destroy (domain->class_vtable_hash);
957         domain->class_vtable_hash = NULL;
958         mono_g_hash_table_destroy (domain->proxy_vtable_hash);
959         domain->proxy_vtable_hash = NULL;
960         mono_g_hash_table_destroy (domain->static_data_hash);
961         domain->static_data_hash = NULL;
962         g_hash_table_destroy (domain->jit_code_hash);
963         domain->jit_code_hash = NULL;
964         if (domain->dynamic_code_hash) {
965                 g_hash_table_foreach (domain->dynamic_code_hash, dynamic_method_info_free, NULL);
966                 g_hash_table_destroy (domain->dynamic_code_hash);
967                 domain->dynamic_code_hash = NULL;
968         }
969         mono_g_hash_table_destroy (domain->ldstr_table);
970         domain->ldstr_table = NULL;
971         mono_jit_info_table_free (domain->jit_info_table);
972         domain->jit_info_table = NULL;
973 #ifdef DEBUG_DOMAIN_UNLOAD
974         mono_mempool_invalidate (domain->mp);
975         mono_code_manager_invalidate (domain->code_mp);
976 #else
977         mono_mempool_destroy (domain->mp);
978         domain->mp = NULL;
979         mono_code_manager_destroy (domain->code_mp);
980         domain->code_mp = NULL;
981 #endif  
982         if (domain->jump_target_hash) {
983                 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
984                 g_hash_table_destroy (domain->jump_target_hash);
985                 domain->jump_target_hash = NULL;
986         }
987         if (domain->type_hash) {
988                 mono_g_hash_table_destroy (domain->type_hash);
989                 domain->type_hash = NULL;
990         }
991         if (domain->refobject_hash) {
992                 mono_g_hash_table_destroy (domain->refobject_hash);
993                 domain->refobject_hash = NULL;
994         }
995         g_hash_table_destroy (domain->class_init_trampoline_hash);
996         domain->class_init_trampoline_hash = NULL;
997         g_hash_table_destroy (domain->jump_trampoline_hash);
998         domain->jump_trampoline_hash = NULL;
999         g_hash_table_destroy (domain->finalizable_objects_hash);
1000         domain->finalizable_objects_hash = NULL;
1001         g_hash_table_destroy (domain->jit_trampoline_hash);
1002         domain->jit_trampoline_hash = NULL;
1003         g_hash_table_destroy (domain->delegate_trampoline_hash);
1004         domain->delegate_trampoline_hash = NULL;
1005         if (domain->special_static_fields) {
1006                 g_hash_table_destroy (domain->special_static_fields);
1007                 domain->special_static_fields = NULL;
1008         }
1009         DeleteCriticalSection (&domain->assemblies_lock);
1010         DeleteCriticalSection (&domain->lock);
1011         domain->setup = NULL;
1012
1013         /* FIXME: anything else required ? */
1014
1015         mono_gc_free_fixed (domain);
1016
1017         if ((domain == mono_root_domain))
1018                 mono_root_domain = NULL;
1019 }
1020
1021 /**
1022  * mono_domain_get_id:
1023  *
1024  * Returns: the a domain for a specific domain id.
1025  */
1026 MonoDomain * 
1027 mono_domain_get_by_id (gint32 domainid) 
1028 {
1029         MonoDomain * domain;
1030
1031         EnterCriticalSection (&appdomains_mutex);
1032         if (domainid < appdomain_list_size)
1033                 domain = appdomains_list [domainid];
1034         else
1035                 domain = NULL;
1036         LeaveCriticalSection (&appdomains_mutex);
1037
1038         return domain;
1039 }
1040
1041 gint32
1042 mono_domain_get_id (MonoDomain *domain)
1043 {
1044         return domain->domain_id;
1045 }
1046
1047 void 
1048 mono_context_set (MonoAppContext * new_context)
1049 {
1050         SET_APPCONTEXT (new_context);
1051 }
1052
1053 MonoAppContext * 
1054 mono_context_get (void)
1055 {
1056         return GET_APPCONTEXT ();
1057 }
1058
1059 MonoImage*
1060 mono_get_corlib (void)
1061 {
1062         return mono_defaults.corlib;
1063 }
1064
1065 MonoClass*
1066 mono_get_object_class (void)
1067 {
1068         return mono_defaults.object_class;
1069 }
1070
1071 MonoClass*
1072 mono_get_byte_class (void)
1073 {
1074         return mono_defaults.byte_class;
1075 }
1076
1077 MonoClass*
1078 mono_get_void_class (void)
1079 {
1080         return mono_defaults.void_class;
1081 }
1082
1083 MonoClass*
1084 mono_get_boolean_class (void)
1085 {
1086         return mono_defaults.boolean_class;
1087 }
1088
1089 MonoClass*
1090 mono_get_sbyte_class (void)
1091 {
1092         return mono_defaults.sbyte_class;
1093 }
1094
1095 MonoClass*
1096 mono_get_int16_class (void)
1097 {
1098         return mono_defaults.int16_class;
1099 }
1100
1101 MonoClass*
1102 mono_get_uint16_class (void)
1103 {
1104         return mono_defaults.uint16_class;
1105 }
1106
1107 MonoClass*
1108 mono_get_int32_class (void)
1109 {
1110         return mono_defaults.int32_class;
1111 }
1112
1113 MonoClass*
1114 mono_get_uint32_class (void)
1115 {
1116         return mono_defaults.uint32_class;
1117 }
1118
1119 MonoClass*
1120 mono_get_intptr_class (void)
1121 {
1122         return mono_defaults.int_class;
1123 }
1124
1125 MonoClass*
1126 mono_get_uintptr_class (void)
1127 {
1128         return mono_defaults.uint_class;
1129 }
1130
1131 MonoClass*
1132 mono_get_int64_class (void)
1133 {
1134         return mono_defaults.int64_class;
1135 }
1136
1137 MonoClass*
1138 mono_get_uint64_class (void)
1139 {
1140         return mono_defaults.uint64_class;
1141 }
1142
1143 MonoClass*
1144 mono_get_single_class (void)
1145 {
1146         return mono_defaults.single_class;
1147 }
1148
1149 MonoClass*
1150 mono_get_double_class (void)
1151 {
1152         return mono_defaults.double_class;
1153 }
1154
1155 MonoClass*
1156 mono_get_char_class (void)
1157 {
1158         return mono_defaults.char_class;
1159 }
1160
1161 MonoClass*
1162 mono_get_string_class (void)
1163 {
1164         return mono_defaults.string_class;
1165 }
1166
1167 MonoClass*
1168 mono_get_enum_class (void)
1169 {
1170         return mono_defaults.enum_class;
1171 }
1172
1173 MonoClass*
1174 mono_get_array_class (void)
1175 {
1176         return mono_defaults.array_class;
1177 }
1178
1179 MonoClass*
1180 mono_get_thread_class (void)
1181 {
1182         return mono_defaults.thread_class;
1183 }
1184
1185 MonoClass*
1186 mono_get_exception_class (void)
1187 {
1188         return mono_defaults.exception_class;
1189 }
1190
1191
1192 static char* get_attribute_value (const gchar **attribute_names, 
1193                                         const gchar **attribute_values, 
1194                                         const char *att_name)
1195 {
1196         int n;
1197         for (n=0; attribute_names[n] != NULL; n++) {
1198                 if (strcmp (attribute_names[n], att_name) == 0)
1199                         return g_strdup (attribute_values[n]);
1200         }
1201         return NULL;
1202 }
1203
1204 static void start_element (GMarkupParseContext *context, 
1205                            const gchar         *element_name,
1206                            const gchar        **attribute_names,
1207                            const gchar        **attribute_values,
1208                            gpointer             user_data,
1209                            GError             **error)
1210 {
1211         AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1212         
1213         if (strcmp (element_name, "configuration") == 0) {
1214                 app_config->configuration_count++;
1215                 return;
1216         }
1217         if (strcmp (element_name, "startup") == 0) {
1218                 app_config->startup_count++;
1219                 return;
1220         }
1221         
1222         if (app_config->configuration_count != 1 || app_config->startup_count != 1)
1223                 return;
1224         
1225         if (strcmp (element_name, "requiredRuntime") == 0) {
1226                 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
1227         } else if (strcmp (element_name, "supportedRuntime") == 0) {
1228                 char *version = get_attribute_value (attribute_names, attribute_values, "version");
1229                 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
1230         }
1231 }
1232
1233 static void end_element   (GMarkupParseContext *context,
1234                            const gchar         *element_name,
1235                            gpointer             user_data,
1236                            GError             **error)
1237 {
1238         AppConfigInfo* app_config = (AppConfigInfo*) user_data;
1239         
1240         if (strcmp (element_name, "configuration") == 0) {
1241                 app_config->configuration_count--;
1242         } else if (strcmp (element_name, "startup") == 0) {
1243                 app_config->startup_count--;
1244         }
1245 }
1246
1247 static const GMarkupParser 
1248 mono_parser = {
1249         start_element,
1250         end_element,
1251         NULL,
1252         NULL,
1253         NULL
1254 };
1255
1256 static AppConfigInfo *
1257 app_config_parse (const char *filename)
1258 {
1259         AppConfigInfo *app_config;
1260         GMarkupParseContext *context;
1261         char *text;
1262         gsize len;
1263         
1264         struct stat buf;
1265         if (stat (filename, &buf) != 0)
1266                 return NULL;
1267         
1268         app_config = g_new0 (AppConfigInfo, 1);
1269
1270         if (!g_file_get_contents (filename, &text, &len, NULL))
1271                 return NULL;
1272
1273         context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
1274         if (g_markup_parse_context_parse (context, text, len, NULL)) {
1275                 g_markup_parse_context_end_parse (context, NULL);
1276         }
1277         g_markup_parse_context_free (context);
1278         g_free (text);
1279         return app_config;
1280 }
1281
1282 static void 
1283 app_config_free (AppConfigInfo* app_config)
1284 {
1285         char *rt;
1286         GSList *list = app_config->supported_runtimes;
1287         while (list != NULL) {
1288                 rt = (char*)list->data;
1289                 g_free (rt);
1290                 list = g_slist_next (list);
1291         }
1292         g_slist_free (app_config->supported_runtimes);
1293         g_free (app_config->required_runtime);
1294         g_free (app_config);
1295 }
1296
1297
1298 static const MonoRuntimeInfo*
1299 get_runtime_by_version (const char *version)
1300 {
1301         int n;
1302         int max = G_N_ELEMENTS (supported_runtimes);
1303         
1304         for (n=0; n<max; n++) {
1305                 if (strcmp (version, supported_runtimes[n].runtime_version) == 0)
1306                         return &supported_runtimes[n];
1307         }
1308         return NULL;
1309 }
1310
1311 static void
1312 get_runtimes_from_exe (const char *exe_file, const MonoRuntimeInfo** runtimes)
1313 {
1314         AppConfigInfo* app_config;
1315         char *version;
1316         char *config_name;
1317         const MonoRuntimeInfo* runtime = NULL;
1318         MonoImage *image = NULL;
1319         
1320         config_name = g_strconcat (exe_file, ".config", NULL);
1321         app_config = app_config_parse (config_name);
1322         g_free (config_name);
1323         
1324         if (app_config != NULL) {
1325                 /* Check supportedRuntime elements, if none is supported, fail.
1326                  * If there are no such elements, look for a requiredRuntime element.
1327                  */
1328                 if (app_config->supported_runtimes != NULL) {
1329                         int n = 0;
1330                         GSList *list = app_config->supported_runtimes;
1331                         while (list != NULL) {
1332                                 version = (char*) list->data;
1333                                 runtime = get_runtime_by_version (version);
1334                                 if (runtime != NULL)
1335                                         runtimes [n++] = runtime;
1336                                 list = g_slist_next (list);
1337                         }
1338                         runtimes [n] = NULL;
1339                         app_config_free (app_config);
1340                         return;
1341                 }
1342                 
1343                 /* Check the requiredRuntime element. This is for 1.0 apps only. */
1344                 if (app_config->required_runtime != NULL) {
1345                         runtimes [0] = get_runtime_by_version (app_config->required_runtime);
1346                         runtimes [1] = NULL;
1347                         app_config_free (app_config);
1348                         return;
1349                 }
1350                 app_config_free (app_config);
1351         }
1352         
1353         /* Look for a runtime with the exact version */
1354         image = mono_image_open (exe_file, NULL);
1355         if (image == NULL) {
1356                 /* The image is wrong or the file was not found. In this case return
1357                  * a default runtime and leave to the initialization method the work of
1358                  * reporting the error.
1359                  */
1360                 runtimes [0] = get_runtime_by_version (DEFAULT_RUNTIME_VERSION);
1361                 runtimes [1] = NULL;
1362                 return;
1363         }
1364
1365         runtimes [0] = get_runtime_by_version (image->version);
1366         runtimes [1] = NULL;
1367 }
1368
1369
1370 /**
1371  * mono_get_runtime_info:
1372  *
1373  * Returns: the version of the current runtime instance.
1374  */
1375 const MonoRuntimeInfo*
1376 mono_get_runtime_info (void)
1377 {
1378         return current_runtime;
1379 }
1380
1381 gchar *
1382 mono_debugger_check_runtime_version (const char *filename)
1383 {
1384         const MonoRuntimeInfo* runtimes [G_N_ELEMENTS (supported_runtimes) + 1];
1385         const MonoRuntimeInfo *rinfo;
1386
1387         get_runtimes_from_exe (filename, runtimes);
1388         rinfo = runtimes [0];
1389
1390         if (!rinfo)
1391                 return g_strdup_printf ("Cannot get runtime version from assembly `%s'", filename);
1392
1393         if (rinfo != current_runtime)
1394                 return g_strdup_printf ("The Mono Debugger is currently using the `%s' runtime, but "
1395                                         "the assembly `%s' requires version `%s'", current_runtime->runtime_version,
1396                                         filename, rinfo->runtime_version);
1397
1398         return NULL;
1399 }