Browse Source

Add a limit to StackAllocatorBuilder

Panagiotis Christopoulos Charitos 9 months ago
parent
commit
0e06b78cb4

+ 5 - 0
AnKi/Gr/Utils/StackGpuMemoryPool.cpp

@@ -55,6 +55,11 @@ public:
 		return true;
 	}
 
+	static constexpr PtrSize getMaxChunkSize()
+	{
+		return 128_MB;
+	}
+
 	Error allocateChunk(PtrSize size, Chunk*& out)
 	{
 		if(!m_allowToGrow && m_chunkCount > 0)

+ 9 - 4
AnKi/Resource/TransferGpuAllocator.h

@@ -154,26 +154,31 @@ private:
 
 		// The rest of the functions implement the StackAllocatorBuilder TInterface.
 
-		constexpr PtrSize getInitialChunkSize() const
+		static constexpr PtrSize getInitialChunkSize()
 		{
 			return kChunkInitialSize;
 		}
 
-		constexpr F64 getNextChunkGrowScale() const
+		static constexpr F64 getNextChunkGrowScale()
 		{
 			return 1.0;
 		}
 
-		constexpr PtrSize getNextChunkGrowBias() const
+		static constexpr PtrSize getNextChunkGrowBias()
 		{
 			return 0;
 		}
 
-		constexpr Bool ignoreDeallocationErrors() const
+		static constexpr Bool ignoreDeallocationErrors()
 		{
 			return false;
 		}
 
+		static constexpr PtrSize getMaxChunkSize()
+		{
+			return kChunkInitialSize;
+		}
+
 		Error allocateChunk(PtrSize size, Chunk*& out);
 
 		void freeChunk(Chunk* chunk);

+ 5 - 0
AnKi/Util/MemoryPool.h

@@ -298,6 +298,11 @@ private:
 			return m_nextChunkBias;
 		}
 
+		static constexpr PtrSize getMaxChunkSize()
+		{
+			return 32_GB;
+		}
+
 		Bool ignoreDeallocationErrors() const
 		{
 			return m_ignoreDeallocationErrors;

+ 1 - 0
AnKi/Util/StackAllocatorBuilder.h

@@ -25,6 +25,7 @@ namespace anki {
 ///                    PtrSize getInitialChunkSize();
 ///                    F64 getNextChunkGrowScale();
 ///                    PtrSize getNextChunkGrowBias();
+///                    PtrSize getMaxChunkSize();
 ///                    Bool ignoreDeallocationErrors();
 ///                    Error allocateChunk(PtrSize size, TChunk*& out);
 ///                    void freeChunk(TChunk* chunk);

+ 10 - 0
AnKi/Util/StackAllocatorBuilder.inl.h

@@ -47,6 +47,12 @@ Error StackAllocatorBuilder<TChunk, TInterface, TLock>::allocate(PtrSize size, P
 	chunk = nullptr;
 	offset = kMaxPtrSize;
 
+	if(size > m_interface.getMaxChunkSize())
+	{
+		ANKI_UTIL_LOGE("Size requested is greated than the max chunk size");
+		return Error::kOutOfMemory;
+	}
+
 	while(true)
 	{
 		// Try to allocate from the current chunk, if there is one
@@ -98,6 +104,10 @@ Error StackAllocatorBuilder<TChunk, TInterface, TLock>::allocate(PtrSize size, P
 
 			nextChunkSize = max(size, nextChunkSize); // Can't have the allocation fail
 
+			nextChunkSize = min(nextChunkSize, m_interface.getMaxChunkSize());
+
+			ANKI_ASSERT(size <= nextChunkSize); // Should have caught this before
+
 			TChunk* nextChunk;
 			if(crntChunk)
 			{