Ignore:
Timestamp:
Aug 24, 1999, 4:36:05 PM (26 years ago)
Author:
phaller
Message:

Add: HandleManager support for memory mapped files

File:
1 edited

Legend:

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

    r578 r659  
    1 /* $Id: HandleManager.cpp,v 1.11 1999-08-19 12:57:41 phaller Exp $ */
     1/* $Id: HandleManager.cpp,v 1.12 1999-08-24 14:36:04 phaller Exp $ */
    22
    33/*
     
    5656#include "HMMutex.h"
    5757#include "HMSemaphore.h"
     58#include "HMFileMapping.h"
    5859
    5960
     
    121122  HMDeviceHandler        *pHMMutex;
    122123  HMDeviceHandler        *pHMSemaphore;
     124  HMDeviceHandler        *pHMFileMapping;
    123125
    124126  ULONG         ulHandleLast;                   /* index of last used handle */
     
    311313
    312314                        /* create handle manager instance for Open32 handles */
    313     HMGlobals.pHMOpen32     = new HMDeviceOpen32Class("\\\\.\\");
    314     HMGlobals.pHMEvent      = new HMDeviceEventClass("\\\\EVENT\\");
    315     HMGlobals.pHMMutex      = new HMDeviceMutexClass("\\\\MUTEX\\");
    316     HMGlobals.pHMSemaphore  = new HMDeviceSemaphoreClass("\\\\SEM\\");
     315    HMGlobals.pHMOpen32       = new HMDeviceOpen32Class("\\\\.\\");
     316    HMGlobals.pHMEvent        = new HMDeviceEventClass("\\\\EVENT\\");
     317    HMGlobals.pHMMutex        = new HMDeviceMutexClass("\\\\MUTEX\\");
     318    HMGlobals.pHMSemaphore    = new HMDeviceSemaphoreClass("\\\\SEM\\");
     319    HMGlobals.pHMFileMapping  = new HMDeviceFileMappingClass("\\\\FILEMAPPING\\");
    317320  }
    318321  return (NO_ERROR);
     
    340343  delete HMGlobals.pHMMutex;
    341344  delete HMGlobals.pHMSemaphore;
     345  delete HMGlobals.pHMFileMapping;
    342346
    343347  return (NO_ERROR);
     
    22372241}
    22382242
     2243
     2244/*****************************************************************************
     2245 * Name      : HANDLE  HMCreateFileMapping
     2246 * Purpose   : Wrapper for the CreateFileMapping() API
     2247 * Parameters:
     2248 * Variables :
     2249 * Result    :
     2250 * Remark    :
     2251 * Status    :
     2252 *
     2253 * Author    : Patrick Haller [Wed, 1998/02/11 20:44]
     2254 *****************************************************************************/
     2255
     2256HANDLE HMCreateFileMapping(HANDLE                hFile,
     2257                           LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
     2258                           DWORD                 flProtect,
     2259                           DWORD                 dwMaximumSizeHigh,
     2260                           DWORD                 dwMaximumSizeLow,
     2261                           LPCTSTR               lpName)
     2262{
     2263  int             iIndex;                     /* index into the handle table */
     2264  int             iIndexNew;                  /* index into the handle table */
     2265  HMDeviceHandler *pDeviceHandler;         /* device handler for this handle */
     2266  PHMHANDLEDATA   pHMHandleData;
     2267  DWORD           rc;                                     /* API return code */
     2268
     2269
     2270  pDeviceHandler = HMGlobals.pHMFileMapping;         /* device is predefined */
     2271
     2272  iIndexNew = _HMHandleGetFree();                         /* get free handle */
     2273  if (-1 == iIndexNew)                            /* oops, no free handles ! */
     2274  {
     2275    SetLastError(ERROR_NOT_ENOUGH_MEMORY);      /* use this as error message */
     2276    return (INVALID_HANDLE_VALUE);                           /* signal error */
     2277  }
     2278
     2279
     2280                           /* initialize the complete HMHANDLEDATA structure */
     2281  pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
     2282  pHMHandleData->dwType     = FILE_TYPE_UNKNOWN;      /* unknown handle type */
     2283  pHMHandleData->dwAccess   = 0;
     2284  pHMHandleData->dwShare    = 0;
     2285  pHMHandleData->dwCreation = 0;
     2286  pHMHandleData->dwFlags    = 0;
     2287  pHMHandleData->lpHandlerData = NULL;
     2288
     2289
     2290      /* we've got to mark the handle as occupied here, since another device */
     2291                   /* could be created within the device handler -> deadlock */
     2292
     2293          /* write appropriate entry into the handle table if open succeeded */
     2294  TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
     2295  TabWin32Handles[iIndexNew].pDeviceHandler         = pDeviceHandler;
     2296
     2297                                                  /* call the device handler */
     2298
     2299  // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
     2300  //              a valid HandleManager-internal handle!
     2301  rc = pDeviceHandler->CreateFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
     2302                                         hFile,
     2303                                         lpFileMappingAttributes,
     2304                                         flProtect,
     2305                                         dwMaximumSizeHigh,
     2306                                         dwMaximumSizeLow,
     2307                                         lpName);
     2308
     2309  if (rc != NO_ERROR)     /* oops, creation failed within the device handler */
     2310  {
     2311    TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
     2312    SetLastError(rc);          /* Hehe, OS/2 and NT are pretty compatible :) */
     2313    return (INVALID_HANDLE_VALUE);                           /* signal error */
     2314  }
     2315
     2316  return iIndexNew;                                   /* return valid handle */
     2317}
     2318
     2319
     2320/*****************************************************************************
     2321 * Name      : HANDLE  HMOpenFileMapping
     2322 * Purpose   : Wrapper for the OpenFileMapping() API
     2323 * Parameters:
     2324 * Variables :
     2325 * Result    :
     2326 * Remark    :
     2327 * Status    :
     2328 *
     2329 * Author    : Patrick Haller [Wed, 1998/02/11 20:44]
     2330 *****************************************************************************/
     2331
     2332HANDLE HMOpenFileMapping(DWORD   fdwAccess,
     2333                         BOOL    fInherit,
     2334                         LPCTSTR lpName)
     2335{
     2336  int             iIndex;                     /* index into the handle table */
     2337  int             iIndexNew;                  /* index into the handle table */
     2338  HMDeviceHandler *pDeviceHandler;         /* device handler for this handle */
     2339  PHMHANDLEDATA   pHMHandleData;
     2340  DWORD           rc;                                     /* API return code */
     2341
     2342
     2343  pDeviceHandler = HMGlobals.pHMFileMapping;         /* device is predefined */
     2344
     2345  iIndexNew = _HMHandleGetFree();                         /* get free handle */
     2346  if (-1 == iIndexNew)                            /* oops, no free handles ! */
     2347  {
     2348    SetLastError(ERROR_NOT_ENOUGH_MEMORY);      /* use this as error message */
     2349    return (INVALID_HANDLE_VALUE);                           /* signal error */
     2350  }
     2351
     2352
     2353                           /* initialize the complete HMHANDLEDATA structure */
     2354  pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
     2355  pHMHandleData->dwType     = FILE_TYPE_UNKNOWN;      /* unknown handle type */
     2356  pHMHandleData->dwAccess   = fdwAccess;
     2357  pHMHandleData->dwShare    = 0;
     2358  pHMHandleData->dwCreation = 0;
     2359  pHMHandleData->dwFlags    = 0;
     2360  pHMHandleData->lpHandlerData = NULL;
     2361
     2362
     2363      /* we've got to mark the handle as occupied here, since another device */
     2364                   /* could be created within the device handler -> deadlock */
     2365
     2366          /* write appropriate entry into the handle table if open succeeded */
     2367  TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
     2368  TabWin32Handles[iIndexNew].pDeviceHandler         = pDeviceHandler;
     2369
     2370                                                  /* call the device handler */
     2371  rc = pDeviceHandler->OpenFileMapping(&TabWin32Handles[iIndexNew].hmHandleData,
     2372                                       fInherit,
     2373                                       lpName);
     2374  if (rc != NO_ERROR)     /* oops, creation failed within the device handler */
     2375  {
     2376    TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
     2377    SetLastError(rc);          /* Hehe, OS/2 and NT are pretty compatible :) */
     2378    return (INVALID_HANDLE_VALUE);                           /* signal error */
     2379  }
     2380
     2381  return iIndexNew;                                   /* return valid handle */
     2382}
     2383
     2384
     2385/*****************************************************************************
     2386 * Name      : HMMapViewOfFile
     2387 * Purpose   : router function for MapViewOfFile
     2388 * Parameters:
     2389 * Variables :
     2390 * Result    :
     2391 * Remark    :
     2392 * Status    :
     2393 *
     2394 * Author    : Patrick Haller [Wed, 1999/06/17 20:44]
     2395 *****************************************************************************/
     2396
     2397LPVOID HMMapViewOfFile(HANDLE hFileMappingObject,
     2398                       DWORD  dwDesiredAccess,
     2399                       DWORD  dwFileOffsetHigh,
     2400                       DWORD  dwFileOffsetLow,
     2401                       DWORD  dwNumberOfBytesToMap)
     2402{
     2403  int       iIndex;                           /* index into the handle table */
     2404  LPVOID    lpResult;                /* result from the device handler's API */
     2405  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     2406
     2407                                                          /* validate handle */
     2408  iIndex = _HMHandleQuery(hFileMappingObject);              /* get the index */
     2409  if (-1 == iIndex)                                               /* error ? */
     2410  {
     2411    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     2412    return (NULL);                                         /* signal failure */
     2413  }
     2414
     2415  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     2416  lpResult = pHMHandle->pDeviceHandler->MapViewOfFile(&pHMHandle->hmHandleData,
     2417                                                      dwDesiredAccess,
     2418                                                      dwFileOffsetHigh,
     2419                                                      dwFileOffsetLow,
     2420                                                      dwNumberOfBytesToMap);
     2421
     2422  return (lpResult);                                  /* deliver return code */
     2423}
     2424
     2425
     2426
     2427/*****************************************************************************
     2428 * Name      : HMMapViewOfFileEx
     2429 * Purpose   : router function for MapViewOfFileEx
     2430 * Parameters:
     2431 * Variables :
     2432 * Result    :
     2433 * Remark    :
     2434 * Status    :
     2435 *
     2436 * Author    : Patrick Haller [Wed, 1999/06/17 20:44]
     2437 *****************************************************************************/
     2438
     2439LPVOID HMMapViewOfFileEx(HANDLE hFileMappingObject,
     2440                         DWORD  dwDesiredAccess,
     2441                         DWORD  dwFileOffsetHigh,
     2442                         DWORD  dwFileOffsetLow,
     2443                         DWORD  dwNumberOfBytesToMap,
     2444                         LPVOID lpBaseAddress)
     2445{
     2446  int       iIndex;                           /* index into the handle table */
     2447  LPVOID    lpResult;                /* result from the device handler's API */
     2448  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     2449
     2450                                                          /* validate handle */
     2451  iIndex = _HMHandleQuery(hFileMappingObject);              /* get the index */
     2452  if (-1 == iIndex)                                               /* error ? */
     2453  {
     2454    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     2455    return (NULL);                                         /* signal failure */
     2456  }
     2457
     2458  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     2459  lpResult = pHMHandle->pDeviceHandler->MapViewOfFileEx(&pHMHandle->hmHandleData,
     2460                                                        dwDesiredAccess,
     2461                                                        dwFileOffsetHigh,
     2462                                                        dwFileOffsetLow,
     2463                                                        dwNumberOfBytesToMap,
     2464                                                        lpBaseAddress);
     2465
     2466  return (lpResult);                                  /* deliver return code */
     2467}
     2468
     2469
     2470/*****************************************************************************
     2471 * Name      : HMUnmapViewOfFile
     2472 * Purpose   : router function for UnmapViewOfFile
     2473 * Parameters:
     2474 * Variables :
     2475 * Result    :
     2476 * Remark    : No handle is specified, that makes things a bit more difficult!
     2477 *             We've got to identify the object by the base address and check
     2478 *             back with HandleManager manually!
     2479 * Status    :
     2480 *
     2481 * Author    : Patrick Haller [Wed, 1999/06/17 20:44]
     2482 *****************************************************************************/
     2483
     2484BOOL HMUnmapViewOfFile(LPVOID lpBaseAddress)
     2485{
     2486  int       iIndex;                           /* index into the handle table */
     2487  BOOL      flResult;                /* result from the device handler's API */
     2488  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     2489
     2490  // Identify the correct handle by the base address. Thus call a special
     2491  // static function within the FileMapping class.
     2492  iIndex = HMDeviceFileMappingClass::findByBaseAddress(lpBaseAddress);                                                          /* validate handle */
     2493  if (-1 == iIndex)                                               /* error ? */
     2494  {
     2495    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     2496    return (FALSE);                                        /* signal failure */
     2497  }
     2498
     2499  flResult = pHMHandle->pDeviceHandler->UnmapViewOfFile(&pHMHandle->hmHandleData,
     2500                                                        lpBaseAddress);
     2501
     2502  // @@@PH automatically call CloseHandle of no more references to the object
     2503  //       are active.
     2504
     2505  return (flResult);                                  /* deliver return code */
     2506}
     2507
     2508
     2509/*****************************************************************************
     2510 * Name      : HMFlushViewOfFile
     2511 * Purpose   : router function for FlushViewOfFile
     2512 * Parameters:
     2513 * Variables :
     2514 * Result    :
     2515 * Remark    : No handle is specified, that makes things a bit more difficult!
     2516 *             We've got to identify the object by the base address and check
     2517 *             back with HandleManager manually!
     2518 * Status    :
     2519 *
     2520 * Author    : Patrick Haller [Wed, 1999/06/17 20:44]
     2521 *****************************************************************************/
     2522
     2523BOOL HMFlushViewOfFile(LPVOID lpBaseAddress,
     2524                       DWORD  dwNumberOfBytesToFlush)
     2525{
     2526  int       iIndex;                           /* index into the handle table */
     2527  BOOL      flResult;                /* result from the device handler's API */
     2528  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     2529
     2530  // Identify the correct handle by the base address. Thus call a special
     2531  // static function within the FileMapping class.
     2532  iIndex = HMDeviceFileMappingClass::findByBaseAddress(lpBaseAddress);                                                          /* validate handle */
     2533  if (-1 == iIndex)                                               /* error ? */
     2534  {
     2535    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     2536    return (FALSE);                                        /* signal failure */
     2537  }
     2538
     2539  flResult = pHMHandle->pDeviceHandler->FlushViewOfFile(&pHMHandle->hmHandleData,
     2540                                                        lpBaseAddress,
     2541                                                        dwNumberOfBytesToFlush);
     2542
     2543  return (flResult);                                  /* deliver return code */
     2544}
Note: See TracChangeset for help on using the changeset viewer.