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

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

Put back handlemanager changes + memory mapped file changes

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