Browse Source

Minor changes in the buddy allocator

Panagiotis Christopoulos Charitos 4 years ago
parent
commit
c605802f26
3 changed files with 23 additions and 10 deletions
  1. 0 1
      AnKi/Renderer/RtShadows.cpp
  2. 11 3
      AnKi/Util/BuddyAllocator.h
  3. 12 6
      AnKi/Util/BuddyAllocator.inl.h

+ 0 - 1
AnKi/Renderer/RtShadows.cpp

@@ -11,7 +11,6 @@
 #include <AnKi/Renderer/MotionVectors.h>
 #include <AnKi/Renderer/MotionVectors.h>
 #include <AnKi/Renderer/DepthDownscale.h>
 #include <AnKi/Renderer/DepthDownscale.h>
 #include <AnKi/Renderer/RenderQueue.h>
 #include <AnKi/Renderer/RenderQueue.h>
-#include <AnKi/Resource/ShaderProgramResourceSystem.h>
 #include <AnKi/Util/Tracer.h>
 #include <AnKi/Util/Tracer.h>
 #include <AnKi/Core/ConfigSet.h>
 #include <AnKi/Core/ConfigSet.h>
 
 

+ 11 - 3
AnKi/Util/BuddyAllocator.h

@@ -23,7 +23,7 @@ public:
 
 
 	~BuddyAllocator()
 	~BuddyAllocator()
 	{
 	{
-		ANKI_ASSERT(m_allocationCount == 0 && "Forgot to deallocate");
+		ANKI_ASSERT(m_userAllocatedSize == 0 && "Forgot to deallocate");
 	}
 	}
 
 
 	/// Allocate memory.
 	/// Allocate memory.
@@ -42,7 +42,14 @@ public:
 	void free(TAllocator alloc, Address address, PtrSize size);
 	void free(TAllocator alloc, Address address, PtrSize size);
 
 
 	/// Print a debug representation of the internal structures.
 	/// Print a debug representation of the internal structures.
-	void debugPrint();
+	void debugPrint() const;
+
+	/// Get some info.
+	void getInfo(PtrSize& userAllocatedSize, PtrSize& realAllocatedSize) const
+	{
+		userAllocatedSize = m_userAllocatedSize;
+		realAllocatedSize = m_realAllocatedSize;
+	}
 
 
 private:
 private:
 	/// Because we need a constexpr version of pow.
 	/// Because we need a constexpr version of pow.
@@ -63,7 +70,8 @@ private:
 
 
 	using FreeList = DynamicArray<Address, PtrSize>;
 	using FreeList = DynamicArray<Address, PtrSize>;
 	Array<FreeList, ORDER_COUNT> m_freeLists;
 	Array<FreeList, ORDER_COUNT> m_freeLists;
-	U32 m_allocationCount = 0;
+	PtrSize m_userAllocatedSize = 0;
+	PtrSize m_realAllocatedSize = 0;
 
 
 	template<typename TAllocator>
 	template<typename TAllocator>
 	PtrSize popFree(TAllocator& alloc, U32 order)
 	PtrSize popFree(TAllocator& alloc, U32 order)

+ 12 - 6
AnKi/Util/BuddyAllocator.inl.h

@@ -14,7 +14,7 @@ Bool BuddyAllocator<T_MAX_MEMORY_RANGE_LOG2>::allocate(TAllocator alloc, PtrSize
 	ANKI_ASSERT(size > 0 && size <= MAX_MEMORY_RANGE);
 	ANKI_ASSERT(size > 0 && size <= MAX_MEMORY_RANGE);
 
 
 	// Lazy initialize
 	// Lazy initialize
-	if(m_allocationCount == 0)
+	if(m_userAllocatedSize == 0)
 	{
 	{
 		const Address startingAddress = 0;
 		const Address startingAddress = 0;
 		m_freeLists[ORDER_COUNT - 1].create(alloc, 1, startingAddress);
 		m_freeLists[ORDER_COUNT - 1].create(alloc, 1, startingAddress);
@@ -55,7 +55,8 @@ Bool BuddyAllocator<T_MAX_MEMORY_RANGE_LOG2>::allocate(TAllocator alloc, PtrSize
 	}
 	}
 
 
 	ANKI_ASSERT(address + alignedSize <= MAX_MEMORY_RANGE);
 	ANKI_ASSERT(address + alignedSize <= MAX_MEMORY_RANGE);
-	++m_allocationCount;
+	m_userAllocatedSize += size;
+	m_realAllocatedSize += alignedSize;
 	ANKI_ASSERT(address <= getMaxNumericLimit<Address>());
 	ANKI_ASSERT(address <= getMaxNumericLimit<Address>());
 	outAddress = Address(address);
 	outAddress = Address(address);
 	return true;
 	return true;
@@ -65,13 +66,18 @@ template<U32 T_MAX_MEMORY_RANGE_LOG2>
 template<typename TAllocator>
 template<typename TAllocator>
 void BuddyAllocator<T_MAX_MEMORY_RANGE_LOG2>::free(TAllocator alloc, Address address, PtrSize size)
 void BuddyAllocator<T_MAX_MEMORY_RANGE_LOG2>::free(TAllocator alloc, Address address, PtrSize size)
 {
 {
-	freeInternal(alloc, address, nextPowerOfTwo(size));
+	const PtrSize alignedSize = nextPowerOfTwo(size);
+	freeInternal(alloc, address, alignedSize);
 
 
-	--m_allocationCount;
+	ANKI_ASSERT(m_userAllocatedSize >= size);
+	m_userAllocatedSize -= size;
+	ANKI_ASSERT(m_realAllocatedSize >= alignedSize);
+	m_realAllocatedSize -= alignedSize;
 
 
 	// Some checks
 	// Some checks
-	if(m_allocationCount == 0)
+	if(m_userAllocatedSize == 0)
 	{
 	{
+		ANKI_ASSERT(m_realAllocatedSize == 0);
 		for(const FreeList& freeList : m_freeLists)
 		for(const FreeList& freeList : m_freeLists)
 		{
 		{
 			ANKI_ASSERT(freeList.getSize() == 0);
 			ANKI_ASSERT(freeList.getSize() == 0);
@@ -129,7 +135,7 @@ void BuddyAllocator<T_MAX_MEMORY_RANGE_LOG2>::freeInternal(TAllocator& alloc, Pt
 }
 }
 
 
 template<U32 T_MAX_MEMORY_RANGE_LOG2>
 template<U32 T_MAX_MEMORY_RANGE_LOG2>
-void BuddyAllocator<T_MAX_MEMORY_RANGE_LOG2>::debugPrint()
+void BuddyAllocator<T_MAX_MEMORY_RANGE_LOG2>::debugPrint() const
 {
 {
 	BitSet<MAX_MEMORY_RANGE>* freeBytes = new BitSet<MAX_MEMORY_RANGE>(false);
 	BitSet<MAX_MEMORY_RANGE>* freeBytes = new BitSet<MAX_MEMORY_RANGE>(false);
 	for(I32 order = ORDER_COUNT - 1; order >= 0; --order)
 	for(I32 order = ORDER_COUNT - 1; order >= 0; --order)