ClassAllocatorBuilder.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (C) 2009-present, 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/Util/List.h>
  7. #include <AnKi/Util/DynamicArray.h>
  8. namespace anki {
  9. /// @addtogroup util_memory
  10. /// @{
  11. /// @memberof ClassAllocatorBuilder
  12. class ClassAllocatorBuilderStats
  13. {
  14. public:
  15. PtrSize m_allocatedSize;
  16. PtrSize m_inUseSize;
  17. U32 m_chunkCount; ///< Can be assosiated with the number of allocations.
  18. };
  19. /// This is a convenience class used to build class memory allocators.
  20. /// @tparam TChunk This is the type of the internally allocated chunks. This should be having the following members:
  21. /// @code
  22. /// BitSet<SOME_UPPER_LIMIT> m_inUseSuballocations
  23. /// U32 m_suballocationCount;
  24. /// void* m_class;
  25. /// @endcode
  26. /// And should inherit from IntrusiveListEnabled<TChunk>
  27. /// @tparam TInterface This is the type of the interface that contains various info. Should have the following members:
  28. /// @code
  29. /// U32 getClassCount();
  30. /// void getClassInfo(U32 classIdx, PtrSize& chunkSize, PtrSize& suballocationSize) const;
  31. /// Error allocateChunk(U32 classIdx, Chunk*& chunk);
  32. /// void freeChunk(TChunk* out);
  33. /// @endcode
  34. /// @tparam TLock This an optional lock. Can be a Mutex or SpinLock or some dummy class.
  35. /// @tparam TMemoryPool The memory pool used in internal allocations.
  36. template<typename TChunk, typename TInterface, typename TLock, typename TMemoryPool = SingletonMemoryPoolWrapper<DefaultMemoryPool>>
  37. class ClassAllocatorBuilder
  38. {
  39. public:
  40. /// Create.
  41. ClassAllocatorBuilder(const TMemoryPool& pool = TMemoryPool())
  42. : m_classes(pool)
  43. {
  44. }
  45. ClassAllocatorBuilder(const ClassAllocatorBuilder&) = delete; // Non-copyable
  46. /// Calls @a destroy().
  47. ~ClassAllocatorBuilder()
  48. {
  49. destroy();
  50. }
  51. ClassAllocatorBuilder& operator=(const ClassAllocatorBuilder&) = delete; // Non-copyable
  52. /// Initialize it. Feel free to feedle with the TInterface before you do that.
  53. void init();
  54. /// Destroy the allocator builder.
  55. void destroy();
  56. /// Allocate memory.
  57. /// @param size The size to allocate.
  58. /// @param alignment The alignment of the returned address.
  59. /// @param[out] chunk The chunk that the memory belongs to.
  60. /// @param[out] offset The offset inside the chunk.
  61. /// @note This is thread safe.
  62. Error allocate(PtrSize size, PtrSize alignment, TChunk*& chunk, PtrSize& offset);
  63. /// Free memory.
  64. /// @param chunk The chunk the allocation belongs to.
  65. /// @param offset The memory offset inside the chunk.
  66. void free(TChunk* chunk, PtrSize offset);
  67. /// Access the interface.
  68. /// @note Not thread safe. Don't call it while calling allocate or free.
  69. TInterface& getInterface()
  70. {
  71. return m_interface;
  72. }
  73. /// Access the interface.
  74. /// @note Not thread safe. Don't call it while calling allocate or free.
  75. const TInterface& getInterface() const
  76. {
  77. return m_interface;
  78. }
  79. /// Get some statistics.
  80. /// @note It's thread-safe because it will lock. Don't overuse it.
  81. void getStats(ClassAllocatorBuilderStats& stats) const;
  82. private:
  83. /// A class of allocations. It's a list of memory chunks. Each chunk is dividied in suballocations.
  84. class Class
  85. {
  86. public:
  87. /// The active chunks.
  88. IntrusiveList<TChunk> m_chunkList;
  89. /// The size of each chunk.
  90. PtrSize m_chunkSize = 0;
  91. /// The max size a suballocation can have.
  92. PtrSize m_suballocationSize = 0;
  93. /// Lock.
  94. mutable TLock m_mtx;
  95. };
  96. /// The interface as decribed in the class docs.
  97. TInterface m_interface;
  98. /// All the classes.
  99. DynamicArray<Class, TMemoryPool> m_classes;
  100. Class* findClass(PtrSize size, PtrSize alignment);
  101. Bool isInitialized() const
  102. {
  103. return m_classes[0].m_chunkSize != 0;
  104. }
  105. };
  106. /// @}
  107. } // end namespace anki
  108. #include <AnKi/Util/ClassAllocatorBuilder.inl.h>