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.

144 lines
2.9 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. m_iPhysicalAllocs++;
  76. #endif
  77. }
  78. if( pi )
  79. {
  80. pi->data = data;
  81. *ph = reinterpret_cast<HANDLE>(pi);
  82. #if DBG
  83. m_iLogicalAllocs++;
  84. #endif
  85. }
  86. hr = (*ph) ? S_OK : E_OUTOFMEMORY;
  87. }
  88. return hr;
  89. }
  90. template <class T>
  91. HRESULT CFastHeap<T>::Free(HANDLE h)
  92. {
  93. CCSLock::Locker lock(m_csLock);
  94. HRESULT hr = E_INVALIDARG;
  95. if( h )
  96. {
  97. #if DBG
  98. m_iLogicalAllocs--;
  99. #endif
  100. ASSERT(m_iCached >= 0);
  101. HeapItem *pi = reinterpret_cast<HeapItem*>(h);
  102. if( m_iCached < m_iCacheSize )
  103. {
  104. // the number of cached items is less than the
  105. // cache size, so we put this item in the cache
  106. pi->pNext = m_pFreeList;
  107. m_pFreeList = pi;
  108. m_iCached++;
  109. }
  110. else
  111. {
  112. // enough items cached, delete this one
  113. delete pi;
  114. #if DBG
  115. m_iPhysicalAllocs--;
  116. #endif
  117. }
  118. hr = S_OK;
  119. }
  120. return hr;
  121. }
  122. template <class T>
  123. HRESULT CFastHeap<T>::GetItem(HANDLE h, T **ppData)
  124. {
  125. // just return the item ptr. no need to aquire the CS as this is
  126. // readonly function and Alloc/Free cannot invalidate the item.
  127. *ppData = &(reinterpret_cast<HeapItem*>(h)->data);
  128. return S_OK;
  129. }