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

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

VirtualQuery implemented + preliminary memory mapped code

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