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.

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