Browse Source

More memory allocator related changes

Marko Pintera 12 years ago
parent
commit
8606499f7a

+ 1 - 1
CamelotCore/Include/CmCameraRTTI.h

@@ -29,7 +29,7 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return std::shared_ptr<Camera>(new Camera());
+			return std::shared_ptr<Camera>(CM_NEW(Camera, PoolAlloc) Camera(), &MemAllocDeleter<Camera, PoolAlloc>::deleter);
 		}
 	};
 }

+ 2 - 1
CamelotCore/Include/CmFontImportOptionsRTTI.h

@@ -30,7 +30,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return ImportOptionsPtr(new FontImportOptions());
+			return ImportOptionsPtr(CM_NEW(FontImportOptions, PoolAlloc) FontImportOptions(),
+				&MemAllocDeleter<FontImportOptions, PoolAlloc>::deleter);
 		}
 	};
 }

+ 4 - 3
CamelotCore/Include/CmFontRTTI.h

@@ -72,7 +72,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return std::shared_ptr<FontData>(new FontData());
+			return std::shared_ptr<FontData>(CM_NEW(FontData, PoolAlloc) FontData(),
+				&MemAllocDeleter<FontData, PoolAlloc>::deleter);
 		}
 	};
 
@@ -140,7 +141,7 @@ namespace CamelotEngine
 	protected:
 		virtual void onDeserializationStarted(IReflectable* obj)
 		{
-			FontInitData* initData = new FontInitData();
+			FontInitData* initData = CM_NEW(FontInitData, PoolAlloc) FontInitData();
 
 			Font* font = static_cast<Font*>(obj);
 			font->mRTTIData = initData;
@@ -153,7 +154,7 @@ namespace CamelotEngine
 
 			font->initialize(initData->fontDataPerSize);
 
-			delete initData;
+			CM_DELETE(initData, FontInitData, PoolAlloc);
 		}
 	};
 }

+ 14 - 8
CamelotCore/Include/CmGameObjectHandle.h

@@ -9,9 +9,13 @@ namespace CamelotEngine
 		GameObjectHandleData()
 		{ }
 
-		GameObjectHandleData(GameObject* ptr)
-			:mPtr(ptr)
-		{ }
+		GameObjectHandleData(GameObject* ptr, void(*deleter)(GameObject*))
+		{
+			if(deleter != nullptr)
+				mPtr = std::shared_ptr<GameObject>(ptr, deleter);
+			else
+				mPtr = std::shared_ptr<GameObject>(ptr);
+		}
 
 		std::shared_ptr<GameObject> mPtr;
 	};
@@ -55,7 +59,8 @@ namespace CamelotEngine
 		GameObjectHandle()
 			:GameObjectHandleBase()
 		{	
-			mData = std::shared_ptr<GameObjectHandleData>(new GameObjectHandleData());
+			mData = std::shared_ptr<GameObjectHandleData>(CM_NEW(GameObjectHandleData, PoolAlloc) GameObjectHandleData(),
+				&MemAllocDeleter<GameObjectHandleData, PoolAlloc>::deleter);
 		}
 
 		template <typename T1>
@@ -90,15 +95,16 @@ namespace CamelotEngine
 	private:
 		friend SceneObject;
 
-		explicit GameObjectHandle(T* ptr)
+		explicit GameObjectHandle(T* ptr, void(*deleter)(GameObject*) = nullptr)
 			:GameObjectHandleBase()
 		{
-			mData = std::shared_ptr<GameObjectHandleData>(new GameObjectHandleData((GameObject*)ptr));
+			mData = std::shared_ptr<GameObjectHandleData>(CM_NEW(GameObjectHandleData, PoolAlloc) GameObjectHandleData((GameObject*)ptr, deleter),
+				&MemAllocDeleter<GameObjectHandleData, PoolAlloc>::deleter);
 		}
 
-		static GameObjectHandle<T> _create(T* ptr)
+		static GameObjectHandle<T> _create(T* ptr, void(*deleter)(GameObject*))
 		{
-			return GameObjectHandle<T>(ptr);
+			return GameObjectHandle<T>(ptr, deleter);
 		}
 
 		void destroy()

+ 2 - 1
CamelotCore/Include/CmGpuProgramImportOptionsRTTI.h

@@ -50,7 +50,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return ImportOptionsPtr(new GpuProgramImportOptions());
+			return ImportOptionsPtr(CM_NEW(GpuProgramImportOptions, PoolAlloc) GpuProgramImportOptions(),
+				&MemAllocDeleter<GpuProgramImportOptions, PoolAlloc>::deleter);
 		}
 	};
 }

+ 2 - 1
CamelotCore/Include/CmImportOptionsRTTI.h

@@ -26,7 +26,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return ImportOptionsPtr(new ImportOptions());
+			return ImportOptionsPtr(CM_NEW(ImportOptions, PoolAlloc) ImportOptions(),
+				&MemAllocDeleter<ImportOptions, PoolAlloc>::deleter);
 		}
 	};
 }

+ 1 - 1
CamelotCore/Include/CmMaterial.h

@@ -45,7 +45,7 @@ namespace CamelotEngine
 			StructData(void* _data, UINT32 _size)
 				:size(_size)
 			{
-				data = std::shared_ptr<void>(new UINT8[_size]);
+				data = std::shared_ptr<void>(CM_NEW_BYTES(_size, ScratchAlloc), &MemAllocBytesDeleter<ScratchAlloc>::deleter);
 				memcpy(data.get(), _data, size);
 			}
 

+ 6 - 3
CamelotCore/Include/CmMeshDataRTTI.h

@@ -30,7 +30,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject() 
 		{
-			return std::shared_ptr<MeshData::VertexElementData>(new MeshData::VertexElementData());
+			return std::shared_ptr<MeshData::VertexElementData>(CM_NEW(MeshData::VertexElementData, PoolAlloc) MeshData::VertexElementData(),
+				&MemAllocDeleter<MeshData::VertexElementData, PoolAlloc>::deleter);
 		}
 
 		virtual const String& getRTTIName() 
@@ -70,7 +71,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject() 
 		{
-			return std::shared_ptr<MeshData::IndexElementData>(new MeshData::IndexElementData());
+			return std::shared_ptr<MeshData::IndexElementData>(CM_NEW(MeshData::IndexElementData, PoolAlloc) MeshData::IndexElementData(),
+				&MemAllocDeleter<MeshData::IndexElementData, PoolAlloc>::deleter);
 		}
 
 		virtual const String& getRTTIName() 
@@ -166,7 +168,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject() 
 		{
-			return std::shared_ptr<MeshData>(new MeshData());
+			return std::shared_ptr<MeshData>(CM_NEW(MeshData, PoolAlloc) MeshData(),
+				&MemAllocDeleter<MeshData, PoolAlloc>::deleter);
 		}
 
 		virtual const String& getRTTIName() 

+ 2 - 1
CamelotCore/Include/CmPassRTTI.h

@@ -63,7 +63,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return std::shared_ptr<Pass>(new Pass());
+			return std::shared_ptr<Pass>(CM_NEW(Pass, PoolAlloc) Pass(),
+				&MemAllocDeleter<Pass, PoolAlloc>::deleter);
 		}
 	};
 }

+ 2 - 1
CamelotCore/Include/CmRenderableRTTI.h

@@ -29,7 +29,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return std::shared_ptr<Renderable>(new Renderable());
+			return std::shared_ptr<Renderable>(CM_NEW(Renderable, PoolAlloc) Renderable(),
+				&MemAllocDeleter<Renderable, PoolAlloc>::deleter);
 		}
 	};
 }

+ 2 - 1
CamelotCore/Include/CmResourceHandle.h

@@ -128,7 +128,8 @@ namespace CamelotEngine
 		 */
 		void reset()
 		{
-			mData = std::shared_ptr<ResourceHandleData>(new ResourceHandleData());
+			mData = std::shared_ptr<ResourceHandleData>(CM_NEW(ResourceHandleData, ScratchAlloc) ResourceHandleData(),
+				&MemAllocDeleter<ResourceHandleData, ScratchAlloc>::deleter);
 		}
 
 		template<class _Ty>

+ 2 - 1
CamelotCore/Include/CmResourceHandleRTTI.h

@@ -46,7 +46,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return std::shared_ptr<ResourceHandleBase>(new ResourceHandleBase());
+			return std::shared_ptr<ResourceHandleBase>(CM_NEW(ResourceHandleBase, PoolAlloc) ResourceHandleBase(),
+				&MemAllocDeleter<ResourceHandleBase, PoolAlloc>::deleter);
 		}
 	};
 }

+ 2 - 1
CamelotCore/Include/CmResourcesRTTI.h

@@ -23,7 +23,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject() 
 		{
-			return std::shared_ptr<Resources::ResourceMetaData>(new Resources::ResourceMetaData());
+			return std::shared_ptr<Resources::ResourceMetaData>(CM_NEW(Resources::ResourceMetaData, PoolAlloc) Resources::ResourceMetaData(),
+				&MemAllocDeleter<Resources::ResourceMetaData, PoolAlloc>::deleter);
 		}
 
 		virtual const String& getRTTIName() 

+ 2 - 1
CamelotCore/Include/CmSceneObject.h

@@ -216,7 +216,8 @@ namespace CamelotEngine
 			BOOST_STATIC_ASSERT_MSG((boost::is_base_of<CamelotEngine::Component, T>::value), 
 				"Specified type is not a valid Component.");
 
-			GameObjectHandle<T> newComponent = GameObjectHandle<T>::_create(new T(mThisHandle));
+			GameObjectHandle<T> newComponent = GameObjectHandle<T>::_create(CM_NEW(T, PoolAlloc) T(mThisHandle),
+				&MemAllocDeleter<GameObject, PoolAlloc>::deleter);
 			mComponents.push_back(newComponent);
 
 			gSceneManager().notifyComponentAdded(newComponent);

+ 2 - 1
CamelotCore/Include/CmTechniqueRTTI.h

@@ -57,7 +57,8 @@ namespace CamelotEngine
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
-			return std::shared_ptr<Technique>(new Technique());
+			return std::shared_ptr<Technique>(CM_NEW(Technique, PoolAlloc) Technique(),
+				&MemAllocDeleter<Technique, PoolAlloc>::deleter);
 		}
 	};
 }

+ 2 - 2
CamelotCore/Include/CmTextureRTTI.h

@@ -78,7 +78,7 @@ namespace CamelotEngine
 		{
 			Texture* texture = static_cast<Texture*>(obj);
 
-			texture->mRTTIData = new vector<PixelDataPtr>::type();
+			texture->mRTTIData = CM_NEW(vector<PixelDataPtr>::type, PoolAlloc) vector<PixelDataPtr>::type();
 		}
 
 		virtual void onDeserializationEnded(IReflectable* obj)
@@ -106,7 +106,7 @@ namespace CamelotEngine
 				texture->setRawPixels(data, face, mipmap);
 			}
 
-			delete pixelData;
+			CM_DELETE(pixelData, vector<PixelDataPtr>::type, PoolAlloc);
 			texture->mRTTIData = nullptr;	
 		}
 

+ 8 - 7
CamelotCore/Source/CmMesh.cpp

@@ -72,10 +72,10 @@ namespace CamelotEngine
 		mSubMeshes.clear();
 
 		if(mVertexData != nullptr)
-			delete mVertexData;
+			CM_DELETE(mVertexData, VertexData, PoolAlloc);
 
 		if(mIndexData != nullptr)
-			delete mIndexData;
+			CM_DELETE(mIndexData, IndexData, PoolAlloc);
 
 		// Submeshes
 		UINT32 indexOffset = 0;
@@ -93,7 +93,7 @@ namespace CamelotEngine
 		}
 
 		// Indices
-		mIndexData = new IndexData();
+		mIndexData = CM_NEW(IndexData, PoolAlloc) IndexData();
 
 		mIndexData->indexCount = totalIndexCount;
 		mIndexData->indexBuffer = HardwareBufferManager::instance().createIndexBuffer(
@@ -114,7 +114,7 @@ namespace CamelotEngine
 		mIndexData->indexBuffer->unlock();
 
 		// Vertices
-		mVertexData = new VertexData();
+		mVertexData = CM_NEW(VertexData, PoolAlloc) VertexData();
 
 		mVertexData->vertexCount = numVertices;
 		mVertexData->vertexDeclaration = meshData->createDeclaration();
@@ -170,7 +170,8 @@ namespace CamelotEngine
 		if(mIndexData)
 			indexType = mIndexData->indexBuffer->getType();
 
-		MeshDataPtr meshData(new MeshData(indexType));
+		MeshDataPtr meshData(CM_NEW(MeshData, PoolAlloc) MeshData(indexType),
+			&MemAllocDeleter<MeshData, PoolAlloc>::deleter);
 
 		if(mIndexData)
 		{
@@ -262,10 +263,10 @@ namespace CamelotEngine
 		THROW_IF_NOT_RENDER_THREAD;
 
 		if(mVertexData != nullptr)
-			delete mVertexData;
+			CM_DELETE(mVertexData, VertexData, PoolAlloc);
 
 		if(mIndexData != nullptr)
-			delete mIndexData;
+			CM_DELETE(mIndexData, IndexData, PoolAlloc);
 
 		Resource::destroy_internal();
 	}

+ 10 - 9
CamelotCore/Source/CmMeshData.cpp

@@ -21,14 +21,14 @@ namespace CamelotEngine
 			for(auto& vertElem : vertElems.second)
 			{
 				if(vertElem.data != nullptr)
-					delete[] vertElem.data;
+					CM_DELETE_BYTES(vertElem.data, ScratchAlloc);
 			}
 		}
 
 		for(auto& indexData : mIndices)
 		{
 			if(indexData.indices != nullptr)
-				delete[] indexData.indices;
+				CM_DELETE_BYTES(indexData.indices, ScratchAlloc);
 		}
 	}
 
@@ -92,7 +92,7 @@ namespace CamelotEngine
 		clearIfItExists(type, semantic, semanticIdx, streamIdx);
 
 		UINT32 elemSize = VertexElement::getTypeSize(type);
-		UINT8* elements = new UINT8[elemSize * numElements];
+		UINT8* elements = CM_NEW_BYTES(elemSize * numElements, ScratchAlloc);
 
 		vector<VertexElementData>::type& elemData = mVertexData[streamIdx];
 
@@ -113,9 +113,9 @@ namespace CamelotEngine
 		IndexElementData indexData = mIndices[subMesh];
 
 		if(indexData.indices != nullptr)
-			delete[] indexData.indices;
+			CM_DELETE_BYTES(indexData.indices, ScratchAlloc);
 
-		UINT32* indices = new UINT32[numIndices];
+		UINT32* indices = (UINT32*)CM_NEW_BYTES(numIndices * sizeof(UINT32), ScratchAlloc);
 
 		indexData.indices = (UINT8*)indices;
 		indexData.numIndices = numIndices;
@@ -138,9 +138,9 @@ namespace CamelotEngine
 		IndexElementData indexData = mIndices[subMesh];
 
 		if(indexData.indices != nullptr)
-			delete[] indexData.indices;
+			CM_DELETE_BYTES(indexData.indices, ScratchAlloc);
 
-		UINT16* indices = new UINT16[numIndices];
+		UINT16* indices = (UINT16*)CM_NEW_BYTES(numIndices * sizeof(UINT16), ScratchAlloc);
 
 		indexData.indices = (UINT8*)indices;
 		indexData.numIndices = numIndices;
@@ -233,7 +233,8 @@ namespace CamelotEngine
 
 	MeshDataPtr MeshData::combine(const vector<MeshDataPtr>::type& elements)
 	{
-		MeshDataPtr combinedMeshData(new MeshData());
+		MeshDataPtr combinedMeshData(CM_NEW(MeshData, PoolAlloc) MeshData(),
+			&MemAllocDeleter<MeshData, PoolAlloc>::deleter);
 
 		UINT32 subMeshIndex = 0;
 		vector<VertexElement>::type combinedVertexElements;
@@ -343,7 +344,7 @@ namespace CamelotEngine
 		if(findIter != elemData.end())
 		{
 			if(findIter->data != nullptr)
-				delete[] findIter->data;
+				CM_DELETE_BYTES(findIter->data, ScratchAlloc);
 
 			elemData.erase(findIter);
 		}

+ 2 - 1
CamelotCore/Source/CmSceneObject.cpp

@@ -35,7 +35,8 @@ namespace CamelotEngine
 
 	HSceneObject SceneObject::createInternal(const String& name)
 	{
-		HSceneObject sceneObject = HSceneObject::_create(new SceneObject(name));
+		HSceneObject sceneObject = HSceneObject::_create(CM_NEW(SceneObject, PoolAlloc) SceneObject(name),
+			&MemAllocDeleter<GameObject, PoolAlloc>::deleter);
 		sceneObject->mThisHandle = sceneObject;
 
 		return sceneObject;

+ 10 - 0
CamelotUtility/Include/CmMemoryAllocator.h

@@ -41,6 +41,16 @@ namespace CamelotEngine
 		}
 	};
 
+	template<class category>
+	class MemAllocBytesDeleter
+	{
+	public:
+		static void deleter(void* ptr)
+		{
+			MemoryAllocator<category>::free(ptr);
+		}
+	};
+
 	template<class T, class category> 
 	inline T* __cm_construct_array(unsigned int count)
 	{