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.

382 lines
10 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: Pools.cpp
  6. * Content: Pool utility functions
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 01/20/2000 jtk Derived from Utils.h
  13. ***************************************************************************/
  14. #include "dnwsocki.h"
  15. //**********************************************************************
  16. // Constant definitions
  17. //**********************************************************************
  18. //**********************************************************************
  19. // Macro definitions
  20. //**********************************************************************
  21. //**********************************************************************
  22. // Structure definitions
  23. //**********************************************************************
  24. //**********************************************************************
  25. // Variable definitions
  26. //**********************************************************************
  27. #ifndef DPNBUILD_ONLYONETHREAD
  28. static DNCRITICAL_SECTION g_AddressLock;
  29. #endif // !DPNBUILD_ONLYONETHREAD
  30. // Pools
  31. #ifndef DPNBUILD_ONLYONEADAPTER
  32. CFixedPool g_AdapterEntryPool;
  33. #endif // ! DPNBUILD_ONLYONEADAPTER
  34. CFixedPool g_CommandDataPool;
  35. CFixedPool g_SocketAddressPool;
  36. CFixedPool g_EndpointPool;
  37. CFixedPool g_EndpointCommandParametersPool;
  38. CFixedPool g_SocketPortPool;
  39. CFixedPool g_ThreadPoolPool;
  40. CFixedPool g_ReadIODataPool;
  41. CFixedPool g_TimerEntryPool;
  42. CFixedPool g_SocketDataPool;
  43. #ifndef DPNBUILD_ONLYONETHREAD
  44. CFixedPool g_BlockingJobPool;
  45. #endif // ! DPNBUILD_ONLYONETHREAD
  46. #ifndef DPNBUILD_ONLYONEADAPTER
  47. #define ADAPTERENTRY_POOL_INITED 0x00000001
  48. #endif // ! DPNBUILD_ONLYONEADAPTER
  49. #define COMMANDDATA_POOL_INITED 0x00000002
  50. #define ADDRESS_LOCK_INITED 0x00000004
  51. #define SOCKETADDRESS_POOL_INITED 0x00000008
  52. #define ENDPOINT_POOL_INITED 0x00000010
  53. #define EPCMDPARAM_POOL_INITED 0x00000020
  54. #define SOCKETPORT_POOL_INITED 0x00000040
  55. #define THREADPOOL_POOL_INITED 0x00000080
  56. #define READ_POOL_INITED 0x00000100
  57. #define TIMERENTRY_POOL_INITED 0x00000200
  58. #define SOCKETDATA_POOL_INITED 0x00000400
  59. #ifndef DPNBUILD_ONLYONETHREAD
  60. #define BLOCKINGJOB_POOL_INITED 0x00000800
  61. #endif // ! DPNBUILD_ONLYONETHREAD
  62. DWORD g_dwWsockInitFlags = 0;
  63. //**********************************************************************
  64. // Function prototypes
  65. //**********************************************************************
  66. //**********************************************************************
  67. // Function definitions
  68. //**********************************************************************
  69. //**********************************************************************
  70. // ------------------------------
  71. // InitializePools - initialize pools
  72. //
  73. // Entry: Nothing
  74. //
  75. // Exit: Boolean indicating success
  76. // TRUE = success
  77. // FALSE = failure
  78. // ------------------------------
  79. #undef DPF_MODNAME
  80. #define DPF_MODNAME "InitializePools"
  81. BOOL InitializePools( void )
  82. {
  83. #ifndef DPNBUILD_ONLYONEADAPTER
  84. //
  85. // AdapterEntry object pool
  86. //
  87. if (!g_AdapterEntryPool.Initialize(sizeof(CAdapterEntry),
  88. CAdapterEntry::PoolAllocFunction,
  89. CAdapterEntry::PoolInitFunction,
  90. CAdapterEntry::PoolReleaseFunction,
  91. CAdapterEntry::PoolDeallocFunction))
  92. {
  93. goto Failure;
  94. }
  95. g_dwWsockInitFlags |= ADAPTERENTRY_POOL_INITED;
  96. #endif // ! DPNBUILD_ONLYONEADAPTER
  97. //
  98. // command data pool
  99. //
  100. if (!g_CommandDataPool.Initialize(sizeof(CCommandData),
  101. CCommandData::PoolAllocFunction,
  102. CCommandData::PoolInitFunction,
  103. CCommandData::PoolReleaseFunction,
  104. CCommandData::PoolDeallocFunction))
  105. {
  106. goto Failure;
  107. }
  108. g_dwWsockInitFlags |= COMMANDDATA_POOL_INITED;
  109. //
  110. // initialize lock for address and endpoint pools
  111. //
  112. if ( DNInitializeCriticalSection( &g_AddressLock ) == FALSE )
  113. {
  114. goto Failure;
  115. }
  116. DebugSetCriticalSectionRecursionCount( &g_AddressLock, 0 );
  117. DebugSetCriticalSectionGroup( &g_AddressLock, &g_blDPNWSockCritSecsHeld ); // separate dpnwsock CSes from the rest of DPlay's CSes
  118. g_dwWsockInitFlags |= ADDRESS_LOCK_INITED;
  119. //
  120. // address pools
  121. //
  122. if (!g_SocketAddressPool.Initialize(sizeof(CSocketAddress),
  123. CSocketAddress::PoolAllocFunction,
  124. CSocketAddress::PoolGetFunction,
  125. CSocketAddress::PoolReturnFunction,
  126. NULL))
  127. {
  128. goto Failure;
  129. }
  130. g_dwWsockInitFlags |= SOCKETADDRESS_POOL_INITED;
  131. //
  132. // endpoint pools
  133. //
  134. if (!g_EndpointPool.Initialize(sizeof(CEndpoint),
  135. CEndpoint::PoolAllocFunction,
  136. CEndpoint::PoolInitFunction,
  137. CEndpoint::PoolReleaseFunction,
  138. CEndpoint::PoolDeallocFunction))
  139. {
  140. goto Failure;
  141. }
  142. g_dwWsockInitFlags |= ENDPOINT_POOL_INITED;
  143. //
  144. // endpoint command parameter pools
  145. //
  146. if (!g_EndpointCommandParametersPool.Initialize(sizeof(ENDPOINT_COMMAND_PARAMETERS),
  147. NULL,
  148. ENDPOINT_COMMAND_PARAMETERS::PoolInitFunction,
  149. NULL,
  150. NULL))
  151. {
  152. goto Failure;
  153. }
  154. g_dwWsockInitFlags |= EPCMDPARAM_POOL_INITED;
  155. //
  156. // socket port pool
  157. //
  158. if (!g_SocketPortPool.Initialize(sizeof(CSocketPort),
  159. CSocketPort::PoolAllocFunction,
  160. CSocketPort::PoolInitFunction,
  161. #ifdef DBG
  162. CSocketPort::PoolDeinitFunction,
  163. #else // ! DBG
  164. NULL,
  165. #endif // ! DBG
  166. CSocketPort::PoolDeallocFunction))
  167. {
  168. goto Failure;
  169. }
  170. g_dwWsockInitFlags |= SOCKETPORT_POOL_INITED;
  171. //
  172. // thread pool pool
  173. //
  174. if (!g_ThreadPoolPool.Initialize(sizeof(CThreadPool),
  175. CThreadPool::PoolAllocFunction,
  176. NULL,
  177. NULL,
  178. CThreadPool::PoolDeallocFunction))
  179. {
  180. goto Failure;
  181. }
  182. g_dwWsockInitFlags |= THREADPOOL_POOL_INITED;
  183. // pool of read requests
  184. if (!g_ReadIODataPool.Initialize( sizeof(CReadIOData),
  185. CReadIOData::ReadIOData_Alloc,
  186. CReadIOData::ReadIOData_Get,
  187. CReadIOData::ReadIOData_Release,
  188. CReadIOData::ReadIOData_Dealloc))
  189. {
  190. goto Failure;
  191. }
  192. g_dwWsockInitFlags |= READ_POOL_INITED;
  193. // timer entry pool
  194. if (!g_TimerEntryPool.Initialize( sizeof(TIMER_OPERATION_ENTRY),
  195. TimerEntry_Alloc, // function called on pool entry initial allocation
  196. TimerEntry_Get, // function called on entry extraction from pool
  197. TimerEntry_Release, // function called on entry return to pool
  198. TimerEntry_Dealloc // function called on entry free
  199. ))
  200. {
  201. goto Failure;
  202. }
  203. g_dwWsockInitFlags |= TIMERENTRY_POOL_INITED;
  204. // socket data pool
  205. if (!g_SocketDataPool.Initialize( sizeof(CSocketData),
  206. CSocketData::PoolAllocFunction,
  207. CSocketData::PoolInitFunction,
  208. CSocketData::PoolReleaseFunction,
  209. CSocketData::PoolDeallocFunction
  210. ))
  211. {
  212. goto Failure;
  213. }
  214. g_dwWsockInitFlags |= SOCKETDATA_POOL_INITED;
  215. #ifndef DPNBUILD_ONLYONETHREAD
  216. // blocking job pool
  217. if (!g_BlockingJobPool.Initialize( sizeof(BLOCKING_JOB),
  218. NULL,
  219. NULL,
  220. NULL,
  221. NULL))
  222. {
  223. goto Failure;
  224. }
  225. g_dwWsockInitFlags |= BLOCKINGJOB_POOL_INITED;
  226. #endif // ! DPNBUILD_ONLYONETHREAD
  227. return TRUE;
  228. Failure:
  229. DeinitializePools();
  230. return FALSE;
  231. }
  232. //**********************************************************************
  233. //**********************************************************************
  234. // ------------------------------
  235. // DeinitializePools - deinitialize the pools
  236. //
  237. // Entry: Nothing
  238. //
  239. // Exit: Nothing
  240. // ------------------------------
  241. #undef DPF_MODNAME
  242. #define DPF_MODNAME "DeinitializePools"
  243. void DeinitializePools( void )
  244. {
  245. if (g_dwWsockInitFlags & READ_POOL_INITED)
  246. {
  247. g_ReadIODataPool.DeInitialize();
  248. #ifdef DBG
  249. g_dwWsockInitFlags &= ~READ_POOL_INITED;
  250. #endif // DBG
  251. }
  252. if (g_dwWsockInitFlags & TIMERENTRY_POOL_INITED)
  253. {
  254. g_TimerEntryPool.DeInitialize();
  255. #ifdef DBG
  256. g_dwWsockInitFlags &= ~TIMERENTRY_POOL_INITED;
  257. #endif // DBG
  258. }
  259. if (g_dwWsockInitFlags & THREADPOOL_POOL_INITED)
  260. {
  261. g_ThreadPoolPool.DeInitialize();
  262. #ifdef DBG
  263. g_dwWsockInitFlags &= ~THREADPOOL_POOL_INITED;
  264. #endif // DBG
  265. }
  266. if (g_dwWsockInitFlags & SOCKETPORT_POOL_INITED)
  267. {
  268. g_SocketPortPool.DeInitialize();
  269. #ifdef DBG
  270. g_dwWsockInitFlags &= ~SOCKETPORT_POOL_INITED;
  271. #endif // DBG
  272. }
  273. if (g_dwWsockInitFlags & EPCMDPARAM_POOL_INITED)
  274. {
  275. g_EndpointCommandParametersPool.DeInitialize();
  276. #ifdef DBG
  277. g_dwWsockInitFlags &= ~EPCMDPARAM_POOL_INITED;
  278. #endif // DBG
  279. }
  280. if (g_dwWsockInitFlags & ENDPOINT_POOL_INITED)
  281. {
  282. g_EndpointPool.DeInitialize();
  283. #ifdef DBG
  284. g_dwWsockInitFlags &= ~ENDPOINT_POOL_INITED;
  285. #endif // DBG
  286. }
  287. if (g_dwWsockInitFlags & SOCKETADDRESS_POOL_INITED)
  288. {
  289. g_SocketAddressPool.DeInitialize();
  290. #ifdef DBG
  291. g_dwWsockInitFlags &= ~SOCKETADDRESS_POOL_INITED;
  292. #endif // DBG
  293. }
  294. if (g_dwWsockInitFlags & ADDRESS_LOCK_INITED)
  295. {
  296. DNDeleteCriticalSection( &g_AddressLock );
  297. #ifdef DBG
  298. g_dwWsockInitFlags &= ~ADDRESS_LOCK_INITED;
  299. #endif // DBG
  300. }
  301. if (g_dwWsockInitFlags & COMMANDDATA_POOL_INITED)
  302. {
  303. g_CommandDataPool.DeInitialize();
  304. #ifdef DBG
  305. g_dwWsockInitFlags &= ~COMMANDDATA_POOL_INITED;
  306. #endif // DBG
  307. }
  308. #ifndef DPNBUILD_ONLYONEADAPTER
  309. if (g_dwWsockInitFlags & ADAPTERENTRY_POOL_INITED)
  310. {
  311. g_AdapterEntryPool.DeInitialize();
  312. #ifdef DBG
  313. g_dwWsockInitFlags &= ~ADAPTERENTRY_POOL_INITED;
  314. #endif // DBG
  315. }
  316. #endif // ! DPNBUILD_ONLYONEADAPTER
  317. if (g_dwWsockInitFlags & SOCKETDATA_POOL_INITED)
  318. {
  319. g_SocketDataPool.DeInitialize();
  320. #ifdef DBG
  321. g_dwWsockInitFlags &= ~SOCKETDATA_POOL_INITED;
  322. #endif // DBG
  323. }
  324. #ifndef DPNBUILD_ONLYONETHREAD
  325. if (g_dwWsockInitFlags & BLOCKINGJOB_POOL_INITED)
  326. {
  327. g_BlockingJobPool.DeInitialize();
  328. #ifdef DBG
  329. g_dwWsockInitFlags &= ~BLOCKINGJOB_POOL_INITED;
  330. #endif // DBG
  331. }
  332. #endif // ! DPNBUILD_ONLYONETHREAD
  333. DNASSERT(g_dwWsockInitFlags == 0);
  334. g_dwWsockInitFlags = 0;
  335. }
  336. //**********************************************************************