MicroObjectRecycler.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. trimCacheInternal(0);
  34. }
  35. U32 getCacheSize() const
  36. {
  37. return m_objectCache.getSize();
  38. }
  39. private:
  40. GrDynamicArray<T*> m_objectCache;
  41. Mutex m_mtx;
  42. // Begin trim cache adjustment vars
  43. U32 m_availableObjectsAfterTrim = 1;
  44. static constexpr U32 kMaxRequestsPerAdjustment = 128;
  45. U32 m_cacheMisses = 0;
  46. U32 m_requests = 0;
  47. // End trim cache adjustment vars
  48. #if ANKI_ASSERTIONS_ENABLED
  49. U32 m_inUseObjects = 0;
  50. #endif
  51. void trimCacheInternal(U32 aliveObjectCountAfterTrim);
  52. void adjustAliveObjectCount();
  53. };
  54. /// @}
  55. } // end namespace anki
  56. #include <AnKi/Gr/BackendCommon/MicroObjectRecycler.inl.h>