Bladeren bron

Use uint32_t everywhere

Daniele Bartolini 10 jaren geleden
bovenliggende
commit
8d7283a9ea

+ 2 - 2
engine/core/memory/allocator.h

@@ -22,7 +22,7 @@ public:
 
 	/// 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 = DEFAULT_ALIGN) = 0;
+	virtual void* allocate(uint32_t size, uint32_t align = DEFAULT_ALIGN) = 0;
 
 	/// Deallocates a previously allocated block of memory pointed by @a data.
 	virtual void deallocate(void* data) = 0;
@@ -36,7 +36,7 @@ public:
 	virtual uint32_t total_allocated() = 0;
 
 	/// Default memory alignment in bytes.
-	static const size_t DEFAULT_ALIGN = 4;
+	static const uint32_t DEFAULT_ALIGN = 4;
 	static const uint32_t SIZE_NOT_TRACKED = 0xffffffffu;
 
 private:

+ 4 - 8
engine/core/memory/linear_allocator.cpp

@@ -9,7 +9,7 @@
 namespace crown
 {
 
-LinearAllocator::LinearAllocator(Allocator& backing, size_t size)
+LinearAllocator::LinearAllocator(Allocator& backing, uint32_t size)
 	: _backing(&backing)
 	, _physical_start(NULL)
 	, _total_size(size)
@@ -18,7 +18,7 @@ LinearAllocator::LinearAllocator(Allocator& backing, size_t size)
 	_physical_start = backing.allocate(size);
 }
 
-LinearAllocator::LinearAllocator(void* start, size_t size)
+LinearAllocator::LinearAllocator(void* start, uint32_t size)
 	: _backing(NULL)
 	, _physical_start(start)
 	, _total_size(size)
@@ -29,22 +29,18 @@ LinearAllocator::LinearAllocator(void* start, size_t size)
 LinearAllocator::~LinearAllocator()
 {
 	if (_backing)
-	{
 		_backing->deallocate(_physical_start);
-	}
 
 	CE_ASSERT(_offset == 0, "Memory leak of %ld bytes, maybe you forgot to call clear()?", _offset);
 }
 
-void* LinearAllocator::allocate(size_t size, size_t align)
+void* LinearAllocator::allocate(uint32_t size, uint32_t align)
 {
-	const size_t actual_size = size + align;
+	const uint32_t actual_size = size + align;
 
 	// Memory exhausted
 	if (_offset + actual_size > _total_size)
-	{
 		return NULL;
-	}
 
 	void* user_ptr = memory::align_top((char*) _physical_start + _offset, align);
 

+ 5 - 5
engine/core/memory/linear_allocator.h

@@ -18,12 +18,12 @@ class LinearAllocator : public Allocator
 {
 public:
 
-	LinearAllocator(Allocator& backing, size_t size);
-	LinearAllocator(void* start, size_t size);
+	LinearAllocator(Allocator& backing, uint32_t size);
+	LinearAllocator(void* start, uint32_t size);
 	~LinearAllocator();
 
 	/// @copydoc Allocator::allocate()
-	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
+	void* allocate(uint32_t size, uint32_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
 	/// @note
@@ -46,8 +46,8 @@ private:
 	Allocator* _backing;
 
 	void* _physical_start;
-	size_t _total_size;
-	size_t _offset;
+	uint32_t _total_size;
+	uint32_t _offset;
 };
 
 } // namespace crown

+ 7 - 9
engine/core/memory/memory.cpp

@@ -6,7 +6,7 @@
 #include "memory.h"
 #include "allocator.h"
 #include "mutex.h"
-#include <stdlib.h> // malloc, free
+#include <stdlib.h> // malloc
 
 // void* operator new(size_t) throw (std::bad_alloc)
 // {
@@ -54,11 +54,11 @@ namespace memory
 		}
 
 		/// @copydoc Allocator::allocate()
-		void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN)
+		void* allocate(uint32_t size, uint32_t align = Allocator::DEFAULT_ALIGN)
 		{
 			ScopedMutex sm(_mutex);
 
-			size_t actual_size = actual_allocation_size(size, align);
+			uint32_t actual_size = actual_allocation_size(size, align);
 
 			Header* h = (Header*)malloc(actual_size);
 			h->size = actual_size;
@@ -103,7 +103,7 @@ namespace memory
 		}
 
 		/// Returns the size in bytes of the block of memory pointed by @a data
-		size_t get_size(const void* data)
+		uint32_t get_size(const void* data)
 		{
 			ScopedMutex sm(_mutex);
 			Header* h = header(data);
@@ -118,7 +118,7 @@ namespace memory
 			uint32_t size;
 		};
 
-		size_t actual_allocation_size(size_t size, size_t align)
+		uint32_t actual_allocation_size(uint32_t size, uint32_t align)
 		{
 			return size + align + sizeof(Header);
 		}
@@ -129,14 +129,12 @@ namespace memory
 			ptr--;
 
 			while (*ptr == memory::PADDING_VALUE)
-			{
 				ptr--;
-			}
 
 			return (Header*)ptr;
 		}
 
-		void* data(Header* header, size_t align)
+		void* data(Header* header, uint32_t align)
 		{
 			return memory::align_top(header + 1, align);
 		}
@@ -155,7 +153,7 @@ namespace memory
 	private:
 
 		Mutex _mutex;
-		size_t _allocated_size;
+		uint32_t _allocated_size;
 		uint32_t _allocation_count;
 	};
 } // namespace memory

+ 2 - 4
engine/core/memory/memory.h

@@ -23,19 +23,17 @@ namespace 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)
+	inline void* align_top(void* p, uint32_t align)
 	{
 		CE_ASSERT(align >= 1, "Alignment must be > 1");
 		CE_ASSERT(align % 2 == 0 || align == 1, "Alignment must be a power of two");
 
 		uintptr_t ptr = (uintptr_t)p;
 
-		const size_t mod = ptr % align;
+		const uint32_t mod = ptr % align;
 
 		if (mod)
-		{
 			ptr += align - mod;
-		}
 
 		return (void*) ptr;
 	}

+ 5 - 5
engine/core/memory/pool_allocator.cpp

@@ -9,7 +9,7 @@
 namespace crown
 {
 
-PoolAllocator::PoolAllocator(Allocator& backing, size_t num_blocks, size_t block_size, size_t block_align)
+PoolAllocator::PoolAllocator(Allocator& backing, uint32_t num_blocks, uint32_t block_size, uint32_t block_align)
 	: _backing(backing)
 	, _start(NULL)
 	, _freelist(NULL)
@@ -22,14 +22,14 @@ PoolAllocator::PoolAllocator(Allocator& backing, size_t num_blocks, size_t block
 	CE_ASSERT(block_size > 0, "Unsupported block size");
 	CE_ASSERT(block_align > 0, "Unsupported block alignment");
 
-	size_t actual_block_size = block_size + block_align;
-	size_t pool_size = num_blocks * actual_block_size;
+	uint32_t actual_block_size = block_size + block_align;
+	uint32_t pool_size = num_blocks * actual_block_size;
 
 	char* mem = (char*) backing.allocate(pool_size, block_align);
 
 	// Initialize intrusive freelist
 	char* cur = mem;
-	for (size_t bb = 0; bb < num_blocks - 1; bb++)
+	for (uint32_t bb = 0; bb < num_blocks - 1; bb++)
 	{
 		uintptr_t* next = (uintptr_t*) cur;
 		*next = (uintptr_t) cur + actual_block_size;
@@ -48,7 +48,7 @@ PoolAllocator::~PoolAllocator()
 	_backing.deallocate(_start);
 }
 
-void* PoolAllocator::allocate(size_t size, size_t align)
+void* PoolAllocator::allocate(uint32_t size, uint32_t align)
 {
 	CE_ASSERT(size == _block_size, "Size must match block size");
 	CE_ASSERT(align == _block_align, "Align must match block align");

+ 5 - 5
engine/core/memory/pool_allocator.h

@@ -20,13 +20,13 @@ 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 = Allocator::DEFAULT_ALIGN);
+	PoolAllocator(Allocator& backing, uint32_t num_blocks, uint32_t block_size, uint32_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 = Allocator::DEFAULT_ALIGN);
+	void* allocate(uint32_t size, uint32_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
 	void deallocate(void* data);
@@ -43,11 +43,11 @@ private:
 
 	void* _start;
 	void* _freelist;
-	size_t _block_size;
-	size_t _block_align;
+	uint32_t _block_size;
+	uint32_t _block_align;
 
 	uint32_t _num_allocations;
-	size_t _allocated_size;
+	uint32_t _allocated_size;
 };
 
 } // namespace crown

+ 1 - 1
engine/core/memory/proxy_allocator.cpp

@@ -17,7 +17,7 @@ ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator)
 	CE_ASSERT(name != NULL, "Name must be != NULL");
 }
 
-void* ProxyAllocator::allocate(size_t size, size_t align)
+void* ProxyAllocator::allocate(uint32_t size, uint32_t align)
 {
 	void* p = _allocator.allocate(size, align);
 	ALLOCATE_MEMORY(_name, _allocator.allocated_size(p));

+ 1 - 1
engine/core/memory/proxy_allocator.h

@@ -23,7 +23,7 @@ public:
 	ProxyAllocator(const char* name, Allocator& allocator);
 
 	/// @copydoc Allocator::allocate()
-	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
+	void* allocate(uint32_t size, uint32_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
 	void deallocate(void* data);

+ 4 - 4
engine/core/memory/stack_allocator.cpp

@@ -9,10 +9,10 @@
 namespace crown
 {
 
-StackAllocator::StackAllocator(void* start, size_t size)
+StackAllocator::StackAllocator(void* start, uint32_t size)
 	: _physical_start(start)
-	, _total_size(size)
 	, _top(start)
+	, _total_size(size)
 	, _allocation_count(0)
 {
 }
@@ -23,9 +23,9 @@ StackAllocator::~StackAllocator()
 		"Missing %d deallocations causing a leak of %ld bytes", _allocation_count, total_allocated());
 }
 
-void* StackAllocator::allocate(size_t size, size_t align)
+void* StackAllocator::allocate(uint32_t size, uint32_t align)
 {
-	const size_t actual_size = sizeof(Header) + size + align;
+	const uint32_t actual_size = sizeof(Header) + size + align;
 
 	// Memory exhausted
 	if ((char*) _top + actual_size > (char*) _physical_start + _total_size)

+ 3 - 3
engine/core/memory/stack_allocator.h

@@ -19,11 +19,11 @@ class StackAllocator : public Allocator
 {
 public:
 
-	StackAllocator(void* start, size_t size);
+	StackAllocator(void* start, uint32_t size);
 	~StackAllocator();
 
 	/// @copydoc Allocator::allocate()
-	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
+	void* allocate(uint32_t size, uint32_t align = Allocator::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
 	/// @note
@@ -46,8 +46,8 @@ private:
 	};
 
 	void* _physical_start;
-	size_t _total_size;
 	void* _top;
+	uint32_t _total_size;
 	uint32_t _allocation_count;
 };
 

+ 8 - 8
engine/core/memory/temp_allocator.h

@@ -24,7 +24,7 @@ namespace crown
 /// The memory is automatically freed when the allocator is destroyed.
 ///
 /// @ingroup Memory
-template <size_t SIZE>
+template <uint32_t SIZE>
 class TempAllocator : public Allocator
 {
 public:
@@ -35,7 +35,7 @@ public:
 	~TempAllocator();
 
 	/// @copydoc Allocator::allocate()
-	void* allocate(size_t size, size_t align = Allocator::DEFAULT_ALIGN);
+	void* allocate(uint32_t size, uint32_t align = Allocator::DEFAULT_ALIGN);
 
 	/// Does nothing, the memory is automatically freed when the
 	/// allocator is destroyed.
@@ -54,7 +54,7 @@ private:
 	char* _begin;
 	char* _end;
 	char* _cur;
-	size_t _chunk_size;
+	uint32_t _chunk_size;
 	char _buffer[SIZE];
 };
 
@@ -66,7 +66,7 @@ typedef TempAllocator<1024> TempAllocator1024;
 typedef TempAllocator<2048> TempAllocator2048;
 typedef TempAllocator<4096> TempAllocator4096;
 
-template <size_t SIZE>
+template <uint32_t SIZE>
 inline TempAllocator<SIZE>::TempAllocator(Allocator& backing)
 	: _backing(backing)
 	, _begin(&_buffer[0])
@@ -78,7 +78,7 @@ inline TempAllocator<SIZE>::TempAllocator(Allocator& backing)
 	_cur += sizeof(void*);
 }
 
-template <size_t SIZE>
+template <uint32_t SIZE>
 inline TempAllocator<SIZE>::~TempAllocator()
 {
 	union { char* as_char; void** as_dvoid; };
@@ -93,12 +93,12 @@ inline TempAllocator<SIZE>::~TempAllocator()
 	}
 }
 
-template <size_t SIZE>
-inline void* TempAllocator<SIZE>::allocate(size_t size, size_t align)
+template <uint32_t SIZE>
+inline void* TempAllocator<SIZE>::allocate(uint32_t size, uint32_t align)
 {
 	_cur = (char*) memory::align_top(_cur, align);
 
-	if (size > size_t(_end - _cur))
+	if (size > uint32_t(_end - _cur))
 	{
 		uint32_t to_allocate = sizeof(void*) + size + align;