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.

236 lines
5.2 KiB

  1. //--------------------------------------------------------------------
  2. // Copyright (C)1998 Microsoft Corporation, All Rights Reserved.
  3. //
  4. // memory.cpp
  5. //
  6. // Simple memory allocation routines. We use our own private heap
  7. // so we won't (have less chances of) interfering with any other
  8. // service code.
  9. //
  10. // Author:
  11. //
  12. // Edward Reus (EdwardR) 03-04-98 Initial coding.
  13. //
  14. // Edward Reus (EdwardR) 06-08-98 Convert to use private heap.
  15. //
  16. //--------------------------------------------------------------------
  17. #include "precomp.h"
  18. #pragma warning (disable:4200)
  19. typedef struct _PDU_MEMORY
  20. {
  21. LIST_ENTRY Link;
  22. DWORD dwPduSize;
  23. UCHAR Pdu[];
  24. } PDU_MEMORY;
  25. #pragma warning (default:4200)
  26. static HANDLE g_hHeap = 0; // Can't use INVALID_HANDLE_VALUE.
  27. static LIST_ENTRY g_FreePduList;
  28. static BOOL g_fListInitialized = FALSE;
  29. #ifdef DBG_MEM
  30. static LONG g_lPduCount = 0;
  31. #endif
  32. //--------------------------------------------------------------------
  33. // InitializeMemory()
  34. //
  35. //--------------------------------------------------------------------
  36. DWORD InitializeMemory()
  37. {
  38. DWORD dwStatus = NO_ERROR;
  39. #define INITIAL_HEAP_PAGES 64
  40. if (!g_hHeap)
  41. {
  42. SYSTEM_INFO SystemInfo;
  43. GetSystemInfo(&SystemInfo);
  44. DWORD dwFlags = 0;
  45. DWORD dwInitialSize = INITIAL_HEAP_PAGES * SystemInfo.dwPageSize;
  46. DWORD dwMaxSize = 0;
  47. g_hHeap = HeapCreate( dwFlags, dwInitialSize, dwMaxSize );
  48. if (!g_hHeap)
  49. {
  50. dwStatus = GetLastError();
  51. }
  52. }
  53. return dwStatus;
  54. }
  55. //--------------------------------------------------------------------
  56. // AllocateMemory()
  57. //
  58. //--------------------------------------------------------------------
  59. void *AllocateMemory( DWORD dwBytes )
  60. {
  61. DWORD dwStatus;
  62. void *pvMemory;
  63. if (!g_hHeap)
  64. {
  65. dwStatus = InitializeMemory();
  66. }
  67. if ((g_hHeap) && (dwBytes > 0))
  68. {
  69. #ifdef DBG_MEM_VALIDATE
  70. HeapValidate(g_hHeap,0,0);
  71. #endif
  72. pvMemory = HeapAlloc(g_hHeap,0,dwBytes);
  73. }
  74. else
  75. {
  76. pvMemory = 0;
  77. }
  78. return pvMemory;
  79. }
  80. //--------------------------------------------------------------------
  81. // FreeMemory()
  82. //
  83. //--------------------------------------------------------------------
  84. DWORD FreeMemory( void *pvMemory )
  85. {
  86. DWORD dwStatus = NO_ERROR;
  87. if (g_hHeap)
  88. {
  89. #ifdef DBG_MEM_VALIDATE
  90. HeapValidate(g_hHeap,0,0);
  91. #endif
  92. if (pvMemory)
  93. {
  94. if (!HeapFree(g_hHeap,0,pvMemory))
  95. {
  96. dwStatus = GetLastError();
  97. }
  98. }
  99. }
  100. else
  101. {
  102. #ifdef DBG_MEM
  103. DbgPrint("IrXfer.dll: IrTran-P: Free memory with NULL g_hHeap.\n");
  104. #endif
  105. }
  106. return dwStatus;
  107. }
  108. //--------------------------------------------------------------------
  109. // UninitializeMemory()
  110. //
  111. //--------------------------------------------------------------------
  112. DWORD UninitializeMemory()
  113. {
  114. DWORD dwStatus = NO_ERROR;
  115. #ifdef DBG_MEM_VALIDATE
  116. HeapValidate(g_hHeap,0,0);
  117. #endif
  118. if (g_hHeap)
  119. {
  120. if (!HeapDestroy(g_hHeap))
  121. {
  122. dwStatus = GetLastError();
  123. }
  124. }
  125. g_hHeap = 0;
  126. return dwStatus;
  127. }
  128. //--------------------------------------------------------------------
  129. // NewPdu()
  130. //
  131. //--------------------------------------------------------------------
  132. SCEP_HEADER *NewPdu( DWORD dwPduSize )
  133. {
  134. SCEP_HEADER *pPdu;
  135. PDU_MEMORY *pPduMemory;
  136. LIST_ENTRY *pLink;
  137. if (!g_fListInitialized)
  138. {
  139. InitializeListHead(&g_FreePduList);
  140. g_fListInitialized = TRUE;
  141. }
  142. if (dwPduSize == 0)
  143. {
  144. dwPduSize = MAX_PDU_SIZE;
  145. }
  146. if (IsListEmpty(&g_FreePduList))
  147. {
  148. pPduMemory
  149. = (PDU_MEMORY*)AllocateMemory( sizeof(PDU_MEMORY)+dwPduSize );
  150. if (pPduMemory)
  151. {
  152. pPduMemory->Link.Flink = 0;
  153. pPduMemory->Link.Blink = 0;
  154. pPduMemory->dwPduSize = dwPduSize;
  155. }
  156. }
  157. else
  158. {
  159. pLink = RemoveHeadList(&g_FreePduList);
  160. pPduMemory = CONTAINING_RECORD(pLink,PDU_MEMORY,Link);
  161. }
  162. if (pPduMemory)
  163. {
  164. pPdu = (SCEP_HEADER*)(pPduMemory->Pdu);
  165. }
  166. else
  167. {
  168. pPdu = 0;
  169. }
  170. #ifdef DBG_MEM
  171. if (pPdu)
  172. {
  173. InterlockedIncrement(&g_lPduCount);
  174. }
  175. DbgPrint("NewPdu(): Count: %d Bytes: %d Addr: 0x%x\n",
  176. g_lPduCount, dwPduSize, pPdu );
  177. #endif
  178. return pPdu;
  179. }
  180. //--------------------------------------------------------------------
  181. // DeletePdu()
  182. //
  183. //--------------------------------------------------------------------
  184. void DeletePdu( SCEP_HEADER *pPdu )
  185. {
  186. PDU_MEMORY *pPduMemory;
  187. if (pPdu)
  188. {
  189. pPduMemory = CONTAINING_RECORD(pPdu,PDU_MEMORY,Pdu);
  190. InsertTailList(&g_FreePduList,&pPduMemory->Link);
  191. #ifdef DBG_MEM
  192. InterlockedDecrement(&g_lPduCount);
  193. DbgPrint("DeletePdu(): Count: %d\n",g_lPduCount);
  194. #endif
  195. }
  196. }