Daniele Bartolini před 11 roky
rodič
revize
31144a1756

+ 17 - 14
engine/core/mem/linear_allocator.cpp

@@ -32,29 +32,32 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 LinearAllocator::LinearAllocator(Allocator& backing, size_t size)
-	: m_backing(&backing)
-	, m_physical_start(NULL)
-	, m_total_size(size)
-	, m_offset(0)
+	: _backing(&backing)
+	, _physical_start(NULL)
+	, _total_size(size)
+	, _offset(0)
 {
-	m_physical_start = backing.allocate(size);
+	_physical_start = backing.allocate(size);
 }
 
 //-----------------------------------------------------------------------------
 LinearAllocator::LinearAllocator(void* start, size_t size)
-	: m_backing(NULL), m_physical_start(start), m_total_size(size), m_offset(0)
+	: _backing(NULL)
+	, _physical_start(start)
+	, _total_size(size)
+	, _offset(0)
 {
 }
 
 //-----------------------------------------------------------------------------
 LinearAllocator::~LinearAllocator()
 {
-	if (m_backing)
+	if (_backing)
 	{
-		m_backing->deallocate(m_physical_start);
+		_backing->deallocate(_physical_start);
 	}
 
-	CE_ASSERT(m_offset == 0, "Memory leak of %ld bytes, maybe you forgot to call clear()?", m_offset);
+	CE_ASSERT(_offset == 0, "Memory leak of %ld bytes, maybe you forgot to call clear()?", _offset);
 }
 
 //-----------------------------------------------------------------------------
@@ -63,14 +66,14 @@ void* LinearAllocator::allocate(size_t size, size_t align)
 	const size_t actual_size = size + align;
 
 	// Memory exhausted
-	if (m_offset + actual_size > m_total_size)
+	if (_offset + actual_size > _total_size)
 	{
 		return NULL;
 	}
 
-	void* user_ptr = memory::align_top((char*) m_physical_start + m_offset, align);
+	void* user_ptr = memory::align_top((char*) _physical_start + _offset, align);
 
-	m_offset += actual_size;
+	_offset += actual_size;
 
 	return user_ptr;
 }
@@ -84,13 +87,13 @@ void LinearAllocator::deallocate(void* /*data*/)
 //-----------------------------------------------------------------------------
 void LinearAllocator::clear()
 {
-	m_offset = 0;
+	_offset = 0;
 }
 
 //-----------------------------------------------------------------------------
 size_t LinearAllocator::allocated_size()
 {
-	return m_offset;
+	return _offset;
 }
 
 } // namespace crown

+ 6 - 5
engine/core/mem/linear_allocator.h

@@ -50,7 +50,7 @@ public:
 	/// @note
 	/// The linear allocator does not support deallocating
 	/// individual allocations, rather you have to call
-	/// clear() to free all memory at once.
+	/// clear() to free all allocated memory at once.
 	void deallocate(void* data);
 
 	/// Frees all the allocations made by allocate()
@@ -61,10 +61,11 @@ public:
 
 private:
 
-	Allocator* m_backing;
-	void* m_physical_start;
-	size_t m_total_size;
-	size_t m_offset;
+	Allocator* _backing;
+	
+	void* _physical_start;
+	size_t _total_size;
+	size_t _offset;
 };
 
 } // namespace crown

+ 16 - 16
engine/core/mem/memory.cpp

@@ -66,21 +66,21 @@ namespace memory
 	public:
 
 		HeapAllocator()
-			: m_allocated_size(0)
-			, m_allocation_count(0)
+			: _allocated_size(0)
+			, _allocation_count(0)
 		{
 		}
 
 		~HeapAllocator()
 		{
-			CE_ASSERT(m_allocation_count == 0 && allocated_size() == 0,
-				"Missing %d deallocations causing a leak of %ld bytes", m_allocation_count, allocated_size());
+			CE_ASSERT(_allocation_count == 0 && allocated_size() == 0,
+				"Missing %d deallocations causing a leak of %ld bytes", _allocation_count, allocated_size());
 		}
 
 		/// @copydoc Allocator::allocate()
 		void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN)
 		{
-			ScopedMutex sm(m_mutex);
+			ScopedMutex sm(_mutex);
 
 			size_t actual_size = actual_allocation_size(size, align);
 
@@ -91,8 +91,8 @@ namespace memory
 
 			pad(h, data);
 
-			m_allocated_size += actual_size;
-			m_allocation_count++;
+			_allocated_size += actual_size;
+			_allocation_count++;
 
 			return data;
 		}
@@ -100,15 +100,15 @@ namespace memory
 		/// @copydoc Allocator::deallocate()
 		void deallocate(void* data)
 		{
-			ScopedMutex sm(m_mutex);
+			ScopedMutex sm(_mutex);
 
 			if (!data)
 				return;
 
 			Header* h = header(data);
 
-			m_allocated_size -= h->size;
-			m_allocation_count--;
+			_allocated_size -= h->size;
+			_allocation_count--;
 
 			free(h);
 		}
@@ -116,14 +116,14 @@ namespace memory
 		/// @copydoc Allocator::allocated_size()
 		size_t allocated_size()
 		{
-			ScopedMutex sm(m_mutex);
-			return m_allocated_size;
+			ScopedMutex sm(_mutex);
+			return _allocated_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);
+			ScopedMutex sm(_mutex);
 			Header* h = header(data);
 			return h->size;
 		}
@@ -176,9 +176,9 @@ namespace memory
 
 	private:
 
-		Mutex		m_mutex;
-		size_t		m_allocated_size;
-		uint32_t	m_allocation_count;
+		Mutex _mutex;
+		size_t _allocated_size;
+		uint32_t _allocation_count;
 	};
 } // namespace memory
 

+ 24 - 24
engine/core/mem/pool_allocator.cpp

@@ -32,13 +32,13 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 PoolAllocator::PoolAllocator(Allocator& backing, size_t num_blocks, size_t block_size, size_t block_align)
-	: m_backing(backing)
-	, m_start(NULL)
-	, m_freelist(NULL)
-	, m_block_size(block_size)
-	, m_block_align(block_align)
-	, m_num_allocations(0)
-	, m_allocated_size(0)
+	: _backing(backing)
+	, _start(NULL)
+	, _freelist(NULL)
+	, _block_size(block_size)
+	, _block_align(block_align)
+	, _num_allocations(0)
+	, _allocated_size(0)
 {
 	CE_ASSERT(num_blocks > 0, "Unsupported number of blocks");
 	CE_ASSERT(block_size > 0, "Unsupported block size");
@@ -61,29 +61,29 @@ PoolAllocator::PoolAllocator(Allocator& backing, size_t num_blocks, size_t block
 	uintptr_t* end = (uintptr_t*) cur;
 	*end = (uintptr_t) NULL;
 
-	m_start = mem;
-	m_freelist = mem;
+	_start = mem;
+	_freelist = mem;
 }
 
 //-----------------------------------------------------------------------------
 PoolAllocator::~PoolAllocator()
 {
-	m_backing.deallocate(m_start);
+	_backing.deallocate(_start);
 }
 
 //-----------------------------------------------------------------------------
 void* PoolAllocator::allocate(size_t size, size_t align)
 {
-	CE_ASSERT(size == m_block_size, "Size must match block size");
-	CE_ASSERT(align == m_block_align, "Align must match block align");
-	CE_ASSERT(m_freelist != NULL, "Out of memory");
+	CE_ASSERT(size == _block_size, "Size must match block size");
+	CE_ASSERT(align == _block_align, "Align must match block align");
+	CE_ASSERT(_freelist != NULL, "Out of memory");
 
-	uintptr_t next_free = *((uintptr_t*) m_freelist);
-	void* user_ptr = m_freelist;
-	m_freelist = (void*) next_free;
+	uintptr_t next_free = *((uintptr_t*) _freelist);
+	void* user_ptr = _freelist;
+	_freelist = (void*) next_free;
 
-	m_num_allocations++;
-	m_allocated_size += m_block_size;
+	_num_allocations++;
+	_allocated_size += _block_size;
 
 	return user_ptr;
 }
@@ -94,21 +94,21 @@ void PoolAllocator::deallocate(void* data)
 	if (!data)
 		return;
 
-	CE_ASSERT(m_num_allocations > 0, "Did not allocate");
+	CE_ASSERT(_num_allocations > 0, "Did not allocate");
 
 	uintptr_t* next = (uintptr_t*) data;
-	*next = (uintptr_t) m_freelist;
+	*next = (uintptr_t) _freelist;
 
-	m_freelist = data;
+	_freelist = data;
 
-	m_num_allocations--;
-	m_allocated_size -= m_block_size;
+	_num_allocations--;
+	_allocated_size -= _block_size;
 }
 
 //-----------------------------------------------------------------------------
 size_t PoolAllocator::allocated_size()
 {
-	return m_allocated_size;
+	return _allocated_size;
 }
 
 } // namespace crown

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

@@ -57,15 +57,15 @@ public:
 
 private:
 
-	Allocator&	m_backing;
-
-	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;
+	Allocator&	_backing;
+
+	void* _start;
+	void* _freelist;
+	size_t _block_size;
+	size_t _block_align;
+	
+	uint32_t _num_allocations;
+	size_t _allocated_size;
 };
 
 } // namespace crown

+ 13 - 13
engine/core/mem/proxy_allocator.cpp

@@ -37,10 +37,10 @@ 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)
+	: _allocator(allocator)
+	, _name(name)
+	, _total_allocated(0)
+	, _next(NULL)
 {
 	ScopedMutex sm(g_proxy_allocators_mutex);
 
@@ -48,7 +48,7 @@ ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator)
 
 	if(g_proxy_allocators_head != NULL)
 	{
-		m_next = g_proxy_allocators_head;
+		_next = g_proxy_allocators_head;
 	}
 
 	g_proxy_allocators_head = this;
@@ -57,27 +57,27 @@ ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator)
 //-----------------------------------------------------------------------------
 void* ProxyAllocator::allocate(size_t size, size_t align)
 {
-	m_total_allocated += size;
+	_total_allocated += size;
 
-	return m_allocator.allocate(size, align);
+	return _allocator.allocate(size, align);
 }
 
 //-----------------------------------------------------------------------------
 void ProxyAllocator::deallocate(void* data)
 {
-	m_allocator.deallocate(data);
+	_allocator.deallocate(data);
 }
 
 //-----------------------------------------------------------------------------
 size_t ProxyAllocator::allocated_size()
 {
-	return m_total_allocated;
+	return _total_allocated;
 }
 
 //-----------------------------------------------------------------------------
 const char* ProxyAllocator::name() const
 {
-	return m_name;
+	return _name;
 }
 
 //-----------------------------------------------------------------------------
@@ -91,7 +91,7 @@ uint32_t ProxyAllocator::count()
 	while (head != NULL)
 	{
 		++count;
-		head = head->m_next;
+		head = head->_next;
 	}
 
 	return count;
@@ -111,7 +111,7 @@ ProxyAllocator* ProxyAllocator::find(const char* name)
 			return head;
 		}
 
-		head = head->m_next;
+		head = head->_next;
 	}
 
 	return NULL;
@@ -131,7 +131,7 @@ ProxyAllocator* ProxyAllocator::next(ProxyAllocator* a)
 		return NULL;
 	}
 
-	return a->m_next;
+	return a->_next;
 }
 
 } // namespace crown

+ 5 - 5
engine/core/mem/proxy_allocator.h

@@ -75,11 +75,11 @@ public:
 
 private:
 
-	Allocator& m_allocator;
-	const char* m_name;
-	size_t m_total_allocated;
-
-	ProxyAllocator* m_next;
+	Allocator& _allocator;
+	
+	const char* _name;
+	size_t _total_allocated;
+	ProxyAllocator* _next;
 };
 
 } // namespace crown

+ 18 - 18
engine/core/mem/stack_allocator.cpp

@@ -32,18 +32,18 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 StackAllocator::StackAllocator(void* start, size_t size)
-	: m_physical_start(start)
-	, m_total_size(size)
-	, m_top(start)
-	, m_allocation_count(0)
+	: _physical_start(start)
+	, _total_size(size)
+	, _top(start)
+	, _allocation_count(0)
 {
 }
 
 //-----------------------------------------------------------------------------
 StackAllocator::~StackAllocator()
 {
-	CE_ASSERT(m_allocation_count == 0 && allocated_size() == 0,
-		"Missing %d deallocations causing a leak of %ld bytes", m_allocation_count, allocated_size());
+	CE_ASSERT(_allocation_count == 0 && allocated_size() == 0,
+		"Missing %d deallocations causing a leak of %ld bytes", _allocation_count, allocated_size());
 }
 
 //-----------------------------------------------------------------------------
@@ -52,25 +52,25 @@ void* StackAllocator::allocate(size_t size, size_t align)
 	const size_t actual_size = sizeof(Header) + size + align;
 
 	// Memory exhausted
-	if ((char*) m_top + actual_size > (char*) m_physical_start + m_total_size)
+	if ((char*) _top + actual_size > (char*) _physical_start + _total_size)
 	{
 		return NULL;
 	}
 
 	// The offset from TOS to the start of the buffer
-	uint32_t offset = (char*) m_top - (char*) m_physical_start;
+	uint32_t offset = (char*) _top - (char*) _physical_start;
 
 	// Align user data only, ignore header alignment
-	m_top = (char*) memory::align_top((char*) m_top + sizeof(Header), align) - sizeof(Header);
+	_top = (char*) memory::align_top((char*) _top + sizeof(Header), align) - sizeof(Header);
 
-	Header* header = (Header*) m_top;
+	Header* header = (Header*) _top;
 	header->offset = offset;
-	header->alloc_id = m_allocation_count;
+	header->alloc_id = _allocation_count;
 
-	void* user_ptr = (char*) m_top + sizeof(Header);
-	m_top = (char*) m_top + actual_size;
+	void* user_ptr = (char*) _top + sizeof(Header);
+	_top = (char*) _top + actual_size;
 
-	m_allocation_count++;
+	_allocation_count++;
 
 	return user_ptr;
 }
@@ -83,18 +83,18 @@ void StackAllocator::deallocate(void* data)
 
 	Header* data_header = (Header*) ((char*)data - sizeof(Header));
 
-	CE_ASSERT(data_header->alloc_id == m_allocation_count - 1,
+	CE_ASSERT(data_header->alloc_id == _allocation_count - 1,
 		"Deallocations must occur in LIFO order");
 
-	m_top = (char*) m_physical_start + data_header->offset;
+	_top = (char*) _physical_start + data_header->offset;
 
-	m_allocation_count--;
+	_allocation_count--;
 }
 
 //-----------------------------------------------------------------------------
 size_t StackAllocator::allocated_size()
 {
-	return (char*) m_top - (char*) m_physical_start;
+	return (char*) _top - (char*) _physical_start;
 }
 
 } // namespace crown

+ 4 - 6
engine/core/mem/stack_allocator.h

@@ -63,12 +63,10 @@ private:
 		uint32_t alloc_id;
 	};
 
-	void* m_physical_start;
-	size_t m_total_size;
-
-	void* m_top;
-
-	uint32_t m_allocation_count;
+	void* _physical_start;
+	size_t _total_size;
+	void* _top;
+	uint32_t _allocation_count;
 };
 
 } // namespace crown

+ 29 - 29
engine/core/mem/temp_allocator.h

@@ -60,13 +60,13 @@ public:
 
 private:
 
-	Allocator&	m_backing;
-
-	char* m_begin;
-	char* m_end;
-	char* m_cur;
-	size_t m_chunk_size;
-	char m_buffer[SIZE];
+	Allocator&	_backing;
+	
+	char* _begin;
+	char* _end;
+	char* _cur;
+	size_t _chunk_size;
+	char _buffer[SIZE];
 };
 
 typedef TempAllocator<64> TempAllocator64;
@@ -80,14 +80,14 @@ typedef TempAllocator<4096> TempAllocator4096;
 //-----------------------------------------------------------------------------
 template <size_t SIZE>
 inline TempAllocator<SIZE>::TempAllocator(Allocator& backing)
-	: m_backing(backing)
-	, m_begin(&m_buffer[0])
-	, m_end(&m_buffer[SIZE - 1])
-	, m_cur(&m_buffer[0])
-	, m_chunk_size(4 * 1024)
+	: _backing(backing)
+	, _begin(&_buffer[0])
+	, _end(&_buffer[SIZE - 1])
+	, _cur(&_buffer[0])
+	, _chunk_size(4 * 1024)
 {
-	*(void**) m_begin = 0;
-	m_cur += sizeof(void*);
+	*(void**) _begin = 0;
+	_cur += sizeof(void*);
 }
 
 //-----------------------------------------------------------------------------
@@ -95,13 +95,13 @@ template <size_t SIZE>
 inline TempAllocator<SIZE>::~TempAllocator()
 {
 	union { char* as_char; void** as_dvoid; };
-	as_char = m_buffer;
+	as_char = _buffer;
 
 	void *p = *(void **)as_dvoid;
 	while (p)
 	{
 		void *next = *(void **)p;
-		m_backing.deallocate(p);
+		_backing.deallocate(p);
 		p = next;
 	}
 }
@@ -110,30 +110,30 @@ inline TempAllocator<SIZE>::~TempAllocator()
 template <size_t SIZE>
 inline void* TempAllocator<SIZE>::allocate(size_t size, size_t align)
 {
-	m_cur = (char*) memory::align_top(m_cur, align);
+	_cur = (char*) memory::align_top(_cur, align);
 
-	if (size > size_t(m_end - m_cur))
+	if (size > size_t(_end - _cur))
 	{
 		uint32_t to_allocate = sizeof(void*) + size + align;
 
-		if (to_allocate < m_chunk_size)
+		if (to_allocate < _chunk_size)
 		{
-			to_allocate = m_chunk_size;
+			to_allocate = _chunk_size;
 		}
 
-		m_chunk_size *= 2;
+		_chunk_size *= 2;
 
-		void *p = m_backing.allocate(to_allocate);
-		*(void **)m_begin = p;
-		m_cur = m_begin = (char*) p;
-		m_end = m_begin + to_allocate;
-		*(void**) m_begin = 0;
-		m_cur += sizeof(void*);
+		void *p = _backing.allocate(to_allocate);
+		*(void **)_begin = p;
+		_cur = _begin = (char*) p;
+		_end = _begin + to_allocate;
+		*(void**) _begin = 0;
+		_cur += sizeof(void*);
 		memory::align_top(p, align);
 	}
 
-	void *result = m_cur;
-	m_cur += size;
+	void *result = _cur;
+	_cur += size;
 	return result;
 }