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.

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