浏览代码

Put global memory stuff in memory_globals

Daniele Bartolini 11 年之前
父节点
当前提交
912052443d
共有 2 个文件被更改,包括 142 次插入137 次删除
  1. 105 103
      engine/core/mem/memory.cpp
  2. 37 34
      engine/core/mem/memory.h

+ 105 - 103
engine/core/mem/memory.cpp

@@ -60,147 +60,149 @@ namespace crown
 {
 namespace memory
 {
-
-/// Allocator based on C malloc().
-class HeapAllocator : public Allocator
-{
-public:
-
-	HeapAllocator()
-		: m_allocated_size(0)
-		, m_allocation_count(0)
+	/// Allocator based on C malloc().
+	class HeapAllocator : public Allocator
 	{
-	}
-
-	~HeapAllocator()
-	{
-		CE_ASSERT(m_allocation_count == 0 && allocated_size() == 0,
-			"Missing %d deallocations causing a leak of %ld bytes", m_allocation_count, allocated_size());
-	}
+	public:
 
-	/// @copydoc Allocator::allocate()
-	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN)
-	{
-		ScopedMutex sm(m_mutex);
+		HeapAllocator()
+			: m_allocated_size(0)
+			, m_allocation_count(0)
+		{
+		}
 
-		size_t actual_size = actual_allocation_size(size, align);
+		~HeapAllocator()
+		{
+			CE_ASSERT(m_allocation_count == 0 && allocated_size() == 0,
+				"Missing %d deallocations causing a leak of %ld bytes", m_allocation_count, allocated_size());
+		}
 
-		Header* h = (Header*)malloc(actual_size);
-		h->size = actual_size;
+		/// @copydoc Allocator::allocate()
+		void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN)
+		{
+			ScopedMutex sm(m_mutex);
 
-		void* data = memory::align_top(h + 1, align);
+			size_t actual_size = actual_allocation_size(size, align);
 
-		pad(h, data);
+			Header* h = (Header*)malloc(actual_size);
+			h->size = actual_size;
 
-		m_allocated_size += actual_size;
-		m_allocation_count++;
+			void* data = memory::align_top(h + 1, align);
 
-		return data;
-	}
+			pad(h, data);
 
-	/// @copydoc Allocator::deallocate()
-	void deallocate(void* data)
-	{
-		ScopedMutex sm(m_mutex);
+			m_allocated_size += actual_size;
+			m_allocation_count++;
 
-		if (!data)
-			return;
+			return data;
+		}
 
-		Header* h = header(data);
+		/// @copydoc Allocator::deallocate()
+		void deallocate(void* data)
+		{
+			ScopedMutex sm(m_mutex);
 
-		m_allocated_size -= h->size;
-		m_allocation_count--;
+			if (!data)
+				return;
 
-		free(h);
-	}
+			Header* h = header(data);
 
-	/// @copydoc Allocator::allocated_size()
-	size_t allocated_size()
-	{
-		ScopedMutex sm(m_mutex);
-		return m_allocated_size;
-	}
+			m_allocated_size -= h->size;
+			m_allocation_count--;
 
-	/// Returns the size in bytes of the block of memory pointed by @a data
-	size_t get_size(void* data)
-	{
-		ScopedMutex sm(m_mutex);
-		Header* h = header(data);
-		return h->size;
-	}
+			free(h);
+		}
 
-private:
+		/// @copydoc Allocator::allocated_size()
+		size_t allocated_size()
+		{
+			ScopedMutex sm(m_mutex);
+			return m_allocated_size;
+		}
 
-	// Holds the number of bytes of an allocation
-	struct Header
-	{
-		uint32_t	size;
-	};
+		/// Returns the size in bytes of the block of memory pointed by @a data
+		size_t get_size(void* data)
+		{
+			ScopedMutex sm(m_mutex);
+			Header* h = header(data);
+			return h->size;
+		}
 
-	//-----------------------------------------------------------------------------
-	size_t actual_allocation_size(size_t size, size_t align)
-	{
-		return size + align + sizeof(Header);
-	}
+	private:
 
-	//-----------------------------------------------------------------------------
-	Header* header(void* data)
-	{
-		uint32_t* ptr = (uint32_t*)data;
-		ptr--;
+		// Holds the number of bytes of an allocation
+		struct Header
+		{
+			uint32_t	size;
+		};
 
-		while (*ptr == memory::PADDING_VALUE)
+		//-----------------------------------------------------------------------------
+		size_t actual_allocation_size(size_t size, size_t align)
 		{
-			ptr--;
+			return size + align + sizeof(Header);
 		}
 
-		return (Header*)ptr;
-	}
+		//-----------------------------------------------------------------------------
+		Header* header(void* data)
+		{
+			uint32_t* ptr = (uint32_t*)data;
+			ptr--;
 
-	//-----------------------------------------------------------------------------
-	void* data(Header* header, size_t align)
-	{
-		return memory::align_top(header + 1, align);
-	}
+			while (*ptr == memory::PADDING_VALUE)
+			{
+				ptr--;
+			}
 
-	//-----------------------------------------------------------------------------
-	void pad(Header* header, void* data)
-	{
-		uint32_t* p = (uint32_t*)(header + 1);
+			return (Header*)ptr;
+		}
 
-		while (p != data)
+		//-----------------------------------------------------------------------------
+		void* data(Header* header, size_t align)
 		{
-			*p = memory::PADDING_VALUE;
-			p++;
+			return memory::align_top(header + 1, align);
 		}
-	}
 
-private:
+		//-----------------------------------------------------------------------------
+		void pad(Header* header, void* data)
+		{
+			uint32_t* p = (uint32_t*)(header + 1);
 
-	Mutex		m_mutex;
-	size_t		m_allocated_size;
-	uint32_t	m_allocation_count;
-};
+			while (p != data)
+			{
+				*p = memory::PADDING_VALUE;
+				p++;
+			}
+		}
 
-// Create default allocators
-static char buffer[1024];
-static HeapAllocator* g_default_allocator = NULL;
+	private:
 
-void init()
-{
-	g_default_allocator = new (buffer) HeapAllocator();
-}
+		Mutex		m_mutex;
+		size_t		m_allocated_size;
+		uint32_t	m_allocation_count;
+	};
+} // namespace memory
 
-void shutdown()
+namespace memory_globals
 {
-	g_default_allocator->~HeapAllocator();
-}
+	using namespace memory;
+	// Create default allocators
+	char _buffer[1024];
+	HeapAllocator* _default_allocator = NULL;
 
-} // namespace memory
+	void init()
+	{
+		_default_allocator = new (_buffer) HeapAllocator();
+	}
+
+	void shutdown()
+	{
+		_default_allocator->~HeapAllocator();
+	}
+} // namespace memory_globals
 
 Allocator& default_allocator()
 {
-	return *memory::g_default_allocator;
+	return *memory_globals::_default_allocator;
 }
 
 } // namespace crown

+ 37 - 34
engine/core/mem/memory.h

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "types.h"
 #include "assert.h"
 #include "allocator.h"
+#include "macros.h"
 
 namespace crown
 {
@@ -39,48 +40,52 @@ Allocator& default_allocator();
 /// @defgroup Memory Memory
 namespace memory
 {
+	/// Value used to fill unused memory
+	const uint32_t PADDING_VALUE = 0xFFFFFFFFu;
 
-/// Value used to fill unused memory
-const uint32_t PADDING_VALUE = 0xFFFFFFFFu;
+	/// Returns the pointer @a p aligned to the desired @a align byte
+	inline void* align_top(void* p, size_t align)
+	{
+		CE_ASSERT(align >= 1, "Alignment must be > 1");
+		CE_ASSERT(align % 2 == 0 || align == 1, "Alignment must be a power of two");
 
-/// Constructs the initial default allocators.
-/// @note
-/// Has to be called before anything else during the engine startup.
-void init();
+		uintptr_t ptr = (uintptr_t)p;
 
-/// Destroys the allocators created with memory::init().
-/// @note
-/// Should be the last call of the program.
-void shutdown();
+		const size_t mod = ptr % align;
 
-/// Returns the pointer @a p aligned to the desired @a align byte
-inline void* align_top(void* p, size_t align)
-{
-	CE_ASSERT(align >= 1, "Alignment must be > 1");
-	CE_ASSERT(align % 2 == 0 || align == 1, "Alignment must be a power of two");
+		if (mod)
+		{
+			ptr += align - mod;
+		}
 
-	uintptr_t ptr = (uintptr_t)p;
-
-	const size_t mod = ptr % align;
+		return (void*) ptr;
+	}
 
-	if (mod)
+	/// Respects standard behaviour when calling on NULL @a ptr
+	template <typename T>
+	inline void call_destructor_and_deallocate(Allocator& a, T* ptr)
 	{
-		ptr += align - mod;
-	}
+		if (!ptr)
+			return;
 
-	return (void*) ptr;
-}
+		ptr->~T();
+		a.deallocate(ptr);
+	}
+} // namespace memory
 
-/// Respects standard behaviour when calling on NULL @a ptr
-template <typename T>
-inline void call_destructor_and_deallocate(Allocator& a, T* ptr)
+namespace memory_globals
 {
-	if (!ptr)
-		return;
-
-	ptr->~T();
-	a.deallocate(ptr);
-}
+	/// Constructs the initial default allocators.
+	/// @note
+	/// Has to be called before anything else during the engine startup.
+	void init();
+
+	/// Destroys the allocators created with memory_globals::init().
+	/// @note
+	/// Should be the last call of the program.
+	void shutdown();
+} // namespace memory_globals
+} // namespace crown
 
 /// Allocates memory with @a allocator for the given @a T type
 /// and calls constructor on it.
@@ -93,5 +98,3 @@ inline void call_destructor_and_deallocate(Allocator& a, T* ptr)
 /// @note
 /// @a allocator must be a reference to an existing allocator.
 #define CE_DELETE(allocator, ptr) crown::memory::call_destructor_and_deallocate(allocator, ptr)
-} // namespace memory
-} // namespace crown