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

Last change on this file since 8121 was 8121, checked in by sandervl, 23 years ago

all named file mappings are shared

File size: 25.8 KB
Line 
1/* $Id: mmap.cpp,v 1.54 2002-03-24 13:10:30 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 = GetCurrentProcessId();
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 = GetCurrentProcessId();
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(GetCurrentProcess(), 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(GetCurrentProcess(), 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 dprintf(("Win32MemMap::Release %s (%d)", lpszMapName, referenced-1));
197 --referenced;
198 if(nrMappings == 0 && referenced == 0) {
199 delete this;
200 }
201}
202//******************************************************************************
203//We determine whether a page has been modified by checking it's protection flags
204//If the write flag is set, this means commitPage had to enable this due to a pagefault
205//(all pages are readonly until the app tries to write to it)
206//******************************************************************************
207BOOL Win32MemMap::commitPage(ULONG offset, BOOL fWriteAccess, int nrpages)
208{
209 MEMORY_BASIC_INFORMATION memInfo;
210 LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
211 DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
212 DWORD oldProt, newProt, nrBytesRead, size;
213 int i;
214
215// mapMutex.enter();
216
217 if(image) {
218 return image->commitPage(pageAddr, fWriteAccess);
219 }
220 newProt = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
221
222 dprintf(("Win32MemMap::commitPage %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
223 if(hMemFile != -1)
224 {
225 int faultsize = nrpages*PAGE_SIZE;
226
227 offset = pageAddr - (ULONG)pMapping;
228 if(offset + faultsize > mSize) {
229 faultsize = mSize - offset;
230 }
231
232 while(faultsize) {
233 if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
234 dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, nrpages*PAGE_SIZE));
235 goto fail;
236 }
237 memInfo.RegionSize = min(memInfo.RegionSize, faultsize);
238 //Only changes the state of the pages with the same attribute flags
239 //(returned in memInfo.RegionSize)
240 //If it's smaller than the mNrPages, it simply means one or more of the
241 //other pages have already been committed
242 if(!(memInfo.State & MEM_COMMIT))
243 {
244 if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
245 goto fail;
246 }
247 offset = pageAddr - (ULONG)pMapping;
248 size = memInfo.RegionSize;
249 if(offset + size > mSize) {
250 dprintf(("Adjusting size from %d to %d", size, mSize - offset));
251 size = mSize - offset;
252 }
253 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
254 dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
255 goto fail;
256 }
257 if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
258 dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
259 goto fail;
260 }
261 if(nrBytesRead != size) {
262 dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
263 goto fail;
264 }
265 if(newProt != PAGE_READWRITE) {
266 if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
267 goto fail;
268 }
269 }
270 }
271 faultsize -= memInfo.RegionSize;
272 pageAddr += memInfo.RegionSize;
273 }
274 }
275 else {
276 ULONG sizeleft = nrpages*PAGE_SIZE;
277 while(sizeleft)
278 {
279 if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
280 dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed", pageAddr, sizeleft));
281 goto fail;
282 }
283 memInfo.RegionSize = min(memInfo.RegionSize, sizeleft);
284
285 if(!(memInfo.State & MEM_COMMIT))
286 {//if it's already committed, then the app tried to write to it
287 if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE)
288 goto fail;
289 }
290 memInfo.RegionSize = (memInfo.RegionSize+PAGE_SIZE-1) & ~0xfff;
291 pageAddr += memInfo.RegionSize;
292 sizeleft -= memInfo.RegionSize;
293 }
294 }
295
296// mapMutex.leave();
297 return TRUE;
298fail:
299// mapMutex.leave();
300 return FALSE;
301}
302//******************************************************************************
303//******************************************************************************
304BOOL Win32MemMap::unmapViewOfFile(Win32MemMapView *view)
305{
306 dprintf(("Win32MemMap::unmapViewOfFile %x (nrmaps=%d)", view, nrMappings));
307 mapMutex.enter();
308
309 if(nrMappings == 0)
310 goto fail;
311
312 delete view;
313
314 if(--nrMappings == 0) {
315 VirtualFree(pMapping, 0, MEM_RELEASE);
316 pMapping = NULL;
317 }
318 mapMutex.leave();
319
320 //if there are no more mappings left and the memory map's handle has been
321 //closed, then delete the object
322 if(nrMappings == 0 && referenced == 0) {
323 delete this;
324 }
325 return TRUE;
326fail:
327 mapMutex.leave();
328 if(nrMappings == 0 && referenced == 0) {
329 delete this;
330 }
331 return FALSE;
332}
333//******************************************************************************
334//******************************************************************************
335LPVOID Win32MemMap::mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess)
336{
337 DWORD processId = GetCurrentProcessId();
338
339 mapMutex.enter();
340 ULONG memFlags = (mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY));
341 ULONG fAlloc = 0;
342 Win32MemMapView *mapview;
343
344 //@@@PH: if(fdwAccess & ~(FILE_MAP_WRITE|FILE_MAP_READ|FILE_MAP_COPY))
345 // Docs say FILE_MAP_ALL_ACCESS is same as FILE_MAP_WRITE. Doesn't match reality though.
346 if(fdwAccess & ~FILE_MAP_ALL_ACCESS)
347 goto parmfail;
348 if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
349 goto parmfail;
350 if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY)))
351 goto parmfail;
352
353 //@@@PH
354 if (fdwAccess != FILE_MAP_ALL_ACCESS)
355 if((fdwAccess & FILE_MAP_COPY) && !(mProtFlags & PAGE_WRITECOPY))
356 goto parmfail;
357
358 if(offset+size > mSize && (!(fdwAccess & FILE_MAP_WRITE) || hMemFile == -1))
359 goto parmfail;
360
361 //SvL: TODO: Doesn't work for multiple views
362 if(offset+size > mSize) {
363 mSize = offset+size;
364 }
365
366//TODO: If committed, read file into memory
367#if 0
368 if(mProtFlags & SEC_COMMIT)
369 fAlloc |= MEM_COMMIT;
370 else
371 if(mProtFlags & SEC_RESERVE)
372 fAlloc |= MEM_RESERVE;
373#else
374 fAlloc = MEM_RESERVE;
375#endif
376
377 //Memory has already been allocated for executable image maps (only used internally)
378 if(!pMapping && nrMappings == 0) {//if not mapped, reserve/commit entire view
379 //SvL: Always read/write access or else ReadFile will crash once we
380 // start committing pages.
381 // This is most likely an OS/2 bug and doesn't happen in Aurora
382 // when allocating memory with the PAG_ANY bit set. (without this
383 // flag it will also crash)
384 //All named file mappings are shared (files & memory only)
385 if(lpszMapName) {
386 pMapping = VirtualAllocShared(mSize, fAlloc, PAGE_READWRITE, lpszMapName);
387 }
388 else {
389 pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
390 }
391 if(pMapping == NULL) {
392 dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
393 goto fail;
394 }
395 //Windows NT seems to commit memory for memory maps, regardsless of the SEC_COMMIT flag
396 if((hMemFile == -1 && !image)) {//commit memory
397 VirtualAlloc(pMapping, mSize, MEM_COMMIT, PAGE_READWRITE);
398 }
399 if(hMemFile != -1 && (mProtFlags & SEC_COMMIT)) {
400 DWORD nrPages = mSize >> PAGE_SHIFT;
401 if(mSize & 0xFFF)
402 nrPages++;
403
404 commitPage(0, FALSE, nrPages);
405 }
406 }
407 mapview = new Win32MemMapView(this, offset, (size == 0) ? mSize : size, fdwAccess);
408 if(mapview == NULL) {
409 goto fail;
410 }
411 if(mapview->everythingOk() == FALSE) {
412 dprintf(("Win32MemMap::mapFileView: !mapview->everythingOk"));
413 delete mapview;
414 goto fail;
415 }
416 nrMappings++;
417 mapMutex.leave();
418 return mapview->getViewAddr();
419
420parmfail:
421 dprintf(("Win32MemMap::mapFileView: ERROR_INVALID_PARAMETER"));
422 SetLastError(ERROR_INVALID_PARAMETER);
423fail:
424 mapMutex.leave();
425 return 0;
426}
427//******************************************************************************
428//We determine whether a page has been modified by checking it's protection flags
429//If the write flag is set, this means commitPage had to enable this due to a pagefault
430//(all pages are readonly until the app tries to modify the contents of the page)
431//
432//TODO: Are apps allowed to change the protection flags of memory mapped pages?
433// I'm assuming they aren't for now.
434//******************************************************************************
435BOOL Win32MemMap::flushView(ULONG offset, ULONG cbFlush)
436{
437 LPVOID lpvBase = (LPVOID)((ULONG)pMapping+offset);
438 MEMORY_BASIC_INFORMATION memInfo;
439 ULONG nrBytesWritten, size;
440 int i;
441
442 if(image) //no flushing for image maps
443 return TRUE;
444
445 dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
446 if(nrMappings == 0)
447 goto parmfail;
448
449 if(cbFlush == 0)
450 cbFlush = mSize;
451
452 if(lpvBase < pMapping || (ULONG)lpvBase+cbFlush > (ULONG)pMapping+mSize)
453 goto parmfail;
454
455 if(mProtFlags & PAGE_READONLY)
456 goto parmfail;
457
458 if(hMemFile == -1)
459 goto success; //TODO: Return an error here?
460
461 while(cbFlush) {
462 if(VirtualQuery((LPSTR)lpvBase, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
463 dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
464 goto fail;
465 }
466 //If a page (or range of pages) is reserved or write protected, we
467 //won't bother flushing it to disk
468 if(memInfo.State & MEM_COMMIT &&
469 memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
470 {//committed and allowed for writing?
471 offset = (ULONG)lpvBase - (ULONG)pMapping;
472 size = memInfo.RegionSize;
473 if(size > cbFlush) {
474 size = cbFlush;
475 }
476 dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
477
478 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
479 dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
480 goto fail;
481 }
482 if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
483 dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
484 goto fail;
485 }
486 if(nrBytesWritten != size) {
487 dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
488 goto fail;
489 }
490 }
491 lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
492
493 if(cbFlush < memInfo.RegionSize)
494 break;
495
496 cbFlush -= memInfo.RegionSize;
497 }
498success:
499 return TRUE;
500parmfail:
501 SetLastError(ERROR_INVALID_PARAMETER);
502 return FALSE;
503fail:
504 return FALSE;
505}
506//******************************************************************************
507//******************************************************************************
508Win32MemMap *Win32MemMap::findMap(LPSTR lpszName)
509{
510 if(lpszName == NULL)
511 return NULL;
512
513 globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
514 Win32MemMap *map = memmaps;
515
516 if(map != NULL) {
517 while(map) {
518 if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
519 break;
520 map = map->next;
521 }
522 }
523 globalmapMutex.leave(&hGlobalMapMutex);
524 if(!map) dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
525 return map;
526}
527//******************************************************************************
528//******************************************************************************
529Win32MemMap *Win32MemMap::findMap(ULONG address)
530{
531 globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
532 Win32MemMap *map = memmaps;
533
534 if(map != NULL) {
535 while(map) {
536 if(map->pMapping && (ULONG)map->pMapping <= address &&
537 (ULONG)map->pMapping + map->mSize > address)
538 {
539 break;
540 }
541 map = map->next;
542 }
543 }
544 globalmapMutex.leave(&hGlobalMapMutex);
545 return map;
546}
547//******************************************************************************
548//******************************************************************************
549void Win32MemMap::deleteAll()
550{
551 Win32MemMap *map = memmaps, *nextmap;
552 DWORD processId = GetCurrentProcessId();
553
554 //delete all maps created by this process
555 globalviewMutex.enter();
556 while(map) {
557 nextmap = map->next;
558 if(map->getProcessId() == processId) {
559 //Delete map directly for executable images (only used internally)
560 if(map->getImage()) {
561 delete map;
562 }
563 else {
564 delete map;
565 }
566 }
567 else {
568 //delete all views created by this process for this map
569 Win32MemMapView::deleteViews(map);
570 }
571 map = nextmap;
572 }
573 globalviewMutex.leave();
574}
575//******************************************************************************
576//******************************************************************************
577Win32MemMapView::Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size,
578 ULONG fdwAccess)
579{
580 LPVOID viewaddr = (LPVOID)((ULONG)map->getMappingAddr()+offset);
581 ULONG accessAttr = 0;
582 Win32MemMapView *tmpview = mapviews;
583
584 errorState = 0;
585 mParentMap = map;
586 mSize = size;
587 mOffset = offset;
588 mProcessId = GetCurrentProcessId();
589 pShareViewAddr = NULL;
590
591 switch(fdwAccess) {
592 case FILE_MAP_READ:
593 accessAttr = PAG_READ;
594 mfAccess = MEMMAP_ACCESS_READ;
595 break;
596 case FILE_MAP_ALL_ACCESS:
597 case FILE_MAP_WRITE:
598 case FILE_MAP_WRITE|FILE_MAP_READ:
599 case FILE_MAP_COPY:
600 accessAttr = (PAG_READ|PAG_WRITE);
601 mfAccess = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
602 break;
603 }
604 //Named file mappings from other processes are always shared;
605 //map into our address space
606 if(map->getMemName() != NULL && 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,
712 ULONG *offset,
713 ULONG accessType,
714 Win32MemMapView **pView)
715{
716 if(mapviews == NULL) return NULL;
717
718 globalviewMutex.enter();
719 Win32MemMapView *view = mapviews;
720 ULONG ulViewAddr;
721
722 *offset = 0;
723
724 if(view != NULL)
725 {
726 do
727 {
728 ulViewAddr = (ULONG)view->getViewAddr();
729
730 // if ulViewAddr is > address, we've exhausted
731 // the sorted list already and can abort search.
732 if(ulViewAddr <= address)
733 {
734 if(ulViewAddr + view->getSize() > address &&
735 view->getAccessFlags() >= accessType)
736 {
737 *offset = view->getOffset() + (address - ulViewAddr);
738 goto success;
739 }
740
741 // Not found yet, continue search with next map
742 view = view->next;
743 }
744 else
745 {
746 // list is exhausted, abort loop
747 view = NULL;
748 }
749 }
750 while(view);
751
752 //failure if we get here
753 view = NULL;
754 }
755
756success:
757#ifdef DEBUG
758 if(view && !view->getParentMap()->isImageMap())
759 dprintf(("findMapByView %x %x -> %x off %x",
760 address,
761 accessType,
762 view->getViewAddr(),
763 *offset));
764#endif
765
766 globalviewMutex.leave();
767
768 if(pView)
769 *pView = view;
770
771 return (view) ? view->getParentMap() : NULL;
772}
773//******************************************************************************
774//******************************************************************************
775Win32MemMapView *Win32MemMapView::findView(LPVOID address)
776{
777 Win32MemMapView *view = mapviews;
778
779 globalviewMutex.enter();
780 if(view != NULL) {
781 while(view) {
782 if(view->getViewAddr() == address)
783 {
784 break;
785 }
786 view = view->next;
787 }
788 }
789 globalviewMutex.leave();
790 return view;
791}
792//******************************************************************************
793//******************************************************************************
794
Note: See TracBrowser for help on using the repository browser.