Browse Source

Remove the allocator from most Util classes #2

Panagiotis Christopoulos Charitos 3 years ago
parent
commit
96053e8911

+ 1 - 1
AnKi/Gr/Vulkan/ShaderProgramImpl.cpp

@@ -39,7 +39,7 @@ Error ShaderProgramImpl::init(const ShaderProgramInitInfo& inf)
 
 	// Create the shader references
 	//
-	HashMapAuto<U64, U32> shaderUuidToMShadersIdx(getAllocator()); // Shader UUID to m_shaders idx
+	HashMapRaii<U64, U32> shaderUuidToMShadersIdx(getAllocator()); // Shader UUID to m_shaders idx
 	if(inf.m_computeShader)
 	{
 		m_shaders.emplaceBack(getAllocator(), inf.m_computeShader);

+ 15 - 15
AnKi/Importer/GltfImporter.cpp

@@ -136,7 +136,7 @@ static Error getNodeTransform(const cgltf_node& node, Transform& trf)
 	return Error::kNone;
 }
 
-static Bool stringsExist(const HashMapAuto<CString, StringRaii>& map, const std::initializer_list<CString>& list)
+static Bool stringsExist(const HashMapRaii<CString, StringRaii>& map, const std::initializer_list<CString>& list)
 {
 	for(CString item : list)
 	{
@@ -233,7 +233,7 @@ Error GltfImporter::writeAll()
 	{
 		for(cgltf_node* const* node = scene->nodes; node < scene->nodes + scene->nodes_count && !err; ++node)
 		{
-			err = visitNode(*(*node), Transform::getIdentity(), HashMapAuto<CString, StringRaii>(m_alloc));
+			err = visitNode(*(*node), Transform::getIdentity(), HashMapRaii<CString, StringRaii>(m_alloc));
 		}
 	}
 
@@ -264,7 +264,7 @@ Error GltfImporter::writeAll()
 	return err;
 }
 
-Error GltfImporter::getExtras(const cgltf_extras& extras, HashMapAuto<CString, StringRaii>& out)
+Error GltfImporter::getExtras(const cgltf_extras& extras, HashMapRaii<CString, StringRaii>& out)
 {
 	cgltf_size extrasSize;
 	cgltf_copy_extras_json(m_gltf, &extras, nullptr, &extrasSize);
@@ -408,7 +408,7 @@ Error GltfImporter::parseArrayOfNumbers(CString str, DynamicArrayRaii<F64>& out,
 }
 
 Error GltfImporter::visitNode(const cgltf_node& node, const Transform& parentTrf,
-							  const HashMapAuto<CString, StringRaii>& parentExtras)
+							  const HashMapRaii<CString, StringRaii>& parentExtras)
 {
 	// Check error from a thread
 	const Error threadErr = m_errorInThread.load();
@@ -418,7 +418,7 @@ Error GltfImporter::visitNode(const cgltf_node& node, const Transform& parentTrf
 		return threadErr;
 	}
 
-	HashMapAuto<CString, StringRaii> outExtras(m_alloc);
+	HashMapRaii<CString, StringRaii> outExtras(m_alloc);
 	if(node.light)
 	{
 		ANKI_CHECK(writeLight(node, parentExtras));
@@ -440,11 +440,11 @@ Error GltfImporter::visitNode(const cgltf_node& node, const Transform& parentTrf
 	else if(node.mesh)
 	{
 		// Handle special nodes
-		HashMapAuto<CString, StringRaii> extras(parentExtras);
+		HashMapRaii<CString, StringRaii> extras(parentExtras);
 		ANKI_CHECK(getExtras(node.mesh->extras, extras));
 		ANKI_CHECK(getExtras(node.extras, extras));
 
-		HashMapAuto<CString, StringRaii>::Iterator it;
+		HashMapRaii<CString, StringRaii>::Iterator it;
 
 		const Bool skipRt = (it = extras.find("no_rt")) != extras.getEnd() && (*it == "true" || *it == "1");
 
@@ -698,7 +698,7 @@ Error GltfImporter::visitNode(const cgltf_node& node, const Transform& parentTrf
 			ctx->m_skin = node.skin;
 			ctx->m_rayTracing = !skipRt;
 
-			HashMapAuto<CString, StringRaii>::Iterator it2;
+			HashMapRaii<CString, StringRaii>::Iterator it2;
 			const Bool selfCollision = (it2 = extras.find("collision_mesh")) != extras.getEnd() && *it2 == "self";
 
 			U32 maxLod = 0;
@@ -838,7 +838,7 @@ Error GltfImporter::writeModel(const cgltf_mesh& mesh)
 	const StringRaii modelFname = computeModelResourceFilename(mesh);
 	ANKI_IMPORTER_LOGV("Importing model %s", modelFname.cstr());
 
-	HashMapAuto<CString, StringRaii> extras(m_alloc);
+	HashMapRaii<CString, StringRaii> extras(m_alloc);
 	ANKI_CHECK(getExtras(mesh.extras, extras));
 
 	File file;
@@ -877,7 +877,7 @@ Error GltfImporter::writeModel(const cgltf_mesh& mesh)
 			ANKI_CHECK(file.writeTextf("\t\t\t<mesh2>%s%s</mesh2>\n", m_rpath.cstr(), meshFname.cstr()));
 		}
 
-		HashMapAuto<CString, StringRaii> materialExtras(m_alloc);
+		HashMapRaii<CString, StringRaii> materialExtras(m_alloc);
 		ANKI_CHECK(getExtras(mesh.primitives[primIdx].material->extras, materialExtras));
 		auto mtlOverride = materialExtras.find("material_override");
 		if(mtlOverride != materialExtras.getEnd())
@@ -965,13 +965,13 @@ Error GltfImporter::writeSkeleton(const cgltf_skin& skin)
 	return Error::kNone;
 }
 
-Error GltfImporter::writeLight(const cgltf_node& node, const HashMapAuto<CString, StringRaii>& parentExtras)
+Error GltfImporter::writeLight(const cgltf_node& node, const HashMapRaii<CString, StringRaii>& parentExtras)
 {
 	const cgltf_light& light = *node.light;
 	StringRaii nodeName = getNodeName(node);
 	ANKI_IMPORTER_LOGV("Importing light %s", nodeName.cstr());
 
-	HashMapAuto<CString, StringRaii> extras(parentExtras);
+	HashMapRaii<CString, StringRaii> extras(parentExtras);
 	ANKI_CHECK(getExtras(light.extras, extras));
 	ANKI_CHECK(getExtras(node.extras, extras));
 
@@ -1105,7 +1105,7 @@ Error GltfImporter::writeLight(const cgltf_node& node, const HashMapAuto<CString
 }
 
 Error GltfImporter::writeCamera(const cgltf_node& node,
-								[[maybe_unused]] const HashMapAuto<CString, StringRaii>& parentExtras)
+								[[maybe_unused]] const HashMapRaii<CString, StringRaii>& parentExtras)
 {
 	if(node.camera->type != cgltf_camera_type_perspective)
 	{
@@ -1128,11 +1128,11 @@ Error GltfImporter::writeCamera(const cgltf_node& node,
 	return Error::kNone;
 }
 
-Error GltfImporter::writeModelNode(const cgltf_node& node, const HashMapAuto<CString, StringRaii>& parentExtras)
+Error GltfImporter::writeModelNode(const cgltf_node& node, const HashMapRaii<CString, StringRaii>& parentExtras)
 {
 	ANKI_IMPORTER_LOGV("Importing model node %s", getNodeName(node).cstr());
 
-	HashMapAuto<CString, StringRaii> extras(parentExtras);
+	HashMapRaii<CString, StringRaii> extras(parentExtras);
 	ANKI_CHECK(getExtras(node.extras, extras));
 
 	const StringRaii modelFname = computeModelResourceFilename(*node.mesh);

+ 6 - 6
AnKi/Importer/GltfImporter.h

@@ -76,7 +76,7 @@ private:
 
 	Atomic<I32> m_errorInThread{0};
 
-	HashMapAuto<const void*, U32, PtrHasher> m_nodePtrToIdx{m_alloc}; ///< Need an index for the unnamed nodes.
+	HashMapRaii<const void*, U32, PtrHasher> m_nodePtrToIdx{m_alloc}; ///< Need an index for the unnamed nodes.
 
 	F32 m_lodFactor = 1.0f;
 	U32 m_lodCount = 1;
@@ -89,7 +89,7 @@ private:
 	U32 m_skipLodVertexCountThreshold = 256;
 
 	// Misc
-	Error getExtras(const cgltf_extras& extras, HashMapAuto<CString, StringRaii>& out);
+	Error getExtras(const cgltf_extras& extras, HashMapRaii<CString, StringRaii>& out);
 	Error parseArrayOfNumbers(CString str, DynamicArrayRaii<F64>& out, const U32* expectedArraySize = nullptr);
 	void populateNodePtrToIdx();
 	void populateNodePtrToIdxInternal(const cgltf_node& node, U32& idx);
@@ -144,10 +144,10 @@ private:
 	// Scene
 	Error writeTransform(const Transform& trf);
 	Error visitNode(const cgltf_node& node, const Transform& parentTrf,
-					const HashMapAuto<CString, StringRaii>& parentExtras);
-	Error writeLight(const cgltf_node& node, const HashMapAuto<CString, StringRaii>& parentExtras);
-	Error writeCamera(const cgltf_node& node, const HashMapAuto<CString, StringRaii>& parentExtras);
-	Error writeModelNode(const cgltf_node& node, const HashMapAuto<CString, StringRaii>& parentExtras);
+					const HashMapRaii<CString, StringRaii>& parentExtras);
+	Error writeLight(const cgltf_node& node, const HashMapRaii<CString, StringRaii>& parentExtras);
+	Error writeCamera(const cgltf_node& node, const HashMapRaii<CString, StringRaii>& parentExtras);
+	Error writeModelNode(const cgltf_node& node, const HashMapRaii<CString, StringRaii>& parentExtras);
 };
 /// @}
 

+ 1 - 1
AnKi/Importer/GltfImporterAnimation.cpp

@@ -113,7 +113,7 @@ Error GltfImporter::writeAnimation(const cgltf_animation& anim)
 	ANKI_IMPORTER_LOGV("Importing animation %s", fname.cstr());
 
 	// Gather the channels
-	HashMapAuto<CString, Array<const cgltf_animation_channel*, 3>> channelMap(m_alloc);
+	HashMapRaii<CString, Array<const cgltf_animation_channel*, 3>> channelMap(m_alloc);
 	U32 channelCount = 0;
 	for(U i = 0; i < anim.channels_count; ++i)
 	{

+ 1 - 1
AnKi/Importer/GltfImporterMaterial.cpp

@@ -110,7 +110,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, Bool writeRayTracin
 		return Error::kUserData;
 	}
 
-	HashMapAuto<CString, StringRaii> extras(m_alloc);
+	HashMapRaii<CString, StringRaii> extras(m_alloc);
 	ANKI_CHECK(getExtras(mtl.extras, extras));
 
 	StringRaii xml(m_alloc);

+ 1 - 1
AnKi/Importer/GltfImporterMesh.cpp

@@ -234,7 +234,7 @@ static void decimateSubmesh(F32 factor, SubMesh& submesh, GenericMemoryPoolAlloc
 	// Re-pack
 	DynamicArrayRaii<U32> reindexedIndices(alloc);
 	DynamicArrayRaii<TempVertex> newVerts(alloc);
-	HashMapAuto<U32, U32> vertexStored(alloc);
+	HashMapRaii<U32, U32> vertexStored(alloc);
 	for(U32 idx = 0; idx < newIndices.getSize(); ++idx)
 	{
 		U32 newIdx;

+ 1 - 1
AnKi/ShaderCompiler/ShaderProgramCompiler.cpp

@@ -1066,7 +1066,7 @@ Error compileShaderProgramInternal(CString fname, ShaderProgramFilesystemInterfa
 		DynamicArrayRaii<ShaderProgramBinaryCodeBlock> codeBlocks(binaryAllocator);
 		DynamicArrayRaii<ShaderProgramBinaryMutation> mutations(binaryAllocator, mutationCount);
 		DynamicArrayRaii<U64> codeBlockHashes(tempAllocator);
-		HashMapAuto<U64, U32> mutationHashToIdx(tempAllocator);
+		HashMapRaii<U64, U32> mutationHashToIdx(tempAllocator);
 
 		// Grow the storage of the variants array. Can't have it resize, threads will work on stale data
 		variants.resizeStorage(mutationCount);

+ 3 - 0
AnKi/Util/Forward.h

@@ -20,6 +20,9 @@ class BitMask;
 template<typename, typename, typename, typename>
 class HashMap;
 
+template<typename, typename, typename, typename, typename>
+class HashMapRaii;
+
 template<typename T>
 class Hierarchy;
 

+ 30 - 28
AnKi/Util/HashMap.h

@@ -5,7 +5,7 @@
 
 #pragma once
 
-#include <AnKi/Util/Allocator.h>
+#include <AnKi/Util/CpuMemoryPools.h>
 #include <AnKi/Util/Functions.h>
 #include <AnKi/Util/SparseArray.h>
 
@@ -182,25 +182,25 @@ public:
 	}
 
 	/// Destroy the list.
-	template<typename TAllocator>
-	void destroy(TAllocator alloc)
+	template<typename TMemPool>
+	void destroy(TMemPool& pool)
 	{
-		m_sparseArr.destroy(alloc);
+		m_sparseArr.destroy(pool);
 	}
 
 	/// Construct an element inside the map.
-	template<typename TAllocator, typename... TArgs>
-	Iterator emplace(TAllocator alloc, const TKey& key, TArgs&&... args)
+	template<typename TMemPool, typename... TArgs>
+	Iterator emplace(TMemPool& pool, const TKey& key, TArgs&&... args)
 	{
 		const U64 hash = THasher()(key);
-		return m_sparseArr.emplace(alloc, hash, std::forward<TArgs>(args)...);
+		return m_sparseArr.emplace(pool, hash, std::forward<TArgs>(args)...);
 	}
 
 	/// Erase element.
-	template<typename TAllocator>
-	void erase(TAllocator alloc, Iterator it)
+	template<typename TMemPool>
+	void erase(TMemPool& pool, Iterator it)
 	{
-		m_sparseArr.erase(alloc, it);
+		m_sparseArr.erase(pool, it);
 	}
 
 	/// Find a value using a key.
@@ -223,47 +223,49 @@ protected:
 
 /// Hash map template with automatic cleanup.
 template<typename TKey, typename TValue, typename THasher = DefaultHasher<TKey>,
-		 typename TSparseArrayConfig = HashMapSparseArrayConfig>
-class HashMapAuto : public HashMap<TKey, TValue, THasher, TSparseArrayConfig>
+		 typename TSparseArrayConfig = HashMapSparseArrayConfig,
+		 typename TMemPool = MemoryPoolPtrWrapper<BaseMemoryPool>>
+class HashMapRaii : public HashMap<TKey, TValue, THasher, TSparseArrayConfig>
 {
 public:
 	using Base = HashMap<TKey, TValue, THasher, TSparseArrayConfig>;
+	using MemoryPool = TMemPool;
 
 	/// Default constructor.
-	HashMapAuto(const GenericMemoryPoolAllocator<U8>& alloc)
-		: m_alloc(alloc)
+	HashMapRaii(const TMemPool& pool)
+		: m_pool(pool)
 	{
 	}
 
 	/// Move.
-	HashMapAuto(HashMapAuto&& b)
+	HashMapRaii(HashMapRaii&& b)
 	{
 		*this = std::move(b);
 	}
 
 	/// Copy.
-	HashMapAuto(const HashMapAuto& b)
+	HashMapRaii(const HashMapRaii& b)
 		: Base()
 	{
 		copy(b);
 	}
 
 	/// Destructor.
-	~HashMapAuto()
+	~HashMapRaii()
 	{
 		destroy();
 	}
 
 	/// Move.
-	HashMapAuto& operator=(HashMapAuto&& b)
+	HashMapRaii& operator=(HashMapRaii&& b)
 	{
-		std::move(*static_cast<HashMapAuto>(this));
-		m_alloc = std::move(b.m_alloc);
+		std::move(*static_cast<HashMapRaii>(this));
+		m_pool = std::move(b.m_pool);
 		return *this;
 	}
 
 	/// Copy.
-	HashMapAuto& operator=(const HashMapAuto& b)
+	HashMapRaii& operator=(const HashMapRaii& b)
 	{
 		copy(b);
 		return *this;
@@ -273,29 +275,29 @@ public:
 	template<typename... TArgs>
 	typename Base::Iterator emplace(const TKey& key, TArgs&&... args)
 	{
-		return Base::emplace(m_alloc, key, std::forward<TArgs>(args)...);
+		return Base::emplace(*m_pool, key, std::forward<TArgs>(args)...);
 	}
 
 	/// Erase element.
 	void erase(typename Base::Iterator it)
 	{
-		Base::erase(m_alloc, it);
+		Base::erase(*m_pool, it);
 	}
 
 	/// Clean up the map.
 	void destroy()
 	{
-		Base::destroy(m_alloc);
+		Base::destroy(*m_pool);
 	}
 
 private:
-	GenericMemoryPoolAllocator<U8> m_alloc;
+	MemoryPool m_pool;
 
-	void copy(const HashMapAuto& b)
+	void copy(const HashMapRaii& b)
 	{
 		destroy();
-		m_alloc = b.m_alloc;
-		b.m_sparseArr.clone(m_alloc, Base::m_sparseArr);
+		m_pool = b.m_pool;
+		b.m_sparseArr.clone(*m_pool, Base::m_sparseArr);
 	}
 };
 /// @}

+ 6 - 7
AnKi/Util/Hierarchy.h

@@ -8,7 +8,6 @@
 #include <AnKi/Util/Assert.h>
 #include <AnKi/Util/List.h>
 #include <AnKi/Util/StdTypes.h>
-#include <AnKi/Util/Allocator.h>
 #include <algorithm>
 
 namespace anki {
@@ -39,8 +38,8 @@ public:
 
 	Hierarchy& operator=(const Hierarchy&) = delete; // Non-copyable
 
-	template<typename TAllocator>
-	void destroy(TAllocator alloc);
+	template<typename TMemPool>
+	void destroy(TMemPool& pool);
 
 	const Value* getParent() const
 	{
@@ -63,12 +62,12 @@ public:
 	}
 
 	/// Add a new child.
-	template<typename TAllocator>
-	void addChild(TAllocator alloc, Value* child);
+	template<typename TMemPool>
+	void addChild(TMemPool& pool, Value* child);
 
 	/// Remove a child.
-	template<typename TAllocator>
-	void removeChild(TAllocator alloc, Value* child);
+	template<typename TMemPool>
+	void removeChild(TMemPool& pool, Value* child);
 
 	/// Visit the children and the children's children. Use it with lambda
 	template<typename VisitorFunc>

+ 10 - 10
AnKi/Util/Hierarchy.inl.h

@@ -6,12 +6,12 @@
 namespace anki {
 
 template<typename T>
-template<typename TAllocator>
-void Hierarchy<T>::destroy(TAllocator alloc)
+template<typename TMemPool>
+void Hierarchy<T>::destroy(TMemPool& pool)
 {
 	if(m_parent != nullptr)
 	{
-		m_parent->removeChild(alloc, getSelf());
+		m_parent->removeChild(pool, getSelf());
 		m_parent = nullptr;
 	}
 
@@ -24,12 +24,12 @@ void Hierarchy<T>::destroy(TAllocator alloc)
 		child->m_parent = nullptr;
 	}
 
-	m_children.destroy(alloc);
+	m_children.destroy(pool);
 }
 
 template<typename T>
-template<typename TAllocator>
-void Hierarchy<T>::addChild(TAllocator alloc, Value* child)
+template<typename TMemPool>
+void Hierarchy<T>::addChild(TMemPool& pool, Value* child)
 {
 	ANKI_ASSERT(child != nullptr && "Null arg");
 	ANKI_ASSERT(child != getSelf() && "Cannot put itself");
@@ -38,12 +38,12 @@ void Hierarchy<T>::addChild(TAllocator alloc, Value* child)
 	ANKI_ASSERT(findChild(child) == m_children.getEnd() && "Already has that child");
 
 	child->m_parent = getSelf();
-	m_children.emplaceBack(alloc, child);
+	m_children.emplaceBack(pool, child);
 }
 
 template<typename T>
-template<typename TAllocator>
-void Hierarchy<T>::removeChild(TAllocator alloc, Value* child)
+template<typename TMemPool>
+void Hierarchy<T>::removeChild(TMemPool& pool, Value* child)
 {
 	ANKI_ASSERT(child != nullptr && "Null arg");
 	ANKI_ASSERT(child->m_parent == getSelf() && "Child has other parent");
@@ -52,7 +52,7 @@ void Hierarchy<T>::removeChild(TAllocator alloc, Value* child)
 
 	ANKI_ASSERT(it != m_children.getEnd() && "Child not found");
 
-	m_children.erase(alloc, it);
+	m_children.erase(pool, it);
 	child->m_parent = nullptr;
 }
 

+ 3 - 25
AnKi/Util/Ptr.h

@@ -5,7 +5,7 @@
 
 #pragma once
 
-#include <AnKi/Util/Allocator.h>
+#include <AnKi/Util/CpuMemoryPools.h>
 
 namespace anki {
 
@@ -253,30 +253,8 @@ class DefaultPtrDeleter
 public:
 	void operator()(T* x)
 	{
-		auto alloc = x->getAllocator();
-		alloc.template deleteInstance<T>(x);
-	}
-};
-
-/// UniquePtr alternative deleter.
-template<typename T>
-class AllocatorPtrDeleter
-{
-public:
-	GenericMemoryPoolAllocator<U8> m_allocator;
-
-	AllocatorPtrDeleter()
-	{
-	}
-
-	AllocatorPtrDeleter(GenericMemoryPoolAllocator<U8> alloc)
-		: m_allocator(alloc)
-	{
-	}
-
-	void operator()(T* x)
-	{
-		m_allocator.template deleteInstance<T>(x);
+		auto pool = x->getMemoryPool();
+		deleteInstance<T>(pool, x);
 	}
 };
 

+ 14 - 15
AnKi/Util/SparseArray.h

@@ -8,7 +8,6 @@
 #include <AnKi/Util/StdTypes.h>
 #include <AnKi/Util/Assert.h>
 #include <AnKi/Util/Array.h>
-#include <AnKi/Util/Allocator.h>
 #include <utility>
 
 namespace anki {
@@ -328,12 +327,12 @@ public:
 	}
 
 	/// Destroy the array and free its elements.
-	template<typename TAlloc>
-	void destroy(TAlloc& alloc);
+	template<typename TMemPool>
+	void destroy(TMemPool& pool);
 
 	/// Set a value to an index.
-	template<typename TAlloc, typename... TArgs>
-	Iterator emplace(TAlloc& alloc, Index idx, TArgs&&... args);
+	template<typename TMemPool, typename... TArgs>
+	Iterator emplace(TMemPool& pool, Index idx, TArgs&&... args);
 
 	/// Get an iterator.
 	Iterator find(Index idx)
@@ -358,15 +357,15 @@ public:
 	}
 
 	/// Remove an element.
-	template<typename TAlloc>
-	void erase(TAlloc& alloc, Iterator it);
+	template<typename TMemPool>
+	void erase(TMemPool& pool, Iterator it);
 
 	/// Check the validity of the array.
 	void validate() const;
 
 	/// Create a copy of this.
-	template<typename TAlloc>
-	void clone(TAlloc& alloc, SparseArray& b) const;
+	template<typename TMemPool>
+	void clone(TMemPool& pool, SparseArray& b) const;
 
 	const Config& getConfig() const
 	{
@@ -419,12 +418,12 @@ protected:
 
 	/// Insert a value. This method will move the val to a new place.
 	/// @return One if the idx was a new element or zero if the idx was there already.
-	template<typename TAlloc>
-	Index insert(TAlloc& alloc, Index idx, Value& val);
+	template<typename TMemPool>
+	Index insert(TMemPool& pool, Index idx, Value& val);
 
 	/// Grow the storage and re-insert.
-	template<typename TAlloc>
-	void grow(TAlloc& alloc);
+	template<typename TMemPool>
+	void grow(TMemPool& pool);
 
 	/// Compute the distance between a desired position and the current one. This method does a trick with capacity to
 	/// account for wrapped positions.
@@ -482,8 +481,8 @@ protected:
 		return (pos >= m_capacity) ? getMaxNumericLimit<Index>() : pos;
 	}
 
-	template<typename TAlloc, typename... TArgs>
-	void emplaceInternal(TAlloc& alloc, Index idx, TArgs&&... args);
+	template<typename TMemPool, typename... TArgs>
+	void emplaceInternal(TMemPool& pool, Index idx, TArgs&&... args);
 
 	void destroyElement(Value& v)
 	{

+ 32 - 35
AnKi/Util/SparseArray.inl.h

@@ -8,8 +8,8 @@
 namespace anki {
 
 template<typename T, typename TConfig>
-template<typename TAlloc>
-void SparseArray<T, TConfig>::destroy(TAlloc& alloc)
+template<typename TMemPool>
+void SparseArray<T, TConfig>::destroy(TMemPool& pool)
 {
 	if(m_elements)
 	{
@@ -21,35 +21,35 @@ void SparseArray<T, TConfig>::destroy(TAlloc& alloc)
 			}
 		}
 
-		alloc.deallocate(m_elements, m_capacity);
+		pool.free(m_elements);
 
 		ANKI_ASSERT(m_metadata);
-		alloc.deallocate(m_metadata, m_capacity);
+		pool.free(m_metadata);
 	}
 
 	resetMembers();
 }
 
 template<typename T, typename TConfig>
-template<typename TAlloc, typename... TArgs>
-void SparseArray<T, TConfig>::emplaceInternal(TAlloc& alloc, Index idx, TArgs&&... args)
+template<typename TMemPool, typename... TArgs>
+void SparseArray<T, TConfig>::emplaceInternal(TMemPool& pool, Index idx, TArgs&&... args)
 {
 	if(m_capacity == 0 || calcLoadFactor() > getMaxLoadFactor())
 	{
-		grow(alloc);
+		grow(pool);
 	}
 
 	Value tmp(std::forward<TArgs>(args)...);
-	m_elementCount += insert(alloc, idx, tmp);
+	m_elementCount += insert(pool, idx, tmp);
 
 	invalidateIterators();
 }
 
 template<typename T, typename TConfig>
-template<typename TAlloc, typename... TArgs>
-typename SparseArray<T, TConfig>::Iterator SparseArray<T, TConfig>::emplace(TAlloc& alloc, Index idx, TArgs&&... args)
+template<typename TMemPool, typename... TArgs>
+typename SparseArray<T, TConfig>::Iterator SparseArray<T, TConfig>::emplace(TMemPool& pool, Index idx, TArgs&&... args)
 {
-	emplaceInternal(alloc, idx, std::forward<TArgs>(args)...);
+	emplaceInternal(pool, idx, std::forward<TArgs>(args)...);
 
 	return Iterator(this, findInternal(idx)
 #if ANKI_EXTRA_CHECKS
@@ -60,8 +60,8 @@ typename SparseArray<T, TConfig>::Iterator SparseArray<T, TConfig>::emplace(TAll
 }
 
 template<typename T, typename TConfig>
-template<typename TAlloc>
-typename TConfig::Index SparseArray<T, TConfig>::insert(TAlloc& alloc, Index idx, Value& val)
+template<typename TMemPool>
+typename TConfig::Index SparseArray<T, TConfig>::insert(TMemPool& pool, Index idx, Value& val)
 {
 	while(true)
 	{
@@ -80,7 +80,7 @@ typename TConfig::Index SparseArray<T, TConfig>::insert(TAlloc& alloc, Index idx
 
 				meta.m_alive = true;
 				meta.m_idx = idx;
-				alloc.construct(&crntVal, std::move(val));
+				callConstructor(crntVal, std::move(val));
 
 				return 1;
 			}
@@ -90,7 +90,7 @@ typename TConfig::Index SparseArray<T, TConfig>::insert(TAlloc& alloc, Index idx
 
 				meta.m_idx = idx;
 				destroyElement(crntVal);
-				alloc.construct(&crntVal, std::move(val));
+				callConstructor(crntVal, std::move(val));
 
 				return 0;
 			}
@@ -111,7 +111,7 @@ typename TConfig::Index SparseArray<T, TConfig>::insert(TAlloc& alloc, Index idx
 		if(pos == endPos)
 		{
 			// Didn't found an empty place, need to grow and try again
-			grow(alloc);
+			grow(pool);
 		}
 	}
 
@@ -119,17 +119,16 @@ typename TConfig::Index SparseArray<T, TConfig>::insert(TAlloc& alloc, Index idx
 }
 
 template<typename T, typename TConfig>
-template<typename TAlloc>
-void SparseArray<T, TConfig>::grow(TAlloc& alloc)
+template<typename TMemPool>
+void SparseArray<T, TConfig>::grow(TMemPool& pool)
 {
 	if(m_capacity == 0)
 	{
 		ANKI_ASSERT(m_elementCount == 0);
 		m_capacity = getInitialStorageSize();
-		m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
+		m_elements = static_cast<Value*>(pool.allocate(m_capacity * sizeof(Value), alignof(Value)));
 
-		m_metadata =
-			static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
+		m_metadata = static_cast<Metadata*>(pool.allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
 
 		memset(m_metadata, 0, m_capacity * sizeof(Metadata));
 
@@ -143,9 +142,8 @@ void SparseArray<T, TConfig>::grow(TAlloc& alloc)
 	[[maybe_unused]] const Index oldElementCount = m_elementCount;
 
 	m_capacity *= 2;
-	m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
-	m_metadata =
-		static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
+	m_elements = static_cast<Value*>(pool.allocate(m_capacity * sizeof(Value), alignof(Value)));
+	m_metadata = static_cast<Metadata*>(pool.allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
 	memset(m_metadata, 0, m_capacity * sizeof(Metadata));
 	m_elementCount = 0;
 
@@ -172,7 +170,7 @@ void SparseArray<T, TConfig>::grow(TAlloc& alloc)
 	{
 		if(oldMetadata[pos].m_alive)
 		{
-			Index c = insert(alloc, oldMetadata[pos].m_idx, oldElements[pos]);
+			Index c = insert(pool, oldMetadata[pos].m_idx, oldElements[pos]);
 			ANKI_ASSERT(c > 0);
 			m_elementCount += c;
 
@@ -186,13 +184,13 @@ void SparseArray<T, TConfig>::grow(TAlloc& alloc)
 	ANKI_ASSERT(oldElementCount == m_elementCount);
 
 	// Finalize
-	alloc.getMemoryPool().free(oldElements);
-	alloc.getMemoryPool().free(oldMetadata);
+	pool.free(oldElements);
+	pool.free(oldMetadata);
 }
 
 template<typename T, typename TConfig>
-template<typename TAlloc>
-void SparseArray<T, TConfig>::erase(TAlloc& alloc, Iterator it)
+template<typename TMemPool>
+void SparseArray<T, TConfig>::erase(TMemPool& pool, Iterator it)
 {
 	ANKI_ASSERT(it.m_array == this);
 	ANKI_ASSERT(it.m_elementIdx != getMaxNumericLimit<Index>());
@@ -242,7 +240,7 @@ void SparseArray<T, TConfig>::erase(TAlloc& alloc, Iterator it)
 	// If you erased everything destroy the storage
 	if(m_elementCount == 0)
 	{
-		destroy(alloc);
+		destroy(pool);
 	}
 
 	invalidateIterators();
@@ -331,8 +329,8 @@ typename TConfig::Index SparseArray<T, TConfig>::findInternal(Index idx) const
 }
 
 template<typename T, typename TConfig>
-template<typename TAlloc>
-void SparseArray<T, TConfig>::clone(TAlloc& alloc, SparseArray& b) const
+template<typename TMemPool>
+void SparseArray<T, TConfig>::clone(TMemPool& pool, SparseArray& b) const
 {
 	ANKI_ASSERT(b.m_elements == nullptr && b.m_metadata == nullptr);
 	if(m_capacity == 0)
@@ -341,9 +339,8 @@ void SparseArray<T, TConfig>::clone(TAlloc& alloc, SparseArray& b) const
 	}
 
 	// Allocate memory
-	b.m_elements = static_cast<Value*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Value), alignof(Value)));
-	b.m_metadata =
-		static_cast<Metadata*>(alloc.getMemoryPool().allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
+	b.m_elements = static_cast<Value*>(pool.allocate(m_capacity * sizeof(Value), alignof(Value)));
+	b.m_metadata = static_cast<Metadata*>(pool.allocate(m_capacity * sizeof(Metadata), alignof(Metadata)));
 	memcpy(b.m_metadata, m_metadata, m_capacity * sizeof(Metadata));
 
 	for(U i = 0; i < m_capacity; ++i)