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

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

Memory mapping changes

File size: 7.2 KB
Line 
1/* $Id: virtual.cpp,v 1.6 1999-08-25 12:30:48 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 <handlemanager.h>
22#include "mmap.h"
23
24/***********************************************************************
25 * CreateFileMapping32A (KERNEL32.46)
26 * Creates a named or unnamed file-mapping object for the specified file
27 *
28 * RETURNS
29 * Handle: Success
30 * 0: Mapping object does not exist
31 * NULL: Failure
32 */
33HANDLE WINAPI CreateFileMappingA(
34 HFILE hFile, /* [in] Handle of file to map */
35 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
36 DWORD protect, /* [in] Protection for mapping object */
37 DWORD size_high, /* [in] High-order 32 bits of object size */
38 DWORD size_low, /* [in] Low-order 32 bits of object size */
39 LPCSTR name /* [in] Name of file-mapping object */ )
40{
41 return HMCreateFileMapping(hFile, sa, protect, size_high, size_low, name);
42}
43
44
45/***********************************************************************
46 * CreateFileMapping32W (KERNEL32.47)
47 * See CreateFileMapping32A
48 */
49HANDLE WINAPI CreateFileMappingW( HFILE hFile, LPSECURITY_ATTRIBUTES attr,
50 DWORD protect, DWORD size_high,
51 DWORD size_low, LPCWSTR name )
52{
53 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
54 HANDLE ret = CreateFileMappingA( hFile, attr, protect,
55 size_high, size_low, nameA );
56 HeapFree( GetProcessHeap(), 0, nameA );
57 return ret;
58}
59
60
61/***********************************************************************
62 * OpenFileMapping32A (KERNEL32.397)
63 * Opens a named file-mapping object.
64 *
65 * RETURNS
66 * Handle: Success
67 * NULL: Failure
68 */
69HANDLE WINAPI OpenFileMappingA(
70 DWORD access, /* [in] Access mode */
71 BOOL inherit, /* [in] Inherit flag */
72 LPCSTR name ) /* [in] Name of file-mapping object */
73{
74 dprintf(("OpenFileMappingA: %x %d %s", access, inherit, name));
75 return HMOpenFileMapping(access, inherit, name);
76}
77
78
79/***********************************************************************
80 * OpenFileMapping32W (KERNEL32.398)
81 * See OpenFileMapping32A
82 */
83HANDLE WINAPI OpenFileMappingW( DWORD access, BOOL inherit, LPCWSTR name)
84{
85 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
86 HANDLE ret = OpenFileMappingA( access, inherit, nameA );
87 HeapFree( GetProcessHeap(), 0, nameA );
88 return ret;
89}
90
91
92/***********************************************************************
93 * MapViewOfFile (KERNEL32.385)
94 * Maps a view of a file into the address space
95 *
96 * RETURNS
97 * Starting address of mapped view
98 * NULL: Failure
99 */
100LPVOID WINAPI MapViewOfFile(
101 HANDLE mapping, /* [in] File-mapping object to map */
102 DWORD access, /* [in] Access mode */
103 DWORD offset_high, /* [in] High-order 32 bits of file offset */
104 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
105 DWORD count /* [in] Number of bytes to map */
106)
107{
108 return MapViewOfFileEx( mapping, access, offset_high,
109 offset_low, count, NULL );
110}
111
112
113/***********************************************************************
114 * MapViewOfFileEx (KERNEL32.386)
115 * Maps a view of a file into the address space
116 *
117 * RETURNS
118 * Starting address of mapped view
119 * NULL: Failure
120 */
121LPVOID WINAPI MapViewOfFileEx(
122 HANDLE handle, /* [in] File-mapping object to map */
123 DWORD access, /* [in] Access mode */
124 DWORD offset_high, /* [in] High-order 32 bits of file offset */
125 DWORD offset_low, /* [in] Low-order 32 bits of file offset */
126 DWORD count, /* [in] Number of bytes to map */
127 LPVOID addr /* [in] Suggested starting address for mapped view */
128)
129{
130 return HMMapViewOfFileEx(handle, access, offset_high, offset_low, count, addr);
131}
132
133
134/***********************************************************************
135 * FlushViewOfFile (KERNEL32.262)
136 * Writes to the disk a byte range within a mapped view of a file
137 *
138 * RETURNS
139 * TRUE: Success
140 * FALSE: Failure
141 */
142BOOL WINAPI FlushViewOfFile(
143 LPCVOID base, /* [in] Start address of byte range to flush */
144 DWORD cbFlush /* [in] Number of bytes in range */
145)
146{
147 Win32MemMap *map;
148
149 if (!base)
150 {
151 SetLastError( ERROR_INVALID_PARAMETER );
152 return FALSE;
153 }
154 map = Win32MemMap::findMap((ULONG)base);
155 if(map == NULL) {
156 SetLastError( ERROR_FILE_NOT_FOUND );
157 return FALSE;
158 }
159 return map->flushView((LPVOID)base, cbFlush);
160}
161
162
163/***********************************************************************
164 * UnmapViewOfFile (KERNEL32.540)
165 * Unmaps a mapped view of a file.
166 *
167 * NOTES
168 * Should addr be an LPCVOID?
169 *
170 * RETURNS
171 * TRUE: Success
172 * FALSE: Failure
173 */
174BOOL WINAPI UnmapViewOfFile(LPVOID addr /* [in] Address where mapped view begins */
175)
176{
177 Win32MemMap *map;
178
179 if (!addr)
180 {
181 SetLastError( ERROR_INVALID_PARAMETER );
182 return FALSE;
183 }
184 map = Win32MemMap::findMap((ULONG)addr);
185 if(map == NULL) {
186 SetLastError( ERROR_FILE_NOT_FOUND );
187 return FALSE;
188 }
189 return map->unmapViewOfFile();
190}
191
192/***********************************************************************
193 * VIRTUAL_MapFileW
194 *
195 * Helper function to map a file to memory:
196 * name - file name
197 * [RETURN] ptr - pointer to mapped file
198 */
199HANDLE WINAPI VIRTUAL_MapFileW( LPCWSTR name , LPVOID *lpMapping)
200{
201 HANDLE hFile, hMapping = 0;
202
203 hFile = CreateFileW( name, GENERIC_READ, FILE_SHARE_READ, NULL,
204 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
205 if (hFile != INVALID_HANDLE_VALUE)
206 {
207 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
208 CloseHandle( hFile );
209 if (hMapping)
210 {
211 *lpMapping = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
212 }
213 }
214 return hMapping;
215}
216
217/***********************************************************************
218 * VIRTUAL_MapFileA
219 *
220 * Helper function to map a file to memory:
221 * name - file name
222 * [RETURN] ptr - pointer to mapped file
223 */
224HANDLE WINAPI VIRTUAL_MapFileA( LPCSTR name , LPVOID *lpMapping)
225{
226 HANDLE hFile, hMapping = 0;
227
228 hFile = CreateFileA(name, GENERIC_READ, FILE_SHARE_READ, NULL,
229 OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
230 if (hFile != INVALID_HANDLE_VALUE)
231 {
232 hMapping = CreateFileMappingA( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
233 CloseHandle( hFile );
234 if (hMapping)
235 {
236 *lpMapping = MapViewOfFile( hMapping, FILE_MAP_READ, 0, 0, 0 );
237 }
238 }
239 return hMapping;
240}
Note: See TracBrowser for help on using the repository browser.