Jelajahi Sumber

Organize core/mem in a sane way

Daniele Bartolini 12 tahun lalu
induk
melakukan
f64fc506aa

+ 10 - 38
engine/core/mem/Allocator.h

@@ -26,10 +26,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include <new>
-
 #include "Types.h"
-#include "Memory.h"
 
 namespace crown
 {
@@ -41,52 +38,27 @@ class Allocator
 {
 public:
 
-						Allocator() {}
-	virtual				~Allocator() {}
+	Allocator() {}
+	virtual	~Allocator() {}
 
 	/// Allocates @a size bytes of memory aligned to the specified
 	/// @a align byte and returns a pointer to the first allocated byte.
-	virtual void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN) = 0;
+	virtual void* allocate(size_t size, size_t align = DEFAULT_ALIGN) = 0;
 
 	/// Deallocates a previously allocated block of memory pointed by @a data.
-	virtual void		deallocate(void* data) = 0;
+	virtual void deallocate(void* data) = 0;
 
 	/// Returns the total number of bytes allocated.
-	virtual size_t		allocated_size() = 0;
+	virtual size_t allocated_size() = 0;
+
+	/// Default memory alignment in bytes.
+	static const size_t DEFAULT_ALIGN = 4;
 
 private:
 
 	// Disable copying
-						Allocator(const Allocator&);
-	Allocator&			operator=(const Allocator&);
+	Allocator(const Allocator&);
+	Allocator& operator=(const Allocator&);
 };
 
-CE_EXPORT Allocator& default_allocator();
-
-/// Respects standard behaviour when calling on NULL @a ptr
-template <typename T>
-inline void call_destructor_and_deallocate(Allocator& a, T* ptr)
-{
-	if (ptr != NULL)
-	{
-		ptr->~T();
-
-		a.deallocate(ptr);
-	}
-}
-
-/// Allocates memory with @a allocator for the given @a T type
-/// and calls constructor on it.
-/// @note
-/// @a allocator must be a reference to an existing allocator.
-#define CE_NEW(allocator, T)\
-	new ((allocator).allocate(sizeof(T), CE_ALIGNOF(T))) T
-
-/// Calls destructor on @a ptr and deallocates memory using the
-/// given @a allocator.
-/// @note
-/// @a allocator must be a reference to an existing allocator.
-#define CE_DELETE(allocator, ptr)\
-	call_destructor_and_deallocate(allocator, ptr)
-
 } // namespace crown

+ 5 - 1
engine/core/mem/LinearAllocator.cpp

@@ -25,13 +25,17 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "LinearAllocator.h"
+#include "Memory.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
 LinearAllocator::LinearAllocator(Allocator& backing, size_t size)
-	: m_backing(&backing), m_physical_start(NULL), m_total_size(size), m_offset(0)
+	: m_backing(&backing)
+	, m_physical_start(NULL)
+	, m_total_size(size)
+	, m_offset(0)
 {
 	m_physical_start = backing.allocate(size);
 }

+ 11 - 11
engine/core/mem/LinearAllocator.h

@@ -39,32 +39,32 @@ class LinearAllocator : public Allocator
 {
 public:
 
-				LinearAllocator(Allocator& backing, size_t size);
-				LinearAllocator(void* start, size_t size);
-				~LinearAllocator();
+	LinearAllocator(Allocator& backing, size_t size);
+	LinearAllocator(void* start, size_t size);
+	~LinearAllocator();
 
 	/// @copydoc Allocator::allocate()
-	void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
 	/// @note
 	/// The linear allocator does not support deallocating
 	/// individual allocations, rather you have to call
 	/// clear() to free all memory at once.
-	void		deallocate(void* data);
+	void deallocate(void* data);
 
 	/// Frees all the allocations made by allocate()
-	void		clear();
+	void clear();
 
 	/// @copydoc Allocator::allocated_size()
-	size_t		allocated_size();
+	size_t allocated_size();
 
 private:
 
-	Allocator*	m_backing;
-	void*		m_physical_start;
-	size_t		m_total_size;
-	size_t		m_offset;
+	Allocator* m_backing;
+	void* m_physical_start;
+	size_t m_total_size;
+	size_t m_offset;
 };
 
 } // namespace crown

+ 1 - 1
engine/core/mem/Memory.cpp

@@ -79,7 +79,7 @@ public:
 	}
 
 	/// @copydoc Allocator::allocate()
-	void* allocate(size_t size, size_t align = memory::DEFAULT_ALIGN)
+	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN)
 	{
 		ScopedMutex sm(m_mutex);
 

+ 28 - 2
engine/core/mem/Memory.h

@@ -26,18 +26,22 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <new>
 #include "Types.h"
 #include "Assert.h"
+#include "Allocator.h"
 
 namespace crown
 {
 
+CE_EXPORT Allocator& default_allocator();
+
 /// @defgroup Memory Memory
 namespace memory
 {
 
-const uint32_t	PADDING_VALUE	= 0xFFFFFFFFu;	//!< Value used to fill unused memory
-const size_t	DEFAULT_ALIGN	= 4;			//!< Default memory alignment in bytes
+/// Value used to fill unused memory
+const uint32_t PADDING_VALUE = 0xFFFFFFFFu;
 
 /// Constructs the initial default allocators.
 /// @note
@@ -67,5 +71,27 @@ inline void* align_top(void* p, size_t align)
 	return (void*) ptr;
 }
 
+/// Respects standard behaviour when calling on NULL @a ptr
+template <typename T>
+inline void call_destructor_and_deallocate(Allocator& a, T* ptr)
+{
+	if (!ptr)
+		return;
+
+	ptr->~T();
+	a.deallocate(ptr);
+}
+
+/// Allocates memory with @a allocator for the given @a T type
+/// and calls constructor on it.
+/// @note
+/// @a allocator must be a reference to an existing allocator.
+#define CE_NEW(allocator, T) new ((allocator).allocate(sizeof(T), CE_ALIGNOF(T))) T
+
+/// Calls destructor on @a ptr and deallocates memory using the
+/// given @a allocator.
+/// @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

+ 1 - 0
engine/core/mem/PoolAllocator.cpp

@@ -25,6 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "PoolAllocator.h"
+#include "Assert.h"
 
 namespace crown
 {

+ 11 - 13
engine/core/mem/PoolAllocator.h

@@ -26,8 +26,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "Types.h"
-#include "Memory.h"
 #include "Allocator.h"
 
 namespace crown
@@ -43,31 +41,31 @@ public:
 
 	/// Uses @a backing to allocate the memory pool for containing exactly
 	/// @a num_blocks blocks of @a block_size size each aligned to @a block_align.
-				PoolAllocator(Allocator& backing, size_t num_blocks, size_t block_size, size_t block_align = memory::DEFAULT_ALIGN);
-				~PoolAllocator();
+	PoolAllocator(Allocator& backing, size_t num_blocks, size_t block_size, size_t block_align = Allocator::DEFAULT_ALIGN);
+	~PoolAllocator();
 
 	/// Allocates a block of memory from the memory pool.
 	/// @note
 	/// The @a size and @a align must match those passed to PoolAllocator::PoolAllocator()
-	void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
-	void		deallocate(void* data);
+	void deallocate(void* data);
 
 	/// @copydoc Allocator::allocated_size()
-	size_t		allocated_size();
+	size_t allocated_size();
 
 private:
 
 	Allocator&	m_backing;
 
-	void*		m_start;
-	void*		m_freelist;
-	size_t		m_block_size;
-	size_t		m_block_align;
+	void* m_start;
+	void* m_freelist;
+	size_t m_block_size;
+	size_t m_block_align;
 
-	uint32_t	m_num_allocations;
-	size_t		m_allocated_size;
+	uint32_t m_num_allocations;
+	size_t m_allocated_size;
 };
 
 } // namespace crown

+ 6 - 6
engine/core/mem/ProxyAllocator.cpp

@@ -26,8 +26,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Assert.h"
 #include "ProxyAllocator.h"
-#include "Allocator.h"
 #include "StringUtils.h"
+#include "ScopedMutex.h"
 
 namespace crown
 {
@@ -36,11 +36,11 @@ static ProxyAllocator* g_proxy_allocators_head = NULL;
 static Mutex g_proxy_allocators_mutex;
 
 //-----------------------------------------------------------------------------
-ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator) :
-	m_allocator(allocator),
-	m_name(name),
-	m_total_allocated(0),
-	m_next(NULL)
+ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator)
+	: m_allocator(allocator)
+	, m_name(name)
+	, m_total_allocated(0)
+	, m_next(NULL)
 {
 	ScopedMutex sm(g_proxy_allocators_mutex);
 

+ 14 - 16
engine/core/mem/ProxyAllocator.h

@@ -26,10 +26,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include "Types.h"
-#include "Memory.h"
 #include "Allocator.h"
-#include "ScopedMutex.h"
+#include "Macros.h"
 
 namespace crown
 {
@@ -44,44 +42,44 @@ class CE_EXPORT ProxyAllocator : public Allocator
 public:
 
 	/// Tag all allocations made with @a allocator by the given @a name
-							ProxyAllocator(const char* name, Allocator& allocator);
+	ProxyAllocator(const char* name, Allocator& allocator);
 
 	/// @copydoc Allocator::allocate()
-	void*					allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
-	void					deallocate(void* data);
+	void deallocate(void* data);
 
 	/// @copydoc Allocator::allocated_size()
-	size_t					allocated_size();
+	size_t allocated_size();
 
 	/// Returns the name of the proxy allocator
-	const char*				name() const;
+	const char* name() const;
 
 public:
 
 	/// Returns the total number of proxy allocators.
 	/// in the global list.
-	static uint32_t			count();
+	static uint32_t count();
 
 	/// Returns the proxy allocator @name or NULL if not found.
-	static ProxyAllocator*	find(const char* name);
+	static ProxyAllocator* find(const char* name);
 
 	/// Returns the first proxy allocator in the global list or
 	/// NULL if the list is empty.
-	static ProxyAllocator*	begin();
+	static ProxyAllocator* begin();
 
 	/// Returns the next proxy allocator to @a a in the global list
 	/// or NULL if end-of-list is reached.
-	static ProxyAllocator*	next(ProxyAllocator* a);
+	static ProxyAllocator* next(ProxyAllocator* a);
 
 private:
 
-	Allocator&				m_allocator;
-	const char*				m_name;
-	size_t					m_total_allocated;
+	Allocator& m_allocator;
+	const char* m_name;
+	size_t m_total_allocated;
 
-	ProxyAllocator*			m_next;
+	ProxyAllocator* m_next;
 };
 
 } // namespace crown

+ 6 - 6
engine/core/mem/StackAllocator.cpp

@@ -25,17 +25,17 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "StackAllocator.h"
-#include "OS.h"
+#include "Memory.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-StackAllocator::StackAllocator(void* start, size_t size) :
-	m_physical_start(start),
-	m_total_size(size),
-	m_top(start),
-	m_allocation_count(0)
+StackAllocator::StackAllocator(void* start, size_t size)
+	: m_physical_start(start)
+	, m_total_size(size)
+	, m_top(start)
+	, m_allocation_count(0)
 {
 }
 

+ 9 - 9
engine/core/mem/StackAllocator.h

@@ -40,20 +40,20 @@ class StackAllocator : public Allocator
 {
 public:
 
-				StackAllocator(void* start, size_t size);
-				~StackAllocator();
+	StackAllocator(void* start, size_t size);
+	~StackAllocator();
 
 	/// @copydoc Allocator::allocate()
-	void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
 	/// @note
 	/// Deallocations must occur in LIFO order i.e. the
 	/// last allocation must be freed for first.
-	void		deallocate(void* data);
+	void deallocate(void* data);
 
 	/// @copydoc Allocator::allocated_size()
-	size_t		allocated_size();
+	size_t allocated_size();
 
 private:
 
@@ -63,12 +63,12 @@ private:
 		uint32_t alloc_id;
 	};
 
-	void*		m_physical_start;
-	size_t		m_total_size;
+	void* m_physical_start;
+	size_t m_total_size;
 
-	void*		m_top;
+	void* m_top;
 
-	uint32_t	m_allocation_count;
+	uint32_t m_allocation_count;
 };
 
 } // namespace crown

+ 11 - 10
engine/core/mem/TempAllocator.h

@@ -27,6 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "Allocator.h"
+#include "Memory.h"
 
 namespace crown
 {
@@ -44,28 +45,28 @@ public:
 
 	/// Uses the @a backing allocator when internal memory is exahusted
 	/// or the allocation size exceeds the remaining storage.
-				TempAllocator(Allocator& backing = default_allocator());
-				~TempAllocator();
+	TempAllocator(Allocator& backing = default_allocator());
+	~TempAllocator();
 
 	/// @copydoc Allocator::allocate()
-	void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
 
 	/// Does nothing, the memory is automatically freed when the
 	/// allocator is destroyed.
-	void		deallocate(void* data);
+	void deallocate(void* data);
 
 	/// @copydoc Allocator::allocated_size()
-	size_t		allocated_size();
+	size_t allocated_size();
 
 private:
 
 	Allocator&	m_backing;
 
-	char*		m_begin;
-	char*		m_end;
-	char*		m_cur;
-	size_t		m_chunk_size;
-	char		m_buffer[SIZE];
+	char* m_begin;
+	char* m_end;
+	char* m_cur;
+	size_t m_chunk_size;
+	char m_buffer[SIZE];
 };
 
 typedef TempAllocator<64> TempAllocator64;