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.

176 lines
3.6 KiB

  1. // Copyright (C) 1994-1997 Microsoft Corporation. All rights reserved.
  2. #include "header.h"
  3. // our lame workshop relies on the old exported memory functions
  4. // so we have to keep exporting these but we will now implement these
  5. // using the CRT heap
  6. //
  7. #ifdef HHA
  8. #undef lcSize
  9. int STDCALL lcSize(void* pv)
  10. {
  11. return _msize(pv);
  12. }
  13. void* STDCALL rcalloc(int cb)
  14. {
  15. void* pv = lcMalloc( cb );
  16. memset( pv, 0, cb );
  17. return pv;
  18. }
  19. void STDCALL rfree(void* pv)
  20. {
  21. lcFree( pv );
  22. }
  23. void STDCALL rclearfree(void** pv)
  24. {
  25. lcFree( *pv );
  26. *pv = NULL;
  27. }
  28. void STDCALL rheapcheck()
  29. {
  30. }
  31. void* STDCALL rmalloc(int cb)
  32. {
  33. return lcMalloc( cb );
  34. }
  35. void* STDCALL rrealloc(void* pv, int cb)
  36. {
  37. return lcReAlloc( pv, cb );
  38. }
  39. #define lcSize(pv) _msize(pv)
  40. #endif
  41. #ifdef _DEBUG
  42. #undef THIS_FILE
  43. static const char THIS_FILE[] = __FILE__;
  44. #endif
  45. PSTR lcStrDup(PCSTR psz)
  46. {
  47. if (!psz)
  48. psz = "";
  49. PSTR pszDup = (PSTR) lcMalloc(strlen(psz) + 1);
  50. return strcpy(pszDup, psz);
  51. }
  52. PWSTR lcStrDupW(PCWSTR psz)
  53. {
  54. if (!psz)
  55. psz = L"";
  56. int cb = (lstrlenW(psz)*sizeof(WCHAR)) + sizeof(WCHAR);
  57. PWSTR pszDup = (PWSTR) lcMalloc(cb);
  58. if (pszDup)
  59. CopyMemory(pszDup, psz, cb);
  60. return pszDup;
  61. }
  62. CMem::CMem(void)
  63. {
  64. pb = NULL;
  65. #ifndef HHCTRL
  66. psz = (PSTR) pb;
  67. #endif
  68. }
  69. CMem::CMem(int size)
  70. {
  71. _ASSERT(size > 0);
  72. pb = (PBYTE) lcMalloc(size);
  73. #ifndef HHCTRL
  74. psz = (PSTR) pb;
  75. #endif
  76. _ASSERT(pb);
  77. };
  78. #ifndef HHCTRL
  79. int CMem::size(void) { return lcSize(pb); }
  80. void CMem::resize(int cb) { ReAlloc(cb); }
  81. #endif
  82. #ifdef HHCTRL
  83. #if _DEBUG
  84. ///////////////////////////////////////////////////////////
  85. //
  86. // The new heap status report...
  87. //
  88. // Following variables defined in CTable.cpp, _DEBUG only
  89. extern int g_cbTableAllocated;
  90. extern int g_cbTableReserved;
  91. extern int g_cTables;
  92. void OnReportMemoryUsage(void)
  93. {
  94. // Get the current memory state.
  95. _CrtMemState NewMemState ;
  96. _CrtMemCheckpoint(&NewMemState) ;
  97. char buf[4096] ;
  98. wsprintf(buf,
  99. "\tBlocks\tBytes\r\n\t------\t-----\r\n"
  100. "Free: \t%12ld\t%12ld\r\n"
  101. "Normal:\t%12ld\t%12ld\r\n"
  102. "CRT: \t%12ld\t%12ld\r\n"
  103. "Ignore:\t%12ld\t%12ld\r\n"
  104. "Client: \t%12ld\t%12ld\r\n\r\n"
  105. "Largest Used: %ld\r\n"
  106. "Total Allocations: %ld\r\n\r\nTables (%u): %d bytes\r\n"
  107. "Reserved: %d megs",
  108. NewMemState.lCounts[0], NewMemState.lSizes[0],
  109. NewMemState.lCounts[1], NewMemState.lSizes[1],
  110. NewMemState.lCounts[2], NewMemState.lSizes[2],
  111. NewMemState.lCounts[3], NewMemState.lSizes[3],
  112. NewMemState.lCounts[4], NewMemState.lSizes[4],
  113. NewMemState.lHighWaterCount,
  114. NewMemState.lTotalCount,
  115. g_cTables, g_cbTableAllocated, g_cbTableReserved / (1024*1024)) ;
  116. MsgBox(buf);
  117. // Dump it to the debug output.
  118. _CrtMemDumpStatistics(&NewMemState);
  119. }
  120. ///////////////////////////////////////////////////////////
  121. //
  122. // This class is used to initialize the CRT debug code.
  123. //
  124. class DebugAutoInitializer
  125. {
  126. public:
  127. //--- Place code to initialize the CRT debug code here.
  128. DebugAutoInitializer()
  129. {
  130. // Turn own automatic leak checking.
  131. int f = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) ;
  132. f |= _CRTDBG_LEAK_CHECK_DF ;
  133. _CrtSetDbgFlag(f) ;
  134. //--- LineNumber to break on... (found in hhdebug.ini file)
  135. long BreakNumber = GetPrivateProfileInt( "CRT", "_CrtSetBreakAlloc", 0, "hhdebug.ini" );
  136. if (BreakNumber)
  137. {
  138. _CrtSetBreakAlloc(BreakNumber) ;
  139. }
  140. }
  141. };
  142. DebugAutoInitializer s_DebugAutoInitializer;
  143. #endif // _DEBUG
  144. #endif // HHCTRL