ObjectAllocator.inl.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/Util/ObjectAllocator.h>
  6. namespace anki
  7. {
  8. template<PtrSize T_OBJECT_SIZE, U32 T_OBJECT_ALIGNMENT, U32 T_OBJECTS_PER_CHUNK, typename TIndexType>
  9. template<typename T, typename TAlloc, typename... TArgs>
  10. T* ObjectAllocator<T_OBJECT_SIZE, T_OBJECT_ALIGNMENT, T_OBJECTS_PER_CHUNK, TIndexType>::newInstance(TAlloc& alloc,
  11. TArgs&&... args)
  12. {
  13. static_assert(alignof(T) <= OBJECT_ALIGNMENT, "Wrong object alignment");
  14. static_assert(sizeof(T) <= OBJECT_SIZE, "Wrong object size");
  15. T* out = nullptr;
  16. // Try find one in the chunks
  17. Chunk* chunk = m_chunksHead;
  18. while(chunk)
  19. {
  20. if(chunk->m_unusedCount > 0)
  21. {
  22. // Pop an element
  23. --chunk->m_unusedCount;
  24. out = reinterpret_cast<T*>(&chunk->m_objects[chunk->m_unusedStack[chunk->m_unusedCount]]);
  25. break;
  26. }
  27. chunk = chunk->m_next;
  28. }
  29. if(out == nullptr)
  30. {
  31. // Need to create a new chunk
  32. // Create the chunk
  33. chunk = alloc.template newInstance<Chunk>();
  34. chunk->m_unusedCount = OBJECTS_PER_CHUNK;
  35. for(U32 i = 0; i < OBJECTS_PER_CHUNK; ++i)
  36. {
  37. chunk->m_unusedStack[i] = OBJECTS_PER_CHUNK - (i + 1);
  38. }
  39. if(m_chunksTail)
  40. {
  41. ANKI_ASSERT(m_chunksHead);
  42. chunk->m_prev = m_chunksTail;
  43. m_chunksTail->m_next = chunk;
  44. m_chunksTail = chunk;
  45. }
  46. else
  47. {
  48. m_chunksTail = m_chunksHead = chunk;
  49. }
  50. // Allocate one object
  51. out = reinterpret_cast<T*>(&chunk->m_objects[0]);
  52. --chunk->m_unusedCount;
  53. }
  54. ANKI_ASSERT(out);
  55. // Construct it
  56. alloc.construct(out, std::forward<TArgs>(args)...);
  57. return out;
  58. }
  59. template<PtrSize T_OBJECT_SIZE, U32 T_OBJECT_ALIGNMENT, U32 T_OBJECTS_PER_CHUNK, typename TIndexType>
  60. template<typename T, typename TAlloc>
  61. void ObjectAllocator<T_OBJECT_SIZE, T_OBJECT_ALIGNMENT, T_OBJECTS_PER_CHUNK, TIndexType>::deleteInstance(TAlloc& alloc,
  62. T* obj)
  63. {
  64. static_assert(alignof(T) <= OBJECT_ALIGNMENT, "Wrong object alignment");
  65. static_assert(sizeof(T) <= OBJECT_SIZE, "Wrong object size");
  66. ANKI_ASSERT(obj);
  67. // Find the chunk the obj is in
  68. const Object* const mem = reinterpret_cast<Object*>(obj);
  69. Chunk* chunk = m_chunksHead;
  70. while(chunk)
  71. {
  72. const Object* const begin = chunk->m_objects.getBegin();
  73. const Object* const end = chunk->m_objects.getEnd();
  74. if(mem >= begin && mem < end)
  75. {
  76. // Found it, remove it from the chunk and maybe delete the chunk
  77. ANKI_ASSERT(chunk->m_unusedCount < OBJECTS_PER_CHUNK);
  78. const U32 idx = U32(mem - begin);
  79. // Destroy the object
  80. obj->~T();
  81. // Remove from the chunk
  82. chunk->m_unusedStack[chunk->m_unusedCount] = idx;
  83. ++chunk->m_unusedCount;
  84. // Delete the chunk if it's empty
  85. if(chunk->m_unusedCount == OBJECTS_PER_CHUNK)
  86. {
  87. if(chunk == m_chunksTail)
  88. {
  89. m_chunksTail = chunk->m_prev;
  90. }
  91. if(chunk == m_chunksHead)
  92. {
  93. m_chunksHead = chunk->m_next;
  94. }
  95. if(chunk->m_prev)
  96. {
  97. ANKI_ASSERT(chunk->m_prev->m_next == chunk);
  98. chunk->m_prev->m_next = chunk->m_next;
  99. }
  100. if(chunk->m_next)
  101. {
  102. ANKI_ASSERT(chunk->m_next->m_prev == chunk);
  103. chunk->m_next->m_prev = chunk->m_prev;
  104. }
  105. alloc.deleteInstance(chunk);
  106. }
  107. break;
  108. }
  109. chunk = chunk->m_next;
  110. }
  111. ANKI_ASSERT(chunk != nullptr);
  112. }
  113. } // end namespace anki