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.

150 lines
3.9 KiB

  1. /***************************************************************************\
  2. *
  3. * File: AllocPool.inl
  4. *
  5. * History:
  6. * 1/28/2000: JStall: Created
  7. *
  8. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  9. *
  10. \***************************************************************************/
  11. #if !defined(BASE__AllocPool_inl__INCLUDED)
  12. #define BASE__AllocPool_inl__INCLUDED
  13. #pragma once
  14. /***************************************************************************\
  15. *****************************************************************************
  16. *
  17. * class AllocListNL
  18. *
  19. *****************************************************************************
  20. \***************************************************************************/
  21. //------------------------------------------------------------------------------
  22. template <class T, int cbBlock, class heap>
  23. inline
  24. AllocPoolNL<T, cbBlock, heap>::AllocPoolNL()
  25. {
  26. m_nTop = 0;
  27. }
  28. //------------------------------------------------------------------------------
  29. template <class T, int cbBlock, class heap>
  30. inline
  31. AllocPoolNL<T, cbBlock, heap>::~AllocPoolNL()
  32. {
  33. Destroy();
  34. }
  35. //------------------------------------------------------------------------------
  36. template <class T, int cbBlock, class heap>
  37. inline void
  38. AllocPoolNL<T, cbBlock, heap>::Destroy()
  39. {
  40. if (m_nTop > 0) {
  41. ContextMultiFree(heap::GetHeap(), m_nTop, (void **) m_rgItems, sizeof(T));
  42. m_nTop = 0;
  43. }
  44. }
  45. //------------------------------------------------------------------------------
  46. template <class T, int cbBlock, class heap>
  47. inline T *
  48. AllocPoolNL<T, cbBlock, heap>::New()
  49. {
  50. T * ptNew;
  51. if (m_nTop <= 0) {
  52. //
  53. // Not enough items in the pool to hand any new one out, so we need to
  54. // allocate more. These will NOT be zero-initialized by the memory
  55. // allocator.
  56. //
  57. ContextMultiAlloc(heap::GetHeap(), &m_nTop, (void **) m_rgItems, cbBlock, sizeof(T));
  58. if (m_nTop == 0) {
  59. ptNew = NULL;
  60. goto exit;
  61. }
  62. }
  63. //
  64. // There is an item in the pool, but we need to "scrub" it before handing
  65. // it out.
  66. //
  67. ptNew = m_rgItems[--m_nTop];
  68. ZeroMemory(ptNew, sizeof(T));
  69. placement_new(ptNew, T);
  70. exit:
  71. return ptNew;
  72. }
  73. //------------------------------------------------------------------------------
  74. template <class T, int cbBlock, class heap>
  75. inline void
  76. AllocPoolNL<T, cbBlock, heap>::Delete(T * pvMem)
  77. {
  78. if (pvMem == NULL) {
  79. return;
  80. }
  81. placement_delete(pvMem, T);
  82. m_rgItems[m_nTop++] = pvMem;
  83. if (m_nTop >= cbBlock * 2) {
  84. ContextMultiFree(heap::GetHeap(), cbBlock, (void **) &m_rgItems[cbBlock], sizeof(T));
  85. m_nTop -= cbBlock;
  86. }
  87. }
  88. //------------------------------------------------------------------------------
  89. template <class T, int cbBlock, class heap>
  90. inline BOOL
  91. AllocPoolNL<T, cbBlock, heap>::IsEmpty() const
  92. {
  93. return m_nTop == 0;
  94. }
  95. /***************************************************************************\
  96. *****************************************************************************
  97. *
  98. * class AllocList
  99. *
  100. *****************************************************************************
  101. \***************************************************************************/
  102. //------------------------------------------------------------------------------
  103. template <class T, int cbBlock>
  104. inline T *
  105. AllocPool<T, cbBlock>::New()
  106. {
  107. m_lock.Enter();
  108. T * pNew = AllocPoolNL<T, cbBlock>::New();
  109. m_lock.Leave();
  110. return pNew;
  111. }
  112. //------------------------------------------------------------------------------
  113. template <class T, int cbBlock>
  114. inline void
  115. AllocPool<T, cbBlock>::Delete(T * pvMem)
  116. {
  117. m_lock.Enter();
  118. AllocPoolNL<T, cbBlock>::Delete(pvMem);
  119. m_lock.Leave();
  120. }
  121. #endif // BASE__AllocPool_inl__INCLUDED