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.

365 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. util.cxx
  5. Abstract:
  6. Contains the class implementation of UTILITY classes.
  7. Author:
  8. Madan Appiah (madana) 16-Nov-1994
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. MuraliK 20-March-1996 modularize within svcloc
  13. --*/
  14. #include <svcloc.hxx>
  15. MEMORY * CacheHeap = NULL;
  16. PVOID
  17. MEMORY::Alloc(
  18. DWORD Size
  19. )
  20. /*++
  21. Routine Description:
  22. This member function allocates memory from the local heap. In debug
  23. build this function also keeps track of the memory allocations and
  24. fills up the memory with a known pattern that helps to debug memory
  25. corruptions.
  26. Arguments:
  27. Size : size of the memory block in bytes requested.
  28. Return Value:
  29. Pointer to the alloted memory block. If there is no memory
  30. available, this pointer will be set to NULL.
  31. --*/
  32. {
  33. PVOID MemoryPtr;
  34. MemoryPtr = LocalAlloc(
  35. LMEM_FIXED | LMEM_ZEROINIT,
  36. Size );
  37. #if DBG
  38. TcpsvcsDbgAssert( MemoryPtr != NULL );
  39. _Count++;
  40. _TotalSize += Size;
  41. memset( MemoryPtr, 0xAA, Size );
  42. TcpsvcsDbgPrint(( DEBUG_MEM_ALLOC,
  43. "MEMORY::Alloc, _Count = %ld, _TotalSize = %ld.\n",
  44. _Count, _TotalSize ));
  45. #endif // DBG
  46. return( MemoryPtr );
  47. }
  48. PVOID
  49. MEMORY::ReAlloc(
  50. PVOID OldMemory,
  51. DWORD NewSize
  52. )
  53. /*++
  54. Routine Description:
  55. This member function reallocates memory from the local heap. In
  56. debug build this function also keeps track of the memory allocations and
  57. fills up the memory with a known pattern that helps to debug memory
  58. corruptions.
  59. Arguments:
  60. OldMemory - pointer to the old memory block.
  61. NewSize : size of the new memory block in bytes requested.
  62. Return Value:
  63. Pointer to the alloted memory block. If there is no memory
  64. available, this pointer will be set to NULL.
  65. --*/
  66. {
  67. PVOID NewMemory;
  68. DWORD OldSize;
  69. //
  70. // if there is no previous memory, then alloc a new one.
  71. //
  72. if( OldMemory == NULL ) {
  73. return( Alloc( NewSize ) );
  74. }
  75. //
  76. // get old memory size.
  77. //
  78. OldSize = (DWORD)LocalSize( OldMemory );
  79. TcpsvcsDbgAssert( OldSize != NewSize );
  80. //
  81. // if new size is same as old size, simply return.
  82. //
  83. if( OldSize == NewSize ){
  84. return( OldMemory );
  85. }
  86. //
  87. // if the newsize is zero, free old memory and return zero.
  88. //
  89. if( NewSize == 0 ) {
  90. Free( OldMemory );
  91. return( NULL );
  92. }
  93. //
  94. // realloc.
  95. //
  96. NewMemory = LocalReAlloc(
  97. OldMemory,
  98. NewSize,
  99. LMEM_FIXED | LMEM_ZEROINIT | LMEM_MOVEABLE );
  100. // block move is OK.
  101. #if DBG
  102. TcpsvcsDbgAssert( NewMemory != NULL );
  103. //
  104. // update total memory consumed size and fillin the extra memory
  105. // with pattern.
  106. //
  107. if( NewSize > OldSize ) {
  108. DWORD ModSize = (NewSize - OldSize);
  109. _TotalSize += ModSize;
  110. memset( (LPBYTE)NewMemory + OldSize, 0xAA, ModSize );
  111. }
  112. else {
  113. _TotalSize -= (OldSize - NewSize);
  114. }
  115. TcpsvcsDbgPrint(( DEBUG_MEM_ALLOC,
  116. "MEMORY::Alloc, _Count = %ld, _TotalSize = %ld.\n",
  117. _Count, _TotalSize ));
  118. #endif // DBG
  119. return( NewMemory );
  120. }
  121. VOID
  122. MEMORY::Free(
  123. PVOID MemoryPtr
  124. )
  125. /*++
  126. Routine Description:
  127. This member function frees the memory block that was allocated by
  128. the meber function this->Alloc(). In debug build this function also
  129. keeps track of the memory allocations.
  130. Arguments:
  131. MemoryPtr : Pointer to a memory block that is freed.
  132. Return Value:
  133. None.
  134. --*/
  135. {
  136. PVOID ReturnHandle;
  137. DWORD Size;
  138. if( MemoryPtr == NULL ) {
  139. #if DBG
  140. TcpsvcsDbgPrint(( DEBUG_MISC,
  141. "MEMORY::Free called with NULL.\n" ));
  142. #endif // DBG
  143. return;
  144. }
  145. #if DBG
  146. Size = (DWORD)LocalSize( MemoryPtr );
  147. #endif // DBG
  148. ReturnHandle = LocalFree( MemoryPtr );
  149. #if DBG
  150. TcpsvcsDbgAssert( ReturnHandle == NULL );
  151. _Count--;
  152. _TotalSize -= Size;
  153. TcpsvcsDbgPrint(( DEBUG_MEM_ALLOC,
  154. "MEMORY::Free, _Count = %ld, _TotalSize = %ld.\n",
  155. _Count, _TotalSize ));
  156. #endif // DBG
  157. return;
  158. }
  159. #if 0
  160. void *
  161. operator new(
  162. size_t Size
  163. )
  164. /*++
  165. Routine Description:
  166. Private implemetation of the new operator.
  167. Arguments:
  168. Size : size of the memory requested.
  169. Return Value:
  170. Pointer to the alloted memory block.
  171. --*/
  172. {
  173. if( CacheHeap == NULL ) {
  174. PVOID MemoryPtr;
  175. MemoryPtr = LocalAlloc(
  176. LMEM_FIXED | LMEM_ZEROINIT,
  177. Size );
  178. return( MemoryPtr );
  179. }
  180. else {
  181. return( CacheHeap->Alloc( DWORD(Size) ) );
  182. }
  183. }
  184. void
  185. operator delete(
  186. void *MemoryPtr
  187. )
  188. /*++
  189. Routine Description:
  190. Private implemetation of the free operator.
  191. Arguments:
  192. MemoryPtr : Pointer to a memory block that is freed
  193. Return Value:
  194. None.
  195. --*/
  196. {
  197. if( CacheHeap == NULL ) {
  198. LocalFree( MemoryPtr );
  199. }
  200. else {
  201. CacheHeap->Free( MemoryPtr );
  202. }
  203. return;
  204. }
  205. #endif // 0
  206. PVOID
  207. MIDL_user_allocate(
  208. size_t Size
  209. )
  210. /*++
  211. Routine Description:
  212. MIDL memory allocation.
  213. Arguments:
  214. Size : Memory size requested.
  215. Return Value:
  216. Pointer to the allocated memory block.
  217. --*/
  218. {
  219. if( CacheHeap == NULL ) {
  220. return LocalAlloc( LMEM_FIXED,
  221. Size );
  222. }
  223. else {
  224. return( CacheHeap->Alloc( DWORD(Size) ) );
  225. }
  226. }
  227. VOID
  228. MIDL_user_free(
  229. PVOID MemoryPtr
  230. )
  231. /*++
  232. Routine Description:
  233. MIDL memory allocation.
  234. Arguments:
  235. MmeoryPtr : Pointer to a memory block that is freed.
  236. Return Value:
  237. None.
  238. --*/
  239. {
  240. if( CacheHeap == NULL ) {
  241. LocalFree( MemoryPtr );
  242. }
  243. else {
  244. CacheHeap->Free( MemoryPtr );
  245. }
  246. return;
  247. }
  248.