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.

139 lines
2.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. HeapDelayLocalFree.cpp
  5. Abstract:
  6. Delay calls to LocalFree.
  7. History:
  8. 09/19/2000 robkenny
  9. --*/
  10. #include "precomp.h"
  11. #include "CharVector.h"
  12. IMPLEMENT_SHIM_BEGIN(HeapDelayLocalFree)
  13. #include "ShimHookMacro.h"
  14. APIHOOK_ENUM_BEGIN
  15. APIHOOK_ENUM_ENTRY(LocalFree)
  16. APIHOOK_ENUM_END
  17. CRITICAL_SECTION g_CritSec;
  18. static VectorT<HLOCAL> *g_DelayLocal = NULL;
  19. static DWORD g_DelayBufferSize = 20;
  20. HLOCAL
  21. APIHOOK(LocalFree)(
  22. HLOCAL hMem // handle to local memory object
  23. )
  24. {
  25. if (hMem == NULL)
  26. return NULL;
  27. if (g_DelayLocal)
  28. {
  29. EnterCriticalSection(&g_CritSec);
  30. // If the list is full
  31. if (g_DelayLocal->Size() > 0 &&
  32. g_DelayLocal->Size() >= g_DelayLocal->MaxSize())
  33. {
  34. HLOCAL & hDelayed = g_DelayLocal->Get(0);
  35. #if DBG
  36. DPFN(eDbgLevelInfo, "LocalFree(0x%08x).", hDelayed);
  37. #endif
  38. ORIGINAL_API(LocalFree)(hDelayed);
  39. g_DelayLocal->Remove(0);
  40. }
  41. g_DelayLocal->Append(hMem);
  42. #if DBG
  43. DPFN(eDbgLevelInfo, "Delaying LocalFree(0x%08x).", hMem);
  44. #endif
  45. LeaveCriticalSection(&g_CritSec);
  46. return NULL;
  47. }
  48. HLOCAL returnValue = ORIGINAL_API(LocalFree)(hMem);
  49. return returnValue;
  50. }
  51. BOOL ParseCommandLine(const char * commandLine)
  52. {
  53. InitializeCriticalSection(&g_CritSec);
  54. g_DelayLocal = new VectorT<HLOCAL>;
  55. if (g_DelayLocal)
  56. {
  57. // If we cannot resize the array, stop now
  58. if (!g_DelayLocal->Resize(g_DelayBufferSize))
  59. {
  60. delete g_DelayLocal;
  61. g_DelayLocal = NULL;
  62. // Turn off all hooks:
  63. return FALSE;
  64. }
  65. }
  66. return TRUE;
  67. }
  68. /*++
  69. Register hooked functions
  70. --*/
  71. BOOL
  72. NOTIFY_FUNCTION(
  73. DWORD fdwReason
  74. )
  75. {
  76. if (fdwReason == DLL_PROCESS_ATTACH)
  77. {
  78. return ParseCommandLine(COMMAND_LINE);
  79. }
  80. else if (fdwReason == DLL_PROCESS_DETACH)
  81. {
  82. if (g_DelayLocal)
  83. {
  84. delete g_DelayLocal;
  85. g_DelayLocal = NULL;
  86. }
  87. DeleteCriticalSection(&g_CritSec);
  88. }
  89. return TRUE;
  90. }
  91. /*++
  92. Register hooked functions
  93. --*/
  94. HOOK_BEGIN
  95. CALL_NOTIFY_FUNCTION
  96. APIHOOK_ENTRY(KERNEL32.DLL, LocalFree)
  97. HOOK_END
  98. IMPLEMENT_SHIM_END