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.

141 lines
3.5 KiB

  1. // debug heap support header for Microsoft
  2. #pragma once
  3. #ifndef _XDEBUG_
  4. #define _XDEBUG_
  5. #pragma pack(push,8)
  6. #pragma warning(push,3)
  7. // SUPPORT FOR DEBUG HEAP
  8. #if !defined(_DEBUG)
  9. #define _NEW_CRT new
  10. #define _DELETE_CRT(_P) delete (_P)
  11. #define _DELETE_CRT_VEC(_P) delete[] (_P)
  12. #define _STRING_CRT string
  13. #else
  14. #include <xmemory>
  15. #include <xstring>
  16. _STD_BEGIN
  17. struct _DebugHeapTag_t
  18. { // placement new tag type to select debug CRT heap
  19. int _Type;
  20. };
  21. extern _CRTIMP2 const _DebugHeapTag_t _DebugHeapTag;
  22. _STD_END
  23. _CRTIMP2 void * __cdecl
  24. operator new(size_t, const std::_DebugHeapTag_t&, char *, int)
  25. _THROW1(std::bad_alloc); // allocate from the debug CRT heap
  26. _CRTIMP2 void * __cdecl
  27. operator new[](size_t, const std::_DebugHeapTag_t&, char *, int)
  28. _THROW1(std::bad_alloc); // allocate array from the debug CRT heap
  29. _CRTIMP2 void __cdecl
  30. operator delete(void *, const std::_DebugHeapTag_t&, char *, int)
  31. _THROW0(); // delete if new for debug CRT heap fails
  32. _CRTIMP2 void __cdecl
  33. operator delete[](void *, const std::_DebugHeapTag_t&, char *, int)
  34. _THROW0(); // delete if array new for debug CRT heap fails
  35. #define _NEW_CRT new(std::_DebugHeapTag, __FILE__, __LINE__)
  36. #define _DELETE_CRT(_P) std::_DebugHeapDelete(_P)
  37. #define _DELETE_CRT_VEC(_P) std::_DebugHeapDelete((void *)_P)
  38. #define _STRING_CRT _DebugHeapString
  39. _STD_BEGIN
  40. // TEMPLATE FUNCTION _DebugHeapDelete
  41. template<class _Ty>
  42. void _DebugHeapDelete(_Ty *_Ptr)
  43. { // delete from the debug CRT heap even if operator delete differs
  44. if (_Ptr != 0)
  45. {
  46. _Ptr->~_Ty();
  47. // delete as _NORMAL_BLOCK, not _CRT_BLOCK, since we might have
  48. // facets allocated by normal new.
  49. free(_Ptr);
  50. }
  51. }
  52. // TEMPLATE CLASS _DebugHeapAllocator
  53. template<class _Ty>
  54. class _DebugHeapAllocator
  55. : public allocator<_Ty>
  56. { // an allocator which uses the debug CRT heap
  57. public:
  58. template<class _Other>
  59. struct rebind
  60. { // convert _DebugHeapAllocator<_Ty> to _DebugHeapAllocator<_Other>
  61. typedef _DebugHeapAllocator<_Other> other;
  62. };
  63. pointer allocate(size_type _Count, const void *)
  64. { // allocate array of _Count elements, ignore hint
  65. return (_Ty *)_NEW_CRT char[_Count * sizeof(_Ty)];
  66. }
  67. pointer allocate(size_type _Count)
  68. { // allocate array of _Count elements
  69. return (_Ty *)_NEW_CRT char[_Count * sizeof(_Ty)];
  70. }
  71. void deallocate(pointer _Ptr, size_type)
  72. { // deallocate object at _Ptr, ignore size
  73. _DELETE_CRT_VEC(_Ptr);
  74. }
  75. };
  76. // CLASS _DebugHeapString
  77. class _DebugHeapString
  78. : public basic_string<char, char_traits<char>, _DebugHeapAllocator<char> >
  79. { // a version of std::string allocated on the debug CRT heap
  80. public:
  81. typedef _DebugHeapString _Myt;
  82. typedef basic_string<char, char_traits<char>, _DebugHeapAllocator<char> >
  83. _Mybase;
  84. typedef char _Elem;
  85. _DebugHeapString()
  86. : _Mybase()
  87. { // construct empty string
  88. }
  89. _DebugHeapString(const _Myt& _Right)
  90. : _Mybase(_Right)
  91. { // construct by copying _Right
  92. }
  93. _DebugHeapString(const _Elem *_Ptr)
  94. : _Mybase(_Ptr)
  95. { // construct from [_Ptr, <null>)
  96. }
  97. _DebugHeapString(const string &_Str)
  98. : _Mybase(_Str.c_str())
  99. { // construct from std::string
  100. }
  101. operator string() const
  102. {
  103. return string(c_str());
  104. }
  105. };
  106. _STD_END
  107. #endif /* _DEBUG */
  108. #pragma warning(pop)
  109. #pragma pack(pop)
  110. #endif /* _XDEBUG_ */
  111. /*
  112. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  113. * Consult your license regarding permissions and restrictions.
  114. V3.10:0009 */