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.

170 lines
4.0 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. utilmem.cxx
  5. Abstract:
  6. Miscellaneous useful utilities
  7. Author:
  8. Sergey Solyanik (SergeyS)
  9. --*/
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include <svsutil.hxx>
  13. //
  14. // Allocation sector
  15. //
  16. FuncAlloc g_funcAlloc = svsutil_Alloc;
  17. FuncFree g_funcFree = svsutil_Free;
  18. void *g_pvAllocData = NULL;
  19. void *g_pvFreeData = NULL;
  20. #if (defined (_WINDOWS_) || defined (_WINDOWS_CE_)) && defined (SVSUTIL_DEBUG_HEAP)
  21. class MemAllocChunk
  22. {
  23. public:
  24. unsigned int uiSignature1;
  25. unsigned int uiSignature2;
  26. unsigned int uiSize;
  27. unsigned int uiCalledFrom;
  28. MemAllocChunk *pPrev;
  29. MemAllocChunk *pNext;
  30. unsigned char data[8];
  31. };
  32. static CRITICAL_SECTION csGlobalAlloc;
  33. static unsigned int uiMemAllocated = 0;
  34. static MemAllocChunk *pAllocBlocks = NULL;
  35. #endif
  36. void svsutil_Initialize (void) {
  37. #if (defined (_WINDOWS_) || defined (_WINDOWS_CE_)) && defined (SVSUTIL_DEBUG_HEAP)
  38. InitializeCriticalSection (&csGlobalAlloc);
  39. #endif
  40. }
  41. unsigned int svsutil_TotalAlloc (void) {
  42. #if (defined (_WINDOWS_) || defined (_WINDOWS_CE_)) && defined (SVSUTIL_DEBUG_HEAP)
  43. return uiMemAllocated;
  44. #else
  45. return 0;
  46. #endif
  47. }
  48. void *svsutil_Alloc (int iSize, void *pvData) {
  49. SVSUTIL_ASSERT (iSize > 0);
  50. SVSUTIL_ASSERT (pvData == g_pvAllocData);
  51. #if (defined (_WINDOWS_) || defined (_WINDOWS_CE_)) && defined (SVSUTIL_DEBUG_HEAP)
  52. int iTotalSize = iSize + offsetof (MemAllocChunk, data);
  53. MemAllocChunk *pAlloc = (MemAllocChunk *)malloc(iTotalSize); //(unsigned int *)LocalAlloc (LMEM_FIXED, iSize + 8);
  54. if (! pAlloc)
  55. {
  56. SVSUTIL_ASSERT (0);
  57. return NULL;
  58. }
  59. unsigned int __RetAddr = 0;
  60. #if defined (_X86_)
  61. __asm {
  62. mov eax, [ebp+4]
  63. mov __RetAddr, eax
  64. };
  65. #endif
  66. pAlloc->uiSignature1 = 'SVSA';
  67. pAlloc->uiSignature2 = 'LLOC';
  68. pAlloc->uiSize = (unsigned int)iSize;
  69. pAlloc->uiCalledFrom = __RetAddr;
  70. pAlloc->pPrev = NULL;
  71. EnterCriticalSection (&csGlobalAlloc);
  72. if (pAllocBlocks)
  73. pAllocBlocks->pPrev = pAlloc;
  74. pAlloc->pNext = pAllocBlocks;
  75. pAllocBlocks = pAlloc;
  76. uiMemAllocated += iSize;
  77. LeaveCriticalSection (&csGlobalAlloc);
  78. return(void *)pAlloc->data;
  79. #else
  80. return malloc (iSize); // LocalAlloc (LMEM_FIXED, iSize);
  81. #endif
  82. }
  83. void svsutil_Free (void *pvPtr, void *pvData) {
  84. SVSUTIL_ASSERT (pvPtr);
  85. SVSUTIL_ASSERT (pvData == g_pvFreeData);
  86. #if (defined (_WINDOWS_) || defined (_WINDOWS_CE_)) && defined (SVSUTIL_DEBUG_HEAP)
  87. MemAllocChunk *pAlloc = (MemAllocChunk *)((unsigned char *)pvPtr - offsetof(MemAllocChunk, data));
  88. SVSUTIL_ASSERT (pAlloc->uiSignature1 == 'SVSA');
  89. SVSUTIL_ASSERT (pAlloc->uiSignature2 == 'LLOC');
  90. EnterCriticalSection (&csGlobalAlloc);
  91. if (! pAlloc->pPrev)
  92. {
  93. SVSUTIL_ASSERT (pAlloc == pAllocBlocks);
  94. pAllocBlocks = pAlloc->pNext;
  95. if (pAllocBlocks)
  96. pAllocBlocks->pPrev = NULL;
  97. }
  98. else
  99. {
  100. SVSUTIL_ASSERT (pAlloc != pAllocBlocks);
  101. pAlloc->pPrev->pNext = pAlloc->pNext;
  102. if (pAlloc->pNext)
  103. pAlloc->pNext->pPrev = pAlloc->pPrev;
  104. }
  105. unsigned int __RetAddr = 0;
  106. #if defined (_X86_)
  107. __asm {
  108. mov eax, [ebp+4]
  109. mov __RetAddr, eax
  110. };
  111. #endif
  112. uiMemAllocated -= pAlloc->uiSize;
  113. LeaveCriticalSection (&csGlobalAlloc);
  114. pAlloc->pNext = NULL;
  115. pAlloc->pPrev = NULL;
  116. pAlloc->uiSignature1 = 0;
  117. pAlloc->uiSignature2 = 0;
  118. pAlloc->uiCalledFrom = __RetAddr;
  119. pvPtr = (void *)pAlloc;
  120. #endif
  121. // HLOCAL hres = LocalFree (pvPtr);
  122. // SVSUTIL_ASSERT (! hres);
  123. free (pvPtr);
  124. }
  125. void svsutil_SetAlloc (FuncAlloc a_funcAlloc, FuncFree a_funcFree) {
  126. g_funcAlloc = a_funcAlloc ? a_funcAlloc : svsutil_Alloc;
  127. g_funcFree = a_funcFree ? a_funcFree : svsutil_Free;
  128. }
  129. void svsutil_SetAllocData (void *a_pvAllocData, void *a_pvFreeData) {
  130. g_pvAllocData = a_pvAllocData;
  131. g_pvFreeData = a_pvFreeData;
  132. }