Merge pull request #601 from knocte/sock_improvements
[mono.git] / mono / metadata / image.c
index 5d098a03b04c4cca256cbeaf9e50a328d2d1991f..5847544f469cf97e71c666717fee74bb506acde1 100644 (file)
@@ -31,6 +31,7 @@
 #include <mono/utils/mono-path.h>
 #include <mono/utils/mono-mmap.h>
 #include <mono/utils/mono-io-portability.h>
+#include <mono/utils/atomic.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/object-internals.h>
@@ -121,7 +122,7 @@ mono_cli_rva_image_map (MonoImage *image, guint32 addr)
        for (i = 0; i < top; i++){
                if ((addr >= tables->st_virtual_address) &&
                    (addr < tables->st_virtual_address + tables->st_raw_data_size)){
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
                        if (image->is_module_handle)
                                return addr;
 #endif
@@ -158,7 +159,7 @@ mono_image_rva_map (MonoImage *image, guint32 addr)
                                if (!mono_image_ensure_section_idx (image, i))
                                        return NULL;
                        }
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
                        if (image->is_module_handle)
                                return image->raw_data + addr;
 #endif
@@ -239,7 +240,7 @@ mono_image_ensure_section_idx (MonoImage *image, int section)
 
        if (sect->st_raw_data_ptr + sect->st_raw_data_size > image->raw_data_len)
                return FALSE;
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        if (image->is_module_handle)
                iinfo->cli_sections [section] = image->raw_data + sect->st_virtual_address;
        else
@@ -628,7 +629,7 @@ mono_image_load_module (MonoImage *image, int idx)
                        if (image->modules [idx - 1]) {
                                mono_image_addref (image->modules [idx - 1]);
                                image->modules [idx - 1]->assembly = image->assembly;
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
                                if (image->modules [idx - 1]->is_module_handle)
                                        mono_image_fixup_vtable (image->modules [idx - 1]);
 #endif
@@ -702,7 +703,7 @@ do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
 {
        MonoDotNetHeader64 header64;
 
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        if (!image->is_module_handle)
 #endif
        if (offset + sizeof (MonoDotNetHeader32) > image->raw_data_len)
@@ -823,7 +824,7 @@ do_load_header (MonoImage *image, MonoDotNetHeader *header, int offset)
        SWAPPDE (header->datadir.pe_cli_header);
        SWAPPDE (header->datadir.pe_reserved);
 
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        if (image->is_module_handle)
                image->raw_data_len = header->nt.pe_image_size;
 #endif
@@ -842,7 +843,7 @@ mono_image_load_pe_data (MonoImage *image)
        iinfo = image->image_info;
        header = &iinfo->cli_header;
 
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        if (!image->is_module_handle)
 #endif
        if (offset + sizeof (msdos) > image->raw_data_len)
@@ -1020,6 +1021,12 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
        image->raw_buffer_used = TRUE;
        image->raw_data_len = mono_file_map_size (filed);
        image->raw_data = mono_file_map (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
+#if defined(HAVE_MMAP) && !defined (HOST_WIN32)
+       if (!image->raw_data) {
+               image->fileio_used = TRUE;
+               image->raw_data = mono_file_map_fileio (image->raw_data_len, MONO_MMAP_READ|MONO_MMAP_PRIVATE, mono_file_map_fd (filed), 0, &image->raw_data_handle);
+       }
+#endif
        if (!image->raw_data) {
                mono_file_map_close (filed);
                g_free (image);
@@ -1178,7 +1185,7 @@ mono_image_open_from_data (char *data, guint32 data_len, gboolean need_copy, Mon
        return mono_image_open_from_data_full (data, data_len, need_copy, status, FALSE);
 }
 
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
 /* fname is not duplicated. */
 MonoImage*
 mono_image_open_from_module_handle (HMODULE module_handle, char* fname, gboolean has_entry_point, MonoImageOpenStatus* status)
@@ -1212,7 +1219,7 @@ mono_image_open_full (const char *fname, MonoImageOpenStatus *status, gboolean r
        
        g_return_val_if_fail (fname != NULL, NULL);
        
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        /* Load modules using LoadLibrary. */
        if (!refonly && coree_module_handle) {
                HMODULE module_handle;
@@ -1364,7 +1371,7 @@ mono_image_open_raw (const char *fname, MonoImageOpenStatus *status)
 void
 mono_image_fixup_vtable (MonoImage *image)
 {
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        MonoCLIImageInfo *iinfo;
        MonoPEDirEntry *de;
        MonoVTableFixup *vtfixup;
@@ -1502,7 +1509,7 @@ mono_image_close_except_pools (MonoImage *image)
 
        mono_images_unlock ();
 
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        if (image->is_module_handle && image->has_entry_point) {
                mono_images_lock ();
                if (image->ref_count == 0) {
@@ -1545,7 +1552,7 @@ mono_image_close_except_pools (MonoImage *image)
                }
        }
 
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
        mono_images_lock ();
        if (image->is_module_handle && !image->has_entry_point)
                FreeLibrary ((HMODULE) image->raw_data);
@@ -1553,8 +1560,14 @@ mono_image_close_except_pools (MonoImage *image)
 #endif
 
        if (image->raw_buffer_used) {
-               if (image->raw_data != NULL)
-                       mono_file_unmap (image->raw_data, image->raw_data_handle);
+               if (image->raw_data != NULL) {
+#ifndef HOST_WIN32
+                       if (image->fileio_used)
+                               mono_file_unmap_fileio (image->raw_data, image->raw_data_handle);
+                       else
+#endif
+                               mono_file_unmap (image->raw_data, image->raw_data_handle);
+               }
        }
        
        if (image->raw_data_allocated) {
@@ -1609,6 +1622,9 @@ mono_image_close_except_pools (MonoImage *image)
        free_hash (image->delegate_invoke_cache);
        free_hash (image->delegate_abstract_invoke_cache);
        free_hash (image->delegate_bound_static_invoke_cache);
+       free_hash (image->delegate_invoke_generic_cache);
+       free_hash (image->delegate_begin_invoke_generic_cache);
+       free_hash (image->delegate_end_invoke_generic_cache);
        free_hash (image->remoting_invoke_cache);
        free_hash (image->runtime_invoke_cache);
        free_hash (image->runtime_invoke_direct_cache);
@@ -1627,6 +1643,10 @@ mono_image_close_except_pools (MonoImage *image)
        free_hash (image->thunk_invoke_cache);
        free_hash (image->var_cache_slow);
        free_hash (image->mvar_cache_slow);
+       free_hash (image->wrapper_param_names);
+       free_hash (image->native_wrapper_aot_cache);
+       free_hash (image->pinvoke_scopes);
+       free_hash (image->pinvoke_scope_filenames);
 
        /* The ownership of signatures is not well defined */
        //g_hash_table_foreach (image->memberref_signatures, free_mr_signatures, NULL);
@@ -1713,7 +1733,9 @@ mono_image_close_finish (MonoImage *image)
        if (image->modules)
                g_free (image->modules);
 
+#ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->loader_bytes -= mono_mempool_get_allocated (image->mempool);
+#endif
 
        if (!image->dynamic) {
                if (debug_assembly_unload)
@@ -2000,7 +2022,7 @@ mono_image_load_file_for_image (MonoImage *image, int fileidx)
                }
 
                image->files [fileidx - 1] = res;
-#ifdef ENABLE_COREE
+#ifdef HOST_WIN32
                if (res->is_module_handle)
                        mono_image_fixup_vtable (res);
 #endif
@@ -2209,7 +2231,9 @@ mono_image_alloc (MonoImage *image, guint size)
 {
        gpointer res;
 
+#ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->loader_bytes += size;
+#endif
        mono_image_lock (image);
        res = mono_mempool_alloc (image->mempool, size);
        mono_image_unlock (image);
@@ -2222,7 +2246,9 @@ mono_image_alloc0 (MonoImage *image, guint size)
 {
        gpointer res;
 
+#ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->loader_bytes += size;
+#endif
        mono_image_lock (image);
        res = mono_mempool_alloc0 (image->mempool, size);
        mono_image_unlock (image);
@@ -2235,7 +2261,9 @@ mono_image_strdup (MonoImage *image, const char *s)
 {
        char *res;
 
+#ifndef DISABLE_PERFCOUNTERS
        mono_perfcounters->loader_bytes += strlen (s);
+#endif
        mono_image_lock (image);
        res = mono_mempool_strdup (image->mempool, s);
        mono_image_unlock (image);