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

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

memory map + handle manager fixes

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