source: trunk/src/kernel32/mmap.cpp@ 4230

Last change on this file since 4230 was 4230, checked in by sandervl, 25 years ago

memory map fixes + extra reg keys for init

File size: 22.5 KB
Line 
1/* $Id: mmap.cpp,v 1.44 2000-09-10 21:54:07 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 * TODO: Memory mapped files with views that extend the file (not 100% correct now)
19 * TODO: Suspend all threads when a page is committed (possible that another thread
20 * accesses the same memory before the page is read from disk
21 *
22 * Project Odin Software License can be found in LICENSE.TXT
23 *
24 */
25#include <os2win.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <win\virtual.h>
30#include <vmutex.h>
31#include <handlemanager.h>
32#include "mmap.h"
33#include "oslibdos.h"
34#include <winimagepeldr.h>
35
36#define DBG_LOCALLOG DBG_mmap
37#include "dbglocal.h"
38
39//NOTE: This must be in the local data segment -> if a shared semaphore was
40// created by a different process, the handle returned by DosOpenMutexSem
41// will be returned in hGlobalMapMutex
42HMTX hGlobalMapMutex = 0;
43
44//Global DLL Data
45#pragma data_seg(_GLOBALDATA)
46Win32MemMap *Win32MemMap::memmaps = NULL;
47VMutex globalmapMutex(VMUTEX_SHARED, &hGlobalMapMutex);
48#pragma data_seg()
49VMutex globalviewMutex;
50Win32MemMapView *Win32MemMapView::mapviews = NULL;
51
52//******************************************************************************
53//TODO: sharing between processes
54//******************************************************************************
55Win32MemMap::Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
56 : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0), image(0),
57 fClosed(FALSE)
58{
59 globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
60 next = memmaps;
61 memmaps = this;
62 globalmapMutex.leave(&hGlobalMapMutex);
63
64 hMemFile = hfile;
65
66 mSize = size;
67 mProtFlags = fdwProtect;
68 mProcessId = GetCurrentProcess();
69
70 if(lpszName) {
71 lpszMapName = (char *)_smalloc(strlen(lpszName)+1);
72 strcpy(lpszMapName, lpszName);
73 }
74 else lpszMapName = NULL;
75}
76//******************************************************************************
77//Map constructor used for executable image maps (only used internally)
78//******************************************************************************
79Win32MemMap::Win32MemMap(Win32PeLdrImage *pImage, ULONG baseAddress, ULONG size)
80 : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0), image(0),
81 fClosed(FALSE)
82{
83 globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
84 next = memmaps;
85 memmaps = this;
86 globalmapMutex.leave(&hGlobalMapMutex);
87
88 hMemFile = -1;
89
90 mSize = size;
91 mProtFlags = PAGE_READWRITE;
92 mProcessId = GetCurrentProcess();
93
94 pMapping = (LPVOID)baseAddress;
95
96 image = pImage;
97 lpszMapName= NULL;
98}
99//******************************************************************************
100//******************************************************************************
101BOOL Win32MemMap::Init(HANDLE hMemMap)
102{
103 mapMutex.enter();
104 if(hMemFile != -1)
105 {
106#if 0
107 if(DuplicateHandle(mProcessId, hMemFile, GetCurrentProcess(),
108 &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
109#else
110 DWORD dwOdinOptions;
111
112 if(!(mProtFlags & PAGE_READWRITE)) {
113 dwOdinOptions = DUPLICATE_ACCESS_READ | DUPLICATE_SHARE_DENYNONE;
114 }
115 else dwOdinOptions = DUPLICATE_ACCESS_READWRITE | DUPLICATE_SHARE_DENYNONE;
116
117 if(HMDuplicateHandleOdin(mProcessId, hMemFile, GetCurrentProcess(),
118 &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS, dwOdinOptions) == FALSE)
119#endif
120 {
121 dprintf(("Win32MemMap::Init: DuplicateHandle failed!"));
122 goto fail;
123 }
124 mSize = SetFilePointer(hMemFile, 0, NULL, FILE_BEGIN);
125 mSize = SetFilePointer(hMemFile, 0, NULL, FILE_END);
126 if(mSize == -1) {
127 dprintf(("Win32MemMap::init: SetFilePointer failed to set pos end"));
128 goto fail;
129 }
130 //SvL: Temporary limitation of size (Warp Server Advanced doesn't allow
131 // one to reserve more than 450 MB (unless you override the virtual
132 // memory max limit) of continuous memory; (Warp 4 much less))
133 if(mSize > 64*1024*1024) {
134 mSize = 64*1024*1024;
135 }
136 }
137
138 dprintf(("CreateFileMappingA for file %x, prot %x size %d, name %s", hMemFile, mProtFlags, mSize, lpszMapName));
139 this->hMemMap = hMemMap;
140 mapMutex.leave();
141 return TRUE;
142fail:
143 mapMutex.leave();
144 return FALSE;
145}
146//******************************************************************************
147//******************************************************************************
148Win32MemMap::~Win32MemMap()
149{
150 Win32MemMapView::deleteViews(this); //delete all views of our memory mapped file
151
152 dprintf(("Win32MemMap dtor: deleting view %x %x", pMapping, mSize));
153
154 mapMutex.enter();
155 if(lpszMapName) {
156 free(lpszMapName);
157 }
158 if(pMapping && !image) {
159 if(lpszMapName) {
160 OSLibDosFreeMem(pMapping);
161 }
162 else VirtualFree(pMapping, mSize, MEM_RELEASE);
163
164 pMapping = NULL;
165 }
166 if(hMemFile != -1) {
167 CloseHandle(hMemFile);
168 hMemFile = -1;
169 }
170 mapMutex.leave();
171
172 globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
173 Win32MemMap *map = memmaps;
174
175 if(map == this) {
176 memmaps = next;
177 }
178 else {
179 while(map->next) {
180 if(map->next == this)
181 break;
182 map = map->next;
183 }
184 if(map->next) {
185 map->next = next;
186 }
187 else dprintf(("Win32MemMap::~Win32MemMap: map not found!! (%x)", this));
188 }
189 globalmapMutex.leave(&hGlobalMapMutex);
190}
191//******************************************************************************
192//We determine whether a page has been modified by checking it's protection flags
193//If the write flag is set, this means commitPage had to enable this due to a pagefault
194//(all pages are readonly until the app tries to write to it)
195//******************************************************************************
196BOOL Win32MemMap::commitPage(ULONG offset, BOOL fWriteAccess, int nrpages)
197{
198 MEMORY_BASIC_INFORMATION memInfo;
199 LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
200 DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
201 DWORD oldProt, newProt, nrBytesRead, size;
202 int i;
203
204// mapMutex.enter();
205
206 if(image) {
207 return image->commitPage(pageAddr, fWriteAccess);
208 }
209 newProt = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
210
211 dprintf(("Win32MemMap::commitPage %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
212 if(hMemFile != -1)
213 {
214 int faultsize = nrpages*PAGE_SIZE;
215
216 offset = pageAddr - (ULONG)pMapping;
217 if(offset + faultsize > mSize) {
218 faultsize = mSize - offset;
219 }
220
221 while(faultsize) {
222 if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
223 dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, nrpages*PAGE_SIZE));
224 goto fail;
225 }
226 memInfo.RegionSize = min(memInfo.RegionSize, faultsize);
227 //Only changes the state of the pages with the same attribute flags
228 //(returned in memInfo.RegionSize)
229 //If it's smaller than the mNrPages, it simply means one or more of the
230 //other pages have already been committed
231 if(!(memInfo.State & MEM_COMMIT))
232 {
233 if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
234 goto fail;
235 }
236 if(!fWriteAccess) {
237 offset = pageAddr - (ULONG)pMapping;
238 size = memInfo.RegionSize;
239 if(offset + size > mSize) {
240 dprintf(("Adjusting size from %d to %d", size, mSize - offset));
241 size = mSize - offset;
242 }
243 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
244 dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
245 goto fail;
246 }
247 if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
248 dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
249 goto fail;
250 }
251 if(nrBytesRead != size) {
252 dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
253 goto fail;
254 }
255 }
256 if(newProt != PAGE_READWRITE) {
257 if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
258 goto fail;
259 }
260 }
261 }
262 faultsize -= memInfo.RegionSize;
263 pageAddr += memInfo.RegionSize;
264 }
265 }
266 else {
267 ULONG sizeleft = nrpages*PAGE_SIZE;
268 while(sizeleft)
269 {
270 if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
271 dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed", pageAddr, sizeleft));
272 goto fail;
273 }
274 memInfo.RegionSize = min(memInfo.RegionSize, sizeleft);
275
276 if(!(memInfo.State & MEM_COMMIT))
277 {//if it's already committed, then the app tried to write to it
278 if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE)
279 goto fail;
280 }
281 memInfo.RegionSize = (memInfo.RegionSize+PAGE_SIZE-1) & ~0xfff;
282 pageAddr += memInfo.RegionSize;
283 sizeleft -= memInfo.RegionSize;
284 }
285 }
286
287// mapMutex.leave();
288 return TRUE;
289fail:
290// mapMutex.leave();
291 return FALSE;
292}
293//******************************************************************************
294//******************************************************************************
295BOOL Win32MemMap::unmapViewOfFile(Win32MemMapView *view)
296{
297 dprintf(("Win32MemMap::unmapViewOfFile %x (nrmaps=%d)", view, nrMappings));
298 mapMutex.enter();
299
300 if(nrMappings == 0)
301 goto fail;
302
303 delete view;
304
305 if(--nrMappings == 0) {
306 VirtualFree(pMapping, mSize, MEM_RELEASE);
307 pMapping = NULL;
308 }
309 mapMutex.leave();
310
311 //if there are no more mappings left and the memory map's handle has been
312 //closed, then delete the object
313 if(nrMappings == 0 && fClosed) {
314 delete this;
315 }
316 return TRUE;
317fail:
318 mapMutex.leave();
319 return FALSE;
320}
321//******************************************************************************
322//******************************************************************************
323LPVOID Win32MemMap::mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess)
324{
325 DWORD processId = GetCurrentProcess();
326
327 mapMutex.enter();
328 ULONG memFlags = (mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY));
329 ULONG fAlloc = 0;
330 Win32MemMapView *mapview;
331
332 //@@@PH: if(fdwAccess & ~(FILE_MAP_WRITE|FILE_MAP_READ|FILE_MAP_COPY))
333 // Docs say FILE_MAP_ALL_ACCESS is same as FILE_MAP_WRITE. Doesn't match reality though.
334 if(fdwAccess & ~FILE_MAP_ALL_ACCESS)
335 goto parmfail;
336 if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
337 goto parmfail;
338 if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY)))
339 goto parmfail;
340
341 //@@@PH
342 if (fdwAccess != FILE_MAP_ALL_ACCESS)
343 if((fdwAccess & FILE_MAP_COPY) && !(mProtFlags & PAGE_WRITECOPY))
344 goto parmfail;
345
346 if(offset+size > mSize && (!(fdwAccess & FILE_MAP_WRITE) || hMemFile == -1))
347 goto parmfail;
348
349 //SvL: TODO: Doesn't work for multiple views
350 if(offset+size > mSize) {
351 mSize = offset+size;
352 }
353
354//TODO: If committed, read file into memory
355#if 0
356 if(mProtFlags & SEC_COMMIT)
357 fAlloc |= MEM_COMMIT;
358 else
359 if(mProtFlags & SEC_RESERVE)
360 fAlloc |= MEM_RESERVE;
361#else
362 fAlloc = MEM_RESERVE;
363#endif
364
365 //Memory has already been allocated for executable image maps (only used internally)
366 if(!pMapping && nrMappings == 0) {//if not mapped, reserve/commit entire view
367 //SvL: Always read/write access or else ReadFile will crash once we
368 // start committing pages.
369 // This is most likely an OS/2 bug and doesn't happen in Aurora
370 // when allocating memory with the PAG_ANY bit set. (without this
371 // flag it will also crash)
372 if(hMemFile == -1 && lpszMapName) {
373 pMapping = VirtualAllocShared(mSize, fAlloc, PAGE_READWRITE, lpszMapName);
374 }
375 else {
376 pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
377 }
378 if(pMapping == NULL) {
379 dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
380 goto fail;
381 }
382 //Windows NT seems to commit memory for memory maps, regardsless of the SEC_COMMIT flag
383 if((hMemFile == -1 && !image)) {//commit memory
384 VirtualAlloc(pMapping, mSize, MEM_COMMIT, PAGE_READWRITE);
385 }
386 if(hMemFile != -1 && (mProtFlags & SEC_COMMIT)) {
387 DWORD nrPages = mSize >> PAGE_SHIFT;
388 if(mSize & 0xFFF)
389 nrPages++;
390
391 commitPage(0, FALSE, nrPages);
392 }
393 }
394 mapview = new Win32MemMapView(this, offset, (size == 0) ? mSize : size, fdwAccess);
395 if(mapview == NULL) {
396 goto fail;
397 }
398 if(mapview->everythingOk() == FALSE) {
399 dprintf(("Win32MemMap::mapFileView: !mapview->everythingOk"));
400 delete mapview;
401 goto fail;
402 }
403 nrMappings++;
404 mapMutex.leave();
405 return mapview->getViewAddr();
406
407parmfail:
408 dprintf(("Win32MemMap::mapFileView: ERROR_INVALID_PARAMETER"));
409 SetLastError(ERROR_INVALID_PARAMETER);
410fail:
411 mapMutex.leave();
412 return 0;
413}
414//******************************************************************************
415//We determine whether a page has been modified by checking it's protection flags
416//If the write flag is set, this means commitPage had to enable this due to a pagefault
417//(all pages are readonly until the app tries to modify the contents of the page)
418//
419//TODO: Are apps allowed to change the protection flags of memory mapped pages?
420// I'm assuming they aren't for now.
421//******************************************************************************
422BOOL Win32MemMap::flushView(ULONG offset, ULONG cbFlush)
423{
424 LPVOID lpvBase = (LPVOID)((ULONG)pMapping+offset);
425 MEMORY_BASIC_INFORMATION memInfo;
426 ULONG nrBytesWritten, size;
427 int i;
428
429 if(image) //no flushing for image maps
430 return TRUE;
431
432 dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
433 if(nrMappings == 0)
434 goto parmfail;
435
436 if(cbFlush == 0)
437 cbFlush = mSize;
438
439 if(lpvBase < pMapping || (ULONG)lpvBase+cbFlush > (ULONG)pMapping+mSize)
440 goto parmfail;
441
442 if(mProtFlags & PAGE_READONLY)
443 goto parmfail;
444
445 if(hMemFile == -1)
446 goto success; //TODO: Return an error here?
447
448 while(cbFlush) {
449 if(VirtualQuery((LPSTR)lpvBase, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
450 dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
451 goto fail;
452 }
453 //If a page (or range of pages) is reserved or write protected, we
454 //won't bother flushing it to disk
455 if(memInfo.State & MEM_COMMIT &&
456 memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
457 {//committed and allowed for writing?
458 offset = (ULONG)lpvBase - (ULONG)pMapping;
459 size = memInfo.RegionSize;
460 if(size > cbFlush) {
461 size = cbFlush;
462 }
463 dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
464
465 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
466 dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
467 goto fail;
468 }
469 if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
470 dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
471 goto fail;
472 }
473 if(nrBytesWritten != size) {
474 dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
475 goto fail;
476 }
477 }
478 lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
479
480 if(cbFlush < memInfo.RegionSize)
481 break;
482
483 cbFlush -= memInfo.RegionSize;
484 }
485success:
486 return TRUE;
487parmfail:
488 SetLastError(ERROR_INVALID_PARAMETER);
489 return FALSE;
490fail:
491 return FALSE;
492}
493//******************************************************************************
494//******************************************************************************
495Win32MemMap *Win32MemMap::findMap(LPSTR lpszName)
496{
497 if(lpszName == NULL)
498 return NULL;
499
500 globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
501 Win32MemMap *map = memmaps;
502
503 if(map != NULL) {
504 while(map) {
505 if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
506 break;
507 map = map->next;
508 }
509 }
510 globalmapMutex.leave(&hGlobalMapMutex);
511 if(!map) dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
512 return map;
513}
514//******************************************************************************
515//******************************************************************************
516Win32MemMap *Win32MemMap::findMap(ULONG address)
517{
518 globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
519 Win32MemMap *map = memmaps;
520
521 if(map != NULL) {
522 while(map) {
523 if(map->pMapping && (ULONG)map->pMapping <= address &&
524 (ULONG)map->pMapping + map->mSize > address)
525 {
526 break;
527 }
528 map = map->next;
529 }
530 }
531 globalmapMutex.leave(&hGlobalMapMutex);
532 return map;
533}
534//******************************************************************************
535//******************************************************************************
536void Win32MemMap::deleteAll()
537{
538 Win32MemMap *map = memmaps, *nextmap;
539 DWORD processId = GetCurrentProcess();
540
541 //delete all maps created by this process
542 globalviewMutex.enter();
543 while(map) {
544 nextmap = map->next;
545 if(map->getProcessId() == processId) {
546 //Delete map directly for executable images (only used internally)
547 if(map->getImage()) {
548 delete map;
549 }
550 else {
551 if(!map->isClosed())
552 CloseHandle(memmaps->hMemMap);
553 delete map;
554 }
555 }
556 else {
557 //delete all views created by this process for this map
558 Win32MemMapView::deleteViews(map);
559 }
560 map = nextmap;
561 }
562 globalviewMutex.leave();
563}
564//******************************************************************************
565//******************************************************************************
566Win32MemMapView::Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size,
567 ULONG fdwAccess)
568{
569 LPVOID viewaddr = (LPVOID)((ULONG)map->getMappingAddr()+offset);
570 ULONG accessAttr = 0;
571 Win32MemMapView *tmpview = mapviews;
572
573 errorState = 0;
574 mParentMap = map;
575 mSize = size;
576 mOffset = offset;
577 mProcessId = GetCurrentProcess();
578
579 switch(fdwAccess) {
580 case FILE_MAP_READ:
581 accessAttr = PAG_READ;
582 mfAccess = MEMMAP_ACCESS_READ;
583 break;
584 case FILE_MAP_ALL_ACCESS:
585 case FILE_MAP_WRITE:
586 case FILE_MAP_WRITE|FILE_MAP_READ:
587 case FILE_MAP_COPY:
588 accessAttr = (PAG_READ|PAG_WRITE);
589 mfAccess = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
590 break;
591 }
592 if(map->getMemName() != NULL && map->getFileHandle() == -1) {
593 //shared memory map, so map it into our address space
594 if(OSLibDosGetNamedSharedMem((LPVOID *)&viewaddr, map->getMemName()) != OSLIB_NOERROR) {
595 dprintf(("new OSLibDosGetNamedSharedMem FAILED"));
596 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
597 errorState = 1;
598 return;
599 }
600 }
601
602 //view == memory mapping for executable images (only used internally)
603 if(map->getImage()) {
604 pMapView = map->getMappingAddr();
605 }
606 else {
607 if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
608 dprintf(("new OSLibDosAliasMem FAILED"));
609 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
610 errorState = 1;
611 return;
612 }
613 }
614
615 dprintf(("Win32MemMapView::Win32MemMapView: created %x (alias for %x), size %d", pMapView, viewaddr, size));
616
617 globalviewMutex.enter();
618 if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
619 next = mapviews;
620 mapviews = this;
621 }
622 else {
623 while(tmpview->next) {
624 if(tmpview->next->getViewAddr() > pMapView) {
625 break;
626 }
627 tmpview = tmpview->next;
628 }
629 next = tmpview->next;
630 tmpview->next = this;
631 }
632 globalviewMutex.leave();
633}
634//******************************************************************************
635//******************************************************************************
636Win32MemMapView::~Win32MemMapView()
637{
638 if(errorState != 0)
639 return;
640
641 dprintf(("Win32MemMapView dtor: deleting view %x %x", mOffset, mSize));
642
643 if(mfAccess & MEMMAP_ACCESS_WRITE)
644 mParentMap->flushView(mOffset, mSize);
645
646 //Don't free memory for executable image map views (only used internally)
647 if(!mParentMap->getImage())
648 OSLibDosFreeMem(pMapView);
649
650 globalviewMutex.enter();
651 Win32MemMapView *view = mapviews;
652
653 if(view == this) {
654 mapviews = next;
655 }
656 else {
657 while(view->next) {
658 if(view->next == this)
659 break;
660 view = view->next;
661 }
662 if(view->next) {
663 view->next = next;
664 }
665 else dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
666 }
667 globalviewMutex.leave();
668}
669//******************************************************************************
670//******************************************************************************
671void Win32MemMapView::deleteViews(Win32MemMap *map)
672{
673 globalviewMutex.enter();
674 Win32MemMapView *view = mapviews, *nextview;
675
676 if(view != NULL) {
677 while(view) {
678 nextview = view->next;
679 if(view->getParentMap() == map)
680 {
681 globalviewMutex.leave();
682 delete view;
683 globalviewMutex.enter();
684 }
685 view = nextview;
686 }
687 }
688 globalviewMutex.leave();
689}
690//******************************************************************************
691//******************************************************************************
692Win32MemMap *Win32MemMapView::findMapByView(ULONG address, ULONG *offset,
693 ULONG accessType,
694 Win32MemMapView **pView)
695{
696 globalviewMutex.enter();
697 Win32MemMapView *view = mapviews;
698
699 *offset = 0;
700
701 if(view != NULL) {
702 while(view && (ULONG)view->getViewAddr() <= address) {
703 if((ULONG)view->getViewAddr() <= address &&
704 (ULONG)view->getViewAddr() + view->getSize() > address &&
705 view->getAccessFlags() >= accessType)
706 {
707 *offset = view->getOffset() + (address - (ULONG)view->getViewAddr());
708 goto success;
709 }
710 view = view->next;
711 }
712 //failure if we get here
713 view = NULL;
714 }
715success:
716 if(view && !view->getParentMap()->isImageMap())
717 dprintf(("findMapByView %x %x -> %x off %x", address, accessType, view->getViewAddr(), *offset));
718
719 globalviewMutex.leave();
720 if(pView) *pView = view;
721 return (view) ? view->getParentMap() : NULL;
722}
723//******************************************************************************
724//******************************************************************************
725Win32MemMapView *Win32MemMapView::findView(LPVOID address)
726{
727 Win32MemMapView *view = mapviews;
728
729 globalviewMutex.enter();
730 if(view != NULL) {
731 while(view) {
732 if(view->getViewAddr() == address)
733 {
734 break;
735 }
736 view = view->next;
737 }
738 }
739 globalviewMutex.leave();
740 return view;
741}
742//******************************************************************************
743//******************************************************************************
744
Note: See TracBrowser for help on using the repository browser.