Ignore:
Timestamp:
Feb 18, 2003, 7:48:55 PM (23 years ago)
Author:
sandervl
Message:

Cleaned up memory map code

File:
1 edited

Legend:

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

    r9489 r9824  
    1 /* $Id: mmap.cpp,v 1.60 2002-12-12 12:33:08 sandervl Exp $ */
     1/* $Id: mmap.cpp,v 1.61 2003-02-18 18:48:55 sandervl Exp $ */
    22
    33/*
     
    7070}
    7171//******************************************************************************
    72 //TODO: sharing between processes
    7372//******************************************************************************
    7473Win32MemMap::Win32MemMap(HFILE hfile, ULONG size, ULONG fdwProtect, LPSTR lpszName)
     
    8079    DosLeaveCriticalSection(&globalmapcritsect);
    8180
    82     hMemFile   = hfile;
     81    hMemFile = hOrgMemFile = hfile;
    8382
    8483    mSize      = size;
     
    103102    DosLeaveCriticalSection(&globalmapcritsect);
    104103
    105     hMemFile   = -1;
     104    hMemFile = hOrgMemFile = -1;
    106105
    107106    mSize      = size;
     
    216215    }
    217216    DosLeaveCriticalSection(&globalmapcritsect);
     217}
     218//******************************************************************************
     219// Win32MemMap::setProtFlags
     220//
     221// Change the protection flags of this memory map if required
     222// This is currently only used when creating a mapping for a file which already
     223// has an existing mapping.
     224//
     225//
     226// Parameters:
     227//
     228//   DWORD dwNewProtect         - new protection flags
     229//
     230// Returns:
     231//   TRUE                       - success
     232//   FALSE                      - failure
     233//
     234// NOTE:
     235//   We're ignoring the SEC_* flags for now
     236//
     237//******************************************************************************
     238BOOL Win32MemMap::setProtFlags(DWORD dwNewProtect)
     239{
     240    if(!(dwNewProtect & (PAGE_READWRITE|PAGE_WRITECOPY))) return TRUE; //no need for changes
     241
     242    if(!(mProtFlags & PAGE_READWRITE))
     243    {//ok, current mapping is readonly; need to change it to readwrite
     244        mProtFlags &= ~PAGE_READONLY;
     245        mProtFlags |= PAGE_READWRITE;
     246
     247        //that's all we need to do for now; memory mappings are readwrite by
     248        //default (see mapViewOfFile)
     249    }
     250    return TRUE;
    218251}
    219252//******************************************************************************
     
    330363}
    331364//******************************************************************************
    332 //******************************************************************************
    333 BOOL Win32MemMap::unmapViewOfFile(Win32MemMapView *view)
    334 {
    335     dprintf(("Win32MemMap::unmapViewOfFile %x (nrmaps=%d)", view, nrMappings));
     365// Win32MemMap::unmapViewOfFile
     366//
     367// Unmap the view identified by addr
     368//
     369// Parameters:
     370//
     371//   LPVOID addr                - view address; doesn't need to be the address
     372//                                returned by MapViewOfFile(Ex) (as MSDN clearly says);
     373//                                can be any address within the view range
     374//
     375// Returns:
     376//   TRUE                       - success
     377//   FALSE                      - failure
     378//
     379//******************************************************************************
     380BOOL Win32MemMap::unmapViewOfFile(LPVOID addr)
     381{
     382    Win32MemMapView *view;
     383
     384    dprintf(("Win32MemMap::unmapViewOfFile %x (nrmaps=%d)", addr, nrMappings));
    336385    mapMutex.enter();
    337386
    338387    if(nrMappings == 0)
     388        goto fail;
     389
     390    view = Win32MemMapView::findView((ULONG)addr);
     391    if(view == NULL)
    339392        goto fail;
    340393
     
    355408fail:
    356409    mapMutex.leave();
    357     if(nrMappings == 0 && referenced == 0) {
    358         delete this;
    359     }
    360410    return FALSE;
    361411}
     
    405455
    406456    //Memory has already been allocated for executable image maps (only used internally)
    407     if(!pMapping && nrMappings == 0) {//if not mapped, reserve/commit entire view
     457    if(!pMapping && nrMappings == 0)
     458    {//if not mapped, reserve/commit entire view
    408459        //SvL: Always read/write access or else ReadFile will crash once we
    409460        //     start committing pages.
     
    411462        //     when allocating memory with the PAG_ANY bit set. (without this
    412463        //     flag it will also crash)
     464        //NOTE: If this is ever changed, then we must update setProtFlags!!!!
     465       
    413466        //All named file mappings are shared (files & memory only)
    414467        if(lpszMapName) {
     
    550603    }
    551604  }
     605  if(map) map->AddRef();
     606
    552607  DosLeaveCriticalSection(&globalmapcritsect);
    553608  if(!map) dprintf(("Win32MemMap::findMap: couldn't find map %s", lpszName));
     609  return map;
     610}
     611//******************************************************************************
     612//******************************************************************************
     613Win32MemMap *Win32MemMap::findMapByFile(HANDLE hFile)
     614{
     615  if(hFile == -1)
     616    return NULL;
     617
     618  DosEnterCriticalSection(&globalmapcritsect);
     619  Win32MemMap *map = memmaps;
     620
     621  if(map != NULL)
     622  {
     623    while(map) {
     624        if(map->hOrgMemFile == hFile)
     625            break;
     626        map = map->next;
     627    }
     628  }
     629  if(map) map->AddRef();
     630  DosLeaveCriticalSection(&globalmapcritsect);
     631  if(!map) dprintf(("Win32MemMap::findMapByFile: couldn't find map with file handle %x", hFile));
    554632  return map;
    555633}
     
    571649    }
    572650  }
     651  if(map) map->AddRef();
    573652  DosLeaveCriticalSection(&globalmapcritsect);
    574653  return map;
     
    738817//******************************************************************************
    739818//******************************************************************************
     819// Win32MemMap::findMapByView
     820//
     821// Find the map of the view that contains the specified starting address
     822// and has the specified access type
     823//
     824// Parameters:
     825//
     826//   ULONG address              - view address
     827//   ULONG *offset              - address of ULONG that receives the offset
     828//                                in the returned memory map
     829//   ULONG accessType           - access type:
     830//                                MEMMAP_ACCESS_READ
     831//                                MEMMAP_ACCESS_WRITE
     832//                                MEMMAP_ACCESS_EXECUTE
     833//
     834// Returns:
     835//   <> NULL                    - success, address of parent map object
     836//   NULL                       - failure
     837//
     838//******************************************************************************
     839//******************************************************************************
    740840Win32MemMap *Win32MemMapView::findMapByView(ULONG address,
    741841                                            ULONG *offset,
    742                                             ULONG accessType,
    743                                             Win32MemMapView **pView)
    744 {
     842                                            ULONG accessType)
     843{
     844  Win32MemMap *map = NULL;
     845  ULONG ulOffset;
     846
    745847  if(mapviews == NULL) return NULL;
    746848
     
    748850  Win32MemMapView *view = mapviews;
    749851  ULONG ulViewAddr;
     852
     853  if(!offset)  offset = &ulOffset;
    750854
    751855  *offset = 0;
     
    782886    view = NULL;
    783887  }
    784 
    785888success:
    786889#ifdef DEBUG
     
    793896#endif
    794897
     898  if(view) {
     899      map = view->getParentMap();
     900      if(map) map->AddRef();
     901  }
     902
    795903  DosLeaveCriticalSection(&globalmapcritsect);
    796904
    797   if(pView)
    798       *pView = view;
    799 
    800   return (view) ? view->getParentMap() : NULL;
    801 }
    802 //******************************************************************************
    803 //******************************************************************************
    804 Win32MemMapView *Win32MemMapView::findView(LPVOID address)
    805 {
     905  return map;
     906}
     907//******************************************************************************
     908// Win32MemMap::findView
     909//
     910// Find the view that contains the specified starting address
     911//
     912// Parameters:
     913//
     914//   LPVOID address             - view address
     915//
     916// Returns:
     917//   <> NULL                    - success, address view object
     918//   NULL                       - failure
     919//
     920//******************************************************************************
     921Win32MemMapView *Win32MemMapView::findView(ULONG address)
     922{
     923  ULONG ulViewAddr;
     924
     925  DosEnterCriticalSection(&globalmapcritsect);
    806926  Win32MemMapView *view = mapviews;
    807927
    808   DosEnterCriticalSection(&globalmapcritsect);
    809928  if(view != NULL) {
    810929    while(view) {
    811         if(view->getViewAddr() == address)
     930        ulViewAddr = (ULONG)view->getViewAddr();
     931        if(ulViewAddr <= address && ulViewAddr + view->getSize() > address)
    812932        {
    813933            break;
     
    821941//******************************************************************************
    822942//******************************************************************************
    823 
Note: See TracChangeset for help on using the changeset viewer.