MicroObjectRecycler.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/BackendCommon/Common.h>
  7. #include <AnKi/Util/DynamicArray.h>
  8. namespace anki {
  9. /// @addtogroup graphics
  10. /// @{
  11. /// Helper class for MicroXXX objects. It expects a specific interface for the T.
  12. template<typename T>
  13. class MicroObjectRecycler
  14. {
  15. public:
  16. MicroObjectRecycler()
  17. {
  18. }
  19. ~MicroObjectRecycler()
  20. {
  21. destroy();
  22. }
  23. /// It's thread-safe.
  24. void destroy();
  25. /// Find a new one to reuse. It's thread-safe.
  26. T* findToReuse();
  27. /// Release an object back to the recycler. It's thread-safe.
  28. void recycle(T* s);
  29. /// Destroy those objects that their fence is done. It's thread-safe.
  30. void trimCache()
  31. {
  32. LockGuard<Mutex> lock(m_mtx);
  33. checkDoneFences();
  34. trimCacheInternal(0);
  35. }
  36. U32 getCacheSize() const
  37. {
  38. return m_objects.getSize();
  39. }
  40. private:
  41. class Object
  42. {
  43. public:
  44. T* m_microObject;
  45. Bool m_fenceDone;
  46. };
  47. GrDynamicArray<Object> m_objects;
  48. Mutex m_mtx;
  49. // Begin trim cache adjustment vars
  50. U32 m_readyObjectsAfterTrim = 1;
  51. static constexpr U32 m_maxRequestsPerAdjustment = 128;
  52. U32 m_cacheMisses = 0;
  53. U32 m_requests = 0;
  54. U32 m_minCacheSizePerRequest = kMaxU32;
  55. // End trim cache adjustment vars
  56. #if ANKI_EXTRA_CHECKS
  57. U32 m_createdAndNotRecycled = 0;
  58. #endif
  59. void trimCacheInternal(U32 aliveObjectCountAfterTrim);
  60. void adjustAliveObjectCount();
  61. void checkDoneFences();
  62. };
  63. /// @}
  64. } // end namespace anki
  65. #include <AnKi/Gr/BackendCommon/MicroObjectRecycler.inl.h>