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

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

LoadLibraryExA + memory map close fix, added new registry keys for installation, don't log guard page violation

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