Browse Source

Even more memory allocator related changes

Marko Pintera 12 years ago
parent
commit
735836d51d

+ 0 - 1
CamelotUtility/CamelotUtility.vcxproj

@@ -182,7 +182,6 @@
     <ClInclude Include="Include\CmInt2.h" />
     <ClInclude Include="Include\CmInt2.h" />
     <ClInclude Include="Include\CmIReflectable.h" />
     <ClInclude Include="Include\CmIReflectable.h" />
     <ClInclude Include="Include\CmKeyValuePair.h" />
     <ClInclude Include="Include\CmKeyValuePair.h" />
-    <ClInclude Include="Include\CmLockedPtr.h" />
     <ClInclude Include="Include\CmLog.h" />
     <ClInclude Include="Include\CmLog.h" />
     <ClInclude Include="Include\CmManagedDataBlock.h" />
     <ClInclude Include="Include\CmManagedDataBlock.h" />
     <ClInclude Include="Include\CmMathAsm.h" />
     <ClInclude Include="Include\CmMathAsm.h" />

+ 0 - 3
CamelotUtility/CamelotUtility.vcxproj.filters

@@ -201,9 +201,6 @@
     <ClInclude Include="Include\CmAsyncOp.h">
     <ClInclude Include="Include\CmAsyncOp.h">
       <Filter>Header Files\Threading</Filter>
       <Filter>Header Files\Threading</Filter>
     </ClInclude>
     </ClInclude>
-    <ClInclude Include="Include\CmLockedPtr.h">
-      <Filter>Header Files</Filter>
-    </ClInclude>
     <ClInclude Include="Include\CmPixelData.h">
     <ClInclude Include="Include\CmPixelData.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>

+ 1 - 1
CamelotUtility/Include/CmAsyncOp.h

@@ -27,7 +27,7 @@ namespace CamelotEngine
 
 
 	public:
 	public:
 		AsyncOp()
 		AsyncOp()
-			:mData(new AsyncOpData())
+			:mData(CM_NEW(AsyncOpData, ScratchAlloc) AsyncOpData(), &MemAllocDeleter<AsyncOpData, ScratchAlloc>::deleter)
 		{}
 		{}
 
 
 		/**
 		/**

+ 0 - 65
CamelotUtility/Include/CmLockedPtr.h

@@ -1,65 +0,0 @@
-#pragma once
-
-#include "CmPrerequisitesUtil.h"
-
-#if CM_DEBUG_MODE
-#include "CmException.h"
-#endif
-
-namespace CamelotEngine
-{
-	/**
-	 * @brief	A pointer that once locked will not allow its value to be accessed.
-	 * 			
-	 * @note	This is useful with multithreading where you need to pass along some value to a different
-	 * 			thread, but want to ensure that no other thread can modify the value (without actually creating a copy
-	 * 			of all the data).
-	 * 			
-	 *			LockedPtr MUST be the exclusive owner of the data it is pointing to. 
-	 */
-	template <class T>
-	class LockedPtr
-	{
-	public:
-		explicit LockedPtr(T* data)
-			:mLocked(false)
-		{
-			mData = new std::shared_ptr<T>(data);
-		}
-
-		T* operator->() const 
-		{ 
-			throwIfLocked();
-			return !mLocked ? mData.get() : nullptr; 
-		}
-
-		void lock() { mLocked = true; }
-
-		template<class _Ty>
-		struct CM_Bool_struct
-		{
-			int _Member;
-		};
-
-		// Conversion to bool
-		// (Why not just directly convert to bool? Because then we can assign pointer to bool and that's weird)
-		operator int CM_Bool_struct<T>::*() const
-		{
-			return ((mData.get() != 0) ? &CM_Bool_struct<T>::_Member : 0);
-		}
-
-	private:
-		std::shared_ptr<T> mData;
-		bool mLocked;
-
-#if CM_DEBUG_MODE
-		void throwIfLocked() const
-		{
-			if(mLocked)
-			{
-				CM_EXCEPT(InternalErrorException, "Attempting to access locked data!");
-			}
-		}
-	};
-#endif
-}

+ 2 - 1
CamelotUtility/Include/CmPixelDataRTTI.h

@@ -81,7 +81,8 @@ namespace CamelotEngine
 
 
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		virtual std::shared_ptr<IReflectable> newRTTIObject()
 		{
 		{
-			PixelDataPtr newPixelData(new PixelData());
+			PixelDataPtr newPixelData(CM_NEW(PixelData, PoolAlloc) PixelData(),
+				&MemAllocDeleter<PixelData, PoolAlloc>::deleter);
 			newPixelData->ownsData = true;
 			newPixelData->ownsData = true;
 
 
 			return newPixelData;
 			return newPixelData;

+ 3 - 3
CamelotUtility/Include/CmPrerequisitesUtil.h

@@ -61,6 +61,8 @@ THE SOFTWARE
 // Short-hand names for various built-in types
 // Short-hand names for various built-in types
 #include "CmTypes.h"
 #include "CmTypes.h"
 
 
+#include "CmMemoryAllocator.h"
+
 // Useful threading defines
 // Useful threading defines
 #include "CmThreadDefines.h"
 #include "CmThreadDefines.h"
 
 
@@ -72,6 +74,4 @@ THE SOFTWARE
 
 
 #include "CmRTTIPrerequisites.h"
 #include "CmRTTIPrerequisites.h"
 
 
-#include "CmString.h"
-
-#include "CmMemoryAllocator.h"
+#include "CmString.h"

+ 2 - 2
CamelotUtility/Include/CmString.h

@@ -382,12 +382,12 @@ namespace CamelotEngine {
 			memory += sizeof(UINT32);
 			memory += sizeof(UINT32);
 
 
 			UINT32 stringSize = size - sizeof(UINT32);
 			UINT32 stringSize = size - sizeof(UINT32);
-			char* buffer = new char[stringSize + 1]; // TODO - Use a better allocator
+			char* buffer = (char*)CM_NEW_BYTES(stringSize + 1, ScratchAlloc);
 			memcpy(buffer, memory, stringSize); 
 			memcpy(buffer, memory, stringSize); 
 			buffer[stringSize] = '\0';
 			buffer[stringSize] = '\0';
 			data = String(buffer);
 			data = String(buffer);
 
 
-			delete[] buffer; 
+			CM_DELETE_BYTES(buffer, ScratchAlloc);
 
 
 			return size;
 			return size;
 		}
 		}

+ 2 - 4
CamelotUtility/Include/CmThreadDefines.h

@@ -49,7 +49,6 @@ THE SOFTWARE
 // like CM_AUTO_MUTEX but mutex held by pointer
 // like CM_AUTO_MUTEX but mutex held by pointer
 #define CM_AUTO_SHARED_MUTEX mutable boost::recursive_mutex *CM_AUTO_MUTEX_NAME;
 #define CM_AUTO_SHARED_MUTEX mutable boost::recursive_mutex *CM_AUTO_MUTEX_NAME;
 #define CM_LOCK_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); boost::recursive_mutex::scoped_lock cmAutoMutexLock(*CM_AUTO_MUTEX_NAME);
 #define CM_LOCK_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); boost::recursive_mutex::scoped_lock cmAutoMutexLock(*CM_AUTO_MUTEX_NAME);
-#define CM_NEW_AUTO_SHARED_MUTEX assert(!CM_AUTO_MUTEX_NAME); CM_AUTO_MUTEX_NAME = new boost::recursive_mutex();
 #define CM_DELETE_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); delete CM_AUTO_MUTEX_NAME;
 #define CM_DELETE_AUTO_SHARED_MUTEX assert(CM_AUTO_MUTEX_NAME); delete CM_AUTO_MUTEX_NAME;
 #define CM_COPY_AUTO_SHARED_MUTEX(from) assert(!CM_AUTO_MUTEX_NAME); CM_AUTO_MUTEX_NAME = from;
 #define CM_COPY_AUTO_SHARED_MUTEX(from) assert(!CM_AUTO_MUTEX_NAME); CM_AUTO_MUTEX_NAME = from;
 #define CM_SET_AUTO_SHARED_MUTEX_NULL CM_AUTO_MUTEX_NAME = 0;
 #define CM_SET_AUTO_SHARED_MUTEX_NULL CM_AUTO_MUTEX_NAME = 0;
@@ -73,8 +72,8 @@ THE SOFTWARE
 #define CM_THREAD_POINTER_DELETE(var) var.reset(0)
 #define CM_THREAD_POINTER_DELETE(var) var.reset(0)
 // Thread objects and related functions
 // Thread objects and related functions
 #define CM_THREAD_TYPE boost::thread
 #define CM_THREAD_TYPE boost::thread
-#define CM_THREAD_CREATE(name, worker) boost::thread* name = new boost::thread(worker);
-#define CM_THREAD_DESTROY(name) delete name;
+#define CM_THREAD_CREATE(name, worker) boost::thread* name = new (CamelotEngine::MemoryAllocator<CamelotEngine::GenAlloc>::allocate(sizeof(boost::thread))) boost::thread(worker);
+#define CM_THREAD_DESTROY(name) CamelotEngine::__cm_destruct<boost::thread, CamelotEngine::GenAlloc>(name);
 #define CM_THREAD_HARDWARE_CONCURRENCY boost::thread::hardware_concurrency()
 #define CM_THREAD_HARDWARE_CONCURRENCY boost::thread::hardware_concurrency()
 #define CM_THREAD_CURRENT_ID boost::this_thread::get_id()
 #define CM_THREAD_CURRENT_ID boost::this_thread::get_id()
 #define CM_THREAD_ID_TYPE boost::thread::id
 #define CM_THREAD_ID_TYPE boost::thread::id
@@ -94,7 +93,6 @@ THE SOFTWARE
 #define CM_LOCK_MUTEX_NAMED(mutexName, lockName)
 #define CM_LOCK_MUTEX_NAMED(mutexName, lockName)
 #define CM_AUTO_SHARED_MUTEX
 #define CM_AUTO_SHARED_MUTEX
 #define CM_LOCK_AUTO_SHARED_MUTEX
 #define CM_LOCK_AUTO_SHARED_MUTEX
-#define CM_NEW_AUTO_SHARED_MUTEX
 #define CM_DELETE_AUTO_SHARED_MUTEX
 #define CM_DELETE_AUTO_SHARED_MUTEX
 #define CM_COPY_AUTO_SHARED_MUTEX(from)
 #define CM_COPY_AUTO_SHARED_MUTEX(from)
 #define CM_SET_AUTO_SHARED_MUTEX_NULL
 #define CM_SET_AUTO_SHARED_MUTEX_NULL

+ 4 - 4
CamelotUtility/Source/CmFileSerializer.cpp

@@ -11,12 +11,12 @@ namespace CamelotEngine
 {
 {
 	FileSerializer::FileSerializer()
 	FileSerializer::FileSerializer()
 	{
 	{
-		mWriteBuffer = new UINT8[WRITE_BUFFER_SIZE];
+		mWriteBuffer = CM_NEW_BYTES(WRITE_BUFFER_SIZE, ScratchAlloc);
 	}
 	}
 
 
 	FileSerializer::~FileSerializer()
 	FileSerializer::~FileSerializer()
 	{
 	{
-		delete[] mWriteBuffer;
+		CM_DELETE_BYTES(mWriteBuffer, ScratchAlloc);
 	}
 	}
 
 
 	void FileSerializer::encode(IReflectable* object, std::string fileLocation)
 	void FileSerializer::encode(IReflectable* object, std::string fileLocation)
@@ -42,7 +42,7 @@ namespace CamelotEngine
 				"File size is larger that UINT32 can hold. Ask a programmer to use a bigger data type.");
 				"File size is larger that UINT32 can hold. Ask a programmer to use a bigger data type.");
 		}
 		}
 
 
-		UINT8* readBuffer = new UINT8[(UINT32)fileSize]; // TODO - Low priority. Consider upgrading BinarySerializer so we don't have to read everything at once
+		UINT8* readBuffer = CM_NEW_BYTES((UINT32)fileSize, ScratchAlloc); // TODO - Low priority. Consider upgrading BinarySerializer so we don't have to read everything at once
 
 
 		mInputStream.seekg(0, std::ios::beg);
 		mInputStream.seekg(0, std::ios::beg);
 		mInputStream.read((char*)readBuffer, fileSize);
 		mInputStream.read((char*)readBuffer, fileSize);
@@ -53,7 +53,7 @@ namespace CamelotEngine
 		mInputStream.close();
 		mInputStream.close();
 		mInputStream.clear();
 		mInputStream.clear();
 
 
-		delete[] readBuffer;
+		CM_DELETE_BYTES(readBuffer, ScratchAlloc);
 
 
 		return object;
 		return object;
 	}
 	}

+ 2 - 2
CamelotUtility/Source/CmLog.cpp

@@ -17,12 +17,12 @@ namespace CamelotEngine
 		flush();
 		flush();
 
 
 		for(auto iter = mEntries.begin(); iter != mEntries.end(); ++iter)
 		for(auto iter = mEntries.begin(); iter != mEntries.end(); ++iter)
-			delete *iter;
+			CM_DELETE(*iter, LogEntry, PoolAlloc);
 	}
 	}
 
 
 	void Log::logMsg(const String& message, const String& level)
 	void Log::logMsg(const String& message, const String& level)
 	{
 	{
-		LogEntry* newEntry = new LogEntry(message, level);
+		LogEntry* newEntry = CM_NEW(LogEntry, PoolAlloc) LogEntry(message, level);
 		mEntries.push_back(newEntry);
 		mEntries.push_back(newEntry);
 
 
 		doOnEntryAdded(*newEntry);
 		doOnEntryAdded(*newEntry);

+ 2 - 2
CamelotUtility/Source/CmTexAtlasGenerator.cpp

@@ -17,7 +17,7 @@ namespace CamelotEngine
 		~TexAtlasNode()
 		~TexAtlasNode()
 		{
 		{
 			if(children != nullptr)
 			if(children != nullptr)
-				delete[] children;
+				CM_DELETE_ARRAY(children, TexAtlasNode, 2, ScratchAlloc);
 		}
 		}
 
 
 		UINT32 x, y, width, height;
 		UINT32 x, y, width, height;
@@ -60,7 +60,7 @@ namespace CamelotEngine
 				float dw = (float)(width - element.input.width);
 				float dw = (float)(width - element.input.width);
 				float dh = (height - element.input.height) * aspect;
 				float dh = (height - element.input.height) * aspect;
 
 
-				children = new TexAtlasNode[2];
+				children = CM_NEW_ARRAY(TexAtlasNode, 2, ScratchAlloc);
 
 
 				if (dw > dh)
 				if (dw > dh)
 				{
 				{

+ 2 - 2
CamelotUtility/Source/CmTime.cpp

@@ -8,13 +8,13 @@ namespace CamelotEngine
 	Time::Time()
 	Time::Time()
 		:mAppStartTime(0), mLastFrameTime(0), mFrameDelta(0.0f), mTimeSinceStart(0.0f), mCurrentFrame(0)
 		:mAppStartTime(0), mLastFrameTime(0), mFrameDelta(0.0f), mTimeSinceStart(0.0f), mCurrentFrame(0)
 	{
 	{
-		mTimer = new Timer();
+		mTimer = CM_NEW(Timer, GenAlloc) Timer();
 		mAppStartTime = mTimer->getMicroseconds();
 		mAppStartTime = mTimer->getMicroseconds();
 	}
 	}
 
 
 	Time::~Time()
 	Time::~Time()
 	{
 	{
-		delete mTimer;
+		CM_DELETE(mTimer, Timer, GenAlloc);
 	}
 	}
 
 
 	void Time::update()
 	void Time::update()