Leaked source code of windows server 2003
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.

180 lines
3.4 KiB

  1. /* Copyright (c) 1999 Microsoft Corporation */
  2. #include "mymem.h"
  3. #include "mylog.h"
  4. extern PPHONESP_MEMINFO gpMemFirst, gpMemLast;
  5. extern CRITICAL_SECTION csMemoryList;
  6. extern BOOL gbBreakOnLeak;
  7. extern HANDLE ghHeap;
  8. #if DBG
  9. LPVOID
  10. WINAPI
  11. MemAllocReal(
  12. DWORD dwSize,
  13. DWORD dwLine,
  14. PSTR pszFile
  15. )
  16. {
  17. //
  18. // Alloc 16 extra bytes so we can make sure the pointer we pass back
  19. // is 64-bit aligned & have space to store the original pointer
  20. //
  21. PPHONESP_MEMINFO pHold;
  22. PDWORD_PTR pAligned;
  23. PBYTE p;
  24. p = (LPBYTE)HeapAlloc(ghHeap, HEAP_ZERO_MEMORY, dwSize + sizeof(PHONESP_MEMINFO) + 16);
  25. if (p == NULL)
  26. {
  27. return NULL;
  28. }
  29. // note note note - this only works because mymeminfo is
  30. // a 16 bit multiple in size. if it wasn't, this
  31. // align stuff would cause problems.
  32. pAligned = (PDWORD_PTR) (p + 8 - (((DWORD_PTR) p) & (DWORD_PTR)0x7));
  33. *pAligned = (DWORD_PTR) p;
  34. pHold = (PPHONESP_MEMINFO)((DWORD_PTR)pAligned + 8);
  35. pHold->dwSize = dwSize;
  36. pHold->dwLine = dwLine;
  37. pHold->pszFile = pszFile;
  38. EnterCriticalSection(&csMemoryList);
  39. if (gpMemLast != NULL)
  40. {
  41. gpMemLast->pNext = pHold;
  42. pHold->pPrev = gpMemLast;
  43. gpMemLast = pHold;
  44. }
  45. else
  46. {
  47. gpMemFirst = gpMemLast = pHold;
  48. }
  49. LeaveCriticalSection(&csMemoryList);
  50. return (LPVOID)(pHold + 1);
  51. }
  52. #else
  53. LPVOID
  54. WINAPI
  55. MemAllocReal(
  56. DWORD dwSize
  57. )
  58. {
  59. PDWORD_PTR pAligned;
  60. PBYTE p;
  61. if (p = (LPBYTE)HeapAlloc(ghHeap, HEAP_ZERO_MEMORY, dwSize + 16))
  62. {
  63. pAligned = (PDWORD_PTR) (p + 8 - (((DWORD_PTR) p) & (DWORD_PTR)0x7));
  64. *pAligned = (DWORD_PTR) p;
  65. pAligned = (PDWORD_PTR)((DWORD_PTR)pAligned + 8);
  66. }
  67. else
  68. {
  69. pAligned = NULL;
  70. }
  71. return ((LPVOID) pAligned);
  72. }
  73. #endif
  74. VOID
  75. WINAPI
  76. MemFree(
  77. LPVOID p
  78. )
  79. {
  80. LPVOID pOrig;
  81. if (p == NULL)
  82. {
  83. return;
  84. }
  85. #if DBG
  86. {
  87. PPHONESP_MEMINFO pHold;
  88. pHold = (PPHONESP_MEMINFO)(((LPBYTE)p) - sizeof(PHONESP_MEMINFO));
  89. EnterCriticalSection(&csMemoryList);
  90. if (pHold->pPrev)
  91. {
  92. pHold->pPrev->pNext = pHold->pNext;
  93. }
  94. else
  95. {
  96. gpMemFirst = pHold->pNext;
  97. }
  98. if (pHold->pNext)
  99. {
  100. pHold->pNext->pPrev = pHold->pPrev;
  101. }
  102. else
  103. {
  104. gpMemLast = pHold->pPrev;
  105. }
  106. LeaveCriticalSection(&csMemoryList);
  107. pOrig = (LPVOID) *((PDWORD_PTR)((DWORD_PTR)pHold - 8));
  108. }
  109. #else
  110. pOrig = (LPVOID) *((PDWORD_PTR)((DWORD_PTR)p - 8));
  111. #endif
  112. HeapFree(ghHeap,0, pOrig);
  113. return;
  114. }
  115. #if DBG
  116. void
  117. DumpMemoryList()
  118. {
  119. PPHONESP_MEMINFO pHold;
  120. if (gpMemFirst == NULL)
  121. {
  122. LOG((PHONESP_TRACE, "DumpMemoryList: All memory deallocated"));
  123. return;
  124. }
  125. pHold = gpMemFirst;
  126. while (pHold)
  127. {
  128. LOG((PHONESP_ERROR, "DumpMemoryList: %lx not freed - LINE %d FILE %s!", pHold+1, pHold->dwLine, pHold->pszFile));
  129. pHold = pHold->pNext;
  130. }
  131. if (gbBreakOnLeak)
  132. {
  133. DebugBreak();
  134. }
  135. }
  136. #endif