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.

289 lines
4.0 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. LPVOID pMem = NULL;
  86. pMem = LocalAlloc(LPTR, cb);
  87. if (pMem) {
  88. memset((LPBYTE) pMem, 0, cb);
  89. }
  90. return (pMem);
  91. }
  92. BOOL
  93. FreeSPDMem(
  94. LPVOID pMem
  95. )
  96. {
  97. return(LocalFree(pMem) == NULL);
  98. }
  99. LPVOID
  100. ReallocSPDMem(
  101. LPVOID pOldMem,
  102. DWORD cbOld,
  103. DWORD cbNew
  104. )
  105. {
  106. LPVOID pNewMem;
  107. pNewMem=AllocSPDMem(cbNew);
  108. if (pOldMem && pNewMem) {
  109. memcpy(pNewMem, pOldMem, min(cbNew, cbOld));
  110. FreeSPDMem(pOldMem);
  111. }
  112. return pNewMem;
  113. }
  114. LPWSTR
  115. AllocSPDStr(
  116. LPWSTR pStr
  117. )
  118. /*++
  119. Routine Description:
  120. This function will allocate enough local memory to store the specified
  121. string, and copy that string to the allocated memory
  122. Arguments:
  123. pStr - Pointer to the string that needs to be allocated and stored
  124. Return Value:
  125. NON-NULL - A pointer to the allocated memory containing the string
  126. FALSE/NULL - The operation failed. Extended error status is available
  127. using GetLastError.
  128. --*/
  129. {
  130. LPWSTR pMem;
  131. if (!pStr)
  132. return 0;
  133. if (pMem = (LPWSTR)AllocSPDMem( wcslen(pStr)*sizeof(WCHAR) + sizeof(WCHAR) ))
  134. wcscpy(pMem, pStr);
  135. return pMem;
  136. }
  137. BOOL
  138. FreeSPDStr(
  139. LPWSTR pStr
  140. )
  141. {
  142. return pStr ? FreeSPDMem(pStr)
  143. : FALSE;
  144. }
  145. BOOL
  146. ReallocSPDStr(
  147. LPWSTR *ppStr,
  148. LPWSTR pStr
  149. )
  150. {
  151. FreeSPDStr(*ppStr);
  152. *ppStr=AllocSPDStr(pStr);
  153. return TRUE;
  154. }
  155. DWORD
  156. AllocateSPDMemory(
  157. DWORD cb,
  158. LPVOID * ppMem
  159. )
  160. {
  161. DWORD dwError = 0;
  162. LPBYTE pMem = NULL;
  163. pMem = AllocSPDMem(cb);
  164. if (!pMem) {
  165. dwError = GetLastError();
  166. }
  167. *ppMem = pMem;
  168. return(dwError);
  169. }
  170. void
  171. FreeSPDMemory(
  172. LPVOID pMem
  173. )
  174. {
  175. if (pMem) {
  176. FreeSPDMem(pMem);
  177. }
  178. return;
  179. }
  180. DWORD
  181. AllocateSPDString(
  182. LPWSTR pszString,
  183. LPWSTR * ppszNewString
  184. )
  185. {
  186. LPWSTR pszNewString = NULL;
  187. DWORD dwError = 0;
  188. pszNewString = AllocSPDStr(pszString);
  189. if (!pszNewString) {
  190. dwError = GetLastError();
  191. }
  192. *ppszNewString = pszNewString;
  193. return(dwError);
  194. }
  195. void
  196. FreeSPDString(
  197. LPWSTR pszString
  198. )
  199. {
  200. if (pszString) {
  201. FreeSPDStr(pszString);
  202. }
  203. return;
  204. }