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.

195 lines
6.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996.
  5. //
  6. // File: mydebug.hxx
  7. //
  8. // Contents: Functions to manage the debug heap
  9. //
  10. // Classes: ( none )
  11. //
  12. // Functions: void* operator new( size_t )
  13. // void* operator new( size_t, const char*, int )
  14. // void* my_new( size_t, const char*, int )
  15. // void operator delete( void* )
  16. //
  17. // Coupling:
  18. //
  19. // Notes: To enable the debug version, include this header file, use the
  20. // NEW macro instead of the new keyword, and include the following
  21. // line in your sources file:
  22. // DEBUG_CRTS=1
  23. //
  24. // History: 10-20-1996 ericne Created
  25. //
  26. //----------------------------------------------------------------------------
  27. #ifndef _MYDEBUG_
  28. #define _MYDEBUG_
  29. #ifdef _DEBUG
  30. #include <windows.h>
  31. #include <crtdbg.h>
  32. #include <malloc.h>
  33. #include <string.h>
  34. #include <new.h>
  35. // This macro strips the path information from a file name
  36. #define __MYFILE__ ( strrchr( __FILE__, '\\' ) + 1 )
  37. //+--------------------------------------------------------------------
  38. //
  39. // Function: my_new
  40. //
  41. // Synopsis:
  42. //
  43. // Arguments: [size] --
  44. // [file] --
  45. // [line] --
  46. //
  47. // Returns:
  48. //
  49. // History: 10-20-1996 ericne Created
  50. //
  51. // Notes:
  52. //
  53. //---------------------------------------------------------------------
  54. inline void * _CRTAPI1 my_new( size_t size, const char* file, int line )
  55. {
  56. void *pvoid = NULL;
  57. _PNH old_new_handler = NULL;
  58. do
  59. {
  60. // Check the new handler routine again in case another
  61. // thread has changed it
  62. old_new_handler = _query_new_handler( );
  63. // Call _malloc_dbg to allocate space on the debug heap
  64. pvoid = _malloc_dbg( size, _NORMAL_BLOCK, file, line );
  65. } while( ( NULL == pvoid ) &&
  66. ( 1 != _query_new_mode( ) ) &&
  67. ( NULL != old_new_handler ) &&
  68. ( 0 != old_new_handler( size ) ) );
  69. // Short-circuit evaluation of the above expression prevents the
  70. // new handler from being called if old_new_handler is NULL
  71. // Return the pointer
  72. return( pvoid );
  73. } // my_new
  74. //+--------------------------------------------------------------------
  75. //
  76. // Function: new
  77. //
  78. // Synopsis: Calls my_new. This function is needed to shadow the
  79. // default global new operator
  80. //
  81. // Arguments: [size] -- The size of the memory to be allocated
  82. //
  83. // Returns: void *
  84. //
  85. // History: 10-20-1996 ericne Created
  86. //
  87. // Notes:
  88. //
  89. //---------------------------------------------------------------------
  90. inline void * _CRTAPI1 operator new( size_t size )
  91. {
  92. return( my_new( size, NULL, 0 ) );
  93. } // new
  94. //+--------------------------------------------------------------------
  95. //
  96. // Function: new
  97. //
  98. // Synopsis: Calls my_new.
  99. //
  100. // Arguments: [size] -- Size of the user's memory block
  101. // [file] -- name of source file requesting the allocation
  102. // [line] -- line in source file of allocation request
  103. //
  104. // Returns:
  105. //
  106. // History: 10-20-1996 ericne Created
  107. //
  108. // Notes: If you use the NEW macro, this function will be called
  109. // with __MYFILE__ and __LINE__ as the 2nd and 3rd params
  110. //
  111. //---------------------------------------------------------------------
  112. inline void * _CRTAPI1 operator new( size_t size,
  113. const char* file,
  114. int line )
  115. {
  116. return( my_new( size, file, line ) );
  117. } // new
  118. //+--------------------------------------------------------------------
  119. //
  120. // Function: delete
  121. //
  122. // Synopsis: calls _free_dbg to clean up the debug heap
  123. //
  124. // Arguments: [pMemory] -- pointer to the users memory
  125. //
  126. // Returns: void
  127. //
  128. // History: 10-20-1996 ericne Created
  129. //
  130. // Notes:
  131. //
  132. //---------------------------------------------------------------------
  133. inline void _CRTAPI1 operator delete( void *pMemory )
  134. {
  135. // free the memory
  136. _free_dbg( pMemory, _NORMAL_BLOCK );
  137. } // delete
  138. #define NEW new( __MYFILE__, __LINE__ )
  139. #define DEBUG_INIT( ) \
  140. _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE ); \
  141. _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT ); \
  142. _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE ); \
  143. _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT )
  144. #define SET_CRT_DEBUG_FIELD( field ) \
  145. _CrtSetDbgFlag( (field) | _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) )
  146. #define CLEAR_CRT_DEBUG_FIELD( field ) \
  147. _CrtSetDbgFlag( ~(field) & _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) )
  148. #else
  149. #define NEW new
  150. #define DEBUG_INIT( ) ( (void) 0 )
  151. #define SET_CRT_DEBUG_FIELD( field ) ( (void) 0 )
  152. #define CLEAR_CRT_DEBUG_FIELD( field ) ( (void) 0 )
  153. #endif
  154. #endif