ClassGpuAllocator.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/Common.h>
  7. namespace anki
  8. {
  9. // Forward
  10. class ClassGpuAllocatorChunk;
  11. class ClassGpuAllocatorClass;
  12. /// @addtogroup graphics
  13. /// @{
  14. /// The user defined output of an allocation.
  15. class ClassGpuAllocatorMemory
  16. {
  17. };
  18. /// The user defined methods to allocate memory.
  19. class ClassGpuAllocatorInterface
  20. {
  21. public:
  22. virtual ~ClassGpuAllocatorInterface()
  23. {
  24. }
  25. /// Allocate memory. Should be thread safe.
  26. virtual ANKI_USE_RESULT Error allocate(U32 classIdx, ClassGpuAllocatorMemory*& mem) = 0;
  27. /// Free memory. Should be thread safe.
  28. virtual void free(ClassGpuAllocatorMemory* mem) = 0;
  29. /// Get the number of classes.
  30. virtual U32 getClassCount() const = 0;
  31. /// Get info for a class. Each chunk will be chunkSize size and it can host chunkSize/slotSize sub-allocations in
  32. /// it.
  33. virtual void getClassInfo(U32 classIdx, PtrSize& slotSize, PtrSize& chunkSize) const = 0;
  34. };
  35. /// The output of an allocation.
  36. class ClassGpuAllocatorHandle
  37. {
  38. friend class ClassGpuAllocator;
  39. public:
  40. ClassGpuAllocatorMemory* m_memory = nullptr;
  41. PtrSize m_offset = 0; ///< Relative offset inside m_memory
  42. explicit operator Bool() const
  43. {
  44. return m_memory != nullptr;
  45. }
  46. private:
  47. ClassGpuAllocatorChunk* m_chunk = nullptr;
  48. Bool valid() const
  49. {
  50. return (m_memory && m_chunk) || (m_memory == nullptr && m_chunk == nullptr);
  51. }
  52. };
  53. /// Class based allocator.
  54. class ClassGpuAllocator
  55. {
  56. public:
  57. ClassGpuAllocator()
  58. {
  59. }
  60. ClassGpuAllocator(const ClassGpuAllocator&) = delete; // Non-copyable
  61. ~ClassGpuAllocator();
  62. ClassGpuAllocator& operator=(const ClassGpuAllocator&) = delete; // Non-copyable
  63. void init(GenericMemoryPoolAllocator<U8> alloc, ClassGpuAllocatorInterface* iface);
  64. /// Allocate memory.
  65. ANKI_USE_RESULT Error allocate(PtrSize size, U alignment, ClassGpuAllocatorHandle& handle);
  66. /// Free allocated memory.
  67. void free(ClassGpuAllocatorHandle& handle);
  68. PtrSize getAllocatedMemory() const
  69. {
  70. return m_allocatedMem;
  71. }
  72. private:
  73. using Class = ClassGpuAllocatorClass;
  74. using Chunk = ClassGpuAllocatorChunk;
  75. GenericMemoryPoolAllocator<U8> m_alloc;
  76. ClassGpuAllocatorInterface* m_iface = nullptr;
  77. /// The memory classes.
  78. DynamicArray<Class> m_classes;
  79. PtrSize m_allocatedMem = 0; ///< An estimate.
  80. Class* findClass(PtrSize size, U alignment);
  81. Chunk* findChunkWithUnusedSlot(Class& cl);
  82. /// Create a new chunk.
  83. ANKI_USE_RESULT Error createChunk(Class& cl, Chunk*& chunk);
  84. /// Destroy a chunk.
  85. void destroyChunk(Class& cl, Chunk& chunk);
  86. };
  87. /// @}
  88. } // end namespace anki