StackGpuAllocator.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include <AnKi/Gr/Utils/StackGpuAllocator.h>
  6. namespace anki
  7. {
  8. class StackGpuAllocatorChunk
  9. {
  10. public:
  11. StackGpuAllocatorChunk* m_next;
  12. StackGpuAllocatorMemory* m_mem;
  13. Atomic<PtrSize> m_offset;
  14. PtrSize m_size;
  15. };
  16. StackGpuAllocator::~StackGpuAllocator()
  17. {
  18. Chunk* chunk = m_chunkListHead;
  19. while(chunk)
  20. {
  21. if(chunk->m_mem)
  22. {
  23. m_iface->free(chunk->m_mem);
  24. }
  25. Chunk* next = chunk->m_next;
  26. m_alloc.deleteInstance(chunk);
  27. chunk = next;
  28. }
  29. }
  30. void StackGpuAllocator::init(GenericMemoryPoolAllocator<U8> alloc, StackGpuAllocatorInterface* iface)
  31. {
  32. ANKI_ASSERT(iface);
  33. m_alloc = alloc;
  34. m_iface = iface;
  35. iface->getChunkGrowInfo(m_scale, m_bias, m_initialSize);
  36. ANKI_ASSERT(m_scale >= 1.0);
  37. ANKI_ASSERT(m_initialSize > 0);
  38. m_alignment = iface->getMaxAlignment();
  39. ANKI_ASSERT(m_alignment > 0);
  40. alignRoundUp(m_alignment, m_initialSize);
  41. }
  42. Error StackGpuAllocator::allocate(PtrSize size, StackGpuAllocatorHandle& handle)
  43. {
  44. alignRoundUp(m_alignment, size);
  45. ANKI_ASSERT(size > 0);
  46. ANKI_ASSERT(size <= m_initialSize && "The chunks should have enough space to hold at least one allocation");
  47. Chunk* crntChunk;
  48. Bool retry = true;
  49. do
  50. {
  51. crntChunk = m_crntChunk.load();
  52. PtrSize offset;
  53. if(crntChunk && ((offset = crntChunk->m_offset.fetchAdd(size)) + size) <= crntChunk->m_size)
  54. {
  55. // All is fine, there is enough space in the chunk
  56. handle.m_memory = crntChunk->m_mem;
  57. handle.m_offset = offset;
  58. retry = false;
  59. }
  60. else
  61. {
  62. // Need new chunk
  63. LockGuard<Mutex> lock(m_lock);
  64. // Make sure that only one thread will create a new chunk
  65. if(m_crntChunk.load() == crntChunk)
  66. {
  67. // We can create a new chunk
  68. if(crntChunk == nullptr || crntChunk->m_next == nullptr)
  69. {
  70. // Need to create a new chunk
  71. Chunk* newChunk = m_alloc.newInstance<Chunk>();
  72. if(crntChunk)
  73. {
  74. crntChunk->m_next = newChunk;
  75. newChunk->m_size = PtrSize(F32(crntChunk->m_size) * m_scale + F32(m_bias));
  76. }
  77. else
  78. {
  79. newChunk->m_size = m_initialSize;
  80. if(m_chunkListHead == nullptr)
  81. {
  82. m_chunkListHead = newChunk;
  83. }
  84. }
  85. alignRoundUp(m_alignment, newChunk->m_size);
  86. newChunk->m_next = nullptr;
  87. newChunk->m_offset.setNonAtomically(0);
  88. ANKI_CHECK(m_iface->allocate(newChunk->m_size, newChunk->m_mem));
  89. m_crntChunk.store(newChunk);
  90. }
  91. else
  92. {
  93. // Need to recycle one
  94. crntChunk->m_next->m_offset.setNonAtomically(0);
  95. m_crntChunk.store(crntChunk->m_next);
  96. }
  97. }
  98. }
  99. } while(retry);
  100. return Error::NONE;
  101. }
  102. void StackGpuAllocator::reset()
  103. {
  104. m_crntChunk.setNonAtomically(m_chunkListHead);
  105. if(m_chunkListHead)
  106. {
  107. m_chunkListHead->m_offset.setNonAtomically(0);
  108. }
  109. }
  110. } // end namespace anki