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

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

memory map commit bugfix

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