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

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

mmap + share hack

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