3 * Unix/Windows implementation for filemap.
6 * Paolo Molaro (lupus@ximian.com)
8 * Copyright 2008-2008 Novell, Inc.
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
25 #include "mono-mmap.h"
28 mono_file_map_open (const char* name)
31 gunichar2 *wname = g_utf8_to_utf16 (name, -1, 0, 0, 0);
36 result = (MonoFileMap *) _wfopen ((wchar_t *) wname, L"rb");
40 int fd = open (name, O_RDONLY);
43 return (MonoFileMap *)(size_t)fd;
48 mono_file_map_size (MonoFileMap *fmap)
51 if (fstat (mono_file_map_fd (fmap), &stat_buf) < 0)
53 return stat_buf.st_size;
57 mono_file_map_fd (MonoFileMap *fmap)
60 return fileno ((FILE*)fmap);
62 return (int)(size_t)fmap;
67 mono_file_map_close (MonoFileMap *fmap)
70 return fclose ((FILE*)fmap);
72 return close (mono_file_map_fd (fmap));
76 #if !defined (HOST_WIN32)
78 static mono_file_map_alloc_fn alloc_fn = (mono_file_map_alloc_fn) malloc;
79 static mono_file_map_release_fn release_fn = (mono_file_map_release_fn) free;
82 mono_file_map_set_allocator (mono_file_map_alloc_fn alloc, mono_file_map_release_fn release)
84 alloc_fn = alloc == NULL ? (mono_file_map_alloc_fn) malloc : alloc;
85 release_fn = release == NULL ? (mono_file_map_release_fn) free : release;
89 mono_file_map_fileio (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
93 void *ptr = (*alloc_fn) (length);
96 cur_offset = lseek (fd, 0, SEEK_CUR);
97 if (lseek (fd, offset, SEEK_SET) != offset) {
101 bytes_read = read (fd, ptr, length);
102 if (bytes_read != length)
104 lseek (fd, cur_offset, SEEK_SET);
110 mono_file_unmap_fileio (void *addr, void *handle)
112 (*release_fn) (addr);
115 #if !defined(HAVE_MMAP)
117 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
119 return mono_file_map_fileio (length, flags, fd, offset, ret_handle);
123 mono_file_unmap (void *addr, void *handle)
125 return mono_file_unmap_fileio(addr, handle);