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.

182 lines
4.0 KiB

  1. #include "ctlspriv.h"
  2. // Define some things for debug.h
  3. //
  4. #define SZ_DEBUGINI "ccshell.ini"
  5. #ifndef UNIX
  6. #define SZ_DEBUGSECTION "comctl32"
  7. #define SZ_MODULE "COMCTL32"
  8. #else
  9. #define SZ_DEBUGSECTION "comctrl"
  10. #define SZ_MODULE "COMCTRL"
  11. #endif
  12. #define DECLARE_DEBUG
  13. #include <debug.h>
  14. //========== Memory Management =============================================
  15. //----------------------------------------------------------------------------
  16. // Define a Global Shared Heap that we use allocate memory out of that we
  17. // Need to share between multiple instances.
  18. #ifndef WINNT
  19. HANDLE g_hSharedHeap = NULL;
  20. #define GROWABLE 0
  21. #define MAXHEAPSIZE GROWABLE
  22. #define HEAP_SHARED 0x04000000 /* put heap in shared memory */
  23. #endif
  24. void Mem_Terminate()
  25. {
  26. #ifndef WINNT
  27. // Assuming that everything else has exited
  28. //
  29. if (g_hSharedHeap != NULL)
  30. HeapDestroy(g_hSharedHeap);
  31. g_hSharedHeap = NULL;
  32. #endif
  33. }
  34. #ifndef WINNT
  35. HANDLE InitSharedHeap(void)
  36. {
  37. ENTERCRITICAL;
  38. if (g_hSharedHeap == NULL)
  39. {
  40. g_hSharedHeap = HeapCreate(HEAP_SHARED, 1, MAXHEAPSIZE);
  41. }
  42. LEAVECRITICAL;
  43. return g_hSharedHeap;
  44. }
  45. #endif
  46. void * WINAPI Alloc(long cb)
  47. {
  48. // I will assume that this is the only one that needs the checks to
  49. // see if the heap has been previously created or not
  50. #if defined(WINNT) || defined(MAINWIN)
  51. return (void *)LocalAlloc(LPTR, cb);
  52. #else
  53. HANDLE hHeap = GetSharedHeapHandle();
  54. // If still NULL we have problems!
  55. if (hHeap == NULL)
  56. return(NULL);
  57. return HeapAlloc(hHeap, HEAP_ZERO_MEMORY, cb);
  58. #endif
  59. }
  60. void * WINAPI ReAlloc(void * pb, long cb)
  61. {
  62. if (pb == NULL)
  63. return Alloc(cb);
  64. #if defined(WINNT) || defined(MAINWIN)
  65. return (void *)LocalReAlloc((HLOCAL)pb, cb, LMEM_ZEROINIT | LMEM_MOVEABLE);
  66. #else
  67. return HeapReAlloc(g_hSharedHeap, HEAP_ZERO_MEMORY, pb, cb);
  68. #endif
  69. }
  70. BOOL WINAPI Free(void * pb)
  71. {
  72. #if defined(WINNT) || defined(MAINWIN)
  73. return (LocalFree((HLOCAL)pb) == NULL);
  74. #else
  75. return HeapFree(g_hSharedHeap, 0, pb);
  76. #endif
  77. }
  78. DWORD_PTR WINAPI GetSize(void * pb)
  79. {
  80. #if defined(WINNT) || defined(MAINWIN)
  81. return LocalSize((HLOCAL)pb);
  82. #else
  83. return HeapSize(g_hSharedHeap, 0, pb);
  84. #endif
  85. }
  86. //----------------------------------------------------------------------------
  87. // The following functions are for debug only and are used to try to
  88. // calculate memory usage.
  89. //
  90. #ifdef DEBUG
  91. typedef struct _HEAPTRACE
  92. {
  93. DWORD cAlloc;
  94. DWORD cFailure;
  95. DWORD cReAlloc;
  96. ULONG_PTR cbMaxTotal;
  97. DWORD cCurAlloc;
  98. ULONG_PTR cbCurTotal;
  99. } HEAPTRACE;
  100. HEAPTRACE g_htShell = {0}; // Start of zero...
  101. LPVOID WINAPI ControlAlloc(HANDLE hheap, DWORD cb)
  102. {
  103. LPVOID lp = HeapAlloc(hheap, HEAP_ZERO_MEMORY, cb);;
  104. if (lp == NULL)
  105. {
  106. g_htShell.cFailure++;
  107. return NULL;
  108. }
  109. // Update counts.
  110. g_htShell.cAlloc++;
  111. g_htShell.cCurAlloc++;
  112. g_htShell.cbCurTotal += cb;
  113. if (g_htShell.cbCurTotal > g_htShell.cbMaxTotal)
  114. g_htShell.cbMaxTotal = g_htShell.cbCurTotal;
  115. return lp;
  116. }
  117. LPVOID WINAPI ControlReAlloc(HANDLE hheap, LPVOID pb, DWORD cb)
  118. {
  119. LPVOID lp;
  120. SIZE_T cbOld;
  121. cbOld = HeapSize(hheap, 0, pb);
  122. lp = HeapReAlloc(hheap, HEAP_ZERO_MEMORY, pb,cb);
  123. if (lp == NULL)
  124. {
  125. g_htShell.cFailure++;
  126. return NULL;
  127. }
  128. // Update counts.
  129. g_htShell.cReAlloc++;
  130. g_htShell.cbCurTotal += cb - cbOld;
  131. if (g_htShell.cbCurTotal > g_htShell.cbMaxTotal)
  132. g_htShell.cbMaxTotal = g_htShell.cbCurTotal;
  133. return lp;
  134. }
  135. BOOL WINAPI ControlFree(HANDLE hheap, LPVOID pb)
  136. {
  137. SIZE_T cbOld = HeapSize(hheap, 0, pb);
  138. BOOL fRet = HeapFree(hheap, 0, pb);
  139. if (fRet)
  140. {
  141. // Update counts.
  142. g_htShell.cCurAlloc--;
  143. g_htShell.cbCurTotal -= cbOld;
  144. }
  145. return(fRet);
  146. }
  147. SIZE_T WINAPI ControlSize(HANDLE hheap, LPVOID pb)
  148. {
  149. return (DWORD) HeapSize(hheap, 0, pb);
  150. }
  151. #endif // DEBUG
  152. #if defined(FULL_DEBUG) && defined(WIN32)
  153. #include "../inc/deballoc.c"
  154. #endif // defined(FULL_DEBUG) && defined(WIN32)