2 * mono-filemap.c: Unix/Windows implementation for filemap.
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2008-2008 Novell, Inc.
23 #include "mono-mmap.h"
26 mono_file_map_open (const char* name)
29 gunichar2 *wname = g_utf8_to_utf16 (name, -1, 0, 0, 0);
34 result = (MonoFileMap *) _wfopen ((wchar_t *) wname, L"rb");
38 int fd = open (name, O_RDONLY);
41 return (MonoFileMap *)(size_t)fd;
46 mono_file_map_size (MonoFileMap *fmap)
49 if (fstat (mono_file_map_fd (fmap), &stat_buf) < 0)
51 return stat_buf.st_size;
55 mono_file_map_fd (MonoFileMap *fmap)
58 return fileno ((FILE*)fmap);
60 return (int)(size_t)fmap;
65 mono_file_map_close (MonoFileMap *fmap)
68 return fclose ((FILE*)fmap);
70 return close (mono_file_map_fd (fmap));
74 #if !defined (HOST_WIN32)
76 static mono_file_map_alloc_fn alloc_fn = (mono_file_map_alloc_fn) malloc;
77 static mono_file_map_release_fn release_fn = (mono_file_map_release_fn) free;
80 mono_file_map_set_allocator (mono_file_map_alloc_fn alloc, mono_file_map_release_fn release)
82 alloc_fn = alloc == NULL ? (mono_file_map_alloc_fn) malloc : alloc;
83 release_fn = release == NULL ? (mono_file_map_release_fn) free : release;
87 mono_file_map_fileio (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
91 void *ptr = (*alloc_fn) (length);
94 cur_offset = lseek (fd, 0, SEEK_CUR);
95 if (lseek (fd, offset, SEEK_SET) != offset) {
99 bytes_read = read (fd, ptr, length);
100 if (bytes_read != length)
102 lseek (fd, cur_offset, SEEK_SET);
108 mono_file_unmap_fileio (void *addr, void *handle)
110 (*release_fn) (addr);
113 #if !defined(HAVE_MMAP)
115 mono_file_map (size_t length, int flags, int fd, guint64 offset, void **ret_handle)
117 return mono_file_map_fileio (length, flags, fd, offset, ret_handle);
121 mono_file_unmap (void *addr, void *handle)
123 return mono_file_unmap_fileio(addr, handle);