MicroObjectRecycler.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2009-2021, 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/Vulkan/Common.h>
  7. #include <AnKi/Util/DynamicArray.h>
  8. namespace anki
  9. {
  10. /// @addtogroup vulkan
  11. /// @{
  12. /// Helper class for MicroXXX objects.
  13. template<typename T>
  14. class MicroObjectRecycler
  15. {
  16. public:
  17. MicroObjectRecycler()
  18. {
  19. }
  20. MicroObjectRecycler(GrAllocator<U8> alloc)
  21. {
  22. init(alloc);
  23. }
  24. ~MicroObjectRecycler()
  25. {
  26. destroy();
  27. }
  28. void init(GrAllocator<U8> alloc)
  29. {
  30. m_alloc = alloc;
  31. }
  32. /// It's thread-safe.
  33. void destroy();
  34. /// Find a new one to reuse. It's thread-safe.
  35. T* findToReuse();
  36. /// Release an object back to the recycler. It's thread-safe.
  37. void recycle(T* s);
  38. /// Destroy those objects that their fence is done. It's thread-safe.
  39. void trimCache();
  40. private:
  41. GrAllocator<U8> m_alloc;
  42. DynamicArray<T*> m_objects;
  43. Mutex m_mtx;
  44. #if ANKI_EXTRA_CHECKS
  45. U32 m_createdAndNotRecycled = 0;
  46. #endif
  47. void releaseFences();
  48. };
  49. /// @}
  50. } // end namespace anki
  51. #include <AnKi/Gr/Vulkan/MicroObjectRecycler.inl.h>