source: trunk/src/kernel32/hmmmap.cpp@ 9826

Last change on this file since 9826 was 9826, checked in by sandervl, 23 years ago

updates

File size: 9.3 KB
Line 
1/* $Id: hmmmap.cpp,v 1.22 2003-02-18 18:58:47 sandervl Exp $ */
2
3/*
4 * Project Odin Software License can be found in LICENSE.TXT
5 * Win32 Unified Handle Manager for OS/2
6 * Copyright 1999 Patrick Haller (haller@zebra.fh-weingarten.de)
7 */
8
9#undef DEBUG_LOCAL
10//#define DEBUG_LOCAL
11
12
13/*****************************************************************************
14 * Remark *
15 *****************************************************************************
16
17 */
18
19
20/*****************************************************************************
21 * Includes *
22 *****************************************************************************/
23
24#include <os2win.h>
25#include <stdlib.h>
26#include <string.h>
27#include "unicode.h"
28#include "misc.h"
29
30#include "HandleManager.H"
31#include "HMMMap.h"
32#include "mmap.h"
33#include "heapshared.h"
34
35#define DBG_LOCALLOG DBG_hmmmap
36#include "dbglocal.h"
37
38/*****************************************************************************
39 * Defines *
40 *****************************************************************************/
41
42/*****************************************************************************
43 * Structures *
44 *****************************************************************************/
45
46/*****************************************************************************
47 * Local Prototypes *
48 *****************************************************************************/
49
50//******************************************************************************
51//******************************************************************************
52DWORD HMDeviceMemMapClass::CreateFileMapping(PHMHANDLEDATA pHMHandleData,
53 HFILE hFile,
54 SECURITY_ATTRIBUTES *sa, /* [in] Optional security attributes*/
55 DWORD protect, /* [in] Protection for mapping object */
56 DWORD size_high, /* [in] High-order 32 bits of object size */
57 DWORD size_low, /* [in] Low-order 32 bits of object size */
58 LPCSTR name) /* [in] Name of file-mapping object */
59{
60 Win32MemMap *map;
61
62 if((hFile == -1 && size_low == 0) || size_high ||
63 protect & ~(PAGE_READONLY|PAGE_READWRITE|PAGE_WRITECOPY|SEC_COMMIT|SEC_IMAGE|SEC_RESERVE|SEC_NOCACHE) ||
64 (protect & (PAGE_READONLY|PAGE_READWRITE|PAGE_WRITECOPY)) == 0 ||
65// (hFile == -1 && (protect & SEC_COMMIT)) ||
66 ((protect & SEC_COMMIT) && (protect & SEC_RESERVE)))
67 {
68
69 dprintf(("CreateFileMappingA: invalid parameter (combination)!"));
70 dprintf(("Parameters: %x %x %x %x %s", hFile, protect, size_high, size_low, name));
71 return ERROR_INVALID_PARAMETER;
72 }
73
74 map = Win32MemMap::findMap((LPSTR)name);
75 if(map != NULL) {
76 dprintf(("CreateFileMappingA: duplicating map %s!", name));
77
78 DWORD protflags = map->getProtFlags();
79 switch(protect) {
80 case FILE_MAP_WRITE:
81 if(!(protflags & PAGE_WRITECOPY))
82 dprintf(("Different flags for duplicate!"));
83 break;
84 case FILE_MAP_READ:
85 if(!(protflags & (PAGE_READWRITE | PAGE_READONLY)))
86 dprintf(("Different flags for duplicate!"));
87 break;
88 case FILE_MAP_COPY:
89 if(!(protflags & PAGE_WRITECOPY))
90 dprintf(("Different flags for duplicate!"));
91 break;
92 }
93 //TODO:
94 //Is it allowed to open an existing view with different flags?
95 //(i.e. write access to readonly object)
96 // -> for the same file handle, yes
97
98 //if map already exists, we must create a new handle to the existing
99 //map object and return ERROR_ALREADY_EXISTS
100 pHMHandleData->dwUserData = (ULONG)map;
101 pHMHandleData->dwInternalType = HMTYPE_MEMMAP;
102
103 //findMap already incremented the reference count, so we simply don't
104 //release it here
105 return ERROR_ALREADY_EXISTS;
106 }
107#if 0
108 //We reuse the original memory map object if another one is created for
109 //the same file handle
110 //TODO: different file handles can exist for the same file (DuplicateHandle)
111 map = Win32MemMap::findMapByFile(hFile);
112 if(map) {
113 dprintf(("CreateFileMappingA: duplicating map with file %x!", hFile));
114
115 //if map already exists, we must create a new handle to the existing
116 //map object
117 pHMHandleData->dwUserData = (ULONG)map;
118 pHMHandleData->dwInternalType = HMTYPE_MEMMAP;
119
120 //findMap already incremented the reference count, so we simply don't
121 //release it here
122 return ERROR_SUCCESS;
123 }
124#endif
125 else {
126 map = new Win32MemMap(hFile, size_low, protect, (LPSTR)name);
127
128 if(map == NULL) {
129 dprintf(("CreateFileMappingA: can't create Win32MemMap object!"));
130 return ERROR_OUTOFMEMORY;
131 }
132
133 if(map->Init(size_low) == FALSE) {
134 dprintf(("CreateFileMappingA: init failed!"));
135 delete map;
136 return ERROR_GEN_FAILURE;
137 }
138 }
139 pHMHandleData->dwUserData = (ULONG)map;
140 pHMHandleData->dwInternalType = HMTYPE_MEMMAP;
141 return NO_ERROR;
142}
143//******************************************************************************
144//******************************************************************************
145DWORD HMDeviceMemMapClass::OpenFileMapping(PHMHANDLEDATA pHMHandleData,
146 DWORD access, /* [in] Access mode */
147 BOOL inherit, /* [in] Inherit flag */
148 LPCSTR name ) /* [in] Name of file-mapping object */
149{
150 Win32MemMap *map;
151 DWORD protflags;
152 DWORD ret;
153
154 if(name == NULL)
155 return ERROR_INVALID_PARAMETER;
156
157 map = Win32MemMap::findMap((LPSTR)name);
158 if(map == NULL) {
159 dprintf(("OpenFileMapping: mapping %s not found", name));
160 return ERROR_FILE_NOT_FOUND;
161 }
162 protflags = map->getProtFlags();
163 switch(access) {
164 case FILE_MAP_WRITE:
165 case FILE_MAP_ALL_ACCESS:
166 if(!(protflags & (PAGE_WRITECOPY|PAGE_READWRITE))) {
167 ret = ERROR_INVALID_PARAMETER;
168 goto fail;
169 }
170 break;
171 case FILE_MAP_READ:
172 if(!(protflags & (PAGE_READWRITE | PAGE_READONLY))) {
173 ret = ERROR_INVALID_PARAMETER;
174 goto fail;
175 }
176 break;
177 case FILE_MAP_COPY:
178 if(!(protflags & PAGE_WRITECOPY)) {
179 ret = ERROR_INVALID_PARAMETER;
180 goto fail;
181 }
182 break;
183 }
184 //findMap already incremented the reference count, so we simply don't
185 //release it here
186 pHMHandleData->dwUserData = (ULONG)map;
187 pHMHandleData->dwInternalType = HMTYPE_MEMMAP;
188 return NO_ERROR;
189
190fail:
191 map->Release();
192 return ret;
193}
194//******************************************************************************
195//******************************************************************************
196LPVOID HMDeviceMemMapClass::MapViewOfFileEx(PHMHANDLEDATA pHMHandleData,
197 DWORD dwDesiredAccess,
198 DWORD dwFileOffsetHigh,
199 DWORD dwFileOffsetLow,
200 DWORD dwNumberOfBytesToMap,
201 LPVOID lpBaseAddress)
202{
203 Win32MemMap *map;
204
205 dprintf(("KERNEL32: HMDeviceMemMapClass::MapViewOfFileEx(%08xh,%08xh,%08xh,%08xh,%08xh,%08xh)\n",
206 pHMHandleData->hHMHandle,
207 dwDesiredAccess,
208 dwFileOffsetHigh,
209 dwFileOffsetLow,
210 dwNumberOfBytesToMap,
211 lpBaseAddress));
212
213 if(lpBaseAddress != NULL)
214 {
215#if 0
216 //No can do. Let us choose the address
217 dprintf(("Can't create view to virtual address %x", lpBaseAddress));
218 SetLastError(ERROR_OUTOFMEMORY);
219 return NULL;
220#else
221 // PH 2000/05/24 IBM VAJ3 uses this function.
222 // I don't think we'll ever succeed in EXACTLY copying the original
223 // behaviour of the function. Maybe ignoring the base address helps?
224 dprintf(("WARNING: suggested virtual address %x IGNORED! (experimental API violation)",
225 lpBaseAddress));
226#endif
227 }
228
229 if(pHMHandleData->dwUserData == NULL || pHMHandleData->dwInternalType != HMTYPE_MEMMAP) {
230 dprintf(("MapViewOfFileEx: invalid handle data!"));
231 SetLastError(ERROR_INVALID_HANDLE);
232 return NULL;
233 }
234 map = (Win32MemMap *)pHMHandleData->dwUserData;
235
236 return map->mapViewOfFile(dwNumberOfBytesToMap, dwFileOffsetLow, dwDesiredAccess);
237}
238//******************************************************************************
239//******************************************************************************
240BOOL HMDeviceMemMapClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
241{
242 Win32MemMap *map;
243
244 dprintf(("HMDeviceMemMapClass::CloseHandle %x", pHMHandleData->dwUserData));
245 if(pHMHandleData->dwUserData == NULL || pHMHandleData->dwInternalType != HMTYPE_MEMMAP) {
246 dprintf(("HMDeviceMemMapClass::CloseHandle: invalid handle data!"));
247 return FALSE;
248 }
249 //Although an application may close the file handle used to create a file
250 //mapping object, the system keeps the corresponding file open until the last
251 //view of the file is unmapped.
252 map = (Win32MemMap *)pHMHandleData->dwUserData;
253 map->Release();
254
255 return TRUE;
256}
257//******************************************************************************
258//******************************************************************************
Note: See TracBrowser for help on using the repository browser.