This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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
16 #include <mono/os/gc_wrapper.h>
17
18 #include <mono/metadata/object.h>
19 #include <mono/metadata/domain-internals.h>
20 #include <mono/metadata/class-internals.h>
21 #include <mono/metadata/assembly.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/metadata/cil-coff.h>
24 #include <mono/metadata/rawbuffer.h>
25
26 /* #define DEBUG_DOMAIN_UNLOAD */
27
28 static guint32 appdomain_thread_id = -1;
29 static guint32 context_thread_id = -1;
30
31 static gint32 appdomain_id_counter = 0;
32
33 static MonoGHashTable * appdomains_list = NULL;
34
35 static CRITICAL_SECTION appdomains_mutex;
36
37 static MonoDomain *mono_root_domain = NULL;
38
39 static MonoJitInfoTable *
40 mono_jit_info_table_new (void)
41 {
42         return g_array_new (FALSE, FALSE, sizeof (gpointer));
43 }
44
45 static void
46 mono_jit_info_table_free (MonoJitInfoTable *table)
47 {
48         g_array_free (table, TRUE);
49 }
50
51 static int
52 mono_jit_info_table_index (MonoJitInfoTable *table, char *addr)
53 {
54         int left = 0, right = table->len;
55
56         while (left < right) {
57                 int pos = (left + right) / 2;
58                 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
59                 char *start = ji->code_start;
60                 char *end = start + ji->code_size;
61
62                 if (addr < start)
63                         right = pos;
64                 else if (addr >= end) 
65                         left = pos + 1;
66                 else
67                         return pos;
68         }
69
70         return left;
71 }
72
73 MonoJitInfo *
74 mono_jit_info_table_find (MonoDomain *domain, char *addr)
75 {
76         MonoJitInfoTable *table = domain->jit_info_table;
77         int left = 0, right;
78
79         mono_domain_lock (domain);
80
81         right = table->len;
82         while (left < right) {
83                 int pos = (left + right) / 2;
84                 MonoJitInfo *ji = g_array_index (table, gpointer, pos);
85                 char *start = ji->code_start;
86                 char *end = start + ji->code_size;
87
88                 if (addr < start)
89                         right = pos;
90                 else if (addr >= end) 
91                         left = pos + 1;
92                 else {
93                         mono_domain_unlock (domain);
94                         return ji;
95                 }
96         }
97         mono_domain_unlock (domain);
98
99         /* maybe it is shared code, so we also search in the root domain */
100         if (domain != mono_root_domain)
101                 return mono_jit_info_table_find (mono_root_domain, addr);
102
103         return NULL;
104 }
105
106 void
107 mono_jit_info_table_add (MonoDomain *domain, MonoJitInfo *ji)
108 {
109         MonoJitInfoTable *table = domain->jit_info_table;
110         gpointer start = ji->code_start;
111         int pos;
112
113         mono_domain_lock (domain);
114         pos = mono_jit_info_table_index (table, start);
115
116         g_array_insert_val (table, pos, ji);
117         mono_domain_unlock (domain);
118 }
119
120 static int
121 ldstr_hash (const char* str)
122 {
123         guint len, h;
124         const char *end;
125         len = mono_metadata_decode_blob_size (str, &str) - 1;
126         end = str + len;
127         /* if len == 0 *str will point to the mark byte */
128         h = len? *str: 0;
129         /*
130          * FIXME: The distribution may not be so nice with lots of
131          * null chars in the string.
132          */
133         for (str += 1; str < end; str++)
134                 h = (h << 5) - h + *str;
135         return h;
136 }
137
138 static gboolean
139 ldstr_equal (const char *str1, const char *str2) {
140         int len, len2;
141         if (str1 == str2)
142                 return TRUE;
143         len = mono_metadata_decode_blob_size (str1, NULL) - 1;
144         len2 = mono_metadata_decode_blob_size (str2, NULL) - 1;
145         if (len != len2)
146                 return 0;
147         return memcmp (str1, str2, len) == 0;
148 }
149
150 static gboolean
151 mono_string_equal (MonoString *s1, MonoString *s2)
152 {
153         int l1 = mono_string_length (s1);
154         int l2 = mono_string_length (s2);
155
156         if (l1 != l2)
157                 return FALSE;
158
159         return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0; 
160 }
161
162 static guint
163 mono_string_hash (MonoString *s)
164 {
165         const guint16 *p = mono_string_chars (s);
166         int i, len = mono_string_length (s);
167         guint h = 0;
168
169         for (i = 0; i < len; i++) {
170                 h = (h << 5) - h + *p;
171                 p++;
172         }
173
174         return h;       
175 }
176
177 #if HAVE_BOEHM_GC
178 static void
179 domain_finalizer (void *obj, void *data) {
180         g_print ("domain finalized\n");
181 }
182 #endif
183
184 MonoDomain *
185 mono_domain_create (void)
186 {
187         MonoDomain *domain;
188
189 #if HAVE_BOEHM_GC
190         domain = GC_MALLOC (sizeof (MonoDomain));
191         GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
192 #else
193         domain = g_new0 (MonoDomain, 1);
194 #endif
195         domain->domain = NULL;
196         domain->setup = NULL;
197         domain->friendly_name = NULL;
198         domain->search_path = NULL;
199
200         domain->mp = mono_mempool_new ();
201         domain->code_mp = mono_code_manager_new ();
202         domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
203         domain->assemblies = g_hash_table_new (g_str_hash, g_str_equal);
204         domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
205         domain->proxy_vtable_hash = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
206         domain->static_data_hash = mono_g_hash_table_new (NULL, NULL);
207         domain->jit_code_hash = g_hash_table_new (NULL, NULL);
208         domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
209         domain->jit_info_table = mono_jit_info_table_new ();
210         domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
211         domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
212         domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
213
214         InitializeCriticalSection (&domain->lock);
215
216         EnterCriticalSection (&appdomains_mutex);
217         mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
218         LeaveCriticalSection (&appdomains_mutex);
219
220         return domain;
221 }
222
223 /**
224  * mono_init:
225  * 
226  * Creates the initial application domain and initializes the mono_defaults
227  * structure.
228  * This function is guaranteed to not run any IL code.
229  *
230  * Returns: the initial domain.
231  */
232 MonoDomain *
233 mono_init (const char *filename)
234 {
235         static MonoDomain *domain = NULL;
236         MonoAssembly *ass;
237         MonoImageOpenStatus status = MONO_IMAGE_OK;
238         MonoAssemblyName corlib_aname;
239
240         if (domain)
241                 g_assert_not_reached ();
242
243         appdomain_thread_id = TlsAlloc ();
244         context_thread_id = TlsAlloc ();
245
246         InitializeCriticalSection (&appdomains_mutex);
247
248         mono_metadata_init ();
249         mono_raw_buffer_init ();
250         mono_images_init ();
251         mono_assemblies_init ();
252         mono_loader_init ();
253
254         /* FIXME: When should we release this memory? */
255         appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
256
257         domain = mono_domain_create ();
258         mono_root_domain = domain;
259
260         TlsSetValue (appdomain_thread_id, domain);
261
262         /* find the corlib */
263         corlib_aname.name = "mscorlib";
264         ass = mono_assembly_load (&corlib_aname, NULL, &status);
265         if ((status != MONO_IMAGE_OK) || (ass == NULL)) {
266                 switch (status){
267                 case MONO_IMAGE_ERROR_ERRNO:
268                         g_print ("The assembly mscorlib.dll was not found or could not be loaded.\n");
269                         g_print ("It should have been installed in the `%s' directory.\n",
270                                  mono_assembly_getrootdir ());
271                         break;
272                 case MONO_IMAGE_IMAGE_INVALID:
273                         g_print ("The file %s/mscorlib.dll is an invalid CIL image\n",
274                                  mono_assembly_getrootdir ());
275                         break;
276                 case MONO_IMAGE_MISSING_ASSEMBLYREF:
277                         g_print ("Missing assembly reference in %s/mscorlib.dll\n",
278                                  mono_assembly_getrootdir ());
279                         break;
280                 case MONO_IMAGE_OK:
281                         /* to suppress compiler warning */
282                         break;
283                 }
284                 
285                 exit (1);
286         }
287         mono_defaults.corlib = mono_assembly_get_image (ass);
288
289         mono_defaults.object_class = mono_class_from_name (
290                 mono_defaults.corlib, "System", "Object");
291         g_assert (mono_defaults.object_class != 0);
292
293         mono_defaults.void_class = mono_class_from_name (
294                 mono_defaults.corlib, "System", "Void");
295         g_assert (mono_defaults.void_class != 0);
296
297         mono_defaults.boolean_class = mono_class_from_name (
298                 mono_defaults.corlib, "System", "Boolean");
299         g_assert (mono_defaults.boolean_class != 0);
300
301         mono_defaults.byte_class = mono_class_from_name (
302                 mono_defaults.corlib, "System", "Byte");
303         g_assert (mono_defaults.byte_class != 0);
304
305         mono_defaults.sbyte_class = mono_class_from_name (
306                 mono_defaults.corlib, "System", "SByte");
307         g_assert (mono_defaults.sbyte_class != 0);
308
309         mono_defaults.int16_class = mono_class_from_name (
310                 mono_defaults.corlib, "System", "Int16");
311         g_assert (mono_defaults.int16_class != 0);
312
313         mono_defaults.uint16_class = mono_class_from_name (
314                 mono_defaults.corlib, "System", "UInt16");
315         g_assert (mono_defaults.uint16_class != 0);
316
317         mono_defaults.int32_class = mono_class_from_name (
318                 mono_defaults.corlib, "System", "Int32");
319         g_assert (mono_defaults.int32_class != 0);
320
321         mono_defaults.uint32_class = mono_class_from_name (
322                 mono_defaults.corlib, "System", "UInt32");
323         g_assert (mono_defaults.uint32_class != 0);
324
325         mono_defaults.uint_class = mono_class_from_name (
326                 mono_defaults.corlib, "System", "UIntPtr");
327         g_assert (mono_defaults.uint_class != 0);
328
329         mono_defaults.int_class = mono_class_from_name (
330                 mono_defaults.corlib, "System", "IntPtr");
331         g_assert (mono_defaults.int_class != 0);
332
333         mono_defaults.int64_class = mono_class_from_name (
334                 mono_defaults.corlib, "System", "Int64");
335         g_assert (mono_defaults.int64_class != 0);
336
337         mono_defaults.uint64_class = mono_class_from_name (
338                 mono_defaults.corlib, "System", "UInt64");
339         g_assert (mono_defaults.uint64_class != 0);
340
341         mono_defaults.single_class = mono_class_from_name (
342                 mono_defaults.corlib, "System", "Single");
343         g_assert (mono_defaults.single_class != 0);
344
345         mono_defaults.double_class = mono_class_from_name (
346                 mono_defaults.corlib, "System", "Double");
347         g_assert (mono_defaults.double_class != 0);
348
349         mono_defaults.char_class = mono_class_from_name (
350                 mono_defaults.corlib, "System", "Char");
351         g_assert (mono_defaults.char_class != 0);
352
353         mono_defaults.string_class = mono_class_from_name (
354                 mono_defaults.corlib, "System", "String");
355         g_assert (mono_defaults.string_class != 0);
356
357         mono_defaults.enum_class = mono_class_from_name (
358                 mono_defaults.corlib, "System", "Enum");
359         g_assert (mono_defaults.enum_class != 0);
360
361         mono_defaults.array_class = mono_class_from_name (
362                 mono_defaults.corlib, "System", "Array");
363         g_assert (mono_defaults.array_class != 0);
364
365         mono_defaults.delegate_class = mono_class_from_name (
366                 mono_defaults.corlib, "System", "Delegate");
367         g_assert (mono_defaults.delegate_class != 0 );
368
369         mono_defaults.multicastdelegate_class = mono_class_from_name (
370                 mono_defaults.corlib, "System", "MulticastDelegate");
371         g_assert (mono_defaults.multicastdelegate_class != 0 );
372
373         mono_defaults.asyncresult_class = mono_class_from_name (
374                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", 
375                 "AsyncResult");
376         g_assert (mono_defaults.asyncresult_class != 0 );
377
378         mono_defaults.waithandle_class = mono_class_from_name (
379                 mono_defaults.corlib, "System.Threading", "WaitHandle");
380         g_assert (mono_defaults.waithandle_class != 0 );
381
382         mono_defaults.typehandle_class = mono_class_from_name (
383                 mono_defaults.corlib, "System", "RuntimeTypeHandle");
384         g_assert (mono_defaults.typehandle_class != 0);
385
386         mono_defaults.methodhandle_class = mono_class_from_name (
387                 mono_defaults.corlib, "System", "RuntimeMethodHandle");
388         g_assert (mono_defaults.methodhandle_class != 0);
389
390         mono_defaults.fieldhandle_class = mono_class_from_name (
391                 mono_defaults.corlib, "System", "RuntimeFieldHandle");
392         g_assert (mono_defaults.fieldhandle_class != 0);
393
394         mono_defaults.monotype_class = mono_class_from_name (
395                 mono_defaults.corlib, "System", "MonoType");
396         g_assert (mono_defaults.monotype_class != 0);
397
398         mono_defaults.exception_class = mono_class_from_name (
399                 mono_defaults.corlib, "System", "Exception");
400         g_assert (mono_defaults.exception_class != 0);
401
402         mono_defaults.threadabortexception_class = mono_class_from_name (
403                 mono_defaults.corlib, "System.Threading", "ThreadAbortException");
404         g_assert (mono_defaults.threadabortexception_class != 0);
405
406         mono_defaults.thread_class = mono_class_from_name (
407                 mono_defaults.corlib, "System.Threading", "Thread");
408         g_assert (mono_defaults.thread_class != 0);
409
410         mono_defaults.appdomain_class = mono_class_from_name (
411                 mono_defaults.corlib, "System", "AppDomain");
412         g_assert (mono_defaults.appdomain_class != 0);
413
414         mono_defaults.transparent_proxy_class = mono_class_from_name (
415                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "TransparentProxy");
416         g_assert (mono_defaults.transparent_proxy_class != 0);
417
418         mono_defaults.real_proxy_class = mono_class_from_name (
419                 mono_defaults.corlib, "System.Runtime.Remoting.Proxies", "RealProxy");
420         g_assert (mono_defaults.real_proxy_class != 0);
421
422         mono_defaults.mono_method_message_class = mono_class_from_name (
423                 mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
424         g_assert (mono_defaults.mono_method_message_class != 0);
425
426         mono_defaults.field_info_class = mono_class_from_name (
427                 mono_defaults.corlib, "System.Reflection", "FieldInfo");
428         g_assert (mono_defaults.field_info_class != 0);
429
430         mono_defaults.method_info_class = mono_class_from_name (
431                 mono_defaults.corlib, "System.Reflection", "MethodInfo");
432         g_assert (mono_defaults.method_info_class != 0);
433
434         mono_defaults.stringbuilder_class = mono_class_from_name (
435                 mono_defaults.corlib, "System.Text", "StringBuilder");
436         g_assert (mono_defaults.stringbuilder_class != 0);
437
438         mono_defaults.math_class = mono_class_from_name (
439                 mono_defaults.corlib, "System", "Math");
440         g_assert (mono_defaults.math_class != 0);
441
442         mono_defaults.stack_frame_class = mono_class_from_name (
443                 mono_defaults.corlib, "System.Diagnostics", "StackFrame");
444         g_assert (mono_defaults.stack_frame_class != 0);
445
446         mono_defaults.stack_trace_class = mono_class_from_name (
447                 mono_defaults.corlib, "System.Diagnostics", "StackTrace");
448         g_assert (mono_defaults.stack_trace_class != 0);
449
450         mono_defaults.marshal_class = mono_class_from_name (
451                 mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
452         g_assert (mono_defaults.marshal_class != 0);
453
454         mono_defaults.iserializeable_class = mono_class_from_name (
455                 mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
456         g_assert (mono_defaults.iserializeable_class != 0);
457
458         mono_defaults.serializationinfo_class = mono_class_from_name (
459                 mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
460         g_assert (mono_defaults.serializationinfo_class != 0);
461
462         mono_defaults.streamingcontext_class = mono_class_from_name (
463                 mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
464         g_assert (mono_defaults.streamingcontext_class != 0);
465
466         mono_defaults.typed_reference_class =  mono_class_from_name (
467                 mono_defaults.corlib, "System", "TypedReference");
468         g_assert (mono_defaults.typed_reference_class != 0);
469
470         mono_defaults.argumenthandle_class =  mono_class_from_name (
471                 mono_defaults.corlib, "System", "RuntimeArgumentHandle");
472         g_assert (mono_defaults.argumenthandle_class != 0);
473
474         mono_defaults.marshalbyrefobject_class =  mono_class_from_name (
475                 mono_defaults.corlib, "System", "MarshalByRefObject");
476         g_assert (mono_defaults.marshalbyrefobject_class != 0);
477
478         mono_defaults.monitor_class =  mono_class_from_name (
479                 mono_defaults.corlib, "System.Threading", "Monitor");
480         g_assert (mono_defaults.monitor_class != 0);
481
482         mono_defaults.iremotingtypeinfo_class = mono_class_from_name (
483                 mono_defaults.corlib, "System.Runtime.Remoting", "IRemotingTypeInfo");
484         g_assert (mono_defaults.iremotingtypeinfo_class != 0);
485
486         domain->friendly_name = g_path_get_basename (filename);
487
488         return domain;
489 }
490
491 MonoDomain*
492 mono_get_root_domain (void)
493 {
494         return mono_root_domain;
495 }
496
497 /**
498  * mono_domain_get:
499  *
500  * Returns the current domain.
501  */
502 inline MonoDomain *
503 mono_domain_get ()
504 {
505         return ((MonoDomain *)TlsGetValue (appdomain_thread_id));
506 }
507
508 /**
509  * mono_domain_set_internal:
510  * @domain: the new domain
511  *
512  * Sets the current domain to @domain.
513  */
514 inline void
515 mono_domain_set_internal (MonoDomain *domain)
516 {
517         TlsSetValue (appdomain_thread_id, domain);
518         TlsSetValue (context_thread_id, domain->default_context);
519 }
520
521 typedef struct {
522         MonoDomainFunc func;
523         gpointer user_data;
524 } DomainInfo;
525
526 static void
527 copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
528 {
529         MonoGHashTable *dest = (MonoGHashTable*)user_data;
530
531         mono_g_hash_table_insert (dest, key, data);
532 }
533
534 static void
535 foreach_domain (gpointer key, gpointer data, gpointer user_data)
536 {
537         DomainInfo *dom_info = user_data;
538
539         dom_info->func ((MonoDomain*)data, dom_info->user_data);
540 }
541
542 void
543 mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
544 {
545         DomainInfo dom_info;
546         MonoGHashTable *copy;
547
548         /*
549          * Create a copy of the hashtable to avoid calling the user callback
550          * inside the lock because that could lead to deadlocks.
551          * We can do this because this function is not perf. critical.
552          */
553         copy = mono_g_hash_table_new (NULL, NULL);
554         EnterCriticalSection (&appdomains_mutex);
555         mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
556         LeaveCriticalSection (&appdomains_mutex);
557
558         dom_info.func = func;
559         dom_info.user_data = user_data;
560         mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
561
562         mono_g_hash_table_destroy (copy);
563 }
564
565 /**
566  * mono_domain_assembly_open:
567  * @domain: the application domain
568  * @name: file name of the assembly
569  *
570  * fixme: maybe we should integrate this with mono_assembly_open ??
571  */
572 MonoAssembly *
573 mono_domain_assembly_open (MonoDomain *domain, const char *name)
574 {
575         MonoAssembly *ass;
576
577         mono_domain_lock (domain);
578         if ((ass = g_hash_table_lookup (domain->assemblies, name))) {
579                 mono_domain_unlock (domain);
580                 return ass;
581         }
582         mono_domain_unlock (domain);
583
584         if (!(ass = mono_assembly_open (name, NULL)))
585                 return NULL;
586
587         return ass;
588 }
589
590 static void
591 remove_assembly (gpointer key, gpointer value, gpointer user_data)
592 {
593         mono_assembly_close ((MonoAssembly *)value);
594 }
595
596 static void
597 delete_jump_list (gpointer key, gpointer value, gpointer user_data)
598 {
599         g_slist_free (value);
600 }
601
602 void
603 mono_domain_free (MonoDomain *domain, gboolean force)
604 {
605         if ((domain == mono_root_domain) && !force) {
606                 g_warning ("cant unload root domain");
607                 return;
608         }
609
610         EnterCriticalSection (&appdomains_mutex);
611         mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
612         LeaveCriticalSection (&appdomains_mutex);
613         
614         g_free (domain->friendly_name);
615         g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
616
617         mono_g_hash_table_destroy (domain->env);
618         g_hash_table_destroy (domain->assemblies);
619         mono_g_hash_table_destroy (domain->class_vtable_hash);
620         mono_g_hash_table_destroy (domain->proxy_vtable_hash);
621         mono_g_hash_table_destroy (domain->static_data_hash);
622         g_hash_table_destroy (domain->jit_code_hash);
623         mono_g_hash_table_destroy (domain->ldstr_table);
624         mono_jit_info_table_free (domain->jit_info_table);
625 #ifdef DEBUG_DOMAIN_UNLOAD
626         mono_mempool_invalidate (domain->mp);
627         mono_code_manager_invalidate (domain->code_mp);
628 #else
629         mono_mempool_destroy (domain->mp);
630         mono_code_manager_destroy (domain->code_mp);
631 #endif  
632         if (domain->jump_target_hash) {
633                 g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
634                 g_hash_table_destroy (domain->jump_target_hash);
635         }
636         mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
637         g_hash_table_destroy (domain->finalizable_objects_hash);
638         if (domain->special_static_fields)
639                 g_hash_table_destroy (domain->special_static_fields);
640         DeleteCriticalSection (&domain->lock);
641         domain->setup = NULL;
642
643         /* FIXME: anything else required ? */
644
645 #if HAVE_BOEHM_GC
646 #else
647         g_free (domain);
648 #endif
649
650         if ((domain == mono_root_domain))
651                 mono_root_domain = NULL;
652 }
653
654 /**
655  * mono_domain_get_id:
656  *
657  * Returns the a domain for a specific domain id.
658  */
659 MonoDomain * 
660 mono_domain_get_by_id (gint32 domainid) 
661 {
662         MonoDomain * domain;
663
664         EnterCriticalSection (&appdomains_mutex);
665         domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
666         LeaveCriticalSection (&appdomains_mutex);
667
668         return domain;
669 }
670
671 gint32
672 mono_domain_get_id (MonoDomain *domain)
673 {
674         return domain->domain_id;
675 }
676
677 void 
678 mono_context_set (MonoAppContext * new_context)
679 {
680         TlsSetValue (context_thread_id, new_context);
681 }
682
683 MonoAppContext * 
684 mono_context_get (void)
685 {
686         return ((MonoAppContext *)TlsGetValue (context_thread_id));
687 }
688
689 MonoImage*
690 mono_get_corlib (void)
691 {
692         return mono_defaults.corlib;
693 }
694
695 MonoClass*
696 mono_get_object_class (void)
697 {
698         return mono_defaults.object_class;
699 }
700
701 MonoClass*
702 mono_get_byte_class (void)
703 {
704         return mono_defaults.byte_class;
705 }
706
707 MonoClass*
708 mono_get_void_class (void)
709 {
710         return mono_defaults.void_class;
711 }
712
713 MonoClass*
714 mono_get_boolean_class (void)
715 {
716         return mono_defaults.boolean_class;
717 }
718
719 MonoClass*
720 mono_get_sbyte_class (void)
721 {
722         return mono_defaults.sbyte_class;
723 }
724
725 MonoClass*
726 mono_get_int16_class (void)
727 {
728         return mono_defaults.int16_class;
729 }
730
731 MonoClass*
732 mono_get_uint16_class (void)
733 {
734         return mono_defaults.uint16_class;
735 }
736
737 MonoClass*
738 mono_get_int32_class (void)
739 {
740         return mono_defaults.int32_class;
741 }
742
743 MonoClass*
744 mono_get_uint32_class (void)
745 {
746         return mono_defaults.uint32_class;
747 }
748
749 MonoClass*
750 mono_get_intptr_class (void)
751 {
752         return mono_defaults.int_class;
753 }
754
755 MonoClass*
756 mono_get_uintptr_class (void)
757 {
758         return mono_defaults.uint_class;
759 }
760
761 MonoClass*
762 mono_get_int64_class (void)
763 {
764         return mono_defaults.int64_class;
765 }
766
767 MonoClass*
768 mono_get_uint64_class (void)
769 {
770         return mono_defaults.uint64_class;
771 }
772
773 MonoClass*
774 mono_get_single_class (void)
775 {
776         return mono_defaults.single_class;
777 }
778
779 MonoClass*
780 mono_get_double_class (void)
781 {
782         return mono_defaults.double_class;
783 }
784
785 MonoClass*
786 mono_get_char_class (void)
787 {
788         return mono_defaults.char_class;
789 }
790
791 MonoClass*
792 mono_get_string_class (void)
793 {
794         return mono_defaults.string_class;
795 }
796
797 MonoClass*
798 mono_get_enum_class (void)
799 {
800         return mono_defaults.enum_class;
801 }
802
803 MonoClass*
804 mono_get_array_class (void)
805 {
806         return mono_defaults.array_class;
807 }
808
809 MonoClass*
810 mono_get_thread_class (void)
811 {
812         return mono_defaults.thread_class;
813 }
814
815 MonoClass*
816 mono_get_exception_class (void)
817 {
818         return mono_defaults.exception_class;
819 }
820