| 1 | /* $Id: mmap.cpp,v 1.18 1999-09-01 00:03:51 phaller 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 | //@@@PH: if(fdwAccess & ~(FILE_MAP_WRITE|FILE_MAP_READ|FILE_MAP_COPY))
|
|---|
| 239 | // Docs say FILE_MAP_ALL_ACCESS is same as FILE_MAP_WRITE. Doesn't match reality though.
|
|---|
| 240 | if(fdwAccess & ~FILE_MAP_ALL_ACCESS)
|
|---|
| 241 | goto parmfail;
|
|---|
| 242 | if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
|
|---|
| 243 | goto parmfail;
|
|---|
| 244 | if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY)))
|
|---|
| 245 | goto parmfail;
|
|---|
| 246 |
|
|---|
| 247 | //@@@PH
|
|---|
| 248 | if (fdwAccess != FILE_MAP_ALL_ACCESS)
|
|---|
| 249 | if((fdwAccess & FILE_MAP_COPY) && !(mProtFlags & PAGE_WRITECOPY))
|
|---|
| 250 | goto parmfail;
|
|---|
| 251 |
|
|---|
| 252 | if(offset+size > mSize)
|
|---|
| 253 | goto parmfail;
|
|---|
| 254 |
|
|---|
| 255 | //TODO: If committed, read file into memory
|
|---|
| 256 | #if 0
|
|---|
| 257 | if(mProtFlags & SEC_COMMIT)
|
|---|
| 258 | fAlloc |= MEM_COMMIT;
|
|---|
| 259 | else
|
|---|
| 260 | if(mProtFlags & SEC_RESERVE)
|
|---|
| 261 | fAlloc |= MEM_RESERVE;
|
|---|
| 262 | #else
|
|---|
| 263 | fAlloc = MEM_RESERVE;
|
|---|
| 264 | #endif
|
|---|
| 265 |
|
|---|
| 266 | if(nrMappings == 0) {//if not mapped, reserve/commit entire view
|
|---|
| 267 | //SvL: Always read/write access or else ReadFile will crash once we
|
|---|
| 268 | // start committing pages.
|
|---|
| 269 | // This is most likely an OS/2 bug and doesn't happen in Aurora
|
|---|
| 270 | // when allocating memory with the PAG_ANY bit set. (without this
|
|---|
| 271 | // flag it will also crash)
|
|---|
| 272 | pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
|
|---|
| 273 | if(pMapping == NULL) {
|
|---|
| 274 | dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
|
|---|
| 275 | goto fail;
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 | mapview = new Win32MemMapView(this, offset, (size == 0) ? mSize : size, fdwAccess);
|
|---|
| 279 | if(mapview == NULL) {
|
|---|
| 280 | goto fail;
|
|---|
| 281 | }
|
|---|
| 282 | if(mapview->everythingOk() == FALSE) {
|
|---|
| 283 | delete mapview;
|
|---|
| 284 | goto fail;
|
|---|
| 285 | }
|
|---|
| 286 | nrMappings++;
|
|---|
| 287 | mapMutex.leave();
|
|---|
| 288 | return mapview->getViewAddr();
|
|---|
| 289 |
|
|---|
| 290 | parmfail:
|
|---|
| 291 | dprintf(("Win32MemMap::mapFileView: ERROR_INVALID_PARAMETER"));
|
|---|
| 292 | SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 293 | fail:
|
|---|
| 294 | mapMutex.leave();
|
|---|
| 295 | return 0;
|
|---|
| 296 | }
|
|---|
| 297 | //******************************************************************************
|
|---|
| 298 | //We determine whether a page has been modified by checking it's protection flags
|
|---|
| 299 | //If the write flag is set, this means commitPage had to enable this due to a pagefault
|
|---|
| 300 | //(all pages are readonly until the app tries to modify the contents of the page)
|
|---|
| 301 | //
|
|---|
| 302 | //TODO: Are apps allowed to change the protection flags of memory mapped pages?
|
|---|
| 303 | // I'm assuming they aren't for now.
|
|---|
| 304 | //******************************************************************************
|
|---|
| 305 | BOOL Win32MemMap::flushView(ULONG offset, ULONG cbFlush)
|
|---|
| 306 | {
|
|---|
| 307 | LPVOID lpvBase = (LPVOID)((ULONG)pMapping+offset);
|
|---|
| 308 | MEMORY_BASIC_INFORMATION memInfo;
|
|---|
| 309 | ULONG nrBytesWritten, size;
|
|---|
| 310 | int i;
|
|---|
| 311 |
|
|---|
| 312 | dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
|
|---|
| 313 | if(nrMappings == 0)
|
|---|
| 314 | goto parmfail;
|
|---|
| 315 |
|
|---|
| 316 | if(cbFlush == 0)
|
|---|
| 317 | cbFlush = mSize;
|
|---|
| 318 |
|
|---|
| 319 | if(lpvBase < pMapping || (ULONG)lpvBase+cbFlush > (ULONG)pMapping+mSize)
|
|---|
| 320 | goto parmfail;
|
|---|
| 321 |
|
|---|
| 322 | if(mProtFlags & PAGE_READONLY)
|
|---|
| 323 | goto parmfail;
|
|---|
| 324 |
|
|---|
| 325 | if(hMemFile == -1)
|
|---|
| 326 | goto success; //TODO: Return an error here?
|
|---|
| 327 |
|
|---|
| 328 | while(cbFlush) {
|
|---|
| 329 | if(VirtualQuery((LPSTR)lpvBase, &memInfo, cbFlush) == 0) {
|
|---|
| 330 | dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
|
|---|
| 331 | goto fail;
|
|---|
| 332 | }
|
|---|
| 333 | //If a page (or range of pages) is reserved or write protected, we
|
|---|
| 334 | //won't bother flushing it to disk
|
|---|
| 335 | if(memInfo.State & MEM_COMMIT &&
|
|---|
| 336 | memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
|
|---|
| 337 | {//committed and allowed for writing?
|
|---|
| 338 | offset = (ULONG)lpvBase - (ULONG)pMapping;
|
|---|
| 339 | size = memInfo.RegionSize;
|
|---|
| 340 | if(size > cbFlush) {
|
|---|
| 341 | size = cbFlush;
|
|---|
| 342 | }
|
|---|
| 343 | dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
|
|---|
| 344 |
|
|---|
| 345 | if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
|
|---|
| 346 | dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
|
|---|
| 347 | goto fail;
|
|---|
| 348 | }
|
|---|
| 349 | if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
|
|---|
| 350 | dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
|
|---|
| 351 | goto fail;
|
|---|
| 352 | }
|
|---|
| 353 | if(nrBytesWritten != size) {
|
|---|
| 354 | dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
|
|---|
| 355 | goto fail;
|
|---|
| 356 | }
|
|---|
| 357 | }
|
|---|
| 358 | lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
|
|---|
| 359 |
|
|---|
| 360 | if(cbFlush < memInfo.RegionSize)
|
|---|
| 361 | break;
|
|---|
| 362 |
|
|---|
| 363 | cbFlush -= memInfo.RegionSize;
|
|---|
| 364 | }
|
|---|
| 365 | success:
|
|---|
| 366 | return TRUE;
|
|---|
| 367 | parmfail:
|
|---|
| 368 | SetLastError(ERROR_INVALID_PARAMETER);
|
|---|
| 369 | return FALSE;
|
|---|
| 370 | fail:
|
|---|
| 371 | return FALSE;
|
|---|
| 372 | }
|
|---|
| 373 | //******************************************************************************
|
|---|
| 374 | //******************************************************************************
|
|---|
| 375 | Win32MemMap *Win32MemMap::findMap(LPSTR lpszName)
|
|---|
| 376 | {
|
|---|
| 377 | globalmapMutex.enter();
|
|---|
| 378 | Win32MemMap *map = memmaps;
|
|---|
| 379 |
|
|---|
| 380 | if(map != NULL) {
|
|---|
| 381 | while(map) {
|
|---|
| 382 | if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
|
|---|
| 383 | break;
|
|---|
| 384 | map = map->next;
|
|---|
| 385 | }
|
|---|
| 386 | }
|
|---|
| 387 | globalmapMutex.leave();
|
|---|
| 388 | dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
|
|---|
| 389 | return map;
|
|---|
| 390 | }
|
|---|
| 391 | //******************************************************************************
|
|---|
| 392 | //******************************************************************************
|
|---|
| 393 | Win32MemMap *Win32MemMap::findMap(ULONG address)
|
|---|
| 394 | {
|
|---|
| 395 | globalmapMutex.enter();
|
|---|
| 396 | Win32MemMap *map = memmaps;
|
|---|
| 397 |
|
|---|
| 398 | if(map != NULL) {
|
|---|
| 399 | while(map) {
|
|---|
| 400 | if(map->pMapping && (ULONG)map->pMapping <= address &&
|
|---|
| 401 | (ULONG)map->pMapping + map->mSize > address)
|
|---|
| 402 | {
|
|---|
| 403 | break;
|
|---|
| 404 | }
|
|---|
| 405 | map = map->next;
|
|---|
| 406 | }
|
|---|
| 407 | }
|
|---|
| 408 | globalmapMutex.leave();
|
|---|
| 409 | return map;
|
|---|
| 410 | }
|
|---|
| 411 | //******************************************************************************
|
|---|
| 412 | //Assumes mutex has been acquired
|
|---|
| 413 | //******************************************************************************
|
|---|
| 414 | void Win32MemMap::deleteAll()
|
|---|
| 415 | {
|
|---|
| 416 | while(memmaps) {
|
|---|
| 417 | CloseHandle(memmaps->hMemMap);
|
|---|
| 418 | }
|
|---|
| 419 | }
|
|---|
| 420 | //******************************************************************************
|
|---|
| 421 | //******************************************************************************
|
|---|
| 422 | Win32MemMap *Win32MemMap::memmaps = NULL;
|
|---|
| 423 |
|
|---|
| 424 | //******************************************************************************
|
|---|
| 425 | //******************************************************************************
|
|---|
| 426 | Win32MemMapView::Win32MemMapView(Win32MemMap *map, ULONG offset, ULONG size,
|
|---|
| 427 | ULONG fdwAccess)
|
|---|
| 428 | {
|
|---|
| 429 | LPVOID viewaddr = (LPVOID)((ULONG)map->getMappingAddr()+offset);
|
|---|
| 430 | ULONG accessAttr = 0;
|
|---|
| 431 | Win32MemMapView *tmpview = mapviews;
|
|---|
| 432 |
|
|---|
| 433 | errorState = 0;
|
|---|
| 434 | mParentMap = map;
|
|---|
| 435 | mSize = size;
|
|---|
| 436 | mOffset = offset;
|
|---|
| 437 |
|
|---|
| 438 | switch(fdwAccess) {
|
|---|
| 439 | case FILE_MAP_READ:
|
|---|
| 440 | accessAttr = PAG_READ;
|
|---|
| 441 | mfAccess = MEMMAP_ACCESS_READ;
|
|---|
| 442 | break;
|
|---|
| 443 | case FILE_MAP_WRITE:
|
|---|
| 444 | case FILE_MAP_COPY:
|
|---|
| 445 | accessAttr = (PAG_READ|PAG_WRITE);
|
|---|
| 446 | mfAccess = MEMMAP_ACCESS_WRITE;
|
|---|
| 447 | break;
|
|---|
| 448 | }
|
|---|
| 449 | if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
|
|---|
| 450 | dprintf(("new OSLibDosAliasMem FAILED"));
|
|---|
| 451 | SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|---|
| 452 | errorState = 1;
|
|---|
| 453 | return;
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | dprintf(("Win32MemMapView::Win32MemMapView: created %x (alias for %x), size %d", pMapView, viewaddr, size));
|
|---|
| 457 |
|
|---|
| 458 | globalviewMutex.enter();
|
|---|
| 459 | if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
|
|---|
| 460 | next = mapviews;
|
|---|
| 461 | mapviews = this;
|
|---|
| 462 | }
|
|---|
| 463 | else {
|
|---|
| 464 | while(tmpview->next) {
|
|---|
| 465 | if(tmpview->next->getViewAddr() > pMapView) {
|
|---|
| 466 | break;
|
|---|
| 467 | }
|
|---|
| 468 | tmpview = tmpview->next;
|
|---|
| 469 | }
|
|---|
| 470 | next = tmpview->next;
|
|---|
| 471 | tmpview->next = this;
|
|---|
| 472 | }
|
|---|
| 473 | globalviewMutex.leave();
|
|---|
| 474 | }
|
|---|
| 475 | //******************************************************************************
|
|---|
| 476 | //******************************************************************************
|
|---|
| 477 | Win32MemMapView::~Win32MemMapView()
|
|---|
| 478 | {
|
|---|
| 479 | if(errorState != 0)
|
|---|
| 480 | return;
|
|---|
| 481 |
|
|---|
| 482 | if(mfAccess != MEMMAP_ACCESS_READ)
|
|---|
| 483 | mParentMap->flushView(mOffset, mSize);
|
|---|
| 484 |
|
|---|
| 485 | OSLibDosFreeMem(pMapView);
|
|---|
| 486 |
|
|---|
| 487 | globalviewMutex.enter();
|
|---|
| 488 | Win32MemMapView *view = mapviews;
|
|---|
| 489 |
|
|---|
| 490 | if(view == this) {
|
|---|
| 491 | mapviews = next;
|
|---|
| 492 | }
|
|---|
| 493 | else {
|
|---|
| 494 | while(view->next) {
|
|---|
| 495 | if(view->next == this)
|
|---|
| 496 | break;
|
|---|
| 497 | view = view->next;
|
|---|
| 498 | }
|
|---|
| 499 | if(view->next) {
|
|---|
| 500 | view->next = next;
|
|---|
| 501 | }
|
|---|
| 502 | else dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
|
|---|
| 503 | }
|
|---|
| 504 | globalviewMutex.leave();
|
|---|
| 505 | }
|
|---|
| 506 | //******************************************************************************
|
|---|
| 507 | //******************************************************************************
|
|---|
| 508 | void Win32MemMapView::deleteView(Win32MemMap *map)
|
|---|
| 509 | {
|
|---|
| 510 | globalviewMutex.enter();
|
|---|
| 511 | Win32MemMapView *view = mapviews;
|
|---|
| 512 |
|
|---|
| 513 | if(view != NULL) {
|
|---|
| 514 | while(view) {
|
|---|
| 515 | if(view->getParentMap() == map)
|
|---|
| 516 | {
|
|---|
| 517 | globalviewMutex.leave();
|
|---|
| 518 | delete view;
|
|---|
| 519 | return;
|
|---|
| 520 | }
|
|---|
| 521 | view = view->next;
|
|---|
| 522 | }
|
|---|
| 523 | }
|
|---|
| 524 | globalviewMutex.leave();
|
|---|
| 525 | }
|
|---|
| 526 | //******************************************************************************
|
|---|
| 527 | //******************************************************************************
|
|---|
| 528 | Win32MemMap *Win32MemMapView::findMapByView(ULONG address, ULONG *offset,
|
|---|
| 529 | ULONG accessType,
|
|---|
| 530 | Win32MemMapView **pView)
|
|---|
| 531 | {
|
|---|
| 532 | globalviewMutex.enter();
|
|---|
| 533 | Win32MemMapView *view = mapviews;
|
|---|
| 534 |
|
|---|
| 535 | *offset = 0;
|
|---|
| 536 |
|
|---|
| 537 | if(view != NULL) {
|
|---|
| 538 | while(view && (ULONG)view->getViewAddr() <= address) {
|
|---|
| 539 | if((ULONG)view->getViewAddr() <= address &&
|
|---|
| 540 | (ULONG)view->getViewAddr() + view->getSize() >= address &&
|
|---|
| 541 | view->getAccessFlags() >= accessType)
|
|---|
| 542 | {
|
|---|
| 543 | *offset = view->getOffset() + (address - (ULONG)view->getViewAddr());
|
|---|
| 544 | goto success;
|
|---|
| 545 | }
|
|---|
| 546 | view = view->next;
|
|---|
| 547 | }
|
|---|
| 548 | //failure if we get here
|
|---|
| 549 | view = NULL;
|
|---|
| 550 | }
|
|---|
| 551 | success:
|
|---|
| 552 | globalviewMutex.leave();
|
|---|
| 553 | if(pView) *pView = view;
|
|---|
| 554 | return (view) ? view->getParentMap() : NULL;
|
|---|
| 555 | }
|
|---|
| 556 | //******************************************************************************
|
|---|
| 557 | //******************************************************************************
|
|---|
| 558 | Win32MemMapView *Win32MemMapView::findView(LPVOID address)
|
|---|
| 559 | {
|
|---|
| 560 | Win32MemMapView *view = mapviews;
|
|---|
| 561 |
|
|---|
| 562 | if(view != NULL) {
|
|---|
| 563 | while(view) {
|
|---|
| 564 | if(view->getViewAddr() == address)
|
|---|
| 565 | {
|
|---|
| 566 | break;
|
|---|
| 567 | }
|
|---|
| 568 | view = view->next;
|
|---|
| 569 | }
|
|---|
| 570 | }
|
|---|
| 571 | return view;
|
|---|
| 572 | }
|
|---|
| 573 | //******************************************************************************
|
|---|
| 574 | //******************************************************************************
|
|---|
| 575 | Win32MemMapView *Win32MemMapView::mapviews = NULL;
|
|---|
| 576 |
|
|---|