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

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

mutex fixes + added vsemaphore class

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