Daniele Bartolini 10 年之前
父節點
當前提交
806a258861

+ 3 - 0
genie/toolchain.lua

@@ -238,6 +238,9 @@ function toolchain(build_dir, lib_dir)
 		objdir (build_dir .. "win32" .. "/obj")
 
 	configuration { "x64", "vs*" }
+		defines {
+			"_WIN64",
+		}
 		targetdir (build_dir .. "win64" .. "/bin")
 		objdir (build_dir .. "win64" .. "/obj")
 

+ 5 - 7
src/core/memory/linear_allocator.h

@@ -16,6 +16,11 @@ namespace crown
 /// @ingroup Memory
 class LinearAllocator : public Allocator
 {
+	Allocator* _backing;
+	void* _physical_start;
+	uint32_t _total_size;
+	uint32_t _offset;
+
 public:
 
 	/// Allocates @a size bytes from @a backing.
@@ -43,13 +48,6 @@ public:
 
 	/// @copydoc Allocator::total_allocated()
 	uint32_t total_allocated() { return _offset; }
-
-private:
-
-	Allocator* _backing;
-	void* _physical_start;
-	uint32_t _total_size;
-	uint32_t _offset;
 };
 
 } // namespace crown

+ 56 - 52
src/core/memory/memory.cpp

@@ -38,7 +38,8 @@ namespace memory
 {
 	// Header stored at the beginning of a memory allocation to indicate the
 	// size of the allocated data.
-	struct Header {
+	struct Header
+	{
 		uint32_t size;
 	};
 
@@ -47,32 +48,53 @@ namespace memory
 	const uint32_t HEADER_PAD_VALUE = 0xffffffffu;
 
 	// Given a pointer to the header, returns a pointer to the data that follows it.
-	inline void *data_pointer(Header *header, uint32_t align) {
+	inline void *data_pointer(Header* header, uint32_t align) {
 		void *p = header + 1;
 		return memory::align_top(p, align);
 	}
 
 	// Given a pointer to the data, returns a pointer to the header before it.
-	inline Header *header(const void *data)
+	inline Header*header(const void* data)
 	{
-		uint32_t *p = (uint32_t *)data;
+		uint32_t *p = (uint32_t*)data;
 		while (p[-1] == HEADER_PAD_VALUE)
 			--p;
-		return (Header *)p - 1;
+		return (Header*)p - 1;
 	}
 
 	// Stores the size in the header and pads with HEADER_PAD_VALUE up to the
 	// data pointer.
-	inline void fill(Header *header, void *data, uint32_t size)
+	inline void fill(Header*header, void *data, uint32_t size)
 	{
 		header->size = size;
-		uint32_t *p = (uint32_t *)(header + 1);
+		uint32_t *p = (uint32_t*)(header + 1);
 		while (p < data)
 			*p++ = HEADER_PAD_VALUE;
 	}
+
+	inline uint32_t actual_allocation_size(uint32_t size, uint32_t align)
+	{
+		return size + align + sizeof(Header);
+	}
+
+	inline void pad(Header* header, void* data)
+	{
+		uint32_t* p = (uint32_t*)(header + 1);
+
+		while (p != data)
+		{
+			*p = HEADER_PAD_VALUE;
+			p++;
+		}
+	}
+
 	/// Allocator based on C malloc().
 	class HeapAllocator : public Allocator
 	{
+		Mutex _mutex;
+		uint32_t _allocated_size;
+		uint32_t _allocation_count;
+
 	public:
 
 		HeapAllocator()
@@ -146,30 +168,6 @@ namespace memory
 			Header* h = header(data);
 			return h->size;
 		}
-
-	private:
-
-		uint32_t actual_allocation_size(uint32_t size, uint32_t align)
-		{
-			return size + align + sizeof(Header);
-		}
-
-		void pad(Header* header, void* data)
-		{
-			uint32_t* p = (uint32_t*)(header + 1);
-
-			while (p != data)
-			{
-				*p = HEADER_PAD_VALUE;
-				p++;
-			}
-		}
-
-	private:
-
-		Mutex _mutex;
-		uint32_t _allocated_size;
-		uint32_t _allocation_count;
 	};
 
 	// Copyright (C) 2012 Bitsquid AB
@@ -195,10 +193,10 @@ namespace memory
 		Allocator &_backing;
 
 		// Start and end of the ring buffer.
-		char *_begin, *_end;
+		char*_begin, *_end;
 
 		// Pointers to where to allocate memory and where to free memory.
-		char *_allocate, *_free;
+		char*_allocate, *_free;
 
 	public:
 		/// Creates a ScratchAllocator. The allocator will use the backing
@@ -206,14 +204,16 @@ namespace memory
 		/// that don't fit in the ring buffer.
 		///
 		/// size specifies the size of the ring buffer.
-		ScratchAllocator(Allocator &backing, uint32_t size) : _backing(backing) {
-			_begin = (char *)_backing.allocate(size);
+		ScratchAllocator(Allocator &backing, uint32_t size) : _backing(backing)
+		{
+			_begin = (char*)_backing.allocate(size);
 			_end = _begin + size;
 			_allocate = _begin;
 			_free = _begin;
 		}
 
-		~ScratchAllocator() {
+		~ScratchAllocator()
+		{
 			CE_ASSERT(_free == _allocate, "Memory leak");
 			_backing.deallocate(_begin);
 		}
@@ -227,22 +227,23 @@ namespace memory
 			return p >= _free || p < _allocate;
 		}
 
-		virtual void *allocate(uint32_t size, uint32_t align) {
+		virtual void *allocate(uint32_t size, uint32_t align)
+		{
 			CE_ASSERT(align % 4 == 0, "Must be 4-byte aligned");
 			size = ((size + 3)/4)*4;
 
-			char *p = _allocate;
-			Header *h = (Header *)p;
-			char *data = (char *)data_pointer(h, align);
+			char* p = _allocate;
+			Header* h = (Header*)p;
+			char* data = (char*)data_pointer(h, align);
 			p = data + size;
 
 			// Reached the end of the buffer, wrap around to the beginning.
 			if (p > _end) {
-				h->size = (_end - (char *)h) | 0x80000000u;
+				h->size = uint32_t(_end - (char*)h) | 0x80000000u;
 
 				p = _begin;
-				h = (Header *)p;
-				data = (char *)data_pointer(h, align);
+				h = (Header*)p;
+				data = (char*)data_pointer(h, align);
 				p = data + size;
 			}
 
@@ -250,12 +251,13 @@ namespace memory
 			if (in_use(p))
 				return _backing.allocate(size, align);
 
-			fill(h, data, p - (char *)h);
+			fill(h, data, uint32_t(p - (char*)h));
 			_allocate = p;
 			return data;
 		}
 
-		virtual void deallocate(void *p) {
+		virtual void deallocate(void *p)
+		{
 			if (!p)
 				return;
 
@@ -265,13 +267,13 @@ namespace memory
 			}
 
 			// Mark this slot as free
-			Header *h = header(p);
+			Header*h = header(p);
 			CE_ASSERT((h->size & 0x80000000u) == 0, "Not free");
 			h->size = h->size | 0x80000000u;
 
 			// Advance the free pointer past all free slots.
 			while (_free != _allocate) {
-				Header *h = (Header *)_free;
+				Header*h = (Header*)_free;
 				if ((h->size & 0x80000000u) == 0)
 					break;
 
@@ -281,13 +283,15 @@ namespace memory
 			}
 		}
 
-		virtual uint32_t allocated_size(const void *p) {
-			Header *h = header(p);
-			return h->size - ((char *)p - (char *)h);
+		virtual uint32_t allocated_size(const void *p)
+		{
+			Header* h = header(p);
+			return h->size - uint32_t((char*)p - (char*)h);
 		}
 
-		virtual uint32_t total_allocated() {
-			return _end - _begin;
+		virtual uint32_t total_allocated()
+		{
+			return uint32_t(_end - _begin);
 		}
 	};
 } // namespace memory

+ 10 - 12
src/core/memory/pool_allocator.h

@@ -16,6 +16,16 @@ namespace crown
 /// @ingroup Memory
 class PoolAllocator : public Allocator
 {
+	Allocator&	_backing;
+
+	void* _start;
+	void* _freelist;
+	uint32_t _block_size;
+	uint32_t _block_align;
+
+	uint32_t _num_allocations;
+	uint32_t _allocated_size;
+
 public:
 
 	/// Uses @a backing to allocate the memory pool for containing exactly
@@ -36,18 +46,6 @@ public:
 
 	/// @copydoc Allocator::total_allocated()
 	uint32_t total_allocated();
-
-private:
-
-	Allocator&	_backing;
-
-	void* _start;
-	void* _freelist;
-	uint32_t _block_size;
-	uint32_t _block_align;
-
-	uint32_t _num_allocations;
-	uint32_t _allocated_size;
 };
 
 } // namespace crown

+ 3 - 3
src/core/memory/proxy_allocator.cpp

@@ -10,9 +10,9 @@
 namespace crown
 {
 
-ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator)
-	: _name(name)
-	, _allocator(allocator)
+ProxyAllocator::ProxyAllocator(Allocator& allocator, const char* name)
+	: _allocator(allocator)
+	, _name(name)
 {
 	CE_ASSERT(name != NULL, "Name must be != NULL");
 }

+ 4 - 6
src/core/memory/proxy_allocator.h

@@ -17,10 +17,13 @@ namespace crown
 /// @ingroup Memory
 class ProxyAllocator : public Allocator
 {
+	Allocator& _allocator;
+	const char* _name;
+
 public:
 
 	/// Tag all allocations made with @a allocator by the given @a name
-	ProxyAllocator(const char* name, Allocator& allocator);
+	ProxyAllocator(Allocator& allocator, const char* name);
 
 	/// @copydoc Allocator::allocate()
 	void* allocate(uint32_t size, uint32_t align = Allocator::DEFAULT_ALIGN);
@@ -36,11 +39,6 @@ public:
 
 	/// Returns the name of the proxy allocator
 	const char* name() const;
-
-private:
-
-	const char* _name;
-	Allocator& _allocator;
 };
 
 } // namespace crown

+ 14 - 13
src/core/memory/stack_allocator.cpp

@@ -9,9 +9,9 @@
 namespace crown
 {
 
-StackAllocator::StackAllocator(void* start, uint32_t size)
-	: _physical_start(start)
-	, _top(start)
+StackAllocator::StackAllocator(char* begin, uint32_t size)
+	: _begin(begin)
+	, _top(begin)
 	, _total_size(size)
 	, _allocation_count(0)
 {
@@ -31,23 +31,23 @@ void* StackAllocator::allocate(uint32_t size, uint32_t align)
 	const uint32_t actual_size = sizeof(Header) + size + align;
 
 	// Memory exhausted
-	if ((char*) _top + actual_size > (char*) _physical_start + _total_size)
+	if (_top + actual_size > _begin + _total_size)
 	{
 		return NULL;
 	}
 
 	// The offset from TOS to the start of the buffer
-	uint32_t offset = (char*) _top - (char*) _physical_start;
+	uint32_t offset = uint32_t(_top - _begin);
 
 	// Align user data only, ignore header alignment
-	_top = (char*) memory::align_top((char*) _top + sizeof(Header), align) - sizeof(Header);
+	_top = (char*)memory::align_top(_top + sizeof(Header), align) - sizeof(Header);
 
 	Header* header = (Header*) _top;
 	header->offset = offset;
 	header->alloc_id = _allocation_count;
 
-	void* user_ptr = (char*) _top + sizeof(Header);
-	_top = (char*) _top + actual_size;
+	void* user_ptr = _top + sizeof(Header);
+	_top = _top + actual_size;
 
 	_allocation_count++;
 
@@ -59,19 +59,20 @@ void StackAllocator::deallocate(void* data)
 	if (!data)
 		return;
 
-	Header* data_header = (Header*) ((char*)data - sizeof(Header));
+	Header* data_header = (Header*)((char*)data - sizeof(Header));
 
-	CE_ASSERT(data_header->alloc_id == _allocation_count - 1,
-		"Deallocations must occur in LIFO order");
+	CE_ASSERT(data_header->alloc_id == _allocation_count - 1
+		, "Deallocations must occur in LIFO order"
+		);
 
-	_top = (char*) _physical_start + data_header->offset;
+	_top = _begin + data_header->offset;
 
 	_allocation_count--;
 }
 
 uint32_t StackAllocator::total_allocated()
 {
-	return (char*) _top - (char*) _physical_start;
+	return uint32_t(_top - _begin);
 }
 
 } // namespace crown

+ 12 - 14
src/core/memory/stack_allocator.h

@@ -17,9 +17,20 @@ namespace crown
 /// @ingroup Memory
 class StackAllocator : public Allocator
 {
+	struct Header
+	{
+		uint32_t offset;
+		uint32_t alloc_id;
+	};
+
+	char* _begin;
+	char* _top;
+	uint32_t _total_size;
+	uint32_t _allocation_count;
+
 public:
 
-	StackAllocator(void* start, uint32_t size);
+	StackAllocator(char* begin, uint32_t size);
 	~StackAllocator();
 
 	/// @copydoc Allocator::allocate()
@@ -36,19 +47,6 @@ public:
 
 	/// @copydoc Allocator::total_allocated()
 	uint32_t total_allocated();
-
-private:
-
-	struct Header
-	{
-		uint32_t offset;
-		uint32_t alloc_id;
-	};
-
-	void* _physical_start;
-	void* _top;
-	uint32_t _total_size;
-	uint32_t _allocation_count;
 };
 
 } // namespace crown

+ 2 - 2
src/device.cpp

@@ -93,7 +93,7 @@ struct BgfxCallback : public bgfx::CallbackI
 struct BgfxAllocator : public bx::AllocatorI
 {
 	BgfxAllocator(Allocator& a)
-		: _allocator("bgfx", a)
+		: _allocator(a, "bgfx")
 	{
 	}
 
@@ -300,7 +300,7 @@ void Device::update()
 		const int64_t time = _current_time - _last_time;
 		_last_time = _current_time;
 		const double freq = (double) os::clockfrequency();
-		_last_delta_time = time * (1.0 / freq);
+		_last_delta_time = float(time * (1.0 / freq));
 		_time_since_start += _last_delta_time;
 
 		profiler_globals::clear();

+ 1 - 1
src/resource/resource_manager.cpp

@@ -28,7 +28,7 @@ namespace crown
 const ResourceManager::ResourceEntry ResourceManager::ResourceEntry::NOT_FOUND = { 0xffffffffu, NULL };
 
 ResourceManager::ResourceManager(ResourceLoader& rl)
-	: _resource_heap("resource", default_allocator())
+	: _resource_heap(default_allocator(), "resource")
 	, _loader(&rl)
 	, _type_data(default_allocator())
 	, _rm(default_allocator())