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