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.

141 lines
4.0 KiB

  1. //---------------------------------------------------------------------------
  2. // CacheList.cpp - manages list of CRenderCache objects
  3. //---------------------------------------------------------------------------
  4. #include "stdafx.h"
  5. #include "CacheList.h"
  6. //---------------------------------------------------------------------------
  7. DWORD _tls_CacheListIndex = 0xffffffff; // index to tls pObjectPool
  8. //---------------------------------------------------------------------------
  9. CCacheList::CCacheList()
  10. {
  11. Log(LOG_CACHE, L"CCacheList: CREATED cache list for this thread");
  12. }
  13. //---------------------------------------------------------------------------
  14. CCacheList::~CCacheList()
  15. {
  16. for (int i=0; i < _CacheEntries.m_nSize; i++)
  17. {
  18. if (_CacheEntries[i])
  19. delete _CacheEntries[i];
  20. }
  21. Log(LOG_CACHE, L"~CCacheList: DELETED cache list for this thread");
  22. }
  23. //---------------------------------------------------------------------------
  24. HRESULT CCacheList::GetCacheObject(CRenderObj *pRenderObj, int iSlot, CRenderCache **ppCache)
  25. {
  26. static int iPassCount = 0;
  27. HRESULT hr = S_OK;
  28. CRenderCache *pCache;
  29. if (iSlot >= _CacheEntries.m_nSize)
  30. {
  31. hr = Resize(iSlot);
  32. if (FAILED(hr))
  33. goto exit;
  34. }
  35. pCache = _CacheEntries[iSlot];
  36. //---- is this an old object (old objects are freed on discovery) ----
  37. if (pCache)
  38. {
  39. BOOL fBad = (pRenderObj->_iUniqueId != pCache->_iUniqueId);
  40. if (! fBad)
  41. {
  42. //---- verify integrity of cache/render design ----
  43. if (pRenderObj != pCache->_pRenderObj)
  44. {
  45. // should never happen
  46. Log(LOG_ERROR, L"cache object doesn't match CRenderObj");
  47. fBad = TRUE;
  48. }
  49. }
  50. if (fBad)
  51. {
  52. Log(LOG_CACHE, L"GetCacheObject: deleting OLD OBJECT (slot=%d)", iSlot);
  53. delete pCache;
  54. pCache = NULL;
  55. _CacheEntries[iSlot] = NULL;
  56. }
  57. }
  58. //---- create an object on demand ----
  59. if (! pCache)
  60. {
  61. Log(LOG_CACHE, L"GetCacheObject: creating cache obj ON DEMAND (slot=%d)", iSlot);
  62. pCache = new CRenderCache(pRenderObj, pRenderObj->_iUniqueId);
  63. if (! pCache)
  64. {
  65. hr = MakeError32(E_OUTOFMEMORY);
  66. goto exit;
  67. }
  68. _CacheEntries[iSlot] = pCache;
  69. ASSERT(pRenderObj == pCache->_pRenderObj);
  70. }
  71. else
  72. {
  73. Log(LOG_CACHE, L"GetCacheObject: using EXISTING OBJ (slot=%d)", iSlot);
  74. }
  75. *ppCache = pCache;
  76. exit:
  77. return hr;
  78. }
  79. //---------------------------------------------------------------------------
  80. HRESULT CCacheList::Resize(int iMaxSlotNum)
  81. {
  82. HRESULT hr = S_OK;
  83. Log(LOG_CACHE, L"CCacheList::Resize: new MaxSlot=%d", iMaxSlotNum);
  84. int iOldSize = _CacheEntries.m_nSize;
  85. if (iMaxSlotNum >= iOldSize)
  86. {
  87. typedef CRenderCache *Entry;
  88. Entry *pNew = (Entry *)realloc(_CacheEntries.m_aT,
  89. (iMaxSlotNum+1) * sizeof(Entry));
  90. if (! pNew)
  91. hr = MakeError32(E_OUTOFMEMORY);
  92. else
  93. {
  94. _CacheEntries.m_nAllocSize = iMaxSlotNum + 1;
  95. _CacheEntries.m_aT = pNew;
  96. _CacheEntries.m_nSize = iMaxSlotNum + 1;
  97. for (int i=iOldSize; i < _CacheEntries.m_nSize; i++)
  98. _CacheEntries[i] = NULL;
  99. }
  100. }
  101. return hr;
  102. }
  103. //---------------------------------------------------------------------------
  104. CCacheList *GetTlsCacheList(BOOL fOkToCreate)
  105. {
  106. CCacheList *pList = NULL;
  107. if (_tls_CacheListIndex != 0xffffffff) // init-ed in ProcessAttach()
  108. {
  109. pList = (CCacheList *)TlsGetValue(_tls_CacheListIndex);
  110. if ((! pList) && (fOkToCreate)) // not yet initialized
  111. {
  112. //---- create a thread-local cache list ----
  113. pList = new CCacheList();
  114. TlsSetValue(_tls_CacheListIndex, pList);
  115. }
  116. }
  117. return pList;
  118. }
  119. //---------------------------------------------------------------------------