2
0

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. ANKI_ASSERT(m_initialSize >= m_alignment);
  41. ANKI_ASSERT((m_initialSize % m_alignment) == 0);
  42. }
  43. Error StackGpuAllocator::allocate(PtrSize size, StackGpuAllocatorHandle& handle)
  44. {
  45. alignRoundUp(m_alignment, size);
  46. ANKI_ASSERT(size > 0);
  47. ANKI_ASSERT(size <= m_initialSize && "The chunks should have enough space to hold at least one allocation");
  48. Chunk* crntChunk;
  49. Bool retry = true;
  50. do
  51. {
  52. crntChunk = m_crntChunk.load();
  53. PtrSize offset;
  54. if(crntChunk && ((offset = crntChunk->m_offset.fetchAdd(size)) + size) <= crntChunk->m_size)
  55. {
  56. // All is fine, there is enough space in the chunk
  57. handle.m_memory = crntChunk->m_mem;
  58. handle.m_offset = offset;
  59. retry = false;
  60. }
  61. else
  62. {
  63. // Need new chunk
  64. LockGuard<Mutex> lock(m_lock);
  65. // Make sure that only one thread will create a new chunk
  66. if(m_crntChunk.load() == crntChunk)
  67. {
  68. // We can create a new chunk
  69. if(crntChunk == nullptr || crntChunk->m_next == nullptr)
  70. {
  71. // Need to create a new chunk
  72. Chunk* newChunk = m_alloc.newInstance<Chunk>();
  73. if(crntChunk)
  74. {
  75. crntChunk->m_next = newChunk;
  76. newChunk->m_size = PtrSize(F32(crntChunk->m_size) * m_scale + F32(m_bias));
  77. }
  78. else
  79. {
  80. newChunk->m_size = m_initialSize;
  81. if(m_chunkListHead == nullptr)
  82. {
  83. m_chunkListHead = newChunk;
  84. }
  85. }
  86. alignRoundUp(m_alignment, newChunk->m_size);
  87. newChunk->m_next = nullptr;
  88. newChunk->m_offset.setNonAtomically(0);
  89. ANKI_CHECK(m_iface->allocate(newChunk->m_size, newChunk->m_mem));
  90. m_crntChunk.store(newChunk);
  91. }
  92. else
  93. {
  94. // Need to recycle one
  95. crntChunk->m_next->m_offset.setNonAtomically(0);
  96. m_crntChunk.store(crntChunk->m_next);
  97. }
  98. }
  99. }
  100. } while(retry);
  101. return Error::NONE;
  102. }
  103. void StackGpuAllocator::reset()
  104. {
  105. m_crntChunk.setNonAtomically(m_chunkListHead);
  106. if(m_chunkListHead)
  107. {
  108. m_chunkListHead->m_offset.setNonAtomically(0);
  109. }
  110. }
  111. } // end namespace anki