Changeset 9824 for trunk/src


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

Cleaned up memory map code

Location:
trunk/src/kernel32
Files:
7 edited

Legend:

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

    r9315 r9824  
    1 /* $Id: exceptions.cpp,v 1.66 2002-10-01 11:37:19 sandervl Exp $ */
     1/* $Id: exceptions.cpp,v 1.67 2003-02-18 18:48:53 sandervl Exp $ */
    22
    33/*
     
    12401240        map = Win32MemMapView::findMapByView(pERepRec->ExceptionInfo[1], &offset, accessflag);
    12411241        if(map == NULL) {
    1242                 goto continueFail;
    1243         }
    1244         if(map->commitPage(offset, fWriteAccess) == TRUE)
    1245                 goto continueexecution;
     1242            goto continueFail;
     1243        }
     1244        BOOL ret = map->commitPage(offset, fWriteAccess);
     1245        map->Release();
     1246        if(ret == TRUE);
     1247            goto continueexecution;
    12461248
    12471249        //no break;
  • trunk/src/kernel32/hmdisk.cpp

    r9663 r9824  
    1 /* $Id: hmdisk.cpp,v 1.60 2003-01-12 16:19:37 sandervl Exp $ */
     1/* $Id: hmdisk.cpp,v 1.61 2003-02-18 18:48:54 sandervl Exp $ */
    22
    33/*
     
    18461846   
    18471847        map->commitPage(offset & ~0xfff, TRUE, nrpages);
     1848        map->Release();
    18481849    }
    18491850    else  lpRealBuf = (LPVOID)lpBuffer;
     
    21112112   
    21122113        map->commitPage(offset & ~0xfff, TRUE, nrpages);
     2114        map->Release();
    21132115    }
    21142116    else  lpRealBuf = (LPVOID)lpBuffer;
  • trunk/src/kernel32/hmfile.cpp

    r9748 r9824  
    1 /* $Id: hmfile.cpp,v 1.39 2003-02-04 11:28:57 sandervl Exp $ */
     1/* $Id: hmfile.cpp,v 1.40 2003-02-18 18:48:54 sandervl Exp $ */
    22
    33/*
     
    534534
    535535       map->commitPage(offset & ~0xfff, TRUE, nrpages);
     536       map->Release();
    536537  }
    537538  else lpRealBuf = (LPVOID)lpBuffer;
     
    621622 
    622623       map->commitPage(offset & ~0xfff, TRUE, nrpages);
     624       map->Release();
    623625  }
    624626  else lpRealBuf = (LPVOID)lpBuffer;
  • trunk/src/kernel32/hmmmap.cpp

    r8456 r9824  
    1 /* $Id: hmmmap.cpp,v 1.20 2002-05-20 13:47:58 sandervl Exp $ */
     1/* $Id: hmmmap.cpp,v 1.21 2003-02-18 18:48:54 sandervl Exp $ */
    22
    33/*
     
    7474  map = Win32MemMap::findMap((LPSTR)name);
    7575  if(map != NULL) {
    76         dprintf(("CreateFileMappingA: duplicating map %s!", name));
     76        dprintf(("CreateFileMappingA: duplicating map %s!", name));
    7777
    7878        DWORD protflags = map->getProtFlags();
     
    9494        //Is it allowed to open an existing view with different flags?
    9595        //(i.e. write access to readonly object)
    96         dprintf(("CreateFileMapping: duplicate handle of existing file mapping %s", name));
     96        // -> for the same file handle, yes
    9797
    9898        //if map already exists, we must create a new handle to the existing
    9999        //map object and return ERROR_ALREADY_EXISTS
    100         map->AddRef();
    101100        pHMHandleData->dwUserData = (ULONG)map;
    102101        pHMHandleData->dwInternalType = HMTYPE_MEMMAP;
     102
     103        //findMap already incremented the reference count, so we simply don't
     104        //release it here
    103105        return ERROR_ALREADY_EXISTS;
    104106  }
     107 
     108#if 0
     109  //We reuse the original memory map object if another one is created for
     110  //the same file handle
     111  //TODO: different file handles can exist for the same file (DuplicateHandle)
     112  map = Win32MemMap::findMapByFile(hFile);
     113  if(map) {
     114        dprintf(("CreateFileMappingA: duplicating map with file %x!", hFile));
     115
     116        //if map already exists, we must create a new handle to the existing
     117        //map object
     118        pHMHandleData->dwUserData = (ULONG)map;
     119        pHMHandleData->dwInternalType = HMTYPE_MEMMAP;
     120
     121        //findMap already incremented the reference count, so we simply don't
     122        //release it here
     123        return ERROR_SUCCESS;
     124  }
     125#endif
    105126  else {
    106127        map = new Win32MemMap(hFile, size_low, protect, (LPSTR)name);
     
    131152 Win32MemMap *map;
    132153 DWORD        protflags;
     154 DWORD        ret;
    133155
    134156  if(name == NULL)
     
    144166  case FILE_MAP_WRITE:
    145167  case FILE_MAP_ALL_ACCESS:
    146         if(!(protflags & (PAGE_WRITECOPY|PAGE_READWRITE)))
    147                 return ERROR_INVALID_PARAMETER;
     168        if(!(protflags & (PAGE_WRITECOPY|PAGE_READWRITE))) {
     169            ret = ERROR_INVALID_PARAMETER;
     170            goto fail;
     171        }
    148172        break;
    149173  case FILE_MAP_READ:
    150         if(!(protflags & (PAGE_READWRITE | PAGE_READONLY)))
    151                 return ERROR_INVALID_PARAMETER;
     174        if(!(protflags & (PAGE_READWRITE | PAGE_READONLY))) {
     175            ret = ERROR_INVALID_PARAMETER;
     176            goto fail;
     177        }
    152178        break;
    153179  case FILE_MAP_COPY:
    154         if(!(protflags & PAGE_WRITECOPY))
    155                 return ERROR_INVALID_PARAMETER;
     180        if(!(protflags & PAGE_WRITECOPY)) {
     181            ret = ERROR_INVALID_PARAMETER;
     182            goto fail;
     183        }
    156184        break;
    157185  }
    158   map->AddRef();
     186  //findMap already incremented the reference count, so we simply don't
     187  //release it here
    159188  pHMHandleData->dwUserData = (ULONG)map;
    160189  pHMHandleData->dwInternalType = HMTYPE_MEMMAP;
    161190  return NO_ERROR;
     191
     192fail:
     193  map->Release();
     194  return ret;
    162195}
    163196//******************************************************************************
  • 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 
  • trunk/src/kernel32/mmap.h

    r8913 r9824  
    1 /* $Id: mmap.h,v 1.24 2002-07-23 13:51:48 sandervl Exp $ */
     1/* $Id: mmap.h,v 1.25 2003-02-18 18:48:55 sandervl Exp $ */
    22
    33/*
     
    2626
    2727//commit 4 pages at once when the app accesses it
    28 #define NRPAGES_TOCOMMIT    16
     28#define NRPAGES_TOCOMMIT        16
    2929
    30 #define MEMMAP_ACCESS_READ  1
    31 #define MEMMAP_ACCESS_WRITE 2
     30#define MEMMAP_ACCESS_READ      1
     31#define MEMMAP_ACCESS_WRITE     2
    3232#define MEMMAP_ACCESS_EXECUTE   4
    3333
     
    4848   BOOL   flushView(ULONG offset, ULONG cbFlush);
    4949   LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
    50    BOOL   unmapViewOfFile(Win32MemMapView *view);
     50   BOOL   unmapViewOfFile(LPVOID addr);
    5151
    5252   HFILE  getFileHandle()                { return hMemFile; };
    5353   LPSTR  getMemName()                   { return lpszMapName; };
    5454   DWORD  getProtFlags()                 { return mProtFlags; };
     55   BOOL   setProtFlags(DWORD dwNewProtect);
    5556   LPVOID getMappingAddr()               { return pMapping; };
    5657   DWORD  getProcessId()                 { return mProcessId;};
     
    6566
    6667static Win32MemMap *findMap(LPSTR lpszName);
     68static Win32MemMap *findMapByFile(HANDLE hFile);
    6769static Win32MemMap *findMap(ULONG address);
    6870
     
    9294protected:
    9395   HFILE  hMemFile;
     96   HFILE  hOrgMemFile;
    9497   ULONG  mSize;
    9598   ULONG  mProtFlags;
     
    131134
    132135static void             deleteViews(Win32MemMap *map);
    133 static Win32MemMap     *findMapByView(ULONG address, ULONG *offset, ULONG accessType, Win32MemMapView **pView=NULL);
    134 static Win32MemMapView *findView(LPVOID address);
     136static Win32MemMap     *findMapByView(ULONG address, ULONG *offset = NULL,
     137                                      ULONG accessType = MEMMAP_ACCESS_READ);
     138static Win32MemMapView *findView(ULONG address);
    135139
    136140#ifdef __DEBUG_ALLOC__
  • trunk/src/kernel32/virtual.cpp

    r9546 r9824  
    1 /* $Id: virtual.cpp,v 1.49 2002-12-27 15:25:40 sandervl Exp $ */
     1/* $Id: virtual.cpp,v 1.50 2003-02-18 18:48:55 sandervl Exp $ */
    22
    33/*
     
    167167 Win32MemMap *map;
    168168 DWORD offset;
     169 BOOL ret;
    169170
    170171    if (!base)
     
    178179        return FALSE;
    179180    }
    180     return map->flushView(offset, cbFlush);
     181    ret = map->flushView(offset, cbFlush);
     182    map->Release();
     183    return ret;
    181184}
    182185
     
    196199)
    197200{
    198  Win32MemMap *map;
    199  Win32MemMapView *view;
    200 
    201  DWORD offset;
     201    Win32MemMap *map;
     202    BOOL  ret;
    202203
    203204    if (!addr)
     
    206207        return FALSE;
    207208    }
    208     map = Win32MemMapView::findMapByView((ULONG)addr, &offset, MEMMAP_ACCESS_READ, &view);
     209    map = Win32MemMapView::findMapByView((ULONG)addr);
    209210    if(map == NULL) {
    210211        SetLastError( ERROR_FILE_NOT_FOUND );
    211212        return FALSE;
    212213    }
    213     return map->unmapViewOfFile(view);
     214    ret = map->unmapViewOfFile(addr);
     215    map->Release();
     216    return ret;
    214217}
    215218
     
    350353            //TODO: We don't allow protection flag changes for mmaped files now
    351354            map->commitPage(offset, FALSE, nrpages);
     355            map->Release();
    352356            return lpvAddress;
    353357        }
Note: See TracChangeset for help on using the changeset viewer.