source: trunk/src/kernel32/virtual.cpp@ 607

Last change on this file since 607 was 607, checked in by sandervl, 26 years ago

Memory mapped file changes

File size: 7.6 KB
Line 
1/* $Id: virtual.cpp,v 1.1 1999-08-21 16:30:31 sandervl Exp $ */
2
3/*
4 * Win32 virtual memory functions
5 *
6 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) (OS/2 Port)
7 *
8 * NOTE: Quick 'n dirty implementation (read entire file in memory)
9 * Not tested, nor complete (write mapping & protection)
10 *
11 * Based on Wine code (memory\virtual.c):
12 *
13 * Copyright 1997 Alexandre Julliard
14 *
15 * Project Odin Software License can be found in LICENSE.TXT
16 *
17 */
18
19#include <os2win.h>
20#include <stdlib.h>
21#include <string.h>
22#include <win\virtual.h>
23#include <heapstring.h>
24
25
26/***********************************************************************
27 * CreateFileMapping32A (KERNEL32.46)
28 * Creates a named or unnamed file-mapping object for the specified file
29 *
30 * RETURNS
31 * Handle: Success
32 * 0: Mapping object does not exist
33 * NULL: Failure
34 */
35HANDLE WINAPI CreateFileMappingA(
36 HFILE hFile, /* [in] Handle of file to map */
37 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
38 DWORD protect, /* [in] Protection for mapping object */
39 DWORD size_high, /* [in] High-order 32 bits of object size */
40 DWORD size_low, /* [in] Low-order 32 bits of object size */
41 LPCSTR name /* [in] Name of file-mapping object */ )
42{
43 HANDLE dupHandle;
44
45 if(DuplicateHandle(GetCurrentProcess(), hFile, GetCurrentProcess(),
46 &dupHandle, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
47 {
48 dprintf(("CreateFileMappingA: DuplicateHandle failed!"));
49 return 0;
50 }
51 return dupHandle;
52}
53
54
55/***********************************************************************
56 * CreateFileMapping32W (KERNEL32.47)
57 * See CreateFileMapping32A
58 */
59HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES attr,
60 DWORD protect, DWORD size_high,
61 DWORD size_low, LPCWSTR name )
62{
63 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
64 HANDLE ret = CreateFileMappingA( hFile, attr, protect,
65 size_high, size_low, nameA );
66 HeapFree( GetProcessHeap(), 0, nameA );
67 return ret;
68}
69
70
71/***********************************************************************
72 * OpenFileMapping32A (KERNEL32.397)
73 * Opens a named file-mapping object.
74 *
75 * RETURNS
76 * Handle: Success
77 * NULL: Failure
78 */
79HANDLE WINAPI OpenFileMappingA(
80 DWORD access, /* [in] Access mode */
81 BOOL inherit, /* [in] Inherit flag */
82 LPCSTR name ) /* [in] Name of file-mapping object */
83{
84 dprintf(("OpenFileMappingA: NOT IMPLEMENTED"));
85 return 0;
86}
87
88
89/***********************************************************************
90 * OpenFileMapping32W (KERNEL32.398)
91 * See OpenFileMapping32A
92 */
93HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
94{
95 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
96 HANDLE ret = OpenFileMappingA( access, inherit, nameA );
97 HeapFree( GetProcessHeap(), 0, nameA );
98 return ret;
99}
100
101
102/***********************************************************************
103 * MapViewOfFile (KERNEL32.385)
104 * Maps a view of a file into the address space
105 *
106 * RETURNS
107 * Starting address of mapped view
108 * NULL: Failure
109 */
110LPVOID WINAPI MapViewOfFile(
111 HANDLE mapping, /* [in] File-mapping object to map */
112 DWORD access, /* [in] Access mode */
113 DWORD offset_high, /* [in] High-order 32 bits of file offset */
114 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
115 DWORD count /* [in] Number of bytes to map */
116)
117{
118 return MapViewOfFileEx( mapping, access, offset_high,
119 offset_low, count, NULL );
120}
121
122
123/***********************************************************************
124 * MapViewOfFileEx (KERNEL32.386)
125 * Maps a view of a file into the address space
126 *
127 * RETURNS
128 * Starting address of mapped view
129 * NULL: Failure
130 */
131LPVOID WINAPI MapViewOfFileEx(
132 HANDLE handle, /* [in] File-mapping object to map */
133 DWORD access, /* [in] Access mode */
134 DWORD offset_high, /* [in] High-order 32 bits of file offset */
135 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
136 DWORD count, /* [in] Number of bytes to map */
137 LPVOID addr /* [in] Suggested starting address for mapped view */
138)
139{
140 DWORD filesize, bytesread;
141 LPSTR lpBuffer;
142
143 filesize = GetFileSize(handle, 0);
144 if(filesize == 0) {
145 dprintf(("MapViewOfFileEx: filesize = 0!"));
146 return 0;
147 }
148 lpBuffer = (LPSTR)VirtualAlloc(0, filesize, MEM_COMMIT, PAGE_READWRITE);
149 if(lpBuffer == 0) {
150 dprintf(("MapViewOfFileEx: VirtualAlloc failed (%d bytes)!", filesize));
151 return 0;
152 }
153 if(ReadFile(handle, lpBuffer, filesize, &bytesread, NULL) == FALSE) {
154 dprintf(("MapViewOfFileEx: ReadFile failed (%d bytes)!", filesize));
155 VirtualFree(lpBuffer, 0, MEM_RELEASE);
156 return 0;
157 }
158 return lpBuffer;
159}
160
161
162/***********************************************************************
163 * FlushViewOfFile (KERNEL32.262)
164 * Writes to the disk a byte range within a mapped view of a file
165 *
166 * RETURNS
167 * TRUE: Success
168 * FALSE: Failure
169 */
170BOOL WINAPI FlushViewOfFile(
171 LPCVOID base, /* [in] Start address of byte range to flush */
172 DWORD cbFlush /* [in] Number of bytes in range */
173)
174{
175 dprintf(("FlushViewOfFile: NOT IMPLEMENTED"));
176 return TRUE;
177}
178
179
180/***********************************************************************
181 * UnmapViewOfFile (KERNEL32.540)
182 * Unmaps a mapped view of a file.
183 *
184 * NOTES
185 * Should addr be an LPCVOID?
186 *
187 * RETURNS
188 * TRUE: Success
189 * FALSE: Failure
190 */
191BOOL WINAPI UnmapViewOfFile(LPVOID addr /* [in] Address where mapped view begins */
192)
193{
194 if (!addr)
195 {
196 SetLastError( ERROR_INVALID_PARAMETER );
197 return FALSE;
198 }
199 return VirtualFree(addr, 0, MEM_RELEASE);
200}
201
202/***********************************************************************
203 * VIRTUAL_MapFileW
204 *
205 * Helper function to map a file to memory:
206 * name - file name
207 * [RETURN] ptr - pointer to mapped file
208 */
209LPVOID WINAPI VIRTUAL_MapFileW( LPCWSTR name )
210{
211 HANDLE hFile, hMapping;
212 LPVOID ptr = NULL;
213
214 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
215 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
216 if (hFile != INVALID_HANDLE_VALUE)
217 {
218 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
219 CloseHandle( hFile );
220 if (hMapping)
221 {
222 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
223 CloseHandle( hMapping );
224 }
225 }
226 return ptr;
227}
228
229/***********************************************************************
230 * VIRTUAL_MapFileA
231 *
232 * Helper function to map a file to memory:
233 * name - file name
234 * [RETURN] ptr - pointer to mapped file
235 */
236LPVOID WINAPI VIRTUAL_MapFileA( LPCSTR name )
237{
238 HANDLE hFile, hMapping;
239 LPVOID ptr = NULL;
240
241 hFile = CreateFileA( name, GENERIC_READ, FILE_SHARE_READ, NULL,
242 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
243 if (hFile != INVALID_HANDLE_VALUE)
244 {
245 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
246 CloseHandle( hFile );
247 if (hMapping)
248 {
249 ptr = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
250 CloseHandle( hMapping );
251 }
252 }
253 return ptr;
254}
Note: See TracBrowser for help on using the repository browser.