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.

169 lines
4.2 KiB

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Filename : MemoryManagement.h
  4. // Purpose : To collect all memory management issues.
  5. //
  6. // Project : Persistent Query
  7. // Component: Common
  8. //
  9. // Author : urib
  10. //
  11. // Log:
  12. // Apr 13 2000 urib Creation
  13. //
  14. ////////////////////////////////////////////////////////////////////////////////
  15. #ifndef MEMORYMANAGEMENT_H
  16. #define MEMORYMANAGEMENT_H
  17. #include "Excption.h"
  18. #include "Excption.h"
  19. #if !(defined(_PQS_LEAK_DETECTION) && defined(_DEBUG))
  20. ////////////////////////////////////////////////////////////////////////////////
  21. //
  22. // Debug OFF
  23. // Debug OFF
  24. // Debug OFF
  25. //
  26. ////////////////////////////////////////////////////////////////////////////////
  27. inline
  28. void* __cdecl operator new(size_t s) throw (CMemoryException)
  29. {
  30. void *p = malloc(s);
  31. if (NULL == p)
  32. {
  33. throw CMemoryException(L"Define _PQS_LEAK_DETECTION for real data here",
  34. 0);
  35. }
  36. return p;
  37. }
  38. #else // !(defined(_PQS_LEAK_DETECTION) && defined(_DEBUG))
  39. ////////////////////////////////////////////////////////////////////////////////
  40. //
  41. // Debug ON
  42. // Debug ON
  43. // Debug ON
  44. //
  45. ////////////////////////////////////////////////////////////////////////////////
  46. #include <new>
  47. #include <new.h>
  48. #include <crtdbg.h>
  49. #include <stdlib.h>
  50. #include "Injector.h"
  51. #include "Excption.h"
  52. ////////////////////////////////////////////////////////////////////////////////
  53. //
  54. // Throws an exception in case it is recommended by the injector.
  55. //
  56. ////////////////////////////////////////////////////////////////////////////////
  57. inline
  58. void Inject(
  59. ULONG ulSize,
  60. const char * szFileName,
  61. int nLine)
  62. {
  63. if (DoInjection(ulSize, szFileName, nLine))
  64. {
  65. THROW_MEMORY_EXCEPTION();
  66. }
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////
  69. //
  70. // Add injection to the CRT debug allocation routines.
  71. //
  72. ////////////////////////////////////////////////////////////////////////////////
  73. inline
  74. void* dbgRealloc(
  75. void * p,
  76. size_t s,
  77. const char * szFileName,
  78. int nLine)
  79. {
  80. Inject(s,szFileName,nLine);
  81. return _realloc_dbg(p, s, _NORMAL_BLOCK, szFileName, nLine);
  82. }
  83. inline
  84. void* dbgMalloc(
  85. unsigned int s,
  86. const char * szFileName,
  87. int nLine
  88. )
  89. {
  90. Inject(s,szFileName,nLine);
  91. return _malloc_dbg(s, _NORMAL_BLOCK, szFileName, nLine);
  92. }
  93. ////////////////////////////////////////////////////////////////////////////////
  94. //
  95. // Add exception throwing on NULL allocation. Add Injector support.
  96. //
  97. ////////////////////////////////////////////////////////////////////////////////
  98. inline
  99. void* __cdecl operator new(size_t s, const char* pszFile, unsigned long ulLine)
  100. throw (CMemoryException)
  101. {
  102. Inject(s, pszFile, ulLine);
  103. void *p = _malloc_dbg(s, _NORMAL_BLOCK, pszFile, ulLine);
  104. if (NULL == p)
  105. {
  106. WCHAR rwchFilename[1000];
  107. mbstowcs(rwchFilename,
  108. pszFile,
  109. sizeof(rwchFilename) / sizeof(rwchFilename[0]));
  110. throw CMemoryException(rwchFilename, ulLine);
  111. }
  112. return p;
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////
  115. //
  116. // Unwinding placment delete operator exists only in VC 6 and up
  117. //
  118. ////////////////////////////////////////////////////////////////////////////////
  119. inline
  120. void __cdecl operator delete(void * _P, const char *, unsigned long)
  121. {
  122. ::operator delete(_P);
  123. }
  124. ////////////////////////////////////////////////////////////////////////////////
  125. //
  126. // Redirect malloc, realloc and new to the debug version specifying
  127. // allocation location.
  128. //
  129. ////////////////////////////////////////////////////////////////////////////////
  130. #undef malloc
  131. #define malloc(s) dbgMalloc(s, __FILE__, __LINE__)
  132. #undef realloc
  133. #define realloc(p, s) dbgRealloc(p, s, __FILE__, __LINE__)
  134. #define DEBUG_NEW new(__FILE__,__LINE__)
  135. #define new DEBUG_NEW
  136. #endif // !(defined(_PQS_LEAK_DETECTION) && defined(_DEBUG))
  137. #endif MEMORYMANAGEMENT_H