2009-01-14 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / image.c
index 81cf2bea7ce8af68b01e1b924759604a09737376..bcbfa92294d5d358d9e5aa888377f638f2210030 100644 (file)
@@ -17,7 +17,6 @@
 #include <string.h>
 #include "image.h"
 #include "cil-coff.h"
-#include "rawbuffer.h"
 #include "mono-endian.h"
 #include "tabledefs.h"
 #include "tokentype.h"
@@ -29,6 +28,7 @@
 #include <mono/io-layer/io-layer.h>
 #include <mono/utils/mono-logger.h>
 #include <mono/utils/mono-path.h>
+#include <mono/utils/mono-mmap.h>
 #include <mono/utils/mono-io-portability.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/assembly.h>
@@ -460,7 +460,7 @@ mono_image_check_for_module_cctor (MonoImage *image)
        MonoTableInfo *t, *mt;
        t = &image->tables [MONO_TABLE_TYPEDEF];
        mt = &image->tables [MONO_TABLE_METHOD];
-       if (mono_get_runtime_info ()->framework_version [0] == '1') {
+       if (mono_framework_version () == 1) {
                image->checked_module_cctor = TRUE;
                return;
        }
@@ -607,35 +607,7 @@ mono_image_init (MonoImage *image)
                                       class_next_value);
        image->field_cache = g_hash_table_new (NULL, NULL);
 
-       image->delegate_begin_invoke_cache = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       image->delegate_end_invoke_cache = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       image->delegate_invoke_cache = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       image->runtime_invoke_cache  = 
-               g_hash_table_new ((GHashFunc)mono_signature_hash, 
-                                 (GCompareFunc)mono_metadata_signature_equal);
-       
-       image->runtime_invoke_direct_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->managed_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
        image->native_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->remoting_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->cominterop_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->cominterop_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->synchronized_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->unbox_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-
-       image->ldfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->ldflda_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->stfld_wrapper_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->castclass_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->proxy_isinst_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-       image->thunk_invoke_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
        image->typespec_cache = g_hash_table_new (NULL, NULL);
        image->memberref_signatures = g_hash_table_new (NULL, NULL);
@@ -932,7 +904,14 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
        image = g_new0 (MonoImage, 1);
        image->raw_buffer_used = TRUE;
        image->raw_data_len = stat_buf.st_size;
-       image->raw_data = mono_raw_buffer_load (fileno (filed), FALSE, 0, stat_buf.st_size);
+       image->raw_data = mono_file_map (stat_buf.st_size, MONO_MMAP_READ|MONO_MMAP_PRIVATE, fileno (filed), 0, &image->raw_data_handle);
+       if (!image->raw_data) {
+               fclose (filed);
+               g_free (image);
+               if (status)
+                       *status = MONO_IMAGE_IMAGE_INVALID;
+               return NULL;
+       }
        iinfo = g_new0 (MonoCLIImageInfo, 1);
        image->image_info = iinfo;
        image->name = mono_path_resolve_symlinks (fname);
@@ -1340,6 +1319,13 @@ mono_dynamic_stream_reset (MonoDynamicStream* stream)
        }
 }
 
+static inline void
+free_hash (GHashTable *hash)
+{
+       if (hash)
+               g_hash_table_destroy (hash);
+}
+
 /**
  * mono_image_close:
  * @image: The image file we wish to close
@@ -1414,7 +1400,7 @@ mono_image_close (MonoImage *image)
 
        if (image->raw_buffer_used) {
                if (image->raw_data != NULL)
-                       mono_raw_buffer_free (image->raw_data);
+                       mono_file_unmap (image->raw_data, image->raw_data_handle);
        }
        
        if (image->raw_data_allocated) {
@@ -1459,31 +1445,31 @@ mono_image_close (MonoImage *image)
                g_hash_table_foreach (image->name_cache, free_hash_table, NULL);
                g_hash_table_destroy (image->name_cache);
        }
-       g_hash_table_destroy (image->native_wrapper_cache);
-       g_hash_table_destroy (image->managed_wrapper_cache);
-       g_hash_table_destroy (image->delegate_begin_invoke_cache);
-       g_hash_table_destroy (image->delegate_end_invoke_cache);
-       g_hash_table_destroy (image->delegate_invoke_cache);
-       if (image->delegate_abstract_invoke_cache)
-               g_hash_table_destroy (image->delegate_abstract_invoke_cache);
-       g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
-       g_hash_table_destroy (image->remoting_invoke_cache);
-       g_hash_table_destroy (image->runtime_invoke_cache);
-       g_hash_table_destroy (image->runtime_invoke_direct_cache);
-       g_hash_table_destroy (image->synchronized_cache);
-       g_hash_table_destroy (image->unbox_wrapper_cache);
-       g_hash_table_destroy (image->cominterop_invoke_cache);
-       g_hash_table_destroy (image->cominterop_wrapper_cache);
-       g_hash_table_destroy (image->typespec_cache);
-       g_hash_table_destroy (image->ldfld_wrapper_cache);
-       g_hash_table_destroy (image->ldflda_wrapper_cache);
-       g_hash_table_destroy (image->stfld_wrapper_cache);
-       g_hash_table_destroy (image->isinst_cache);
-       g_hash_table_destroy (image->castclass_cache);
-       g_hash_table_destroy (image->proxy_isinst_cache);
-       g_hash_table_destroy (image->thunk_invoke_cache);
-       if (image->static_rgctx_invoke_cache)
-               g_hash_table_destroy (image->static_rgctx_invoke_cache);
+
+       free_hash (image->native_wrapper_cache);
+       free_hash (image->managed_wrapper_cache);
+       free_hash (image->delegate_begin_invoke_cache);
+       free_hash (image->delegate_end_invoke_cache);
+       free_hash (image->delegate_invoke_cache);
+       free_hash (image->delegate_abstract_invoke_cache);
+       if (image->remoting_invoke_cache)
+               g_hash_table_foreach (image->remoting_invoke_cache, free_remoting_wrappers, NULL);
+       free_hash (image->remoting_invoke_cache);
+       free_hash (image->runtime_invoke_cache);
+       free_hash (image->runtime_invoke_direct_cache);
+       free_hash (image->synchronized_cache);
+       free_hash (image->unbox_wrapper_cache);
+       free_hash (image->cominterop_invoke_cache);
+       free_hash (image->cominterop_wrapper_cache);
+       free_hash (image->typespec_cache);
+       free_hash (image->ldfld_wrapper_cache);
+       free_hash (image->ldflda_wrapper_cache);
+       free_hash (image->stfld_wrapper_cache);
+       free_hash (image->isinst_cache);
+       free_hash (image->castclass_cache);
+       free_hash (image->proxy_isinst_cache);
+       free_hash (image->thunk_invoke_cache);
+       free_hash (image->static_rgctx_invoke_cache);
 
        /* The ownership of signatures is not well defined */
        //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
@@ -1525,6 +1511,7 @@ mono_image_close (MonoImage *image)
                g_free (image->modules_loaded);
        if (image->references)
                g_free (image->references);
+       mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
        /*g_print ("destroy image %p (dynamic: %d)\n", image, image->dynamic);*/
        if (!image->dynamic) {
                if (debug_assembly_unload)
@@ -2004,3 +1991,25 @@ mono_image_has_authenticode_entry (MonoImage *image)
        // the Authenticode "pre" (non ASN.1) header is 8 bytes long
        return ((de->rva != 0) && (de->size > 8));
 }
+
+gpointer
+mono_image_alloc (MonoImage *image, guint size)
+{
+       mono_perfcounters->loader_bytes += size;
+       return mono_mempool_alloc (image->mempool, size);
+}
+
+gpointer
+mono_image_alloc0 (MonoImage *image, guint size)
+{
+       mono_perfcounters->loader_bytes += size;
+       return mono_mempool_alloc0 (image->mempool, size);
+}
+
+char*
+mono_image_strdup (MonoImage *image, const char *s)
+{
+       mono_perfcounters->loader_bytes += strlen (s);
+       return mono_mempool_strdup (image->mempool, s);
+}
+