[sgen] Allocate job data dynamically.
[mono.git] / mono / utils / mono-filemap.c
index b2eb9ec151caa03bc8663bda9afc6876d6f8cec8..5278c00c13357af4cb5ba3a41dc11131d074207e 100644 (file)
@@ -14,7 +14,9 @@
 #endif
 #include <fcntl.h>
 #include <string.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 #include <stdlib.h>
 #include <stdio.h>
 
 MonoFileMap *
 mono_file_map_open (const char* name)
 {
+#ifdef WIN32
+       gunichar2 *wname = g_utf8_to_utf16 (name, -1, 0, 0, 0);
+       MonoFileMap *result;
+
+       if (wname == NULL)
+               return NULL;
+       result = (MonoFileMap *) _wfopen ((wchar_t *) wname, L"rb");
+       g_free (wname);
+       return result;
+#else
        return (MonoFileMap *)fopen (name, "rb");
+#endif
 }
 
 guint64 
@@ -41,23 +54,35 @@ mono_file_map_fd (MonoFileMap *fmap)
        return fileno ((FILE*)fmap);
 }
 
-void
+int 
 mono_file_map_close (MonoFileMap *fmap)
 {
        return fclose ((FILE*)fmap);
 }
 
+#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;
+
+void
+mono_file_map_set_allocator (mono_file_map_alloc_fn alloc, mono_file_map_release_fn release)
+{
+       alloc_fn = alloc == NULL     ? (mono_file_map_alloc_fn) malloc : alloc;
+       release_fn = release == NULL ? (mono_file_map_release_fn) free : 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;
-       void *ptr = malloc (length);
+       void *ptr = (*alloc_fn) (length);
        if (!ptr)
                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);
@@ -66,3 +91,23 @@ mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_hand
        return ptr;
 }
 
+int
+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