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.

178 lines
4.4 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: trackva.cxx
  7. //
  8. // Contents: Track virtual address reservations made through VirtualAlloc
  9. //
  10. // History: 15-Mar-96 dlee Created.
  11. //
  12. // Notes: No header/tail checking is done in this allocator.
  13. // Assumes all entry points are called under a lock.
  14. //
  15. //----------------------------------------------------------------------------
  16. #include <pch.cxx>
  17. #pragma hdrstop
  18. DECLARE_DEBUG( va );
  19. DECLARE_INFOLEVEL( va );
  20. #define vaDebugOut(x) vaInlineDebugOut x
  21. const unsigned VM_SIZE = 0x80000000;
  22. const unsigned VM_GRANULARITY = 0x00010000;
  23. const unsigned VM_REPORTUNIT = 2;
  24. inline OffsetInArray( PVOID lpAddr )
  25. {
  26. return ((ULONG)lpAddr / VM_GRANULARITY) * VM_REPORTUNIT;
  27. }
  28. inline SizeToCount( ULONG cb )
  29. {
  30. return ((ULONG)(cb + VM_GRANULARITY - 1) / VM_GRANULARITY) * VM_REPORTUNIT;
  31. }
  32. //+-------------------------------------------------------------------------
  33. //
  34. // Function: RecordVirtualAlloc, private
  35. //
  36. // Synopsis: Record VirtualAlloc Memory reservations in the pbVmTracker
  37. // array
  38. //
  39. //+-------------------------------------------------------------------------
  40. char * pbVmTracker = 0;
  41. void RecordVirtualAlloc(
  42. char * pszSig,
  43. PVOID lpAddr,
  44. DWORD dwSize)
  45. {
  46. if (pbVmTracker == 0)
  47. {
  48. pbVmTracker = (char*) VirtualAlloc( 0,
  49. (VM_SIZE/VM_GRANULARITY) * VM_REPORTUNIT,
  50. MEM_COMMIT,
  51. PAGE_READWRITE );
  52. RecordVirtualAlloc( "Vmtracker", pbVmTracker, VM_SIZE/VM_GRANULARITY );
  53. }
  54. unsigned iOff = OffsetInArray( lpAddr );
  55. unsigned cbSize = SizeToCount( dwSize );
  56. char *psz = &pbVmTracker[ iOff ];
  57. char * pszIn = pszSig;
  58. while (cbSize--)
  59. {
  60. *psz++ = *pszIn++;
  61. if (*pszIn == '\0')
  62. pszIn = pszSig;
  63. }
  64. }
  65. //+-------------------------------------------------------------------------
  66. //
  67. // Function: TrackVirtualAlloc, public
  68. //
  69. // Synopsis: Track VirtualAlloc Memory reservations
  70. //
  71. //+-------------------------------------------------------------------------
  72. PVOID TrackVirtualAlloc(
  73. char * pszSig,
  74. PVOID lpAddr,
  75. DWORD dwSize,
  76. DWORD flAllocationType,
  77. DWORD flProtect )
  78. {
  79. PVOID lpRet = VirtualAlloc( lpAddr, dwSize, flAllocationType, flProtect );
  80. if ( flAllocationType == MEM_RESERVE ||
  81. (lpAddr == 0 && flAllocationType == MEM_COMMIT) )
  82. {
  83. if ( 0 == lpRet )
  84. {
  85. // vaDebugOut(( DEB_WARN,
  86. // "VirtualAlloc with MEM_RESERVE for tag %s failed\n",
  87. // pszSig ));
  88. }
  89. else
  90. {
  91. RecordVirtualAlloc( pszSig, lpRet, dwSize );
  92. }
  93. }
  94. return lpRet;
  95. }
  96. BOOL TrackVirtualFree(
  97. PVOID lpAddr,
  98. DWORD dwSize,
  99. DWORD flFreeType )
  100. {
  101. BOOL fRet = VirtualFree( lpAddr, dwSize, flFreeType );
  102. if ( !fRet )
  103. {
  104. // vaDebugOut(( DEB_WARN,
  105. // "VirtualFree( %08x, %08x, %d ) failed\n",
  106. // lpAddr, dwSize, flFreeType ));
  107. }
  108. else if ( flFreeType == MEM_RELEASE )
  109. {
  110. unsigned iOff = OffsetInArray( lpAddr );
  111. pbVmTracker[ iOff ] = '\0';
  112. }
  113. return fRet;
  114. }
  115. PVOID TrackMapViewOfFile (
  116. char * pszSig,
  117. HANDLE hMap,
  118. DWORD fWrite,
  119. DWORD offHigh,
  120. DWORD offLow,
  121. DWORD cb )
  122. {
  123. void* buf = MapViewOfFile ( hMap,
  124. fWrite,
  125. offHigh,
  126. offLow,
  127. cb );
  128. if ( 0 != buf )
  129. {
  130. RecordVirtualAlloc( pszSig, buf, cb );
  131. }
  132. else
  133. {
  134. // vaDebugOut(( DEB_WARN,
  135. // "MapViewOfFile for tag %s failed\n",
  136. // pszSig ));
  137. }
  138. return buf;
  139. }
  140. BOOL TrackUnmapViewOfFile(
  141. PVOID lpAddr )
  142. {
  143. if ( !UnmapViewOfFile(lpAddr) )
  144. {
  145. // vaDebugOut(( DEB_WARN,
  146. // "UnMapViewOfFile (%08x) failed\n",
  147. // lpAddr ));
  148. return FALSE;
  149. }
  150. else
  151. {
  152. unsigned iOff = OffsetInArray( lpAddr );
  153. pbVmTracker[ iOff ] = '\0';
  154. return TRUE;
  155. }
  156. }