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.

511 lines
8.7 KiB

  1. #ifndef __ALLOCATOR_CPP
  2. #define __ALLOCATOR_CPP
  3. /*
  4. * Class:
  5. *
  6. * WmiAllocator
  7. *
  8. * Description:
  9. *
  10. * Provides abstraction above heap allocation functions
  11. *
  12. * Version:
  13. *
  14. * Initial
  15. *
  16. * Last Changed:
  17. *
  18. * See Source Depot for change history
  19. *
  20. */
  21. #if 0
  22. #include <precomp.h>
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <Allocator.h>
  26. #endif
  27. /******************************************************************************
  28. *
  29. * Name:
  30. *
  31. *
  32. * Description:
  33. *
  34. *
  35. *****************************************************************************/
  36. WmiAllocator :: WmiAllocator () :
  37. m_Heap ( NULL ) ,
  38. m_Options ( e_DefaultAllocation ) ,
  39. m_InitialSize ( 0 ) ,
  40. m_MaximumSize ( 0 ) ,
  41. m_ReferenceCount ( 0 )
  42. {
  43. m_Heap = GetProcessHeap () ;
  44. }
  45. /******************************************************************************
  46. *
  47. * Name:
  48. *
  49. *
  50. * Description:
  51. *
  52. *
  53. *****************************************************************************/
  54. WmiAllocator :: WmiAllocator (
  55. AllocationOptions a_Option ,
  56. size_t a_InitialSize ,
  57. size_t a_MaximumSize
  58. ) :
  59. m_Heap ( NULL ) ,
  60. m_Options ( a_Option ) ,
  61. m_InitialSize ( a_InitialSize ) ,
  62. m_MaximumSize ( a_MaximumSize ) ,
  63. m_ReferenceCount ( 0 )
  64. {
  65. }
  66. /******************************************************************************
  67. *
  68. * Name:
  69. *
  70. *
  71. * Description:
  72. *
  73. *
  74. *****************************************************************************/
  75. WmiAllocator :: ~WmiAllocator ()
  76. {
  77. UnInitialize () ;
  78. }
  79. /******************************************************************************
  80. *
  81. * Name:
  82. *
  83. *
  84. * Description:
  85. *
  86. *
  87. *****************************************************************************/
  88. ULONG WmiAllocator :: AddRef ()
  89. {
  90. return InterlockedIncrement ( & m_ReferenceCount ) ;
  91. }
  92. /******************************************************************************
  93. *
  94. * Name:
  95. *
  96. *
  97. * Description:
  98. *
  99. *
  100. *****************************************************************************/
  101. ULONG WmiAllocator :: Release ()
  102. {
  103. ULONG t_ReferenceCount = InterlockedDecrement ( & m_ReferenceCount ) ;
  104. if ( t_ReferenceCount == 0 )
  105. {
  106. delete this ;
  107. }
  108. return t_ReferenceCount ;
  109. }
  110. /******************************************************************************
  111. *
  112. * Name:
  113. *
  114. *
  115. * Description:
  116. *
  117. *
  118. *****************************************************************************/
  119. WmiStatusCode WmiAllocator :: Win32ToApi ()
  120. {
  121. WmiStatusCode t_Status = e_StatusCode_Success ;
  122. DWORD t_LastError = GetLastError () ;
  123. switch ( t_LastError )
  124. {
  125. case STATUS_NO_MEMORY:
  126. {
  127. t_Status = e_StatusCode_OutOfMemory ;
  128. }
  129. break ;
  130. default:
  131. {
  132. t_Status = e_StatusCode_Unknown ;
  133. }
  134. break ;
  135. }
  136. return t_Status ;
  137. }
  138. /******************************************************************************
  139. *
  140. * Name:
  141. *
  142. *
  143. * Description:
  144. *
  145. *
  146. *****************************************************************************/
  147. WmiStatusCode WmiAllocator :: Initialize ()
  148. {
  149. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  150. if ( m_Heap == NULL )
  151. {
  152. m_Heap = HeapCreate (
  153. m_Options ,
  154. m_InitialSize ,
  155. m_MaximumSize
  156. ) ;
  157. if ( m_Heap == NULL )
  158. {
  159. t_StatusCode = Win32ToApi () ;
  160. }
  161. }
  162. return t_StatusCode ;
  163. }
  164. /******************************************************************************
  165. *
  166. * Name:
  167. *
  168. *
  169. * Description:
  170. *
  171. *
  172. *****************************************************************************/
  173. WmiStatusCode WmiAllocator :: UnInitialize ()
  174. {
  175. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  176. if ( m_Heap )
  177. {
  178. if ( m_Heap != GetProcessHeap () )
  179. {
  180. BOOL t_Status = HeapDestroy ( m_Heap ) ;
  181. if ( t_Status )
  182. {
  183. m_Heap = NULL ;
  184. }
  185. else
  186. {
  187. t_Status = Win32ToApi () ;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. t_StatusCode = e_StatusCode_NotInitialized ;
  194. }
  195. return t_StatusCode ;
  196. }
  197. /******************************************************************************
  198. *
  199. * Name:
  200. *
  201. *
  202. * Description:
  203. *
  204. *
  205. *****************************************************************************/
  206. WmiStatusCode WmiAllocator :: New (
  207. void **a_Allocation ,
  208. size_t a_Size
  209. )
  210. {
  211. return New (
  212. ( AllocationOptions ) ( ( m_Options & ( e_GenerateException | e_NoSerialize ) ) | e_ZeroMemory ) ,
  213. a_Allocation ,
  214. a_Size
  215. ) ;
  216. }
  217. /******************************************************************************
  218. *
  219. * Name:
  220. *
  221. *
  222. * Description:
  223. *
  224. *
  225. *****************************************************************************/
  226. WmiStatusCode WmiAllocator :: New (
  227. AllocationOptions a_Option ,
  228. void **a_Allocation ,
  229. size_t a_Size
  230. )
  231. {
  232. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  233. if ( m_Heap )
  234. {
  235. *a_Allocation = HeapAlloc (
  236. m_Heap ,
  237. a_Option ,
  238. a_Size
  239. ) ;
  240. if ( ! *a_Allocation )
  241. {
  242. t_StatusCode = Win32ToApi () ;
  243. }
  244. }
  245. else
  246. {
  247. t_StatusCode = e_StatusCode_NotInitialized ;
  248. }
  249. return t_StatusCode ;
  250. }
  251. /******************************************************************************
  252. *
  253. * Name:
  254. *
  255. *
  256. * Description:
  257. *
  258. *
  259. *****************************************************************************/
  260. WmiStatusCode WmiAllocator :: ReAlloc (
  261. void *a_Allocation ,
  262. void **a_ReAllocation ,
  263. size_t a_Size
  264. )
  265. {
  266. return ReAlloc (
  267. ( AllocationOptions ) ( ( m_Options & ( e_GenerateException | e_NoSerialize ) ) ) ,
  268. a_Allocation ,
  269. a_ReAllocation ,
  270. a_Size
  271. ) ;
  272. }
  273. /******************************************************************************
  274. *
  275. * Name:
  276. *
  277. *
  278. * Description:
  279. *
  280. *
  281. *****************************************************************************/
  282. WmiStatusCode WmiAllocator :: ReAlloc (
  283. AllocationOptions a_Option ,
  284. void *a_Allocation ,
  285. void **a_ReAllocation ,
  286. size_t a_Size
  287. )
  288. {
  289. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  290. if ( m_Heap )
  291. {
  292. *a_ReAllocation = HeapReAlloc (
  293. m_Heap ,
  294. a_Option ,
  295. a_Allocation ,
  296. a_Size
  297. ) ;
  298. if ( ! *a_ReAllocation )
  299. {
  300. t_StatusCode = Win32ToApi () ;
  301. }
  302. }
  303. else
  304. {
  305. t_StatusCode = e_StatusCode_NotInitialized ;
  306. }
  307. return t_StatusCode ;
  308. }
  309. /******************************************************************************
  310. *
  311. * Name:
  312. *
  313. *
  314. * Description:
  315. *
  316. *
  317. *****************************************************************************/
  318. WmiStatusCode WmiAllocator :: Delete (
  319. void *a_Allocation
  320. )
  321. {
  322. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  323. if ( m_Heap )
  324. {
  325. BOOL t_Status = HeapFree (
  326. m_Heap ,
  327. 0 ,
  328. a_Allocation
  329. ) ;
  330. if ( ! t_Status )
  331. {
  332. t_StatusCode = Win32ToApi () ;
  333. }
  334. }
  335. else
  336. {
  337. t_StatusCode = e_StatusCode_NotInitialized ;
  338. }
  339. return t_StatusCode ;
  340. }
  341. /******************************************************************************
  342. *
  343. * Name:
  344. *
  345. *
  346. * Description:
  347. *
  348. *
  349. *****************************************************************************/
  350. WmiStatusCode WmiAllocator :: Size (
  351. void *a_Allocation ,
  352. size_t &a_Size
  353. )
  354. {
  355. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  356. if ( m_Heap )
  357. {
  358. a_Size = HeapSize (
  359. m_Heap ,
  360. m_Options & e_NoSerialize ,
  361. a_Allocation
  362. ) ;
  363. if ( a_Size == -1 )
  364. {
  365. t_StatusCode = Win32ToApi () ;
  366. }
  367. }
  368. else
  369. {
  370. t_StatusCode = e_StatusCode_AlreadyInitialized ;
  371. }
  372. return t_StatusCode ;
  373. }
  374. /******************************************************************************
  375. *
  376. * Name:
  377. *
  378. *
  379. * Description:
  380. *
  381. *
  382. *****************************************************************************/
  383. WmiStatusCode WmiAllocator :: Compact (
  384. size_t &a_LargestFreeBlock
  385. )
  386. {
  387. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  388. if ( m_Heap )
  389. {
  390. a_LargestFreeBlock = HeapCompact (
  391. m_Heap ,
  392. m_Options & e_NoSerialize
  393. ) ;
  394. if ( a_LargestFreeBlock == 0 && GetLastError () != 0 )
  395. {
  396. t_StatusCode = Win32ToApi () ;
  397. }
  398. }
  399. else
  400. {
  401. t_StatusCode = e_StatusCode_AlreadyInitialized ;
  402. }
  403. return t_StatusCode ;
  404. }
  405. /******************************************************************************
  406. *
  407. * Name:
  408. *
  409. *
  410. * Description:
  411. *
  412. *
  413. *****************************************************************************/
  414. WmiStatusCode WmiAllocator :: Validate ( LPCVOID a_Location )
  415. {
  416. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  417. BOOL t_Status = HeapValidate (
  418. m_Heap,
  419. m_Options & e_NoSerialize ,
  420. a_Location
  421. ) ;
  422. if ( ! t_Status )
  423. {
  424. t_StatusCode = e_StatusCode_InvalidHeap ;
  425. }
  426. return t_StatusCode ;
  427. }
  428. #endif __ALLOCATOR_CPP