Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.9 KiB

  1. /* -------------------------------------------------------------------------
  2. mapfile.h
  3. (was bbmpfile.h)
  4. Definitions for the mapped file class.
  5. Copyright (C) 1995 Microsoft Corporation.
  6. All Rights Reserved.
  7. Author
  8. Lindsay Harris - lindsayh
  9. History
  10. 14:49 on Mon 10 Apr 1995 -by- Lindsay Harris [lindsayh]
  11. First version, based on pvMapFile() and derivatives.
  12. ------------------------------------------------------------------------- */
  13. /*
  14. * Generic class to handle mapped files. Part of the reason for
  15. * turning this into a class is to allow tracking of mapping/unmapping
  16. * and thus to handle clean up of dangling mappings in exception
  17. * handling code. Tracking is enabled if the fTrack parameter is
  18. * specified.
  19. */
  20. #ifndef _MAPFILE_H_
  21. #define _MAPFILE_H_
  22. // Bits used in the m_fFlags field below.
  23. #define MF_TRACKING 0x0001 // Tracking use of this item.
  24. #define MF_RELINQUISH 0x0002 // Someone else to free this item.
  25. class CCreateFile
  26. {
  27. public:
  28. virtual HANDLE CreateFileHandle( LPCSTR szFileName ) = 0;
  29. };
  30. class CMapFile
  31. {
  32. public:
  33. CMapFile( const char *pchFileName, HANDLE & hFile, BOOL fWriteEnable, DWORD cbIncrease = 0, CCreateFile *pCreateFile = NULL);
  34. CMapFile( const WCHAR *pwchFileName, BOOL fWriteEnable, BOOL fTrack );
  35. CMapFile( const char *pchFileName, BOOL fWriteEnable, BOOL fTrack, DWORD cbSizeIncrease = 0 );
  36. CMapFile( HANDLE hFile, BOOL fWriteEnable, BOOL fTrack, DWORD dwSizeIncrease = 0, BOOL fZero = FALSE );
  37. ~CMapFile( void );
  38. // Get the details of this item.
  39. void *pvAddress( DWORD *pcb ) { if( pcb ) *pcb = m_cb; return m_pv; };
  40. // Relinquish control (meaning someone else unmaps file).
  41. void Relinquish( void ) { m_fFlags |= MF_RELINQUISH; };
  42. // Tells if mapping is OK.
  43. BOOL fGood(void) {return NULL != m_pv; };
  44. private:
  45. DWORD m_cb; // Size of this file.
  46. void *m_pv; // Address to use.
  47. DWORD m_fFlags; // See above for the bits used in here.
  48. WCHAR m_rgwchFileName[ MAX_PATH ]; // For error recording.
  49. void MapFromHandle( HANDLE hFile, BOOL fWriteEnable, DWORD cbIncrease, BOOL fZero = FALSE );
  50. };
  51. /*
  52. * For compatability with old code, the original functions remain.
  53. */
  54. void *pvMapFile( DWORD *pdwSize, const char *pchFileName, BOOL bWriteEnable );
  55. void *pvMapFile( DWORD *pdwSize, const WCHAR *pwchFileName, BOOL bWriteEnable );
  56. void *pvMapFile(const char *pchFileName, BOOL bWriteEnable,
  57. DWORD *pdwSizeFinal = NULL, DWORD dwSizeIncrease = 0);
  58. void * pvFromHandle( HANDLE hFile, BOOL bWriteEnable,
  59. DWORD * pdwSizeFinal = NULL, DWORD dwSizeIncrease = 0);
  60. #ifdef DEBUG
  61. //
  62. // CMapFileEx: version with guard pages to be used only in DEBUG builds
  63. // to catch other threads writing into our memory !
  64. //
  65. class CMapFileEx
  66. {
  67. public:
  68. CMapFileEx( HANDLE hFile, BOOL fWriteEnable, BOOL fTrack, DWORD dwSizeIncrease = 0 );
  69. ~CMapFileEx( void );
  70. void Cleanup( void ); // cleanup in case of failure !
  71. // Get the details of this item.
  72. void *pvAddress( DWORD *pcb ) { if( pcb ) *pcb = m_cb; return (void*)m_pv; };
  73. // Relinquish control (meaning someone else unmaps file).
  74. void Relinquish( void ) { m_fFlags |= MF_RELINQUISH; };
  75. // Tells if mapping is OK.
  76. BOOL fGood(void) {return NULL != m_pv; };
  77. // Protect and Unprotect mapping
  78. BOOL ProtectMapping();
  79. BOOL UnprotectMapping();
  80. private:
  81. DWORD m_cb; // Size of this file.
  82. LPBYTE m_pv; // Address to use.
  83. DWORD m_fFlags; // See above for the bits used in here.
  84. WCHAR m_rgwchFileName[ MAX_PATH ]; // For error recording.
  85. HANDLE m_hFile; // handle to the mapped file
  86. LPBYTE m_pvFrontGuard; // front guard page
  87. DWORD m_cbFrontGuardSize; // front guard page size
  88. LPBYTE m_pvRearGuard; // rear guard page
  89. DWORD m_cbRearGuardSize; // rear guard page size
  90. CRITICAL_SECTION m_csProtectMap; // crit sect to protect/unprotect mapping
  91. void MapFromHandle( HANDLE hFile, BOOL fWriteEnable, DWORD cbIncrease );
  92. };
  93. #endif // DEBUG
  94. #endif // _MAPFILE_H_