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.9 KiB

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