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.

282 lines
3.9 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. memory.c
  5. Abstract:
  6. This module provides all the memory management functions for all spooler
  7. components
  8. Author:
  9. Krishna Ganugapati (KrishnaG) 03-Feb-1994
  10. Revision History:
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <imagehlp.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. //#include "oledsdbg.h"
  20. #define SPDAssert(x) NULL
  21. #define WORD_ALIGN_DOWN(addr) ((LPBYTE)((DWORD)addr &= ~1))
  22. #define DWORD_ALIGN_UP(size) ((size+3)&~3)
  23. #if DBG
  24. DWORD dwMemoryLog = 0;
  25. #define MAXDEPTH 10
  26. typedef struct _SPDMEMTAG {
  27. DWORD Tag ;
  28. DWORD Size ;
  29. PVOID pvBackTrace[MAXDEPTH+1];
  30. LPSTR pszSymbol[MAXDEPTH+1];
  31. DWORD uDepth;
  32. LIST_ENTRY List ;
  33. } SPDMEMTAG, *PSPDMEMTAG ;
  34. LIST_ENTRY SPDMemList ;
  35. DWORD SPDMemCount ;
  36. CRITICAL_SECTION SPDMemCritSect ;
  37. /*++
  38. Routine Description:
  39. This function initializes the SPD mem tracking code. Must be call
  40. during DLL load an ONLY during DLL load.
  41. Arguments:
  42. None
  43. Return Value:
  44. None.
  45. --*/
  46. VOID InitSPDMem(
  47. VOID
  48. )
  49. {
  50. InitializeCriticalSection(&SPDMemCritSect) ;
  51. InitializeListHead(&SPDMemList) ;
  52. SPDMemCount = 0 ;
  53. }
  54. /*++
  55. Routine Description:
  56. This function asserts that the mem list is empty on exit.
  57. Arguments:
  58. None
  59. Return Value:
  60. None.
  61. --*/
  62. VOID AssertSPDMemLeaks(
  63. VOID
  64. )
  65. {
  66. SPDAssert(IsListEmpty(&SPDMemList)) ;
  67. }
  68. #endif
  69. LPVOID
  70. AllocSPDMem(
  71. DWORD cb
  72. )
  73. /*++
  74. Routine Description:
  75. This function will allocate local memory. It will possibly allocate extra
  76. memory and fill this with debugging information for the debugging version.
  77. Arguments:
  78. cb - The amount of memory to allocate
  79. Return Value:
  80. NON-NULL - A pointer to the allocated memory
  81. FALSE/NULL - The operation failed. Extended error status is available
  82. using GetLastError.
  83. --*/
  84. {
  85. return(LocalAlloc(LPTR, cb));
  86. }
  87. BOOL
  88. FreeSPDMem(
  89. LPVOID pMem
  90. )
  91. {
  92. return(LocalFree(pMem) == NULL);
  93. }
  94. LPVOID
  95. ReallocSPDMem(
  96. LPVOID pOldMem,
  97. DWORD cbOld,
  98. DWORD cbNew
  99. )
  100. {
  101. LPVOID pNewMem;
  102. pNewMem=AllocSPDMem(cbNew);
  103. if (pOldMem && pNewMem) {
  104. memcpy(pNewMem, pOldMem, min(cbNew, cbOld));
  105. FreeSPDMem(pOldMem);
  106. }
  107. return pNewMem;
  108. }
  109. LPWSTR
  110. AllocSPDStr(
  111. LPWSTR pStr
  112. )
  113. /*++
  114. Routine Description:
  115. This function will allocate enough local memory to store the specified
  116. string, and copy that string to the allocated memory
  117. Arguments:
  118. pStr - Pointer to the string that needs to be allocated and stored
  119. Return Value:
  120. NON-NULL - A pointer to the allocated memory containing the string
  121. FALSE/NULL - The operation failed. Extended error status is available
  122. using GetLastError.
  123. --*/
  124. {
  125. LPWSTR pMem;
  126. if (!pStr)
  127. return 0;
  128. if (pMem = (LPWSTR)AllocSPDMem( wcslen(pStr)*sizeof(WCHAR) + sizeof(WCHAR) ))
  129. wcscpy(pMem, pStr);
  130. return pMem;
  131. }
  132. BOOL
  133. FreeSPDStr(
  134. LPWSTR pStr
  135. )
  136. {
  137. return pStr ? FreeSPDMem(pStr)
  138. : FALSE;
  139. }
  140. BOOL
  141. ReallocSPDStr(
  142. LPWSTR *ppStr,
  143. LPWSTR pStr
  144. )
  145. {
  146. FreeSPDStr(*ppStr);
  147. *ppStr=AllocSPDStr(pStr);
  148. return TRUE;
  149. }
  150. DWORD
  151. AllocateSPDMemory(
  152. DWORD cb,
  153. LPVOID * ppMem
  154. )
  155. {
  156. DWORD dwError = 0;
  157. LPBYTE pMem = NULL;
  158. pMem = AllocSPDMem(cb);
  159. if (!pMem) {
  160. dwError = GetLastError();
  161. }
  162. *ppMem = pMem;
  163. return(dwError);
  164. }
  165. void
  166. FreeSPDMemory(
  167. LPVOID pMem
  168. )
  169. {
  170. if (pMem) {
  171. FreeSPDMem(pMem);
  172. }
  173. return;
  174. }
  175. DWORD
  176. AllocateSPDString(
  177. LPWSTR pszString,
  178. LPWSTR * ppszNewString
  179. )
  180. {
  181. LPWSTR pszNewString = NULL;
  182. DWORD dwError = 0;
  183. pszNewString = AllocSPDStr(pszString);
  184. if (!pszNewString) {
  185. dwError = GetLastError();
  186. }
  187. *ppszNewString = pszNewString;
  188. return(dwError);
  189. }
  190. void
  191. FreeSPDString(
  192. LPWSTR pszString
  193. )
  194. {
  195. if (pszString) {
  196. FreeSPDStr(pszString);
  197. }
  198. return;
  199. }
  200.