1 | /* $Id: mmap.cpp,v 1.17 1999-08-27 16:51:00 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 | *
|
---|
19 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
20 | *
|
---|
21 | */
|
---|
22 | #include <os2win.h>
|
---|
23 | #include <stdio.h>
|
---|
24 | #include <stdlib.h>
|
---|
25 | #include <string.h>
|
---|
26 | #include <win\virtual.h>
|
---|
27 | #include <vmutex.h>
|
---|
28 | #include <handlemanager.h>
|
---|
29 | #include "mmap.h"
|
---|
30 | #include "oslibdos.h"
|
---|
31 |
|
---|
32 | VMutex globalmapMutex;
|
---|
33 | VMutex globalviewMutex;
|
---|
34 |
|
---|
35 | //******************************************************************************
|
---|
36 | //TODO: sharing between processes
|
---|
37 | //******************************************************************************
|
---|
38 | Win32MemMap::Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
|
---|
39 | : nrMappings(0), pMapping(NULL), mMapAccess(0), referenced(0)
|
---|
40 | {
|
---|
41 | globalmapMutex.enter();
|
---|
42 | next = memmaps;
|
---|
43 | memmaps = this;
|
---|
44 | globalmapMutex.leave();
|
---|
45 |
|
---|
46 | hMemFile = hfile;
|
---|
47 |
|
---|
48 | mSize = size;
|
---|
49 | mProtFlags = fdwProtect;
|
---|
50 |
|
---|
51 | if(lpszName) {
|
---|
52 | lpszMapName = (char *)malloc(strlen(lpszName)+1);
|
---|
53 | strcpy(lpszMapName, lpszName);
|
---|
54 | }
|
---|
55 | else lpszMapName = NULL;
|
---|
56 | }
|
---|
57 | //******************************************************************************
|
---|
58 | //******************************************************************************
|
---|
59 | BOOL Win32MemMap::Init(HANDLE hMemMap)
|
---|
60 | {
|
---|
61 | mapMutex.enter();
|
---|
62 | if(hMemFile != -1)
|
---|
63 | {
|
---|
64 | if(DuplicateHandle(GetCurrentProcess(), hMemFile, GetCurrentProcess(),
|
---|
65 | &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
|
---|
66 | {
|
---|
67 | dprintf(("Win32MemMap::Init: DuplicateHandle failed!"));
|
---|
68 | goto fail;
|
---|
69 | }
|
---|
70 | mSize = SetFilePointer(hMemFile, 0, NULL, FILE_END);
|
---|
71 | if(mSize == -1) {
|
---|
72 | dprintf(("Win32MemMap::init: SetFilePointer failed to set pos end"));
|
---|
73 | goto fail;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | dprintf(("CreateFileMappingA for file %x, prot %x size %d, name %s", hMemFile, mProtFlags, mSize, lpszMapName));
|
---|
78 | this->hMemMap = hMemMap;
|
---|
79 | mapMutex.leave();
|
---|
80 | return TRUE;
|
---|
81 | fail:
|
---|
82 | mapMutex.leave();
|
---|
83 | return FALSE;
|
---|
84 | }
|
---|
85 | //******************************************************************************
|
---|
86 | //******************************************************************************
|
---|
87 | Win32MemMap::~Win32MemMap()
|
---|
88 | {
|
---|
89 | for(int i=0;i<nrMappings;i++) {
|
---|
90 | Win32MemMapView::deleteView(this); //delete all views of our memory mapped file
|
---|
91 | }
|
---|
92 | mapMutex.enter();
|
---|
93 | if(lpszMapName) {
|
---|
94 | free(lpszMapName);
|
---|
95 | }
|
---|
96 | if(pMapping) {
|
---|
97 | VirtualFree(pMapping, mSize, MEM_RELEASE);
|
---|
98 | pMapping = NULL;
|
---|
99 | }
|
---|
100 | if(hMemFile != -1) {
|
---|
101 | CloseHandle(hMemFile);
|
---|
102 | hMemFile = -1;
|
---|
103 | }
|
---|
104 | mapMutex.leave();
|
---|
105 |
|
---|
106 | globalmapMutex.enter();
|
---|
107 | Win32MemMap *map = memmaps;
|
---|
108 |
|
---|
109 | if(map == this) {
|
---|
110 | memmaps = next;
|
---|
111 | }
|
---|
112 | else {
|
---|
113 | while(map->next) {
|
---|
114 | if(map->next == this)
|
---|
115 | break;
|
---|
116 | map = map->next;
|
---|
117 | }
|
---|
118 | if(map->next) {
|
---|
119 | map->next = next;
|
---|
120 | }
|
---|
121 | else dprintf(("Win32MemMap::~Win32MemMap: map not found!! (%x)", this));
|
---|
122 | }
|
---|
123 | globalmapMutex.leave();
|
---|
124 | }
|
---|
125 | //******************************************************************************
|
---|
126 | //We determine whether a page has been modified by checking it's protection flags
|
---|
127 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
---|
128 | //(all pages are readonly until the app tries to write to it)
|
---|
129 | //******************************************************************************
|
---|
130 | BOOL Win32MemMap::commitPage(ULONG offset, BOOL fWriteAccess)
|
---|
131 | {
|
---|
132 | MEMORY_BASIC_INFORMATION memInfo;
|
---|
133 | LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
|
---|
134 | DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
|
---|
135 | DWORD oldProt, newProt, nrBytesRead, size;
|
---|
136 |
|
---|
137 | // mapMutex.enter();
|
---|
138 |
|
---|
139 | newProt = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
|
---|
140 |
|
---|
141 | dprintf(("Win32MemMap::commitPage %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
|
---|
142 | if(hMemFile != -1) {
|
---|
143 | if(VirtualQuery((LPSTR)pageAddr, &memInfo, NRPAGES_TOCOMMIT*PAGE_SIZE) == 0) {
|
---|
144 | dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, NRPAGES_TOCOMMIT*PAGE_SIZE));
|
---|
145 | goto fail;
|
---|
146 | }
|
---|
147 | //Only changes the state of the pages with the same attribute flags
|
---|
148 | //(returned in memInfo.RegionSize)
|
---|
149 | //If it's smaller than the mNrPages, it simply means one or more of the
|
---|
150 | //other pages have already been committed
|
---|
151 | if(memInfo.State & MEM_COMMIT)
|
---|
152 | {//if it's already committed, then the app tried to write to it
|
---|
153 | if(!fWriteAccess) {
|
---|
154 | dprintf(("Win32MemMap::commitPage: Huh? Already committed and not trying to write (%x,%x) failed for %x", pageAddr, memInfo.RegionSize));
|
---|
155 | goto fail;
|
---|
156 | }
|
---|
157 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
|
---|
158 | dprintf(("Win32MemMap::commitPage: Failed to set write flag on page (%x,%x) failed for %x", pageAddr, memInfo.RegionSize));
|
---|
159 | goto fail;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else {
|
---|
163 | if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
|
---|
164 | goto fail;
|
---|
165 | }
|
---|
166 | offset = pageAddr - (ULONG)pMapping;
|
---|
167 | size = memInfo.RegionSize;
|
---|
168 | if(offset + size > mSize) {
|
---|
169 | dprintf(("Adjusting size from %d to %d", size, mSize - offset));
|
---|
170 | size = mSize - offset;
|
---|
171 | }
|
---|
172 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
---|
173 | dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
|
---|
174 | goto fail;
|
---|
175 | }
|
---|
176 | if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
|
---|
177 | dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
|
---|
178 | goto fail;
|
---|
179 | }
|
---|
180 | if(nrBytesRead != size) {
|
---|
181 | dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
|
---|
182 | goto fail;
|
---|
183 | }
|
---|
184 | if(mProtFlags & PAGE_READONLY) {
|
---|
185 | if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
|
---|
186 | goto fail;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 | else {
|
---|
192 | if(VirtualQuery((LPSTR)pageAddr, &memInfo, NRPAGES_TOCOMMIT*PAGE_SIZE) == 0) {
|
---|
193 | dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, NRPAGES_TOCOMMIT*PAGE_SIZE));
|
---|
194 | goto fail;
|
---|
195 | }
|
---|
196 | if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE) {
|
---|
197 | goto fail;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | // mapMutex.leave();
|
---|
202 | return TRUE;
|
---|
203 | fail:
|
---|
204 | // mapMutex.leave();
|
---|
205 | return FALSE;
|
---|
206 | }
|
---|
207 | //******************************************************************************
|
---|
208 | //todo: unalias memory
|
---|
209 | //******************************************************************************
|
---|
210 | BOOL Win32MemMap::unmapViewOfFile(Win32MemMapView *view)
|
---|
211 | {
|
---|
212 | mapMutex.enter();
|
---|
213 |
|
---|
214 | if(nrMappings == 0)
|
---|
215 | goto fail;
|
---|
216 |
|
---|
217 | delete view;
|
---|
218 |
|
---|
219 | if(--nrMappings) {
|
---|
220 | VirtualFree(pMapping, mSize, MEM_RELEASE);
|
---|
221 | pMapping = NULL;
|
---|
222 | }
|
---|
223 | mapMutex.leave();
|
---|
224 | return TRUE;
|
---|
225 | fail:
|
---|
226 | mapMutex.leave();
|
---|
227 | return FALSE;
|
---|
228 | }
|
---|
229 | //******************************************************************************
|
---|
230 | //******************************************************************************
|
---|
231 | LPVOID Win32MemMap::mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess)
|
---|
232 | {
|
---|
233 | mapMutex.enter();
|
---|
234 | ULONG memFlags = (mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY));
|
---|
235 | ULONG fAlloc = 0;
|
---|
236 | Win32MemMapView *mapview;
|
---|
237 |
|
---|
238 | if(fdwAccess & ~(FILE_MAP_WRITE|FILE_MAP_READ|FILE_MAP_COPY))
|
---|
239 | goto parmfail;
|
---|
240 | if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
|
---|
241 | goto parmfail;
|
---|
242 | if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY)))
|
---|
243 | goto parmfail;
|
---|
244 | if((fdwAccess & FILE_MAP_COPY) && !(mProtFlags & PAGE_WRITECOPY))
|
---|
245 | goto parmfail;
|
---|
246 | if(offset+size > mSize)
|
---|
247 | goto parmfail;
|
---|
248 |
|
---|
249 | //TODO: If committed, read file into memory
|
---|
250 | #if 0
|
---|
251 | if(mProtFlags & SEC_COMMIT)
|
---|
252 | fAlloc |= MEM_COMMIT;
|
---|
253 | else
|
---|
254 | if(mProtFlags & SEC_RESERVE)
|
---|
255 | fAlloc |= MEM_RESERVE;
|
---|
256 | #else
|
---|
257 | fAlloc = MEM_RESERVE;
|
---|
258 | #endif
|
---|
259 |
|
---|
260 | if(nrMappings == 0) {//if not mapped, reserve/commit entire view
|
---|
261 | //SvL: Always read/write access or else ReadFile will crash once we
|
---|
262 | // start committing pages.
|
---|
263 | // This is most likely an OS/2 bug and doesn't happen in Aurora
|
---|
264 | // when allocating memory with the PAG_ANY bit set. (without this
|
---|
265 | // flag it will also crash)
|
---|
266 | pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
|
---|
267 | if(pMapping == NULL) {
|
---|
268 | dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
|
---|
269 | goto fail;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | mapview = new Win32MemMapView(this, offset, (size == 0) ? mSize : size, fdwAccess);
|
---|
273 | if(mapview == NULL) {
|
---|
274 | goto fail;
|
---|
275 | }
|
---|
276 | if(mapview->everythingOk() == FALSE) {
|
---|
277 | delete mapview;
|
---|
278 | goto fail;
|
---|
279 | }
|
---|
280 | nrMappings++;
|
---|
281 | mapMutex.leave();
|
---|
282 | return mapview->getViewAddr();
|
---|
283 |
|
---|
284 | parmfail:
|
---|
285 | dprintf(("Win32MemMap::mapFileView: ERROR_INVALID_PARAMETER"));
|
---|
286 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
287 | fail:
|
---|
288 | mapMutex.leave();
|
---|
289 | return 0;
|
---|
290 | }
|
---|
291 | //******************************************************************************
|
---|
292 | //We determine whether a page has been modified by checking it's protection flags
|
---|
293 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
---|
294 | //(all pages are readonly until the app tries to modify the contents of the page)
|
---|
295 | //
|
---|
296 | //TODO: Are apps allowed to change the protection flags of memory mapped pages?
|
---|
297 | // I'm assuming they aren't for now.
|
---|
298 | //******************************************************************************
|
---|
299 | BOOL Win32MemMap::flushView(ULONG offset, ULONG cbFlush)
|
---|
300 | {
|
---|
301 | LPVOID lpvBase = (LPVOID)((ULONG)pMapping+offset);
|
---|
302 | MEMORY_BASIC_INFORMATION memInfo;
|
---|
303 | ULONG nrBytesWritten, size;
|
---|
304 | int i;
|
---|
305 |
|
---|
306 | dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
|
---|
307 | if(nrMappings == 0)
|
---|
308 | goto parmfail;
|
---|
309 |
|
---|
310 | if(cbFlush == 0)
|
---|
311 | cbFlush = mSize;
|
---|
312 |
|
---|
313 | if(lpvBase < pMapping || (ULONG)lpvBase+cbFlush > (ULONG)pMapping+mSize)
|
---|
314 | goto parmfail;
|
---|
315 |
|
---|
316 | if(mProtFlags & PAGE_READONLY)
|
---|
317 | goto parmfail;
|
---|
318 |
|
---|
319 | if(hMemFile == -1)
|
---|
320 | goto success; //TODO: Return an error here?
|
---|
321 |
|
---|
322 | while(cbFlush) {
|
---|
323 | if(VirtualQuery((LPSTR)lpvBase, &memInfo, cbFlush) == 0) {
|
---|
324 | dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
|
---|
325 | goto fail;
|
---|
326 | }
|
---|
327 | //If a page (or range of pages) is reserved or write protected, we
|
---|
328 | //won't bother flushing it to disk
|
---|
329 | if(memInfo.State & MEM_COMMIT &&
|
---|
330 | memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
|
---|
331 | {//committed and allowed for writing?
|
---|
332 | offset = (ULONG)lpvBase - (ULONG)pMapping;
|
---|
333 | size = memInfo.RegionSize;
|
---|
334 | if(size > cbFlush) {
|
---|
335 | size = cbFlush;
|
---|
336 | }
|
---|
337 | dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
|
---|
338 |
|
---|
339 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
---|
340 | dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
|
---|
341 | goto fail;
|
---|
342 | }
|
---|
343 | if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
|
---|
344 | dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
|
---|
345 | goto fail;
|
---|
346 | }
|
---|
347 | if(nrBytesWritten != size) {
|
---|
348 | dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
|
---|
349 | goto fail;
|
---|
350 | }
|
---|
351 | }
|
---|
352 | lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
|
---|
353 |
|
---|
354 | if(cbFlush < memInfo.RegionSize)
|
---|
355 | break;
|
---|
356 |
|
---|
357 | cbFlush -= memInfo.RegionSize;
|
---|
358 | }
|
---|
359 | success:
|
---|
360 | return TRUE;
|
---|
361 | parmfail:
|
---|
362 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
363 | return FALSE;
|
---|
364 | fail:
|
---|
365 | return FALSE;
|
---|
366 | }
|
---|
367 | //******************************************************************************
|
---|
368 | //******************************************************************************
|
---|
369 | Win32MemMap *Win32MemMap::findMap(LPSTR lpszName)
|
---|
370 | {
|
---|
371 | globalmapMutex.enter();
|
---|
372 | Win32MemMap *map = memmaps;
|
---|
373 |
|
---|
374 | if(map != NULL) {
|
---|
375 | while(map) {
|
---|
376 | if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
|
---|
377 | break;
|
---|
378 | map = map->next;
|
---|
379 | }
|
---|
380 | }
|
---|
381 | globalmapMutex.leave();
|
---|
382 | dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
|
---|
383 | return map;
|
---|
384 | }
|
---|
385 | //******************************************************************************
|
---|
386 | //******************************************************************************
|
---|
387 | Win32MemMap *Win32MemMap::findMap(ULONG address)
|
---|
388 | {
|
---|
389 | globalmapMutex.enter();
|
---|
390 | Win32MemMap *map = memmaps;
|
---|
391 |
|
---|
392 | if(map != NULL) {
|
---|
393 | while(map) {
|
---|
394 | if(map->pMapping && (ULONG)map->pMapping <= address &&
|
---|
395 | (ULONG)map->pMapping + map->mSize > address)
|
---|
396 | {
|
---|
397 | break;
|
---|
398 | }
|
---|
399 | map = map->next;
|
---|
400 | }
|
---|
401 | }
|
---|
402 | globalmapMutex.leave();
|
---|
403 | return map;
|
---|
404 | }
|
---|
405 | //******************************************************************************
|
---|
406 | //Assumes mutex has been acquired
|
---|
407 | //******************************************************************************
|
---|
408 | void Win32MemMap::deleteAll()
|
---|
409 | {
|
---|
410 | while(memmaps) {
|
---|
411 | CloseHandle(memmaps->hMemMap);
|
---|
412 | }
|
---|
413 | }
|
---|
414 | //******************************************************************************
|
---|
415 | //******************************************************************************
|
---|
416 | Win32MemMap *Win32MemMap::memmaps = NULL;
|
---|
417 |
|
---|
418 | //******************************************************************************
|
---|
419 | //******************************************************************************
|
---|
420 | Win32MemMapView::Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size,
|
---|
421 | ULONG fdwAccess)
|
---|
422 | {
|
---|
423 | LPVOID viewaddr = (LPVOID)((ULONG)map->getMappingAddr()+offset);
|
---|
424 | ULONG accessAttr = 0;
|
---|
425 | Win32MemMapView *tmpview = mapviews;
|
---|
426 |
|
---|
427 | errorState = 0;
|
---|
428 | mParentMap = map;
|
---|
429 | mSize = size;
|
---|
430 | mOffset = offset;
|
---|
431 |
|
---|
432 | switch(fdwAccess) {
|
---|
433 | case FILE_MAP_READ:
|
---|
434 | accessAttr = PAG_READ;
|
---|
435 | mfAccess = MEMMAP_ACCESS_READ;
|
---|
436 | break;
|
---|
437 | case FILE_MAP_WRITE:
|
---|
438 | case FILE_MAP_COPY:
|
---|
439 | accessAttr = (PAG_READ|PAG_WRITE);
|
---|
440 | mfAccess = MEMMAP_ACCESS_WRITE;
|
---|
441 | break;
|
---|
442 | }
|
---|
443 | if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
|
---|
444 | dprintf(("new OSLibDosAliasMem FAILED"));
|
---|
445 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
446 | errorState = 1;
|
---|
447 | return;
|
---|
448 | }
|
---|
449 |
|
---|
450 | dprintf(("Win32MemMapView::Win32MemMapView: created %x (alias for %x), size %d", pMapView, viewaddr, size));
|
---|
451 |
|
---|
452 | globalviewMutex.enter();
|
---|
453 | if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
|
---|
454 | next = mapviews;
|
---|
455 | mapviews = this;
|
---|
456 | }
|
---|
457 | else {
|
---|
458 | while(tmpview->next) {
|
---|
459 | if(tmpview->next->getViewAddr() > pMapView) {
|
---|
460 | break;
|
---|
461 | }
|
---|
462 | tmpview = tmpview->next;
|
---|
463 | }
|
---|
464 | next = tmpview->next;
|
---|
465 | tmpview->next = this;
|
---|
466 | }
|
---|
467 | globalviewMutex.leave();
|
---|
468 | }
|
---|
469 | //******************************************************************************
|
---|
470 | //******************************************************************************
|
---|
471 | Win32MemMapView::~Win32MemMapView()
|
---|
472 | {
|
---|
473 | if(errorState != 0)
|
---|
474 | return;
|
---|
475 |
|
---|
476 | if(mfAccess != MEMMAP_ACCESS_READ)
|
---|
477 | mParentMap->flushView(mOffset, mSize);
|
---|
478 |
|
---|
479 | OSLibDosFreeMem(pMapView);
|
---|
480 |
|
---|
481 | globalviewMutex.enter();
|
---|
482 | Win32MemMapView *view = mapviews;
|
---|
483 |
|
---|
484 | if(view == this) {
|
---|
485 | mapviews = next;
|
---|
486 | }
|
---|
487 | else {
|
---|
488 | while(view->next) {
|
---|
489 | if(view->next == this)
|
---|
490 | break;
|
---|
491 | view = view->next;
|
---|
492 | }
|
---|
493 | if(view->next) {
|
---|
494 | view->next = next;
|
---|
495 | }
|
---|
496 | else dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
|
---|
497 | }
|
---|
498 | globalviewMutex.leave();
|
---|
499 | }
|
---|
500 | //******************************************************************************
|
---|
501 | //******************************************************************************
|
---|
502 | void Win32MemMapView::deleteView(Win32MemMap *map)
|
---|
503 | {
|
---|
504 | globalviewMutex.enter();
|
---|
505 | Win32MemMapView *view = mapviews;
|
---|
506 |
|
---|
507 | if(view != NULL) {
|
---|
508 | while(view) {
|
---|
509 | if(view->getParentMap() == map)
|
---|
510 | {
|
---|
511 | globalviewMutex.leave();
|
---|
512 | delete view;
|
---|
513 | return;
|
---|
514 | }
|
---|
515 | view = view->next;
|
---|
516 | }
|
---|
517 | }
|
---|
518 | globalviewMutex.leave();
|
---|
519 | }
|
---|
520 | //******************************************************************************
|
---|
521 | //******************************************************************************
|
---|
522 | Win32MemMap *Win32MemMapView::findMapByView(ULONG address, ULONG *offset,
|
---|
523 | ULONG accessType,
|
---|
524 | Win32MemMapView **pView)
|
---|
525 | {
|
---|
526 | globalviewMutex.enter();
|
---|
527 | Win32MemMapView *view = mapviews;
|
---|
528 |
|
---|
529 | *offset = 0;
|
---|
530 |
|
---|
531 | if(view != NULL) {
|
---|
532 | while(view && (ULONG)view->getViewAddr() <= address) {
|
---|
533 | if((ULONG)view->getViewAddr() <= address &&
|
---|
534 | (ULONG)view->getViewAddr() + view->getSize() >= address &&
|
---|
535 | view->getAccessFlags() >= accessType)
|
---|
536 | {
|
---|
537 | *offset = view->getOffset() + (address - (ULONG)view->getViewAddr());
|
---|
538 | goto success;
|
---|
539 | }
|
---|
540 | view = view->next;
|
---|
541 | }
|
---|
542 | //failure if we get here
|
---|
543 | view = NULL;
|
---|
544 | }
|
---|
545 | success:
|
---|
546 | globalviewMutex.leave();
|
---|
547 | if(pView) *pView = view;
|
---|
548 | return (view) ? view->getParentMap() : NULL;
|
---|
549 | }
|
---|
550 | //******************************************************************************
|
---|
551 | //******************************************************************************
|
---|
552 | Win32MemMapView *Win32MemMapView::findView(LPVOID address)
|
---|
553 | {
|
---|
554 | Win32MemMapView *view = mapviews;
|
---|
555 |
|
---|
556 | if(view != NULL) {
|
---|
557 | while(view) {
|
---|
558 | if(view->getViewAddr() == address)
|
---|
559 | {
|
---|
560 | break;
|
---|
561 | }
|
---|
562 | view = view->next;
|
---|
563 | }
|
---|
564 | }
|
---|
565 | return view;
|
---|
566 | }
|
---|
567 | //******************************************************************************
|
---|
568 | //******************************************************************************
|
---|
569 | Win32MemMapView *Win32MemMapView::mapviews = NULL;
|
---|
570 |
|
---|