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

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

memory map bugfixes + added method for querying image size

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