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.

142 lines
3.4 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. template<class _Other>
  64. pointer allocate(size_type _Count, const _Other *)
  65. { // allocate array of _Count elements, ignore hint
  66. return (_Ty *)_NEW_CRT char[_Count * sizeof(_Ty)];
  67. }
  68. pointer allocate(size_type _Count)
  69. { // allocate array of _Count elements
  70. return (_Ty *)_NEW_CRT char[_Count * sizeof(_Ty)];
  71. }
  72. void deallocate(pointer _Ptr, size_type)
  73. { // deallocate object at _Ptr, ignore size
  74. _DELETE_CRT_VEC(_Ptr);
  75. }
  76. };
  77. // CLASS _DebugHeapString
  78. class _DebugHeapString
  79. : public basic_string<char, char_traits<char>, _DebugHeapAllocator<char> >
  80. { // a version of std::string allocated on the debug CRT heap
  81. public:
  82. typedef _DebugHeapString _Myt;
  83. typedef basic_string<char, char_traits<char>, _DebugHeapAllocator<char> >
  84. _Mybase;
  85. typedef char _Elem;
  86. _DebugHeapString()
  87. : _Mybase()
  88. { // construct empty string
  89. }
  90. _DebugHeapString(const _Myt& _Right)
  91. : _Mybase(_Right)
  92. { // construct by copying _Right
  93. }
  94. _DebugHeapString(const _Elem *_Ptr)
  95. : _Mybase(_Ptr)
  96. { // construct from [_Ptr, <null>)
  97. }
  98. _DebugHeapString(const string &_Str)
  99. : _Mybase(_Str.c_str())
  100. { // construct from std::string
  101. }
  102. operator string() const
  103. {
  104. return string(c_str());
  105. }
  106. };
  107. _STD_END
  108. #endif /* _DEBUG */
  109. #pragma warning(pop)
  110. #pragma pack(pop)
  111. #endif /* _XDEBUG_ */
  112. /*
  113. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  114. * Consult your license regarding permissions and restrictions.
  115. V3.10:0009 */