Ignore:
Timestamp:
Aug 25, 1999, 12:28:41 PM (26 years ago)
Author:
sandervl
Message:

Put back handlemanager changes + memory mapped file changes

File:
1 edited

Legend:

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

    r664 r678  
    1 /* $Id: mmap.cpp,v 1.8 1999-08-24 18:46:40 sandervl Exp $ */
     1/* $Id: mmap.cpp,v 1.9 1999-08-25 10:28:40 sandervl Exp $ */
    22
    33/*
     
    1818 *
    1919 * TODO: Handles returned should be usable by all apis that accept file handles
     20 * TODO: Sharing memory mapped files between multiple processes
    2021 *
    2122 * Project Odin Software License can be found in LICENSE.TXT
     
    115116//******************************************************************************
    116117//******************************************************************************
     118BOOL Win32MemMap::hasReadAccess()
     119{
     120  return TRUE; //should have at least this
     121}
     122//******************************************************************************
     123//******************************************************************************
     124BOOL Win32MemMap::hasWriteAccess()
     125{
     126  return !(mProtFlags & PAGE_READONLY);
     127}
     128//******************************************************************************
     129//Might want to add this feature for memory mapping executable & dll files in
     130//the loader (done in Win32 with the SEC_IMAGE flag?)
     131//******************************************************************************
     132BOOL Win32MemMap::hasExecuteAccess()
     133{
     134  return FALSE;
     135}
     136//******************************************************************************
     137//******************************************************************************
     138BOOL Win32MemMap::commitPage(LPVOID lpPageFaultAddr, ULONG nrpages)
     139{
     140 DWORD pageAddr = (DWORD)lpPageFaultAddr & ~0xFFF;
     141 DWORD oldProt, newProt, nrBytesRead, offset, size;
     142 
     143//  mapMutex.enter();
     144  newProt  = mProtFlags & (PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY);
     145  newProt |= MEM_COMMIT;
     146
     147  dprintf(("Win32MemMap::commitPage %x (faultaddr %x), nr of pages %d", pageAddr, lpPageFaultAddr, nrpages));
     148  if(VirtualProtect((LPVOID)pageAddr, nrpages*PAGE_SIZE, newProt, &oldProt) == FALSE) {
     149        goto fail;
     150  }
     151  if(hMemFile != -1) {
     152        offset = pageAddr - (ULONG)pMapping;
     153        size   = nrpages*PAGE_SIZE;
     154        if(offset + size > mSize) {
     155                size = mSize - offset;
     156        }
     157        if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
     158                dprintf(("Win32MemMap::commitPage: SetFilePointer failed to set pos to %x", offset));
     159                goto fail;
     160        }
     161        if(ReadFile(hMemFile, (LPSTR)pageAddr, size, &nrBytesRead, NULL) == FALSE) {
     162                dprintf(("Win32MemMap::commitPage: ReadFile failed for %x", pageAddr));
     163                goto fail;
     164        }
     165        if(nrBytesRead != size) {
     166                dprintf(("Win32MemMap::commitPage: ReadFile didn't read all bytes for %x", pageAddr));
     167                goto fail;
     168        }
     169  }
     170
     171//  mapMutex.leave();
     172  return TRUE;
     173fail:
     174//  mapMutex.leave();
     175  return FALSE;
     176}
     177//******************************************************************************
     178//******************************************************************************
    117179BOOL Win32MemMap::unmapViewOfFile()
    118180{
     
    145207  if(fdwAccess & FILE_MAP_COPY && !(mProtFlags & PAGE_WRITECOPY))
    146208        goto parmfail;
    147  
     209
     210//TODO: If committed, read file into memory
     211#if 0 
    148212  if(mProtFlags & SEC_COMMIT)
    149213        fAlloc |= MEM_COMMIT;
     214  else
    150215  if(mProtFlags & SEC_RESERVE)
    151216        fAlloc |= MEM_RESERVE;
     217#else
     218  fAlloc = MEM_RESERVE;
     219#endif
    152220
    153221  if(fMapped == FALSE) {//if not mapped, reserve/commit entire view
     
    179247{
    180248 MEMORY_BASIC_INFORMATION memInfo;
    181  ULONG nrpages, nrBytesWritten;
     249 ULONG nrpages, nrBytesWritten, offset, size;
    182250 int   i;
    183251
    184   mapMutex.enter();
     252//  mapMutex.enter();
    185253  if(fMapped == FALSE)
    186254        goto parmfail;
     
    204272           memInfo.AllocationProtect & (PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
    205273        {//committed and allowed for writing?
    206                 if(WriteFile(hMemFile, (LPSTR)lpvBase+i*PAGE_SIZE, PAGE_SIZE, &nrBytesWritten, NULL) == FALSE) {
     274                offset = (ULONG)lpvBase+i*PAGE_SIZE - (ULONG)pMapping;
     275                size   = PAGE_SIZE;
     276                if(offset + size > mSize) {
     277                        size = mSize - offset;
     278                }
     279                dprintf(("Win32MemMap::flushView for offset %x, size %d", offset, size));
     280
     281                if(SetFilePointer(hMemFile, offset, NULL, FILE_BEGIN) != offset) {
     282                        dprintf(("Win32MemMap::flushView: SetFilePointer failed to set pos to %x", offset));
     283                        goto fail;
     284                }
     285                if(WriteFile(hMemFile, (LPSTR)lpvBase+i*PAGE_SIZE, size, &nrBytesWritten, NULL) == FALSE) {
    207286                        dprintf(("Win32MemMap::flushView: WriteFile failed for %x", (ULONG)lpvBase+i*PAGE_SIZE));
    208287                        goto fail;
    209288                }
    210                 if(nrBytesWritten != PAGE_SIZE) {
     289                if(nrBytesWritten != size) {
    211290                        dprintf(("Win32MemMap::flushView: WriteFile didn't write all bytes for %x", (ULONG)lpvBase+i*PAGE_SIZE));
    212291                        goto fail;
     
    214293        }
    215294  }
    216   mapMutex.leave();
     295//  mapMutex.leave();
    217296  return TRUE;
    218297parmfail:
    219298  SetLastError(ERROR_INVALID_PARAMETER);
    220   mapMutex.leave();
     299//  mapMutex.leave();
    221300  return FALSE;
    222301fail:
    223   mapMutex.leave();
     302//  mapMutex.leave();
    224303  return FALSE;
    225304}
     
    264343//******************************************************************************
    265344//******************************************************************************
     345void Win32MemMap::deleteAll()
     346{
     347  while(memmaps) {
     348        CloseHandle(memmaps->hMemMap);
     349  }
     350}
     351//******************************************************************************
     352//******************************************************************************
    266353Win32MemMap *Win32MemMap::memmaps = NULL;
Note: See TracChangeset for help on using the changeset viewer.