ObjectAllocator.inl.h 3.3 KB

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