MicroObjectRecycler.h 1.1 KB

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