Leaked source code of windows server 2003
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.

116 lines
3.6 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: mem.h
  4. //
  5. // Module: common
  6. //
  7. // Synopsis: Defined memory allocation routines: new delete SaAlloc SaFree and SaRealloc
  8. //
  9. // In retail version, HeapAlloc and HeapFree will be called
  10. //
  11. // In debug version, all allocated memory blocks are tracked and guarded with
  12. // special flag to watch for memory overwritten and memory leak. The memory
  13. // leak is reported when the binary is unloaded. The file name and line number
  14. // are also recorded and will be reported.
  15. //
  16. // You need to link with utils.lib and debug.lib
  17. // If you are using ATL, make sure to include mem.h and debug.h before
  18. // atlbase.h in stdafx.h
  19. // If you are using STL. undef _ATL_NO_DEBUG_CRT before include debug.h and
  20. // mem.h to allow crtdbg.h. However fileName/lineNumber for new will not
  21. // be recorded.
  22. //
  23. // Copyright (C) 1997-1998 Microsoft Corporation. All rights reserved.
  24. //
  25. // Author: fengsun
  26. //
  27. // Created 9/24 98
  28. //
  29. //+----------------------------------------------------------------------------
  30. #ifndef _MEM_INC_
  31. #define _MEM_INC_
  32. #include <windows.h>
  33. #if (defined(_DEBUG) || defined(DEBUG) )
  34. #define DEBUG_MEM // Enabled DEBUG_MEM in debug version
  35. #endif // _DEBUG || DEBUG
  36. //
  37. // If DEBUG_MEM is defined, keep track of all the allocations
  38. // Otherwise, only keep the count for memory leak
  39. //
  40. #if defined(DEBUG_MEM)
  41. //
  42. // Track all the allocated blocks with file name and line number
  43. //
  44. void* AllocDebugMem(long nSize, const char* lpFileName,int nLine);
  45. BOOL FreeDebugMem(void* lpMem);
  46. void* ReAllocDebugMem(void* lpMem, long nSize, const char* lpFileName,int nLine);
  47. BOOL CheckDebugMem();
  48. #define SaAlloc(nSize) AllocDebugMem(nSize,__FILE__, __LINE__)
  49. #define SaFree(lpMem) ((void)FreeDebugMem(lpMem))
  50. #define SaRealloc(pvPtr, nSize) ReAllocDebugMem(pvPtr, nSize,__FILE__, __LINE__)
  51. inline void __cdecl operator delete(void* p)
  52. {SaFree(p);}
  53. inline void* __cdecl operator new(size_t nSize, const char* lpszFileName, int nLine)
  54. { return AllocDebugMem(nSize, lpszFileName, nLine); }
  55. inline void* __cdecl operator new(size_t nSize)
  56. { return AllocDebugMem(nSize, NULL, 0); }
  57. #ifdef _ATL_NO_DEBUG_CRT // new and delete is also defined by crtdbg.h
  58. //
  59. // Redefine new to keep track of the file name and line number
  60. //
  61. #define DEBUG_NEW new(__FILE__, __LINE__)
  62. #define new DEBUG_NEW
  63. #endif // _ATL_NO_DEBUG_CRT
  64. #else // DEBUG_MEM
  65. #define CheckDebugMem() (TRUE)
  66. //
  67. // if _ATL_MIN_CRT is defined, ATL will implement these new/delete and CRT functions
  68. //
  69. #ifdef _ATL_MIN_CRT
  70. #include <stdlib.h>
  71. inline void *SaRealloc(void *pvPtr, size_t nBytes) {return realloc(pvPtr, nBytes);};
  72. inline void *SaAlloc(size_t nBytes) {return malloc(nBytes);};
  73. inline void SaFree(void *pvPtr) {free(pvPtr);};
  74. //
  75. // be consist with debug version. atlimpl.cpp will zero out upon allocation
  76. //
  77. #define _MALLOC_ZEROINIT
  78. #else // _ATL_MIN_CRT
  79. //
  80. // Use our own implementation
  81. //
  82. void *SaRealloc(void *pvPtr, size_t nBytes);
  83. void *SaAlloc(size_t nBytes);
  84. void SaFree(void *pvPtr);
  85. #ifndef NO_INLINE_NEW // sometime, these functions are not inlined and cause link problem, not sure why
  86. inline void __cdecl operator delete(void* p) {SaFree(p);}
  87. inline void* __cdecl operator new( size_t cSize ) { return SaAlloc(cSize); }
  88. #endif // NO_INLINE_NEW
  89. #endif // _ATL_MIN_CRT
  90. #endif // DEBUG_MEM
  91. #endif _MEM_INC_