mikymod 13 лет назад
Родитель
Сommit
0bdd75ce4b

+ 4 - 7
src/CMakeLists.txt

@@ -61,7 +61,6 @@ set (HEADERS
 )
 
 set (CORE_SRC
-	core/Weak.cpp
 )
 
 set (CORE_HEADERS
@@ -72,7 +71,6 @@ set (CORE_HEADERS
 	core/Singleton.h
 	core/Types.h
 	core/VertexData.h
-	core/Weak.h
 	core/WithProperties.h
 )
 
@@ -157,14 +155,13 @@ set (STREAMS_HEADERS
 )
 
 set (MEM_SRC
-	#core/mem/MallocAllocator.cpp
+	core/mem/MallocAllocator.cpp
 )
 
 set (MEM_HEADERS
-	core/mem/Auto.h
-	core/mem/Shared.h
-	#core/mem/Allocator.h
-	#core/mem/MallocAllocator.h
+	core/mem/Memory.h
+	core/mem/Allocator.h
+	core/mem/MallocAllocator.h
 )
 
 set (INPUT_SRC

+ 3 - 2
src/Crown.h

@@ -26,13 +26,11 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 // Core
-#include "Auto.h"
 #include "Exceptions.h"
 #include "SignalSlot.h"
 #include "Types.h"
 #include "CoreEventArgs.h"
 //#include "WithProperties.h"
-#include "Shared.h"
 #include "Singleton.h"
 
 // Core/Math
@@ -70,6 +68,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Generic.h"
 
 // Core/Mem
+#include "Memory.h"
+#include "Allocator.h"
+#include "MallocAllocator.h"
 
 // Core/Streams
 #include "Stream.h"

+ 2 - 0
src/Mesh.cpp

@@ -30,11 +30,13 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "VertexBuffer.h"
 #include "IndexBuffer.h"
 #include "Renderer.h"
+#include "Allocator.h"
 
 namespace crown
 {
 
 Mesh::Mesh() :
+	mMeshChunkList(get_default_allocator()),
 	mVertexBuffer(NULL),
 	mIndexBuffer(NULL),
 	mGlobalVertexCount(0)

+ 4 - 1
src/MeshChunk.cpp

@@ -25,11 +25,14 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "MeshChunk.h"
 #include "Types.h"
+#include "Allocator.h"
 
 namespace crown
 {
 
-MeshChunk::MeshChunk()
+MeshChunk::MeshChunk() :
+	mVertexList(get_default_allocator()),
+	mFaceList(get_default_allocator())
 {
 }
 

+ 0 - 1
src/Texture.h

@@ -28,7 +28,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Types.h"
 #include "Color4.h"
 #include "Resource.h"
-#include "Shared.h"
 #include "Str.h"
 #include "Image.h"
 

+ 5 - 7
src/core/Delegate.h

@@ -25,8 +25,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "Weak.h"
-
 namespace crown
 {
 
@@ -59,7 +57,7 @@ public:
 	 { return new Delegate(mObject.GetPointer()); }
 
 protected:
-	Weak<TClass> mObject;
+	TClass* mObject;
 };
 
 /* Delegate with 1 argument */
@@ -102,7 +100,7 @@ public:
 	TArg0 GetDefaultArg0() const;
 
 protected:
-	Weak<TClass> mObject;
+	TClass* mObject;
 	MethodType mMethod;
 	TArg0 mDefaultArg0;
 };
@@ -173,7 +171,7 @@ public:
 	TArg1 GetDefaultArg1() const;
 
 protected:
-	Weak<TClass> mObject;
+	TClass* mObject;
 	MethodType mMethod;
 	TArg0 mDefaultArg0;
 	TArg1 mDefaultArg1;
@@ -259,7 +257,7 @@ public:
 	TArg2 GetDefaultArg2() const;
 
 protected:
-	Weak<TClass> mObject;
+	TClass* mObject;
 	MethodType mMethod;
 	TArg0 mDefaultArg0;
 	TArg1 mDefaultArg1;
@@ -362,7 +360,7 @@ public:
 	TArg3 GetDefaultArg3() const;
 
 protected:
-	Weak<TClass> mObject;
+	TClass* mObject;
 	MethodType mMethod;
 	TArg0 mDefaultArg0;
 	TArg1 mDefaultArg1;

+ 0 - 110
src/core/Weak.cpp

@@ -1,110 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "Weak.h"
-#include "Dictionary.h"
-#include "Shared.h"
-#include "List.h"
-#include "Log.h"
-
-namespace crown
-{
-
-typedef List<WeakReferenced**> ObjectPPList;
-typedef Shared<ObjectPPList> SharedObjectPPList;
-
-//Dictionary<WeakReferenced*, SharedObjectPPList> mWeakAssociations;
-
-WeakReferenced::WeakReferenced()
-{
-
-}
-
-int count = 0;
-
-WeakReferenced::~WeakReferenced()
-{
-	//Set all Weak pointers that point to this Object to NULL
-	WeakAssociationsStorage::UnRegisterAllByObject(this);
-	//Log::I(Str(count));
-	count += 1;
-}
-
-void WeakAssociationsStorage::Register(WeakReferenced** ptr)
-{
-	if (*ptr == NULL)
-	{
-		return;
-	}
-
-//	SharedObjectPPList& list = mWeakAssociations[*ptr];
-//	if (list.IsNull())
-//	{
-//		list = new ObjectPPList();
-//	}
-//	list->Append(ptr);
-}
-
-void WeakAssociationsStorage::UnRegister(WeakReferenced** ptr)
-{
-	if (*ptr == NULL)
-	{
-		return;
-	}
-
-//	SharedObjectPPList& list = mWeakAssociations[*ptr];
-
-//	int index = list->Find(ptr);
-//	if (index != -1)
-//	{
-//		list->Remove(index);
-//	}
-}
-
-void WeakAssociationsStorage::UnRegisterAllByObject(WeakReferenced* object)
-{
-	if (object == NULL)
-	{
-		return;
-	}
-
-	//SB NOTE: Here, when CRT does the cleanup of the whole dictionary upon application exit, we look for
-	//				 Weak pointers that point to the lists contained in the dictionary, since lists are themselves Objects
-	//				 The underlying RBTree has been designed to be re-entrant on its Clear method.
-//	SharedObjectPPList& list = mWeakAssociations[object];
-//	if (list.IsNull())
-//	{
-//		return;
-//	}
-
-//	ObjectPPList::Enumerator e = list->getBegin();
-//	while(e.next())
-//	{
-//		*e.current() = NULL;
-//	}
-//	mWeakAssociations.Remove(object);
-}
-
-}

+ 0 - 165
src/core/Weak.h

@@ -1,165 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-#include "Exceptions.h"
-
-namespace crown
-{
-
-class WeakReferenced
-{
-public:
-	WeakReferenced();
-	virtual ~WeakReferenced();
-};
-
-class WeakAssociationsStorage
-{
-public:
-	static void UnRegisterAllByObject(WeakReferenced* object);
-
-private:
-	WeakAssociationsStorage() {};
-	~WeakAssociationsStorage() {};
-
-	static void Register(WeakReferenced** ptr);
-	static void UnRegister(WeakReferenced** ptr);
-
-	template<typename T>
-	friend class Weak;
-};
-
-//! A Weak pointer that is set to NULL when the poined object is deleted
-//  T: Must inherit Object
-template<typename T>
-class Weak
-{
-public:
-	Weak(): mPtr(NULL) { }
-	Weak(WeakReferenced* obj): mPtr(obj)
-	{
-		Register();
-	}
-
-	Weak(const Weak<T>& obj)
-	{
-		mPtr = obj.mPtr;
-		Register();
-	}
-
-	~Weak()
-	{
-		UnRegister();
-	}
-
-	inline bool IsNull() const
-	{
-		return mPtr == 0;
-	}
-
-	inline bool IsValid() const
-	{
-		return mPtr != 0;
-	}
-
-	inline T& operator*() const
-	{
-		if (mPtr == 0)
-		{
-			throw NullPointerException("Dereferencing null Weak<T> pointer");
-		}
-
-		return *((T*)mPtr);
-	}
-
-	inline T* operator->() const
-	{
-		if (mPtr == 0)
-		{
-			throw NullPointerException("Dereferencing null Weak<T> pointer");
-		}
-
-		return (T*)mPtr;
-	}
-
-	Weak<T>& operator=(const Weak<T>& obj)
-	{
-		if (mPtr != 0)
-		{
-			UnRegister();
-		}
-
-		mPtr = obj.mPtr;
-		Register();
-		return *this;
-	}
-
-	Weak<T>& operator=(WeakReferenced* obj)
-	{
-		UnRegister();
-		mPtr = obj;
-		Register();
-		return *this;
-	}
-
-	inline bool operator==(const Weak<T>& obj) const
-	{
-		return (mPtr == obj.mPtr);
-	}
-
-	inline bool operator!=(const Weak<T>& obj) const
-	{
-		return (mPtr != obj.mPtr);
-	}
-
-	T* GetPointer()
-	{
-		return (T*)mPtr;
-	}
-
-	const T* GetPointer() const
-	{
-		return (const T*)mPtr;
-	}
-
-private:
-	WeakReferenced* mPtr;
-
-	inline void Register()
-	{
-		WeakAssociationsStorage::Register((WeakReferenced**)&mPtr);
-	}
-
-	inline void UnRegister()
-	{
-		WeakAssociationsStorage::UnRegister((WeakReferenced**)&mPtr);
-	}
-};
-
-} // namespace crown
-

+ 6 - 4
src/core/containers/Array.h

@@ -26,6 +26,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "Types.h"
+#include "Allocator.h"
 #include <cassert>
 
 namespace crown
@@ -33,16 +34,16 @@ namespace crown
 
 /**
 	Fixed-size array.
-
+@note
 	Does not grow in size.
-	Does not initialize memory.
+	Does not call constructors/destructors so it is not very suitable for non-POD items.
 */
 template <typename T, uint SIZE>
 class Array
 {
 public:
 
-							Array();
+							Array(Allocator& allocator);
 							Array(const Array<T, SIZE>& array);
 							~Array();
 
@@ -67,6 +68,7 @@ public:
 
 private:
 
+	Allocator*				m_allocator;
 	uint					m_count;			// The number of items
 	T						m_array[SIZE];
 };
@@ -77,7 +79,7 @@ private:
 	Does not initialize array memory.
 */
 template <typename T, uint SIZE>
-inline Array<T, SIZE>::Array() : m_count(0)
+inline Array<T, SIZE>::Array(Allocator& allocator) : m_allocator(&allocator), m_count(0)
 {
 	assert(SIZE > 0);
 

+ 21 - 16
src/core/containers/List.h

@@ -25,6 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include "Allocator.h"
 #include "Types.h"
 #include <cassert>
 #include <cstring>
@@ -33,16 +34,17 @@ namespace crown
 {
 
 /**
-	Dynamic array.
+	Dynamic array of POD items.
+@note
+	Does not call constructors/destructors so it is not very suitable for non-POD items.
 */
 template <typename T>
 class List
 {
-
 public:
 
-						List();
-						List(uint capacity);
+						List(Allocator& allocator);
+						List(Allocator& allocator, uint capacity);
 						List(const List<T>& list);
 						~List();
 
@@ -70,6 +72,7 @@ public:
 
 private:
 
+	Allocator*			m_allocator;
 	uint				m_capacity;
 	uint				m_size;
 	T*					m_array;
@@ -81,10 +84,11 @@ private:
 	Does not allocate memory.
 */
 template <typename T>
-inline List<T>::List() :
+inline List<T>::List(Allocator& allocator) :
+	m_allocator(&allocator),
 	m_capacity(0),
 	m_size(0),
-	m_array(0)
+	m_array(NULL)
 {
 }
 
@@ -94,10 +98,11 @@ inline List<T>::List() :
 	Allocates capacity * sizeof(T) bytes.
 */
 template <typename T>
-inline List<T>::List(uint capacity) :
+inline List<T>::List(Allocator& allocator, uint capacity) :
+	m_allocator(&allocator),
 	m_capacity(0),
 	m_size(0),
-	m_array(0)
+	m_array(NULL)
 {
 	set_capacity(capacity);
 }
@@ -109,7 +114,7 @@ template <typename T>
 inline List<T>::List(const List<T>& list) :
 	m_capacity(0),
 	m_size(0),
-	m_array(0)
+	m_array(NULL)
 {
 	*this = list;
 }
@@ -122,7 +127,7 @@ inline List<T>::~List()
 {
 	if (m_array)
 	{
-		delete[] m_array;
+		m_allocator->deallocate(m_array);
 	}
 }
 
@@ -204,13 +209,13 @@ inline void List<T>::set_capacity(uint capacity)
 		m_size = capacity;
 	}
 
-	m_array = new T[capacity];
+	m_array = (T*)m_allocator->allocate(capacity * sizeof(T));
 
-	memcpy(m_array, tmp, sizeof(T) * m_size);
+	memcpy(m_array, tmp, m_size * sizeof(T));
 
 	if (tmp)
 	{
-		delete[] tmp;
+		m_allocator->deallocate(tmp);
 	}
 }
 
@@ -277,7 +282,7 @@ inline const List<T>& List<T>::operator=(const List<T>& other)
 {
 	if (m_array)
 	{
-		delete[] m_array;
+		m_allocator->deallocate(m_array);
 	}
 
 	m_size = other.m_size;
@@ -285,9 +290,9 @@ inline const List<T>& List<T>::operator=(const List<T>& other)
 
 	if (m_capacity)
 	{
-		m_array = new T[m_capacity];
+		m_array = (T*)m_allocator->allocate(m_capacity * sizeof(T));
 
-		memcpy(m_array, other.m_array, m_size);
+		memcpy(m_array, other.m_array, m_size * sizeof(T));
 	}
 
 	return *this;

+ 2 - 1
src/core/containers/Str.h

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <cstring>
 #include "List.h"
 #include "Types.h"
+#include "Allocator.h"
 
 namespace crown
 {
@@ -684,7 +685,7 @@ inline void Str::Replace(const Str& toFind, const Str& toReplace)
 	if (mLength < toReplace.mLength)
 		return;
 
-	List<char> tmp;
+	List<char> tmp(get_default_allocator());
 
 	uint i;
 	for (i = 0; i < mLength - (toFind.mLength - 1); i++)

+ 55 - 0
src/core/mem/Allocator.h

@@ -0,0 +1,55 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Memory.h"
+
+namespace crown
+{
+
+class Allocator
+{
+public:
+
+						Allocator() {}
+	virtual				~Allocator() {}
+
+	virtual void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN) = 0;
+	virtual void		deallocate(void* data) = 0;
+	virtual size_t		allocated_size() = 0;
+
+private:
+
+	// Disable copying
+						Allocator(const Allocator&);
+	Allocator&			operator=(const Allocator&);
+};
+
+Allocator& get_default_allocator();
+
+} // namespace crown
+

+ 0 - 191
src/core/mem/Auto.h

@@ -1,191 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-#include <cassert>
-
-namespace crown
-{
-
-template <typename T>
-class Auto
-{
-
-public:
-
-					Auto();
-					Auto(T* obj);
-					Auto(const Auto<T>& obj);
-					~Auto();
-
-	bool			IsNull() const;
-
-	T&				operator*() const;
-	T*				operator->() const;
-
-	Auto<T>&		operator=(const Auto<T>& obj);
-	Auto<T>&		operator=(T* obj);
-
-	bool			operator==(const Auto<T>& obj) const;
-	bool			operator!=(const Auto<T>& obj) const;
-
-	T*				GetPointer();
-
-	T*				GetPointer(bool disown);
-	const T*		GetPointer() const;
-
-private:
-
-	T*				mPtr;
-};
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Auto<T>::Auto() : mPtr(NULL)
-{
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Auto<T>::Auto(T* obj) : mPtr(obj)
-{
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Auto<T>::Auto(const Auto<T>& obj)
-{
-	mPtr = obj.mPtr;
-	const_cast<Auto<T>& >(obj).mPtr = NULL;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Auto<T>::~Auto()
-{
-	if (mPtr)
-	{
-		delete mPtr;
-	}
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline bool Auto<T>::IsNull() const
-{
-	return mPtr == NULL;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline T& Auto<T>::operator*() const
-{
-	assert(mPtr != NULL);
-
-	return *mPtr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline T* Auto<T>::operator->() const
-{
-	assert(mPtr != NULL);
-
-	return mPtr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Auto<T>& Auto<T>::operator=(const Auto<T>& obj)
-{
-	if (mPtr != NULL)
-	{
-		delete mPtr;
-	}
-
-	mPtr = obj.mPtr;
-	obj.mPtr = NULL;
-	return *this;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Auto<T>& Auto<T>::operator=(T* obj)
-{
-	if (mPtr != NULL)
-	{
-		delete mPtr;
-	}
-
-	mPtr = obj;
-	return *this;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline bool Auto<T>::operator==(const Auto<T>& obj) const
-{
-	return (mPtr == obj.mPtr);
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline bool Auto<T>::operator!=(const Auto<T>& obj) const
-{
-	return (mPtr != obj.mPtr);
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline T* Auto<T>::GetPointer()
-{
-	return mPtr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline T* Auto<T>::GetPointer(bool disown)
-{
-	T* ptr = mPtr;
-
-	if (disown)
-	{
-		mPtr = NULL;
-	}
-
-	return ptr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline const T* Auto<T>::GetPointer() const
-{
-	return mPtr;
-}
-
-} // namespace crown
-

+ 129 - 0
src/core/mem/MallocAllocator.cpp

@@ -0,0 +1,129 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "MallocAllocator.h"
+#include <cstdlib>
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+MallocAllocator::MallocAllocator() :
+	m_allocated_size(0)
+{
+}
+
+//-----------------------------------------------------------------------------
+MallocAllocator::~MallocAllocator()
+{
+}
+
+//-----------------------------------------------------------------------------
+void* MallocAllocator::allocate(size_t size, size_t align)
+{
+	size_t actual_size = actual_allocation_size(size, align);
+
+	Header* h = (Header*)malloc(actual_size);
+	h->size = actual_size;
+
+	void* data = memory::align(h + 1, align);
+
+	pad(h, data);
+
+	m_allocated_size += actual_size;
+
+	return data;
+}
+
+//-----------------------------------------------------------------------------
+void MallocAllocator::deallocate(void* data)
+{
+	Header* h = header(data);
+
+	m_allocated_size -= h->size;
+
+	free(h);
+}
+
+//-----------------------------------------------------------------------------
+size_t MallocAllocator::allocated_size()
+{
+	return m_allocated_size;
+}
+
+//-----------------------------------------------------------------------------
+size_t MallocAllocator::get_size(void* data)
+{
+	Header* h = header(data);
+
+	return h->size;
+}
+
+//-----------------------------------------------------------------------------
+size_t MallocAllocator::actual_allocation_size(size_t size, size_t align)
+{
+	return size + align + sizeof(Header);
+}
+
+//-----------------------------------------------------------------------------
+MallocAllocator::Header* MallocAllocator::header(void* data)
+{
+	uint* ptr = (uint*)data;
+	ptr--;
+
+	while (*ptr == memory::PADDING_VALUE)
+	{
+		ptr--;
+	}
+
+	return (Header*)ptr;
+}
+
+//-----------------------------------------------------------------------------
+void* MallocAllocator::data(Header* header, size_t align)
+{
+	return memory::align(header + 1, align);
+}
+
+//-----------------------------------------------------------------------------
+void MallocAllocator::pad(Header* header, void* data)
+{
+	uint* p = (uint*)(header + 1);
+
+	while (p != (uint*)data)
+	{
+		*p = memory::PADDING_VALUE;
+		p++;
+	}
+}
+
+MallocAllocator default_allocator;
+Allocator& get_default_allocator()
+{
+	return default_allocator;
+}
+
+} // namespace crown
+

+ 63 - 0
src/core/mem/MallocAllocator.h

@@ -0,0 +1,63 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Allocator.h"
+
+namespace crown
+{
+
+class MallocAllocator : public Allocator
+{
+public:
+
+				MallocAllocator();
+				~MallocAllocator();
+
+	void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+	void		deallocate(void* data);
+
+	size_t		allocated_size();
+	size_t		get_size(void* data);
+
+private:
+
+	//! Holds the number of bytes of an allocation
+	struct Header
+	{
+		size_t	size;
+	};
+
+	size_t		actual_allocation_size(size_t size, size_t align);
+	Header*		header(void* data);
+	void*		data(Header* header, size_t align);
+	void		pad(Header* header, void* data);
+
+	size_t		m_allocated_size;
+};
+
+} // namespace crown
+

+ 51 - 0
src/core/mem/Memory.h

@@ -0,0 +1,51 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <cassert>
+#include <cstdio>
+#include "Types.h"
+
+namespace crown
+{
+namespace memory
+{
+
+const uint		PADDING_VALUE	= 0xFFFFFFFFu;	//!< Value used to fill unused memory
+const size_t	DEFAULT_ALIGN	= 4;			//!< Default memory alignment in bytes
+
+//! Returns the pointer p aligned to the desired align
+inline void* align(void* p, size_t align)
+{
+	uintptr_t ptr = (uintptr_t)p;
+
+	return (void*)(ptr + (align - ptr % align));
+}
+
+} // namespace memory
+} // namespace crown
+

+ 0 - 273
src/core/mem/Shared.h

@@ -1,273 +0,0 @@
-/*
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-
-namespace crown
-{
-
-class ReferenceCount
-{
-
-public:
-
-					ReferenceCount();
-					~ReferenceCount();
-
-	bool			IsZero();
-
-	void			Increase();
-	void			Decrease();
-
-	uint			GetReferenceCount();
-
-private:
-
-	void			AssertNotNull();
-
-	uint*			mReferences;
-};
-
-//-----------------------------------------------------------------------------
-inline ReferenceCount::ReferenceCount() : mReferences(NULL)
-{
-}
-
-//-----------------------------------------------------------------------------
-inline ReferenceCount::~ReferenceCount()
-{
-	if (mReferences != NULL && *mReferences == 0)
-	{
-		delete mReferences;
-	}
-}
-
-//-----------------------------------------------------------------------------
-inline bool ReferenceCount::IsZero()
-{
-	AssertNotNull();
-
-	return *mReferences == 0;
-}
-
-//-----------------------------------------------------------------------------
-inline void ReferenceCount::Increase()
-{
-	AssertNotNull();
-
-	*mReferences += 1;
-}
-
-//-----------------------------------------------------------------------------
-inline void ReferenceCount::Decrease()
-{
-	AssertNotNull();
-
-	*mReferences -= 1;
-}
-
-//-----------------------------------------------------------------------------
-inline uint ReferenceCount::GetReferenceCount()
-{
-	AssertNotNull();
-
-	return *mReferences;
-}
-
-//-----------------------------------------------------------------------------
-inline void ReferenceCount::AssertNotNull()
-{
-	if (mReferences == NULL)
-	{
-		mReferences = new uint;
-		*mReferences = 0;
-	}
-}
-
-/**
-	Standard reference counted pointer.
-*/
-template <typename T>
-class Shared
-{
-
-public:
-
-					Shared();
-					Shared(T* obj);
-					Shared(Shared<T>& obj);
-					~Shared();
-
-	bool			IsNull() const;
-
-	T&				operator*() const;
-	T*				operator->() const;
-
-	Shared<T>&		operator=(Shared<T>& obj);
-
-	bool			operator==(const Shared<T>& obj) const;
-	bool			operator!=(const Shared<T>& obj) const;
-
-	T*				GetPointer();
-	const T*		GetPointer() const;
-
-	uint			GetReferenceCount();
-
-private:
-
-	T*				mPtr;
-	ReferenceCount	mReferenceCount;
-};
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Shared<T>::Shared() : mPtr(NULL)
-{
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Shared<T>::Shared(T* obj) : mPtr(obj)
-{
-	if (mPtr != NULL)
-	{
-		mReferenceCount.Increase();
-	}
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Shared<T>::Shared(Shared<T>& obj)
-{
-	mPtr = obj.mPtr;
-	mReferenceCount = obj.mReferenceCount;
-
-	if (mPtr != NULL)
-	{
-		obj.mReferenceCount.Increase();
-	}
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Shared<T>::~Shared()
-{
-	if (mPtr != NULL)
-	{
-		mReferenceCount.Decrease();
-
-		if (mReferenceCount.IsZero())
-		{
-			delete mPtr;
-		}
-	}
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline bool Shared<T>::IsNull() const
-{
-	return mPtr == 0;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline T& Shared<T>::operator*() const
-{
-	assert(mPtr != NULL);
-
-	return *mPtr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline T* Shared<T>::operator->() const
-{
-	assert(mPtr != NULL);
-
-	return mPtr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline Shared<T>& Shared<T>::operator=(Shared<T>& obj)
-{
-	if (mPtr != NULL)
-	{
-		mReferenceCount.Decrease();
-
-		if (mReferenceCount.IsZero())
-		{
-			delete mPtr;
-		}
-	}
-
-	mPtr = obj.mPtr;
-	mReferenceCount = obj.mReferenceCount;
-
-	mReferenceCount.Increase();
-
-	return *this;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline bool Shared<T>::operator==(const Shared<T>& obj) const
-{
-	return (mPtr == obj.mPtr);
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline bool Shared<T>::operator!=(const Shared<T>& obj) const
-{
-	return (mPtr != obj.mPtr);
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline T* Shared<T>::GetPointer()
-{
-	return mPtr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline const T* Shared<T>::GetPointer() const
-{
-	return mPtr;
-}
-
-//-----------------------------------------------------------------------------
-template <typename T>
-inline uint Shared<T>::GetReferenceCount()
-{
-	return mReferenceCount.GetReferenceCount();
-}
-
-} // namespace crown
-

+ 14 - 0
src/input/EventDispatcher.cpp

@@ -24,10 +24,24 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "EventDispatcher.h"
+#include "Allocator.h"
 
 namespace crown
 {
 
+//-----------------------------------------------------------------------------
+EventDispatcher::EventDispatcher() :
+	mMouseListenerList(get_default_allocator()),
+	mKeyboardListenerList(get_default_allocator()),
+	mTouchListenerList(get_default_allocator())
+{
+}
+
+//-----------------------------------------------------------------------------
+EventDispatcher::~EventDispatcher()
+{
+}
+
 //-----------------------------------------------------------------------------
 void EventDispatcher::AddMouseListener(MouseListener* listener)
 {

+ 2 - 2
src/input/EventDispatcher.h

@@ -41,8 +41,8 @@ class EventDispatcher
 
 public:
 
-	EventDispatcher() {}
-	~EventDispatcher() {}
+	EventDispatcher();
+	~EventDispatcher();
 
 	void AddMouseListener(MouseListener* listener);
 	void AddKeyboardListener(KeyboardListener* listener);

+ 9 - 9
src/os/OS.h

@@ -165,11 +165,11 @@ class UDPSocket
 {
 	public:
 
-					// Constructor
-					UDPSocket();
-					// Destructor
-					~UDPSocket();
-					// Open connection
+				// Constructor
+				UDPSocket();
+				// Destructor
+				~UDPSocket();
+				// Open connection
 		bool 		open(ushort port);
 					 // Send data through socket
 		bool 		send(IPv4Address &receiver, const void* data, int size );
@@ -190,10 +190,10 @@ class TCPSocket
 {
 	public:
 
-					// Constructor
-					TCPSocket();
-					// Destructor
-					~TCPSocket();
+				// Constructor
+				TCPSocket();
+				// Destructor
+				~TCPSocket();
 					// Open connection (server side)
 		bool 		open(ushort port);
 					// Connect (client side)

+ 5 - 0
src/renderers/gl/GLRenderer.cpp

@@ -36,6 +36,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Log.h"
 #include "Material.h"
 #include "Rect.h"
+#include "Allocator.h"
 
 #include "Config.h"
 #if defined(WINDOWS)
@@ -61,6 +62,10 @@ GLRenderer::GLRenderer() :
 	mMaxVertexIndices(0),
 	mMaxVertexVertices(0),
 
+	mOcclusionQueryList(get_default_allocator()),
+	mVertexBufferList(get_default_allocator()),
+	mIndexBufferList(get_default_allocator()),
+
 	mMaxAnisotropy(0.0f),
 
 	mActiveTextureUnit(0),