Merge pull request #203 from Jester01/jester/mmap
authorZoltan Varga <vargaz@gmail.com>
Mon, 2 Jan 2012 17:20:17 +0000 (09:20 -0800)
committerZoltan Varga <vargaz@gmail.com>
Mon, 2 Jan 2012 17:20:17 +0000 (09:20 -0800)
Make do_mono_image_open try file i/o if mmap fails

mono/metadata/image.c
mono/metadata/metadata-internals.h
mono/utils/mono-filemap.c
mono/utils/mono-mmap.h

index 41a0e48d43884be96cbe52657fbb0eb7ffac9f71..247d9596a11929b99de87825c7d349fae74ba7c6 100644 (file)
@@ -1020,6 +1020,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);
@@ -1553,8 +1559,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) {
index cb9b246a4aaa260e700402a55caa60f9c6d651a9..93a439899271d6619cb1ddb6c974a0b4f418c28b 100644 (file)
@@ -138,6 +138,7 @@ struct _MonoImage {
        guint32 raw_data_len;
        guint8 raw_buffer_used    : 1;
        guint8 raw_data_allocated : 1;
+       guint8 fileio_used : 1;
 
 #ifdef HOST_WIN32
        /* Module was loaded using LoadLibrary. */
index b557ff323315e5600c96ae953d1549d27ac29ce8..5278c00c13357af4cb5ba3a41dc11131d074207e 100644 (file)
@@ -60,7 +60,7 @@ mono_file_map_close (MonoFileMap *fmap)
        return fclose ((FILE*)fmap);
 }
 
-#if !defined(HAVE_MMAP) && !defined (HOST_WIN32)
+#if !defined (HOST_WIN32)
 
 static mono_file_map_alloc_fn alloc_fn = (mono_file_map_alloc_fn) malloc;
 static mono_file_map_release_fn release_fn = (mono_file_map_release_fn) free;
@@ -73,7 +73,7 @@ mono_file_map_set_allocator (mono_file_map_alloc_fn alloc, mono_file_map_release
 }
 
 void *
-mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
+mono_file_map_fileio (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
 {
        guint64 cur_offset;
        size_t bytes_read;
@@ -82,7 +82,7 @@ mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_hand
                return NULL;
        cur_offset = lseek (fd, 0, SEEK_CUR);
        if (lseek (fd, offset, SEEK_SET) != offset) {
-               free (ptr);
+               (*release_fn) (ptr);
                return NULL;
        }
        bytes_read = read (fd, ptr, length);
@@ -92,9 +92,22 @@ mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_hand
 }
 
 int
-mono_file_unmap (void *addr, void *handle)
+mono_file_unmap_fileio (void *addr, void *handle)
 {
        (*release_fn) (addr);
        return 0;
 }
+#if !defined(HAVE_MMAP)
+void *
+mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
+{
+       return mono_file_map_fileio (length, flags, fd, offset, ret_handle);
+}
+
+int
+mono_file_unmap (void *addr, void *handle)
+{
+       return mono_file_unmap_fileio(addr, handle);
+}
+#endif
 #endif
index 7eb67d6a88d83c73e6913b7b269e257ef84b0735..36997e074890904f61ed03cfb79fa8f2f43d7363 100644 (file)
@@ -35,6 +35,10 @@ void* mono_valloc_aligned (size_t length, size_t alignment, int flags);
 int   mono_vfree      (void *addr, size_t length);
 void* mono_file_map   (size_t length, int flags, int fd, guint64 offset, void **ret_handle);
 int   mono_file_unmap (void *addr, void *handle);
+#ifndef HOST_WIN32
+void* mono_file_map_fileio   (size_t length, int flags, int fd, guint64 offset, void **ret_handle);
+int   mono_file_unmap_fileio (void *addr, void *handle);
+#endif
 int   mono_mprotect   (void *addr, size_t length, int flags);
 
 void* mono_shared_area         (void);