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.

99 lines
2.4 KiB

  1. //////////////////////////////////////////////////////
  2. //
  3. // HeapDet.h - Copyright 1995, Don Box
  4. //
  5. // Simple IMallocSpy to track allocation byte count
  6. //
  7. #ifndef _HEAPDET_H
  8. #define _HEAPDET_H
  9. class CoHeapDetective : public IMallocSpy
  10. {
  11. public:
  12. CoHeapDetective();
  13. virtual ~CoHeapDetective();
  14. SIZE_T GetBytesAlloced() const;
  15. #ifdef _HEAPDET_INTERNAL_
  16. __declspec( dllexport )
  17. #else
  18. __declspec( dllimport )
  19. #endif
  20. static class CoHeapDetective*
  21. GetDetective() ;
  22. private:
  23. // paramters to cache between pre/post phases
  24. static HMODULE g_HeapdetLib ;
  25. static long g_cLibRefs ;
  26. //
  27. // Number of References on this instance
  28. //
  29. long m_cRefs ;
  30. SIZE_T m_cbLastAlloc;
  31. void *m_pvLastRealloc;
  32. // total heap usage
  33. SIZE_T m_dwBytesAlloced;
  34. // output device for tracing
  35. HANDLE m_hTraceOutput;
  36. // helper function to send simple trace message to debug window
  37. void Trace(SIZE_T cb, PVOID pv, LPCTSTR szAction, BOOL bSuccess);
  38. // simple alloc header to track allocation size
  39. struct ArenaHeader
  40. {
  41. enum { SIGNATURE = 0x1BADABBAL };
  42. struct ArenaHeader* m_pNext ;
  43. struct ArenaHeader* m_pPrev ;
  44. SIZE_T m_dwAllocSize; // the user's idea of size
  45. DWORD m_dwSignature; // always 0x1BADABBA when good
  46. };
  47. ArenaHeader m_list ;
  48. // helper function to write a valid arena header at ptr
  49. void SetArenaHeader(void *ptr, SIZE_T dwAllocSize);
  50. // helper function to verify and return the prepended
  51. // header (or null if failure)
  52. ArenaHeader *GetHeader(void *ptr);
  53. public:
  54. // IUnknown methods
  55. STDMETHODIMP QueryInterface(REFIID riid, void**ppv);
  56. STDMETHODIMP_(ULONG) AddRef();
  57. STDMETHODIMP_(ULONG) Release();
  58. // IMallocSpy methods
  59. STDMETHODIMP_(SIZE_T) PreAlloc(SIZE_T cbRequest);
  60. STDMETHODIMP_(void*) PostAlloc(void *pActual);
  61. STDMETHODIMP_(void*) PreFree(void *pRequest, BOOL fSpyed);
  62. STDMETHODIMP_(void) PostFree(BOOL fSpyed);
  63. STDMETHODIMP_(SIZE_T) PreRealloc(void *pRequest, SIZE_T cbRequest,
  64. void **ppNewRequest, BOOL fSpyed);
  65. STDMETHODIMP_(void*) PostRealloc(void *pActual, BOOL fSpyed);
  66. STDMETHODIMP_(void*) PreGetSize(void *pRequest, BOOL fSpyed);
  67. STDMETHODIMP_(SIZE_T) PostGetSize(SIZE_T cbActual, BOOL fSpyed);
  68. STDMETHODIMP_(void*) PreDidAlloc(void *pRequest, BOOL fSpyed);
  69. STDMETHODIMP_(int) PostDidAlloc(void *pRequest, BOOL fSpyed, int fActual);
  70. STDMETHODIMP_(void) PreHeapMinimize(void);
  71. STDMETHODIMP_(void) PostHeapMinimize(void);
  72. };
  73. #endif