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.7 KiB

  1. /***************************************************************************\
  2. *
  3. * File: GdiCache.cpp
  4. *
  5. * Description:
  6. * GdiCache.cpp implements the process-wide GDI cache that manages cached and
  7. * temporary GDI objects.
  8. *
  9. *
  10. * History:
  11. * 1/18/2000: JStall: Created
  12. *
  13. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  14. *
  15. \***************************************************************************/
  16. #include "stdafx.h"
  17. #include "Services.h"
  18. #include "GdiCache.h"
  19. /***************************************************************************\
  20. *****************************************************************************
  21. *
  22. * class ObjectCache
  23. *
  24. *****************************************************************************
  25. \***************************************************************************/
  26. //------------------------------------------------------------------------------
  27. void
  28. ObjectCache::Destroy()
  29. {
  30. //
  31. // Remove all temporary regions. These MUST all be released by this point.
  32. //
  33. AssertMsg(m_arAll.GetSize() == m_arFree.GetSize(), "All objects should be free");
  34. #if ENABLE_DUMPCACHESTATS
  35. AutoTrace("%s ObjectCache statistics: %d items\n", m_szName, m_arAll.GetSize());
  36. #endif // ENABLE_DUMPCACHESTATS
  37. int cObjs = m_arAll.GetSize();
  38. for (int idx = 0; idx < cObjs; idx++) {
  39. DestroyObject(m_arAll[idx]);
  40. }
  41. m_arAll.RemoveAll();
  42. m_arFree.RemoveAll();
  43. }
  44. //------------------------------------------------------------------------------
  45. void *
  46. ObjectCache::Pop()
  47. {
  48. void * pObj;
  49. //
  50. // Check if any objects are already freed.
  51. //
  52. if (!m_arFree.IsEmpty()) {
  53. int idxObj = m_arFree.GetSize() - 1;
  54. pObj = m_arFree[idxObj];
  55. Verify(m_arFree.RemoveAt(idxObj));
  56. goto Exit;
  57. }
  58. //
  59. // No cached regions, so create a new one.
  60. //
  61. pObj = Build();
  62. if (pObj == NULL) {
  63. AssertMsg(0, "Could not create a new object- something is probably wrong");
  64. goto Exit;
  65. }
  66. {
  67. int idxAdd = m_arAll.Add(pObj);
  68. if (idxAdd == -1) {
  69. AssertMsg(0, "Could not add object to array- something is probably wrong");
  70. DestroyObject(pObj);
  71. pObj = NULL;
  72. goto Exit;
  73. }
  74. }
  75. Exit:
  76. return pObj;
  77. }
  78. //------------------------------------------------------------------------------
  79. void
  80. ObjectCache::Push(void * pObj)
  81. {
  82. #if DBG
  83. //
  84. // Ensure this object was previously given out, but is not currently listed
  85. // as free.
  86. //
  87. {
  88. BOOL fValid;
  89. int cItems, idx;
  90. fValid = FALSE;
  91. cItems = m_arAll.GetSize();
  92. for (idx = 0; idx < cItems; idx++) {
  93. if (m_arAll[idx] == pObj) {
  94. fValid = TRUE;
  95. break;
  96. }
  97. }
  98. AssertMsg(fValid, "Object not in list of all temporary regions");
  99. cItems = m_arFree.GetSize();
  100. for (idx = 0; idx < cItems; idx++) {
  101. AssertMsg(m_arFree[idx] != pObj, "Object must not be free object list");
  102. }
  103. }
  104. #endif // DBG
  105. //
  106. // Add this object to the list of free objects.
  107. //
  108. if (m_arFree.GetSize() < m_cMaxFree) {
  109. VerifyMsg(m_arFree.Add(pObj) >= 0, "Should be able to add object to list");
  110. } else {
  111. DestroyObject(pObj);
  112. }
  113. }
  114. /***************************************************************************\
  115. *****************************************************************************
  116. *
  117. * class GdiCache
  118. *
  119. *****************************************************************************
  120. \***************************************************************************/