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.

147 lines
3.1 KiB

  1. /*****************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 2000
  4. *
  5. * TITLE: cntutils.inl
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: LazarI
  10. *
  11. * DATE: 23-Dec-2000
  12. *
  13. * DESCRIPTION: Containers and algorithms utility templates (Impl.)
  14. *
  15. *****************************************************************************/
  16. ////////////////////////////////////////////////
  17. //
  18. // class CFastHeap<T>
  19. //
  20. // fast cached heap for fixed chunks
  21. // of memory (MT safe)
  22. //
  23. template <class T>
  24. CFastHeap<T>::CFastHeap<T>(int iCacheSize):
  25. #if DBG
  26. m_iPhysicalAllocs(0),
  27. m_iLogicalAllocs(0),
  28. #endif
  29. m_pFreeList(NULL),
  30. m_iCacheSize(iCacheSize),
  31. m_iCached(0)
  32. {
  33. // nothing
  34. }
  35. template <class T>
  36. CFastHeap<T>::~CFastHeap<T>()
  37. {
  38. // delete the cache
  39. while( m_pFreeList )
  40. {
  41. HeapItem *pItem = m_pFreeList;
  42. m_pFreeList = m_pFreeList->pNext;
  43. delete pItem;
  44. #if DBG
  45. m_iPhysicalAllocs--;
  46. #endif
  47. }
  48. #if DBG
  49. ASSERT(0 == m_iPhysicalAllocs);
  50. ASSERT(0 == m_iLogicalAllocs);
  51. #endif
  52. }
  53. template <class T>
  54. HRESULT CFastHeap<T>::Alloc(const T &data, HANDLE *ph)
  55. {
  56. CCSLock::Locker lock(m_csLock);
  57. HRESULT hr = E_INVALIDARG;
  58. if( ph )
  59. {
  60. *ph = NULL;
  61. HeapItem *pi = NULL;
  62. if( m_pFreeList )
  63. {
  64. // we have an item in the cache, just use it
  65. pi = m_pFreeList;
  66. m_pFreeList = m_pFreeList->pNext;
  67. m_iCached--;
  68. ASSERT(m_iCached >= 0);
  69. }
  70. else
  71. {
  72. // no items in the cache, allocate new one
  73. pi = new HeapItem;
  74. #if DBG
  75. if( pi )
  76. {
  77. m_iPhysicalAllocs++;
  78. }
  79. #endif
  80. }
  81. if( pi )
  82. {
  83. pi->data = data;
  84. *ph = reinterpret_cast<HANDLE>(pi);
  85. #if DBG
  86. m_iLogicalAllocs++;
  87. #endif
  88. }
  89. hr = (*ph) ? S_OK : E_OUTOFMEMORY;
  90. }
  91. return hr;
  92. }
  93. template <class T>
  94. HRESULT CFastHeap<T>::Free(HANDLE h)
  95. {
  96. CCSLock::Locker lock(m_csLock);
  97. HRESULT hr = E_INVALIDARG;
  98. if( h )
  99. {
  100. #if DBG
  101. m_iLogicalAllocs--;
  102. #endif
  103. ASSERT(m_iCached >= 0);
  104. HeapItem *pi = reinterpret_cast<HeapItem*>(h);
  105. if( m_iCached < m_iCacheSize )
  106. {
  107. // the number of cached items is less than the
  108. // cache size, so we put this item in the cache
  109. pi->pNext = m_pFreeList;
  110. m_pFreeList = pi;
  111. m_iCached++;
  112. }
  113. else
  114. {
  115. // enough items cached, delete this one
  116. delete pi;
  117. #if DBG
  118. m_iPhysicalAllocs--;
  119. #endif
  120. }
  121. hr = S_OK;
  122. }
  123. return hr;
  124. }
  125. template <class T>
  126. HRESULT CFastHeap<T>::GetItem(HANDLE h, T **ppData)
  127. {
  128. // just return the item ptr. no need to aquire the CS as this is
  129. // readonly function and Alloc/Free cannot invalidate the item.
  130. *ppData = &(reinterpret_cast<HeapItem*>(h)->data);
  131. return S_OK;
  132. }