1 | /* $Id: mmap.cpp,v 1.19 1999-10-24 22:51:21 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 Memory mapped file & view classes
|
---|
5 | *
|
---|
6 | * Copyright 1999 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | *
|
---|
8 | * NOTE: Memory mapping DOES NOT work when kernel-mode code causes
|
---|
9 | * a pagefault in the memory mapped object. (exceptions aren't
|
---|
10 | * dispatched to our exception handler until after the kernel mode
|
---|
11 | * call returns (too late))
|
---|
12 | *
|
---|
13 | * NOTE: Are apps allowed to change the protection flags of memory mapped pages?
|
---|
14 | * I'm assuming they aren't for now.
|
---|
15 | *
|
---|
16 | * TODO: Handles returned should be usable by all apis that accept file handles
|
---|
17 | * TODO: Sharing memory mapped files between multiple processes
|
---|
18 | *
|
---|
19 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
20 | *
|
---|
21 | */
|
---|
22 | #include <os2win.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <string.h>
|
---|
26 | #include <win\virtual.h>
|
---|
27 | #include <vmutex.h>
|
---|
28 | #include <handlemanager.h>
|
---|
29 | #include "mmap.h"
|
---|
30 | #include "oslibdos.h"
|
---|
31 |
|
---|
32 | //Global DLL Data
|
---|
33 | #pragma data_seg(_GLOBALDATA)
|
---|
34 | Win32MemMapView *Win32MemMapView::mapviews = NULL;
|
---|
35 | Win32MemMap *Win32MemMap::memmaps = NULL;
|
---|
36 | VMutex globalmapMutex(VMUTEX_SHARED);
|
---|
37 | VMutex globalviewMutex(VMUTEX_SHARED);
|
---|
38 | #pragma data_seg()
|
---|
39 |
|
---|
40 | //******************************************************************************
|
---|
41 | //TODO: sharing between processes
|
---|
42 | //******************************************************************************
|
---|
43 | Win32MemMap::Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
|
---|
44 | : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0)
|
---|
45 | {
|
---|
46 | globalmapMutex.enter();
|
---|
47 | next = memmaps;
|
---|
48 | memmaps = this;
|
---|
49 | globalmapMutex.leave();
|
---|
50 |
|
---|
51 | hMemFile = hfile;
|
---|
52 |
|
---|
53 | mSize = size;
|
---|
54 | mProtFlags = fdwProtect;
|
---|
55 | mProcessId = GetCurrentProcess();
|
---|
56 |
|
---|
57 | if(lpszName) {
|
---|
58 | lpszMapName = (char *)_smalloc(strlen(lpszName)+1);
|
---|
59 | strcpy(lpszMapName, lpszName);
|
---|
60 | }
|
---|
61 | else lpszMapName = NULL;
|
---|
62 | }
|
---|
63 | //******************************************************************************
|
---|
64 | //******************************************************************************
|
---|
65 | BOOL Win32MemMap::Init(HANDLE hMemMap)
|
---|
66 | {
|
---|
67 | mapMutex.enter();
|
---|
68 | if(hMemFile != -1)
|
---|
69 | {
|
---|
70 | if(DuplicateHandle(mProcessId, hMemFile, GetCurrentProcess(),
|
---|
71 | &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
|
---|
72 | {
|
---|
73 | dprintf(("Win32MemMap::Init: DuplicateHandle failed!"));
|
---|
74 | goto fail;
|
---|
75 | }
|
---|
76 | mSize = SetFilePointer(hMemFile, 0, NULL, FILE_END);
|
---|
77 | if(mSize == -1) {
|
---|
78 | dprintf(("Win32MemMap::init: SetFilePointer failed to set pos end"));
|
---|
79 | goto fail;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | dprintf(("CreateFileMappingA for file %x, prot %x size %d, name %s", hMemFile, mProtFlags, mSize, lpszMapName));
|
---|
84 | this->hMemMap = hMemMap;
|
---|
85 | mapMutex.leave();
|
---|
86 | return TRUE;
|
---|
87 | fail:
|
---|
88 | mapMutex.leave();
|
---|
89 | return FALSE;
|
---|
90 | }
|
---|
91 | //******************************************************************************
|
---|
92 | //******************************************************************************
|
---|
93 | Win32MemMap::~Win32MemMap()
|
---|
94 | {
|
---|
95 | Win32MemMapView::deleteViews(this); //delete all views of our memory mapped file
|
---|
96 |
|
---|
97 | mapMutex.enter();
|
---|
98 | if(lpszMapName) {
|
---|
99 | free(lpszMapName);
|
---|
100 | }
|
---|
101 | if(pMapping) {
|
---|
102 | if(lpszMapName) {
|
---|
103 | OSLibDosFreeMem(pMapping);
|
---|
104 | }
|
---|
105 | else VirtualFree(pMapping, mSize, MEM_RELEASE);
|
---|
106 |
|
---|
107 | pMapping = NULL;
|
---|
108 | }
|
---|
109 | if(hMemFile != -1) {
|
---|
110 | CloseHandle(hMemFile);
|
---|
111 | hMemFile = -1;
|
---|
112 | }
|
---|
113 | mapMutex.leave();
|
---|
114 |
|
---|
115 | globalmapMutex.enter();
|
---|
116 | Win32MemMap *map = memmaps;
|
---|
117 |
|
---|
118 | if(map == this) {
|
---|
119 | memmaps = next;
|
---|
120 | }
|
---|
121 | else {
|
---|
122 | while(map->next) {
|
---|
123 | if(map->next == this)
|
---|
124 | break;
|
---|
125 | map = map->next;
|
---|
126 | }
|
---|
127 | if(map->next) {
|
---|
128 | map->next = next;
|
---|
129 | }
|
---|
130 | else dprintf(("Win32MemMap::~Win32MemMap: map not found!! (%x)", this));
|
---|
131 | }
|
---|
132 | globalmapMutex.leave();
|
---|
133 | }
|
---|
134 | //******************************************************************************
|
---|
135 | //We determine whether a page has been modified by checking it's protection flags
|
---|
136 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
---|
137 | //(all pages are readonly until the app tries to write to it)
|
---|
138 | //******************************************************************************
|
---|
139 | BOOL Win32MemMap::commitPage(ULONG offset, BOOL fWriteAccess)
|
---|
140 | {
|
---|
141 | MEMORY_BASIC_INFORMATION memInfo;
|
---|
142 | LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
|
---|
143 | DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
|
---|
144 | DWORD oldProt, newProt, nrBytesRead, size;
|
---|
145 |
|
---|
146 | // mapMutex.enter();
|
---|
147 |
|
---|
148 | newProt = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
|
---|
149 |
|
---|
150 | dprintf(("Win32MemMap::commitPage %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
|
---|
151 | if(hMemFile != -1) {
|
---|
152 | if(VirtualQuery((LPSTR)pageAddr, &memInfo, NRPAGES_TOCOMMIT*PAGE_SIZE) == 0) {
|
---|
153 | dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, NRPAGES_TOCOMMIT*PAGE_SIZE));
|
---|
154 | goto fail;
|
---|
155 | }
|
---|
156 | //Only changes the state of the pages with the same attribute flags
|
---|
157 | //(returned in memInfo.RegionSize)
|
---|
158 | //If it's smaller than the mNrPages, it simply means one or more of the
|
---|
159 | //other pages have already been committed
|
---|
160 | if(memInfo.State & MEM_COMMIT)
|
---|
161 | {//if it's already committed, then the app tried to write to it
|
---|
162 | if(!fWriteAccess) {
|
---|
163 | dprintf(("Win32MemMap::commitPage: Huh? Already committed and not trying to write (%x,%x) failed for %x", pageAddr, memInfo.RegionSize));
|
---|
164 | goto fail;
|
---|
165 | }
|
---|
166 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
|
---|
167 | dprintf(("Win32MemMap::commitPage: Failed to set write flag on page (%x,%x) failed for %x", pageAddr, memInfo.RegionSize));
|
---|
168 | goto fail;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | else {
|
---|
172 | if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
|
---|
173 | goto fail;
|
---|
174 | }
|
---|
175 | offset = pageAddr - (ULONG)pMapping;
|
---|
176 | size = memInfo.RegionSize;
|
---|
177 | if(offset + size > mSize) {
|
---|
178 | dprintf(("Adjusting size from %d to %d", size, mSize - offset));
|
---|
179 | size = mSize - offset;
|
---|
180 | }
|
---|
181 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
---|
182 | dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
|
---|
183 | goto fail;
|
---|
184 | }
|
---|
185 | if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
|
---|
186 | dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
|
---|
187 | goto fail;
|
---|
188 | }
|
---|
189 | if(nrBytesRead != size) {
|
---|
190 | dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
|
---|
191 | goto fail;
|
---|
192 | }
|
---|
193 | if(mProtFlags & PAGE_READONLY) {
|
---|
194 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
|
---|
195 | goto fail;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | else {
|
---|
201 | if(VirtualQuery((LPSTR)pageAddr, &memInfo, NRPAGES_TOCOMMIT*PAGE_SIZE) == 0) {
|
---|
202 | dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, NRPAGES_TOCOMMIT*PAGE_SIZE));
|
---|
203 | goto fail;
|
---|
204 | }
|
---|
205 | if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE) {
|
---|
206 | goto fail;
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | // mapMutex.leave();
|
---|
211 | return TRUE;
|
---|
212 | fail:
|
---|
213 | // mapMutex.leave();
|
---|
214 | return FALSE;
|
---|
215 | }
|
---|
216 | //******************************************************************************
|
---|
217 | //******************************************************************************
|
---|
218 | BOOL Win32MemMap::unmapViewOfFile(Win32MemMapView *view)
|
---|
219 | {
|
---|
220 | mapMutex.enter();
|
---|
221 |
|
---|
222 | if(nrMappings == 0)
|
---|
223 | goto fail;
|
---|
224 |
|
---|
225 | delete view;
|
---|
226 |
|
---|
227 | if(--nrMappings) {
|
---|
228 | VirtualFree(pMapping, mSize, MEM_RELEASE);
|
---|
229 | pMapping = NULL;
|
---|
230 | }
|
---|
231 | mapMutex.leave();
|
---|
232 | return TRUE;
|
---|
233 | fail:
|
---|
234 | mapMutex.leave();
|
---|
235 | return FALSE;
|
---|
236 | }
|
---|
237 | //******************************************************************************
|
---|
238 | //******************************************************************************
|
---|
239 | LPVOID Win32MemMap::mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess)
|
---|
240 | {
|
---|
241 | DWORD processId = GetCurrentProcess();
|
---|
242 |
|
---|
243 | mapMutex.enter();
|
---|
244 | ULONG memFlags = (mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY));
|
---|
245 | ULONG fAlloc = 0;
|
---|
246 | Win32MemMapView *mapview;
|
---|
247 |
|
---|
248 | //@@@PH: if(fdwAccess & ~(FILE_MAP_WRITE|FILE_MAP_READ|FILE_MAP_COPY))
|
---|
249 | // Docs say FILE_MAP_ALL_ACCESS is same as FILE_MAP_WRITE. Doesn't match reality though.
|
---|
250 | if(fdwAccess & ~FILE_MAP_ALL_ACCESS)
|
---|
251 | goto parmfail;
|
---|
252 | if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
|
---|
253 | goto parmfail;
|
---|
254 | if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY)))
|
---|
255 | goto parmfail;
|
---|
256 |
|
---|
257 | //@@@PH
|
---|
258 | if (fdwAccess != FILE_MAP_ALL_ACCESS)
|
---|
259 | if((fdwAccess & FILE_MAP_COPY) && !(mProtFlags & PAGE_WRITECOPY))
|
---|
260 | goto parmfail;
|
---|
261 |
|
---|
262 | if(offset+size > mSize)
|
---|
263 | goto parmfail;
|
---|
264 |
|
---|
265 | //TODO: If committed, read file into memory
|
---|
266 | #if 0
|
---|
267 | if(mProtFlags & SEC_COMMIT)
|
---|
268 | fAlloc |= MEM_COMMIT;
|
---|
269 | else
|
---|
270 | if(mProtFlags & SEC_RESERVE)
|
---|
271 | fAlloc |= MEM_RESERVE;
|
---|
272 | #else
|
---|
273 | fAlloc = MEM_RESERVE;
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | if(nrMappings == 0) {//if not mapped, reserve/commit entire view
|
---|
277 | //SvL: Always read/write access or else ReadFile will crash once we
|
---|
278 | // start committing pages.
|
---|
279 | // This is most likely an OS/2 bug and doesn't happen in Aurora
|
---|
280 | // when allocating memory with the PAG_ANY bit set. (without this
|
---|
281 | // flag it will also crash)
|
---|
282 | if(lpszMapName) {
|
---|
283 | pMapping = VirtualAllocShared(mSize, fAlloc, PAGE_READWRITE, lpszMapName);
|
---|
284 | }
|
---|
285 | else {
|
---|
286 | pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
|
---|
287 | }
|
---|
288 | if(pMapping == NULL) {
|
---|
289 | dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
|
---|
290 | goto fail;
|
---|
291 | }
|
---|
292 | }
|
---|
293 | mapview = new Win32MemMapView(this, offset, (size == 0) ? mSize : size, fdwAccess);
|
---|
294 | if(mapview == NULL) {
|
---|
295 | goto fail;
|
---|
296 | }
|
---|
297 | if(mapview->everythingOk() == FALSE) {
|
---|
298 | delete mapview;
|
---|
299 | goto fail;
|
---|
300 | }
|
---|
301 | nrMappings++;
|
---|
302 | mapMutex.leave();
|
---|
303 | return mapview->getViewAddr();
|
---|
304 |
|
---|
305 | parmfail:
|
---|
306 | dprintf(("Win32MemMap::mapFileView: ERROR_INVALID_PARAMETER"));
|
---|
307 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
308 | fail:
|
---|
309 | mapMutex.leave();
|
---|
310 | return 0;
|
---|
311 | }
|
---|
312 | //******************************************************************************
|
---|
313 | //We determine whether a page has been modified by checking it's protection flags
|
---|
314 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
---|
315 | //(all pages are readonly until the app tries to modify the contents of the page)
|
---|
316 | //
|
---|
317 | //TODO: Are apps allowed to change the protection flags of memory mapped pages?
|
---|
318 | // I'm assuming they aren't for now.
|
---|
319 | //******************************************************************************
|
---|
320 | BOOL Win32MemMap::flushView(ULONG offset, ULONG cbFlush)
|
---|
321 | {
|
---|
322 | LPVOID lpvBase = (LPVOID)((ULONG)pMapping+offset);
|
---|
323 | MEMORY_BASIC_INFORMATION memInfo;
|
---|
324 | ULONG nrBytesWritten, size;
|
---|
325 | int i;
|
---|
326 |
|
---|
327 | dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
|
---|
328 | if(nrMappings == 0)
|
---|
329 | goto parmfail;
|
---|
330 |
|
---|
331 | if(cbFlush == 0)
|
---|
332 | cbFlush = mSize;
|
---|
333 |
|
---|
334 | if(lpvBase < pMapping || (ULONG)lpvBase+cbFlush > (ULONG)pMapping+mSize)
|
---|
335 | goto parmfail;
|
---|
336 |
|
---|
337 | if(mProtFlags & PAGE_READONLY)
|
---|
338 | goto parmfail;
|
---|
339 |
|
---|
340 | if(hMemFile == -1)
|
---|
341 | goto success; //TODO: Return an error here?
|
---|
342 |
|
---|
343 | while(cbFlush) {
|
---|
344 | if(VirtualQuery((LPSTR)lpvBase, &memInfo, cbFlush) == 0) {
|
---|
345 | dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
|
---|
346 | goto fail;
|
---|
347 | }
|
---|
348 | //If a page (or range of pages) is reserved or write protected, we
|
---|
349 | //won't bother flushing it to disk
|
---|
350 | if(memInfo.State & MEM_COMMIT &&
|
---|
351 | memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
|
---|
352 | {//committed and allowed for writing?
|
---|
353 | offset = (ULONG)lpvBase - (ULONG)pMapping;
|
---|
354 | size = memInfo.RegionSize;
|
---|
355 | if(size > cbFlush) {
|
---|
356 | size = cbFlush;
|
---|
357 | }
|
---|
358 | dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
|
---|
359 |
|
---|
360 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
---|
361 | dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
|
---|
362 | goto fail;
|
---|
363 | }
|
---|
364 | if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
|
---|
365 | dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
|
---|
366 | goto fail;
|
---|
367 | }
|
---|
368 | if(nrBytesWritten != size) {
|
---|
369 | dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
|
---|
370 | goto fail;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
|
---|
374 |
|
---|
375 | if(cbFlush < memInfo.RegionSize)
|
---|
376 | break;
|
---|
377 |
|
---|
378 | cbFlush -= memInfo.RegionSize;
|
---|
379 | }
|
---|
380 | success:
|
---|
381 | return TRUE;
|
---|
382 | parmfail:
|
---|
383 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
384 | return FALSE;
|
---|
385 | fail:
|
---|
386 | return FALSE;
|
---|
387 | }
|
---|
388 | //******************************************************************************
|
---|
389 | //******************************************************************************
|
---|
390 | Win32MemMap *Win32MemMap::findMap(LPSTR lpszName)
|
---|
391 | {
|
---|
392 | if(lpszName == NULL)
|
---|
393 | return NULL;
|
---|
394 |
|
---|
395 | globalmapMutex.enter();
|
---|
396 | Win32MemMap *map = memmaps;
|
---|
397 |
|
---|
398 | if(map != NULL) {
|
---|
399 | while(map) {
|
---|
400 | if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
|
---|
401 | break;
|
---|
402 | map = map->next;
|
---|
403 | }
|
---|
404 | }
|
---|
405 | globalmapMutex.leave();
|
---|
406 | dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
|
---|
407 | return map;
|
---|
408 | }
|
---|
409 | //******************************************************************************
|
---|
410 | //******************************************************************************
|
---|
411 | Win32MemMap *Win32MemMap::findMap(ULONG address)
|
---|
412 | {
|
---|
413 | globalmapMutex.enter();
|
---|
414 | Win32MemMap *map = memmaps;
|
---|
415 |
|
---|
416 | if(map != NULL) {
|
---|
417 | while(map) {
|
---|
418 | if(map->pMapping && (ULONG)map->pMapping <= address &&
|
---|
419 | (ULONG)map->pMapping + map->mSize > address)
|
---|
420 | {
|
---|
421 | break;
|
---|
422 | }
|
---|
423 | map = map->next;
|
---|
424 | }
|
---|
425 | }
|
---|
426 | globalmapMutex.leave();
|
---|
427 | return map;
|
---|
428 | }
|
---|
429 | //******************************************************************************
|
---|
430 | //Assumes mutex has been acquired
|
---|
431 | //******************************************************************************
|
---|
432 | void Win32MemMap::deleteAll()
|
---|
433 | {
|
---|
434 | Win32MemMap *map = memmaps, *nextmap;
|
---|
435 | DWORD processId = GetCurrentProcess();
|
---|
436 |
|
---|
437 | //delete all maps created by this process
|
---|
438 | while(map) {
|
---|
439 | nextmap = map->next;
|
---|
440 | if(map->getProcessId() == processId) {
|
---|
441 | CloseHandle(memmaps->hMemMap);
|
---|
442 | }
|
---|
443 | else {
|
---|
444 | //delete all views created by this process for this map
|
---|
445 | Win32MemMapView::deleteViews(map);
|
---|
446 | }
|
---|
447 | map = nextmap;
|
---|
448 | }
|
---|
449 | }
|
---|
450 | //******************************************************************************
|
---|
451 | //******************************************************************************
|
---|
452 | Win32MemMapView::Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size,
|
---|
453 | ULONG fdwAccess)
|
---|
454 | {
|
---|
455 | LPVOID viewaddr = (LPVOID)((ULONG)map->getMappingAddr()+offset);
|
---|
456 | ULONG accessAttr = 0;
|
---|
457 | Win32MemMapView *tmpview = mapviews;
|
---|
458 |
|
---|
459 | errorState = 0;
|
---|
460 | mParentMap = map;
|
---|
461 | mSize = size;
|
---|
462 | mOffset = offset;
|
---|
463 | mProcessId = GetCurrentProcess();
|
---|
464 |
|
---|
465 | switch(fdwAccess) {
|
---|
466 | case FILE_MAP_READ:
|
---|
467 | accessAttr = PAG_READ;
|
---|
468 | mfAccess = MEMMAP_ACCESS_READ;
|
---|
469 | break;
|
---|
470 | case FILE_MAP_WRITE:
|
---|
471 | case FILE_MAP_COPY:
|
---|
472 | accessAttr = (PAG_READ|PAG_WRITE);
|
---|
473 | mfAccess = MEMMAP_ACCESS_WRITE;
|
---|
474 | break;
|
---|
475 | }
|
---|
476 | if(map->getMemName() != NULL) {
|
---|
477 | //shared memory map, so map it into our address space
|
---|
478 | if(OSLibDosGetNamedSharedMem((LPVOID *)&viewaddr, map->getMemName()) != OSLIB_NOERROR) {
|
---|
479 | dprintf(("new OSLibDosGetNamedSharedMem FAILED"));
|
---|
480 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
481 | errorState = 1;
|
---|
482 | return;
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
|
---|
487 | dprintf(("new OSLibDosAliasMem FAILED"));
|
---|
488 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
489 | errorState = 1;
|
---|
490 | return;
|
---|
491 | }
|
---|
492 |
|
---|
493 | dprintf(("Win32MemMapView::Win32MemMapView: created %x (alias for %x), size %d", pMapView, viewaddr, size));
|
---|
494 |
|
---|
495 | globalviewMutex.enter();
|
---|
496 | if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
|
---|
497 | next = mapviews;
|
---|
498 | mapviews = this;
|
---|
499 | }
|
---|
500 | else {
|
---|
501 | while(tmpview->next) {
|
---|
502 | if(tmpview->next->getViewAddr() > pMapView) {
|
---|
503 | break;
|
---|
504 | }
|
---|
505 | tmpview = tmpview->next;
|
---|
506 | }
|
---|
507 | next = tmpview->next;
|
---|
508 | tmpview->next = this;
|
---|
509 | }
|
---|
510 | globalviewMutex.leave();
|
---|
511 | }
|
---|
512 | //******************************************************************************
|
---|
513 | //******************************************************************************
|
---|
514 | Win32MemMapView::~Win32MemMapView()
|
---|
515 | {
|
---|
516 | if(errorState != 0)
|
---|
517 | return;
|
---|
518 |
|
---|
519 | if(mfAccess != MEMMAP_ACCESS_READ)
|
---|
520 | mParentMap->flushView(mOffset, mSize);
|
---|
521 |
|
---|
522 | OSLibDosFreeMem(pMapView);
|
---|
523 |
|
---|
524 | globalviewMutex.enter();
|
---|
525 | Win32MemMapView *view = mapviews;
|
---|
526 |
|
---|
527 | if(view == this) {
|
---|
528 | mapviews = next;
|
---|
529 | }
|
---|
530 | else {
|
---|
531 | while(view->next) {
|
---|
532 | if(view->next == this)
|
---|
533 | break;
|
---|
534 | view = view->next;
|
---|
535 | }
|
---|
536 | if(view->next) {
|
---|
537 | view->next = next;
|
---|
538 | }
|
---|
539 | else dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
|
---|
540 | }
|
---|
541 | globalviewMutex.leave();
|
---|
542 | }
|
---|
543 | //******************************************************************************
|
---|
544 | //******************************************************************************
|
---|
545 | void Win32MemMapView::deleteViews(Win32MemMap *map)
|
---|
546 | {
|
---|
547 | DWORD processId = GetCurrentProcess();
|
---|
548 |
|
---|
549 | globalviewMutex.enter();
|
---|
550 | Win32MemMapView *view = mapviews, *nextview;
|
---|
551 |
|
---|
552 | if(view != NULL) {
|
---|
553 | while(view) {
|
---|
554 | nextview = view->next;
|
---|
555 | if(view->getParentMap() == map && view->getProcessId() == processId)
|
---|
556 | {
|
---|
557 | globalviewMutex.leave();
|
---|
558 | delete view;
|
---|
559 | globalviewMutex.enter();
|
---|
560 | }
|
---|
561 | view = nextview;
|
---|
562 | }
|
---|
563 | }
|
---|
564 | globalviewMutex.leave();
|
---|
565 | }
|
---|
566 | //******************************************************************************
|
---|
567 | //******************************************************************************
|
---|
568 | Win32MemMap *Win32MemMapView::findMapByView(ULONG address, ULONG *offset,
|
---|
569 | ULONG accessType,
|
---|
570 | Win32MemMapView **pView)
|
---|
571 | {
|
---|
572 | globalviewMutex.enter();
|
---|
573 | Win32MemMapView *view = mapviews;
|
---|
574 |
|
---|
575 | *offset = 0;
|
---|
576 |
|
---|
577 | if(view != NULL) {
|
---|
578 | while(view && (ULONG)view->getViewAddr() <= address) {
|
---|
579 | if((ULONG)view->getViewAddr() <= address &&
|
---|
580 | (ULONG)view->getViewAddr() + view->getSize() >= address &&
|
---|
581 | view->getAccessFlags() >= accessType)
|
---|
582 | {
|
---|
583 | *offset = view->getOffset() + (address - (ULONG)view->getViewAddr());
|
---|
584 | goto success;
|
---|
585 | }
|
---|
586 | view = view->next;
|
---|
587 | }
|
---|
588 | //failure if we get here
|
---|
589 | view = NULL;
|
---|
590 | }
|
---|
591 | success:
|
---|
592 | globalviewMutex.leave();
|
---|
593 | if(pView) *pView = view;
|
---|
594 | return (view) ? view->getParentMap() : NULL;
|
---|
595 | }
|
---|
596 | //******************************************************************************
|
---|
597 | //******************************************************************************
|
---|
598 | Win32MemMapView *Win32MemMapView::findView(LPVOID address)
|
---|
599 | {
|
---|
600 | Win32MemMapView *view = mapviews;
|
---|
601 |
|
---|
602 | if(view != NULL) {
|
---|
603 | while(view) {
|
---|
604 | if(view->getViewAddr() == address)
|
---|
605 | {
|
---|
606 | break;
|
---|
607 | }
|
---|
608 | view = view->next;
|
---|
609 | }
|
---|
610 | }
|
---|
611 | return view;
|
---|
612 | }
|
---|
613 | //******************************************************************************
|
---|
614 | //******************************************************************************
|
---|
615 |
|
---|