Panagiotis Christopoulos Charitos 12 лет назад
Родитель
Сommit
842d4ba9af
4 измененных файлов с 58 добавлено и 27 удалено
  1. 7 4
      include/anki/util/Functions.h
  2. 34 3
      include/anki/util/Memory.h
  3. 6 6
      src/renderer/Is.cpp
  4. 11 14
      src/util/Memory.cpp

+ 7 - 4
include/anki/util/Functions.h

@@ -29,12 +29,15 @@ extern F64 randRange(F64 min, F64 max);
 
 extern F32 randFloat(F32 max);
 
-/// Align memory
+/// Get align number
 /// @param alignment The bytes of alignment
-/// @param size The value to align
-inline PtrSize alignSizeRoundUp(PtrSize alignment, PtrSize size)
+/// @param value The value to align
+template<typename Type>
+Type getAlignedRoundUp(PtrSize alignment, Type value)
 {
-	return (size + alignment - 1) & ~(alignment - 1);
+	PtrSize v = (PtrSize)value;
+	v = (v + alignment - 1) & ~(alignment - 1);
+	return (Type)v;
 }
 
 /// Get the size in bytes of a vector

+ 34 - 3
include/anki/util/Memory.h

@@ -23,6 +23,8 @@ class Allocator;
 class StackMemoryPool: public NonCopyable
 {
 public:
+
+	/// Safe alignment in bytes
 	static const U SAFE_ALIGNMENT = 
 #if ANKI_CPU_ARCH == ANKI_CPU_ARCH_INTEL
 		16
@@ -83,11 +85,40 @@ private:
 
 	/// Points to the memory and more specifically to the top of the stack
 	std::atomic<U8*> top = {nullptr};
-
-	/// Calculate tha aligned size of an allocation
-	PtrSize calcAlignSize(PtrSize size) const;
 };
 
+/// XXX
+template<typename Type, typename Alloc = Allocator<Type>, typename... Args>
+Type* newObject(Alloc alloc, Args&&... args)
+{
+	typename Alloc::template rebind<Type>::other allocc(alloc);
+
+	Type* x = allocc.allocate(1);
+	allocc.construct(x, std::forward<Args>(args)...);
+	return x;
+}
+
+/// XXX
+template<typename Type, typename Alloc = Allocator<Type>, typename... Args>
+Type* newArray(PtrSize n, Alloc alloc, Args&&... args)
+{
+	typename Alloc::template rebind<Type>::other allocc(alloc);
+
+	Type* x = allocc.allocate(n);
+	allocc.construct(x, std::forward<Args>(args)...);
+	return x;
+}
+
+/// XXX
+template<typename Type, typename Alloc = Allocator<Type>>
+void deleteObject(Alloc alloc, Type* x)
+{
+	typename Alloc::template rebind<Type>::other allocc(alloc);
+
+	allocc.destroy(x);
+	allocc.deallocate(x, 1);
+}
+
 /// Functior that imitates the new operator. The functior allocates memory for
 /// a number of elements and calls their constructor. The interesting thing is
 /// that if the elements size is >1 then it allocates size bigger than the

+ 6 - 6
src/renderer/Is.cpp

@@ -681,17 +681,17 @@ void Is::lightPass()
 	PtrSize pointLightsOffset = 0;
 	PtrSize pointLightsSize = 
 		sizeof(shader::PointLight) * visiblePointLightsCount;
-	pointLightsSize = alignSizeRoundUp(uboAlignment, pointLightsSize);
+	pointLightsSize = getAlignedRoundUp(uboAlignment, pointLightsSize);
 
 	PtrSize spotLightsOffset = pointLightsSize;
 	PtrSize spotLightsSize = 
 		sizeof(shader::SpotLight) * visibleSpotLightsCount;
-	spotLightsSize = alignSizeRoundUp(uboAlignment, spotLightsSize);
+	spotLightsSize = getAlignedRoundUp(uboAlignment, spotLightsSize);
 
 	PtrSize spotTexLightsOffset = spotLightsOffset + spotLightsSize;
 	PtrSize spotTexLightsSize = 
 		sizeof(shader::SpotTexLight) * visibleSpotTexLightsCount;
-	spotTexLightsSize = alignSizeRoundUp(uboAlignment, spotTexLightsSize);
+	spotTexLightsSize = getAlignedRoundUp(uboAlignment, spotTexLightsSize);
 
 	ANKI_ASSERT(spotTexLightsOffset + spotTexLightsSize <= calcLightsUboSize());
 
@@ -944,15 +944,15 @@ PtrSize Is::calcLightsUboSize() const
 	PtrSize size;
 	PtrSize uboAlignment = BufferObject::getUniformBufferOffsetAlignment();
 
-	size = alignSizeRoundUp(
+	size = getAlignedRoundUp(
 		uboAlignment,
 		maxPointLights * sizeof(shader::PointLight));
 
-	size += alignSizeRoundUp(
+	size += getAlignedRoundUp(
 		uboAlignment,
 		maxSpotLights * sizeof(shader::SpotLight));
 
-	size += alignSizeRoundUp(
+	size += getAlignedRoundUp(
 		uboAlignment,
 		maxSpotTexLights * sizeof(shader::SpotTexLight));
 

+ 11 - 14
src/util/Memory.cpp

@@ -1,7 +1,6 @@
 #include "anki/util/Memory.h"
 #include "anki/util/Assert.h"
 #include "anki/util/Exception.h"
-#include "anki/util/Functions.h"
 #include <limits>
 #include <cstdlib>
 #include <cstring>
@@ -16,14 +15,16 @@ struct MemoryBlockHeader
 
 //==============================================================================
 StackMemoryPool::StackMemoryPool(PtrSize size, U32 alignmentBytes_)
-	: alignmentBytes(alignmentBytes_), memsize(calcAlignSize(size))
+	:	alignmentBytes(alignmentBytes_), 
+		memsize(getAlignedRoundUp(alignmentBytes, size))
 {
 	ANKI_ASSERT(memsize > 0);
 	memory = (U8*)::malloc(memsize);
 
 	if(memory != nullptr)
 	{
-		top = (U8*)calcAlignSize((PtrSize)memory);
+		// Align allocated memory
+		top = getAlignedRoundUp(alignmentBytes, memory);
 	}
 	else
 	{
@@ -50,8 +51,8 @@ StackMemoryPool& StackMemoryPool::operator=(StackMemoryPool&& other)
 
 	memory = other.memory;
 	memsize = other.memsize;
-	top.store(other.top.load());
 	alignmentBytes = other.alignmentBytes;
+	top.store(other.top.load());
 
 	other.memory = nullptr;
 	other.memsize = 0;
@@ -72,9 +73,10 @@ void* StackMemoryPool::allocate(PtrSize size_) throw()
 	// memory is nullptr if moved
 	ANKI_ASSERT(memory != nullptr);
 
-	PtrSize memBlockSize = calcAlignSize(sizeof(MemoryBlockHeader));
+	PtrSize memBlockSize = 
+		getAlignedRoundUp(alignmentBytes, sizeof(MemoryBlockHeader));
 	PtrSize size = 
-		calcAlignSize(size_ + memBlockSize);
+		getAlignedRoundUp(alignmentBytes, size_ + memBlockSize);
 
 	ANKI_ASSERT(size < std::numeric_limits<U32>::max() && "Too big allocation");
 
@@ -104,7 +106,8 @@ Bool StackMemoryPool::free(void* ptr) throw()
 	ANKI_ASSERT(memory != nullptr);
 
 	// Correct the p
-	PtrSize memBlockSize = calcAlignSize(sizeof(MemoryBlockHeader));
+	PtrSize memBlockSize = 
+		getAlignedRoundUp(alignmentBytes, sizeof(MemoryBlockHeader));
 	U8* realptr = (U8*)ptr - memBlockSize;
 
 	// realptr should be inside the pool's preallocated memory
@@ -140,13 +143,7 @@ void StackMemoryPool::reset()
 	memset(memory, 0xCC, memsize);
 #endif
 
-	top = (U8*)calcAlignSize((PtrSize)memory);
-}
-
-//==============================================================================
-PtrSize StackMemoryPool::calcAlignSize(PtrSize size) const
-{
-	return alignSizeRoundUp(alignmentBytes, size);
+	top = getAlignedRoundUp(alignmentBytes, memory);
 }
 
 } // end namespace anki