Ignore:
Timestamp:
Oct 18, 2000, 7:09:34 PM (25 years ago)
Author:
sandervl
Message:

LoadLibraryExA + memory map close fix, added new registry keys for installation, don't log guard page violation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/mmap.cpp

    r4230 r4496  
    1 /* $Id: mmap.cpp,v 1.44 2000-09-10 21:54:07 sandervl Exp $ */
     1/* $Id: mmap.cpp,v 1.45 2000-10-18 17:09:33 sandervl Exp $ */
    22
    33/*
     
    3434#include <winimagepeldr.h>
    3535
    36 #define DBG_LOCALLOG    DBG_mmap
     36#define DBG_LOCALLOG    DBG_mmap
    3737#include "dbglocal.h"
    3838
     
    6969
    7070  if(lpszName) {
    71         lpszMapName = (char *)_smalloc(strlen(lpszName)+1);
    72         strcpy(lpszMapName, lpszName);
    73   }
    74   else  lpszMapName = NULL;
     71    lpszMapName = (char *)_smalloc(strlen(lpszName)+1);
     72    strcpy(lpszMapName, lpszName);
     73  }
     74  else  lpszMapName = NULL;
    7575}
    7676//******************************************************************************
     
    105105  {
    106106#if 0
    107         if(DuplicateHandle(mProcessId, hMemFile, GetCurrentProcess(),
     107        if(DuplicateHandle(mProcessId, hMemFile, GetCurrentProcess(),
    108108                           &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
    109109#else
    110         DWORD dwOdinOptions;
    111 
    112         if(!(mProtFlags & PAGE_READWRITE)) {
    113                 dwOdinOptions = DUPLICATE_ACCESS_READ | DUPLICATE_SHARE_DENYNONE;
    114         }
    115         else    dwOdinOptions = DUPLICATE_ACCESS_READWRITE | DUPLICATE_SHARE_DENYNONE;
    116 
    117         if(HMDuplicateHandleOdin(mProcessId, hMemFile, GetCurrentProcess(),
     110    DWORD dwOdinOptions;
     111
     112    if(!(mProtFlags & PAGE_READWRITE)) {
     113        dwOdinOptions = DUPLICATE_ACCESS_READ | DUPLICATE_SHARE_DENYNONE;
     114    }
     115    else    dwOdinOptions = DUPLICATE_ACCESS_READWRITE | DUPLICATE_SHARE_DENYNONE;
     116
     117        if(HMDuplicateHandleOdin(mProcessId, hMemFile, GetCurrentProcess(),
    118118                           &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS, dwOdinOptions) == FALSE)
    119119#endif
    120         {
    121                 dprintf(("Win32MemMap::Init: DuplicateHandle failed!"));
    122                 goto fail;
    123         }
    124         mSize = SetFilePointer(hMemFile, 0, NULL, FILE_BEGIN);
    125         mSize = SetFilePointer(hMemFile, 0, NULL, FILE_END);
    126         if(mSize == -1) {
    127                 dprintf(("Win32MemMap::init: SetFilePointer failed to set pos end"));
    128                 goto fail;
    129         }
    130         //SvL: Temporary limitation of size (Warp Server Advanced doesn't allow
     120        {
     121        dprintf(("Win32MemMap::Init: DuplicateHandle failed!"));
     122        goto fail;
     123        }
     124    mSize = SetFilePointer(hMemFile, 0, NULL, FILE_BEGIN);
     125    mSize = SetFilePointer(hMemFile, 0, NULL, FILE_END);
     126    if(mSize == -1) {
     127        dprintf(("Win32MemMap::init: SetFilePointer failed to set pos end"));
     128        goto fail;
     129    }
     130    //SvL: Temporary limitation of size (Warp Server Advanced doesn't allow
    131131        //     one to reserve more than 450 MB (unless you override the virtual
    132132        //     memory max limit) of continuous memory; (Warp 4 much less))
    133         if(mSize > 64*1024*1024) {
    134                 mSize = 64*1024*1024;
    135         }
     133    if(mSize > 64*1024*1024) {
     134        mSize = 64*1024*1024;
     135    }
    136136  }
    137137
     
    154154  mapMutex.enter();
    155155  if(lpszMapName) {
    156         free(lpszMapName);
     156    free(lpszMapName);
    157157  }
    158158  if(pMapping && !image) {
    159         if(lpszMapName) {
    160                 OSLibDosFreeMem(pMapping);
    161         }
    162         else    VirtualFree(pMapping, mSize, MEM_RELEASE);
    163 
    164         pMapping = NULL;
     159    if(lpszMapName) {
     160        OSLibDosFreeMem(pMapping);
     161    }
     162    else    VirtualFree(pMapping, mSize, MEM_RELEASE);
     163
     164    pMapping = NULL;
    165165  }
    166166  if(hMemFile != -1) {
    167         CloseHandle(hMemFile);
    168         hMemFile = -1;
     167    dprintf(("Win32MemMap dtor: closing memory file %x", hMemFile));
     168    CloseHandle(hMemFile);
     169    hMemFile = -1;
    169170  }
    170171  mapMutex.leave();
     
    174175
    175176  if(map == this) {
    176         memmaps = next;
     177    memmaps = next;
    177178  }
    178179  else {
    179         while(map->next) {
    180                 if(map->next == this)
    181                         break;
    182                 map = map->next;
    183         }
    184         if(map->next) {
    185                 map->next = next;
    186         }
    187         else    dprintf(("Win32MemMap::~Win32MemMap: map not found!! (%x)", this));
     180    while(map->next) {
     181        if(map->next == this)
     182            break;
     183        map = map->next;
     184    }
     185    if(map->next) {
     186         map->next = next;
     187    }
     188    else dprintf(("Win32MemMap::~Win32MemMap: map not found!! (%x)", this));
    188189  }
    189190  globalmapMutex.leave(&hGlobalMapMutex);
     191}
     192//******************************************************************************
     193//If memory map has no more views left, then we can safely delete it when
     194//it's handle is closed
     195//******************************************************************************
     196void Win32MemMap::close()
     197{
     198  fClosed = TRUE;
     199  if(nrMappings == 0) {
     200        delete this;
     201  }
    190202}
    191203//******************************************************************************
     
    205217
    206218  if(image) {
    207         return image->commitPage(pageAddr, fWriteAccess);
     219    return image->commitPage(pageAddr, fWriteAccess);
    208220  }
    209221  newProt  = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
    210222
    211223  dprintf(("Win32MemMap::commitPage %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
    212   if(hMemFile != -1) 
     224  if(hMemFile != -1)
    213225  {
    214         int faultsize = nrpages*PAGE_SIZE;
    215 
    216         offset = pageAddr - (ULONG)pMapping;
    217         if(offset + faultsize > mSize) {
    218                 faultsize = mSize - offset;
    219         }
    220 
    221         while(faultsize) {
    222                 if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
    223                         dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, nrpages*PAGE_SIZE));
    224                         goto fail;
    225                 }
    226                 memInfo.RegionSize = min(memInfo.RegionSize, faultsize);
    227                 //Only changes the state of the pages with the same attribute flags
    228                 //(returned in memInfo.RegionSize)
    229                 //If it's smaller than the mNrPages, it simply means one or more of the
    230                 //other pages have already been committed
    231                 if(!(memInfo.State & MEM_COMMIT))
    232                 {
    233                         if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
    234                                 goto fail;
    235                         }
    236                         if(!fWriteAccess) {
    237                                 offset = pageAddr - (ULONG)pMapping;
    238                                 size   = memInfo.RegionSize;
    239                                 if(offset + size > mSize) {
    240                                         dprintf(("Adjusting size from %d to %d", size, mSize - offset));
    241                                         size = mSize - offset;
    242                                 }
    243                                 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
    244                                         dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
    245                                         goto fail;
    246                                 }
    247                                 if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
    248                                         dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
    249                                         goto fail;
    250                                 }
    251                                 if(nrBytesRead != size) {
    252                                         dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
    253                                         goto fail;
    254                                 }
    255                         }
    256                         if(newProt != PAGE_READWRITE) {
    257                                 if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
    258                                         goto fail;
    259                                 }
    260                         }
    261                 }
    262                 faultsize -= memInfo.RegionSize;
    263                 pageAddr  += memInfo.RegionSize;
    264         }
     226    int faultsize = nrpages*PAGE_SIZE;
     227
     228    offset = pageAddr - (ULONG)pMapping;
     229    if(offset + faultsize > mSize) {
     230        faultsize = mSize - offset;
     231    }
     232
     233    while(faultsize) {
     234        if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
     235            dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed for %x", pageAddr, nrpages*PAGE_SIZE));
     236            goto fail;
     237        }
     238        memInfo.RegionSize = min(memInfo.RegionSize, faultsize);
     239        //Only changes the state of the pages with the same attribute flags
     240        //(returned in memInfo.RegionSize)
     241        //If it's smaller than the mNrPages, it simply means one or more of the
     242            //other pages have already been committed
     243        if(!(memInfo.State & MEM_COMMIT))
     244        {
     245            if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, PAGE_READWRITE) == FALSE) {
     246                goto fail;
     247            }
     248            if(!fWriteAccess) {
     249                offset = pageAddr - (ULONG)pMapping;
     250                size   = memInfo.RegionSize;
     251                if(offset + size > mSize) {
     252                    dprintf(("Adjusting size from %d to %d", size, mSize - offset));
     253                    size = mSize - offset;
     254                }
     255                if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
     256                    dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
     257                    goto fail;
     258                }
     259                if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
     260                    dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
     261                    goto fail;
     262                }
     263                if(nrBytesRead != size) {
     264                    dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
     265                    goto fail;
     266                }
     267            }
     268            if(newProt != PAGE_READWRITE) {
     269                if(VirtualProtect((LPVOID)pageAddr, memInfo.RegionSize, newProt, &oldProt) == FALSE) {
     270                    goto fail;
     271                }
     272            }
     273        }
     274        faultsize -= memInfo.RegionSize;
     275        pageAddr  += memInfo.RegionSize;
     276    }
    265277  }
    266278  else {
    267         ULONG sizeleft = nrpages*PAGE_SIZE;
    268         while(sizeleft)
    269         {
    270                 if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
    271                         dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed", pageAddr, sizeleft));
    272                         goto fail;
    273                 }
    274                 memInfo.RegionSize = min(memInfo.RegionSize, sizeleft);
    275 
    276                 if(!(memInfo.State & MEM_COMMIT))
    277                 {//if it's already committed, then the app tried to write to it
    278                         if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE)
    279                                 goto fail;
    280                 }
    281                 memInfo.RegionSize = (memInfo.RegionSize+PAGE_SIZE-1) & ~0xfff;
    282                 pageAddr += memInfo.RegionSize;
    283                 sizeleft -= memInfo.RegionSize;
    284         }
     279    ULONG sizeleft = nrpages*PAGE_SIZE;
     280    while(sizeleft)
     281    {
     282        if(VirtualQuery((LPSTR)pageAddr, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
     283            dprintf(("Win32MemMap::commitPage: VirtualQuery (%x,%x) failed", pageAddr, sizeleft));
     284            goto fail;
     285        }
     286        memInfo.RegionSize = min(memInfo.RegionSize, sizeleft);
     287
     288        if(!(memInfo.State & MEM_COMMIT))
     289            {//if it's already committed, then the app tried to write to it
     290            if(VirtualAlloc((LPVOID)pageAddr, memInfo.RegionSize, MEM_COMMIT, newProt) == FALSE)
     291                goto fail;
     292        }
     293        memInfo.RegionSize = (memInfo.RegionSize+PAGE_SIZE-1) & ~0xfff;
     294        pageAddr += memInfo.RegionSize;
     295        sizeleft -= memInfo.RegionSize;
     296    }
    285297  }
    286298
     
    299311
    300312  if(nrMappings == 0)
    301         goto fail;
     313    goto fail;
    302314
    303315  delete view;
    304316
    305317  if(--nrMappings == 0) {
    306         VirtualFree(pMapping, mSize, MEM_RELEASE);
    307         pMapping = NULL;
     318    VirtualFree(pMapping, mSize, MEM_RELEASE);
     319    pMapping = NULL;
    308320  }
    309321  mapMutex.leave();
    310  
     322
    311323  //if there are no more mappings left and the memory map's handle has been
    312324  //closed, then delete the object
    313325  if(nrMappings == 0 && fClosed) {
    314         delete this;
     326    delete this;
    315327  }
    316328  return TRUE;
    317329fail:
    318330  mapMutex.leave();
     331  if(nrMappings == 0 && fClosed) {
     332    delete this;
     333  }
    319334  return FALSE;
    320335}
     
    333348  // Docs say FILE_MAP_ALL_ACCESS is same as FILE_MAP_WRITE. Doesn't match reality though.
    334349  if(fdwAccess & ~FILE_MAP_ALL_ACCESS)
    335         goto parmfail;
     350    goto parmfail;
    336351  if((fdwAccess & FILE_MAP_WRITE) && !(mProtFlags & PAGE_READWRITE))
    337         goto parmfail;
     352    goto parmfail;
    338353  if((fdwAccess & FILE_MAP_READ) && !(mProtFlags & (PAGE_READWRITE|PAGE_READONLY)))
    339         goto parmfail;
     354    goto parmfail;
    340355
    341356  //@@@PH
     
    345360
    346361  if(offset+size > mSize && (!(fdwAccess & FILE_MAP_WRITE) || hMemFile == -1))
    347         goto parmfail;
     362    goto parmfail;
    348363
    349364  //SvL: TODO: Doesn't work for multiple views
    350365  if(offset+size > mSize) {
    351         mSize = offset+size;
     366    mSize = offset+size;
    352367  }
    353368
     
    355370#if 0
    356371  if(mProtFlags & SEC_COMMIT)
    357         fAlloc |= MEM_COMMIT;
     372    fAlloc |= MEM_COMMIT;
    358373  else
    359374  if(mProtFlags & SEC_RESERVE)
    360         fAlloc |= MEM_RESERVE;
     375    fAlloc |= MEM_RESERVE;
    361376#else
    362377  fAlloc = MEM_RESERVE;
     
    365380  //Memory has already been allocated for executable image maps (only used internally)
    366381  if(!pMapping && nrMappings == 0) {//if not mapped, reserve/commit entire view
    367         //SvL: Always read/write access or else ReadFile will crash once we
    368         //     start committing pages.
    369         //     This is most likely an OS/2 bug and doesn't happen in Aurora
     382    //SvL: Always read/write access or else ReadFile will crash once we
     383    //     start committing pages.
     384    //     This is most likely an OS/2 bug and doesn't happen in Aurora
    370385        //     when allocating memory with the PAG_ANY bit set. (without this
    371386        //     flag it will also crash)
    372         if(hMemFile == -1 && lpszMapName) {
    373                 pMapping = VirtualAllocShared(mSize, fAlloc, PAGE_READWRITE, lpszMapName);
    374         }
    375         else {
    376                 pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
    377         }
    378         if(pMapping == NULL) {
    379                 dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
    380                 goto fail;
    381         }
    382         //Windows NT seems to commit memory for memory maps, regardsless of the SEC_COMMIT flag
    383         if((hMemFile == -1 && !image)) {//commit memory
    384                 VirtualAlloc(pMapping, mSize, MEM_COMMIT, PAGE_READWRITE);
    385         }
    386         if(hMemFile != -1 && (mProtFlags & SEC_COMMIT)) {
    387                 DWORD nrPages = mSize >> PAGE_SHIFT;
    388                 if(mSize & 0xFFF)
    389                         nrPages++;
    390 
    391                 commitPage(0, FALSE, nrPages);
    392         }
     387    if(hMemFile == -1 && lpszMapName) {
     388        pMapping = VirtualAllocShared(mSize, fAlloc, PAGE_READWRITE, lpszMapName);
     389    }
     390    else {
     391        pMapping = VirtualAlloc(0, mSize, fAlloc, PAGE_READWRITE);
     392    }
     393    if(pMapping == NULL) {
     394        dprintf(("Win32MemMap::mapFileView: VirtualAlloc %x %x %x failed!", mSize, fAlloc, memFlags));
     395        goto fail;
     396    }
     397    //Windows NT seems to commit memory for memory maps, regardsless of the SEC_COMMIT flag
     398    if((hMemFile == -1 && !image)) {//commit memory
     399        VirtualAlloc(pMapping, mSize, MEM_COMMIT, PAGE_READWRITE);
     400    }
     401    if(hMemFile != -1 && (mProtFlags & SEC_COMMIT)) {
     402        DWORD nrPages = mSize >> PAGE_SHIFT;
     403        if(mSize & 0xFFF)
     404            nrPages++;
     405
     406        commitPage(0, FALSE, nrPages);
     407    }
    393408  }
    394409  mapview = new Win32MemMapView(this, offset, (size == 0) ? mSize : size, fdwAccess);
    395410  if(mapview == NULL) {
    396         goto fail;
     411    goto fail;
    397412  }
    398413  if(mapview->everythingOk() == FALSE) {
    399         dprintf(("Win32MemMap::mapFileView: !mapview->everythingOk"));
    400         delete mapview;
    401         goto fail;
     414    dprintf(("Win32MemMap::mapFileView: !mapview->everythingOk"));
     415    delete mapview;
     416    goto fail;
    402417  }
    403418  nrMappings++;
     
    428443
    429444  if(image) //no flushing for image maps
    430         return TRUE;
     445    return TRUE;
    431446
    432447  dprintf(("Win32MemMap::flushView: %x %x", lpvBase, cbFlush));
    433448  if(nrMappings == 0)
    434         goto parmfail;
     449    goto parmfail;
    435450
    436451  if(cbFlush == 0)
    437         cbFlush = mSize;
     452    cbFlush = mSize;
    438453
    439454  if(lpvBase < pMapping || (ULONG)lpvBase+cbFlush > (ULONG)pMapping+mSize)
    440         goto parmfail;
     455    goto parmfail;
    441456
    442457  if(mProtFlags & PAGE_READONLY)
    443         goto parmfail;
     458    goto parmfail;
    444459
    445460  if(hMemFile == -1)
    446         goto success; //TODO: Return an error here?
     461    goto success; //TODO: Return an error here?
    447462
    448463  while(cbFlush) {
    449         if(VirtualQuery((LPSTR)lpvBase, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
    450                 dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
    451                 goto fail;
    452         }
    453         //If a page (or range of pages) is reserved or write protected, we
     464    if(VirtualQuery((LPSTR)lpvBase, &memInfo, sizeof(MEMORY_BASIC_INFORMATION)) == 0) {
     465        dprintf(("Win32MemMap::flushView: VirtualQuery (%x,%x) failed for %x", lpvBase, cbFlush, (ULONG)lpvBase+i*PAGE_SIZE));
     466        goto fail;
     467    }
     468    //If a page (or range of pages) is reserved or write protected, we
    454469        //won't bother flushing it to disk
    455         if(memInfo.State & MEM_COMMIT &&
     470    if(memInfo.State & MEM_COMMIT &&
    456471           memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
    457472        {//committed and allowed for writing?
    458                 offset = (ULONG)lpvBase - (ULONG)pMapping;
    459                 size   = memInfo.RegionSize;
    460                 if(size > cbFlush) {
    461                         size = cbFlush;
    462                 }
    463                 dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
    464 
    465                 if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
    466                         dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
    467                         goto fail;
    468                 }
    469                 if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
    470                         dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
    471                         goto fail;
    472                 }
    473                 if(nrBytesWritten != size) {
    474                         dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
    475                         goto fail;
    476                 }
    477         }
    478         lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
    479 
    480         if(cbFlush < memInfo.RegionSize)
    481                 break;
    482 
    483         cbFlush -= memInfo.RegionSize;
     473        offset = (ULONG)lpvBase - (ULONG)pMapping;
     474        size   = memInfo.RegionSize;
     475        if(size > cbFlush) {
     476            size = cbFlush;
     477        }
     478        dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
     479
     480        if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
     481            dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
     482            goto fail;
     483        }
     484        if(WriteFile(hMemFile, (LPSTR)lpvBase, size, &nrBytesWritten, NULL) == FALSE) {
     485            dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase));
     486            goto fail;
     487        }
     488        if(nrBytesWritten != size) {
     489            dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase));
     490            goto fail;
     491        }
     492    }
     493    lpvBase = (LPVOID)((ULONG)lpvBase + memInfo.RegionSize);
     494
     495    if(cbFlush < memInfo.RegionSize)
     496        break;
     497
     498    cbFlush -= memInfo.RegionSize;
    484499  }
    485500success:
     
    496511{
    497512  if(lpszName == NULL)
    498         return NULL;
     513    return NULL;
    499514
    500515  globalmapMutex.enter(VMUTEX_WAIT_FOREVER, &hGlobalMapMutex);
     
    502517
    503518  if(map != NULL) {
    504         while(map) {
    505                 if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
    506                         break;
    507                 map = map->next;
    508         }
     519    while(map) {
     520        if(map->lpszMapName && !strcmp(map->lpszMapName, lpszName))
     521            break;
     522        map = map->next;
     523    }
    509524  }
    510525  globalmapMutex.leave(&hGlobalMapMutex);
     
    520535
    521536  if(map != NULL) {
    522         while(map) {
    523                 if(map->pMapping && (ULONG)map->pMapping <= address &&
     537    while(map) {
     538        if(map->pMapping && (ULONG)map->pMapping <= address &&
    524539                   (ULONG)map->pMapping + map->mSize > address)
    525                 {
    526                         break;
    527                 }
    528                 map = map->next;
    529         }
     540        {
     541            break;
     542        }
     543        map = map->next;
     544    }
    530545  }
    531546  globalmapMutex.leave(&hGlobalMapMutex);
     
    542557  globalviewMutex.enter();
    543558  while(map) {
    544         nextmap = map->next;
    545         if(map->getProcessId() == processId) {
    546                 //Delete map directly for executable images (only used internally)
    547                 if(map->getImage()) {
    548                         delete map;
    549                 }
    550                 else {
    551                         if(!map->isClosed())
    552                                 CloseHandle(memmaps->hMemMap);
    553                         delete map;
    554                 }
    555         }
    556         else {
    557                 //delete all views created by this process for this map
    558                 Win32MemMapView::deleteViews(map);
    559         }
    560         map = nextmap;
     559    nextmap = map->next;
     560    if(map->getProcessId() == processId) {
     561        //Delete map directly for executable images (only used internally)
     562        if(map->getImage()) {
     563            delete map;
     564        }
     565        else {
     566            if(!map->isClosed())
     567                 CloseHandle(memmaps->hMemMap);
     568            else delete map;
     569        }
     570    }
     571    else {
     572        //delete all views created by this process for this map
     573        Win32MemMapView::deleteViews(map);
     574    }
     575    map = nextmap;
    561576  }
    562577  globalviewMutex.leave();
     
    579594  switch(fdwAccess) {
    580595  case FILE_MAP_READ:
    581         accessAttr = PAG_READ;
    582         mfAccess   = MEMMAP_ACCESS_READ;
    583         break;
     596    accessAttr = PAG_READ;
     597    mfAccess   = MEMMAP_ACCESS_READ;
     598    break;
    584599  case FILE_MAP_ALL_ACCESS:
    585600  case FILE_MAP_WRITE:
    586601  case FILE_MAP_WRITE|FILE_MAP_READ:
    587602  case FILE_MAP_COPY:
    588         accessAttr = (PAG_READ|PAG_WRITE);
    589         mfAccess   = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
    590         break;
     603    accessAttr = (PAG_READ|PAG_WRITE);
     604    mfAccess   = MEMMAP_ACCESS_READ | MEMMAP_ACCESS_WRITE;
     605    break;
    591606  }
    592607  if(map->getMemName() != NULL && map->getFileHandle() == -1) {
    593         //shared memory map, so map it into our address space
    594         if(OSLibDosGetNamedSharedMem((LPVOID *)&viewaddr, map->getMemName()) != OSLIB_NOERROR) {
    595                 dprintf(("new OSLibDosGetNamedSharedMem FAILED"));
    596                 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
    597                 errorState = 1;
    598                 return;
    599         }
    600   }
    601  
     608    //shared memory map, so map it into our address space
     609    if(OSLibDosGetNamedSharedMem((LPVOID *)&viewaddr, map->getMemName()) != OSLIB_NOERROR) {
     610        dprintf(("new OSLibDosGetNamedSharedMem FAILED"));
     611        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
     612        errorState = 1;
     613        return;
     614    }
     615  }
     616
    602617  //view == memory mapping for executable images (only used internally)
    603618  if(map->getImage()) {
    604         pMapView = map->getMappingAddr();
     619    pMapView = map->getMappingAddr();
    605620  }
    606621  else {
    607         if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
    608                 dprintf(("new OSLibDosAliasMem FAILED"));
    609                 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
    610                 errorState = 1;
    611                 return;
    612         }
     622    if(OSLibDosAliasMem(viewaddr, size, &pMapView, accessAttr) != OSLIB_NOERROR) {
     623        dprintf(("new OSLibDosAliasMem FAILED"));
     624        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
     625        errorState = 1;
     626        return;
     627    }
    613628  }
    614629
     
    617632  globalviewMutex.enter();
    618633  if(tmpview == NULL || tmpview->getViewAddr() > pMapView) {
    619         next     = mapviews;
    620         mapviews = this;
     634    next     = mapviews;
     635    mapviews = this;
    621636  }
    622637  else {
    623         while(tmpview->next) {
    624                 if(tmpview->next->getViewAddr() > pMapView) {
    625                         break;
    626                 }
    627                 tmpview = tmpview->next;
    628         }
    629         next          = tmpview->next;
    630         tmpview->next = this;
     638    while(tmpview->next) {
     639        if(tmpview->next->getViewAddr() > pMapView) {
     640            break;
     641        }
     642        tmpview = tmpview->next;
     643    }
     644    next          = tmpview->next;
     645    tmpview->next = this;
    631646  }
    632647  globalviewMutex.leave();
     
    637652{
    638653  if(errorState != 0)
    639         return;
     654    return;
    640655
    641656  dprintf(("Win32MemMapView dtor: deleting view %x %x", mOffset, mSize));
    642657
    643658  if(mfAccess & MEMMAP_ACCESS_WRITE)
    644         mParentMap->flushView(mOffset, mSize);
     659    mParentMap->flushView(mOffset, mSize);
    645660
    646661  //Don't free memory for executable image map views (only used internally)
    647662  if(!mParentMap->getImage())
    648         OSLibDosFreeMem(pMapView);
     663    OSLibDosFreeMem(pMapView);
    649664
    650665  globalviewMutex.enter();
     
    652667
    653668  if(view == this) {
    654         mapviews = next;
     669    mapviews = next;
    655670  }
    656671  else {
    657         while(view->next) {
    658                 if(view->next == this)
    659                         break;
    660                 view = view->next;
    661         }
    662         if(view->next) {
    663                 view->next = next;
    664         }
    665         else    dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
     672    while(view->next) {
     673        if(view->next == this)
     674            break;
     675        view = view->next;
     676    }
     677    if(view->next) {
     678        view->next = next;
     679    }
     680    else    dprintf(("Win32MemMapView::~Win32MemMapView: map not found!! (%x)", this));
    666681  }
    667682  globalviewMutex.leave();
     
    675690
    676691  if(view != NULL) {
    677         while(view) {
    678                 nextview = view->next;
    679                 if(view->getParentMap() == map)
    680                 {
    681                         globalviewMutex.leave();
    682                         delete view;
    683                         globalviewMutex.enter();
    684                 }
    685                 view = nextview;
    686         }
     692    while(view) {
     693        nextview = view->next;
     694        if(view->getParentMap() == map)
     695        {
     696            globalviewMutex.leave();
     697            delete view;
     698            globalviewMutex.enter();
     699        }
     700        view = nextview;
     701    }
    687702  }
    688703  globalviewMutex.leave();
     
    700715
    701716  if(view != NULL) {
    702         while(view && (ULONG)view->getViewAddr() <= address) {
    703                 if((ULONG)view->getViewAddr() <= address &&
     717    while(view && (ULONG)view->getViewAddr() <= address) {
     718        if((ULONG)view->getViewAddr() <= address &&
    704719                   (ULONG)view->getViewAddr() + view->getSize() > address &&
    705720                   view->getAccessFlags() >= accessType)
    706                 {
    707                         *offset = view->getOffset() + (address - (ULONG)view->getViewAddr());
    708                         goto success;
    709                 }
    710                 view = view->next;
    711         }
    712         //failure if we get here
    713         view = NULL;
     721        {
     722            *offset = view->getOffset() + (address - (ULONG)view->getViewAddr());
     723            goto success;
     724        }
     725        view = view->next;
     726    }
     727    //failure if we get here
     728    view = NULL;
    714729  }
    715730success:
    716731  if(view && !view->getParentMap()->isImageMap())
    717         dprintf(("findMapByView %x %x -> %x off %x", address, accessType, view->getViewAddr(), *offset));
     732    dprintf(("findMapByView %x %x -> %x off %x", address, accessType, view->getViewAddr(), *offset));
    718733
    719734  globalviewMutex.leave();
     
    729744  globalviewMutex.enter();
    730745  if(view != NULL) {
    731         while(view) {
    732                 if(view->getViewAddr() == address)
    733                 {
    734                         break;
    735                 }
    736                 view = view->next;
    737         }
     746    while(view) {
     747        if(view->getViewAddr() == address)
     748        {
     749            break;
     750        }
     751        view = view->next;
     752    }
    738753  }
    739754  globalviewMutex.leave();
Note: See TracChangeset for help on using the changeset viewer.