瀏覽代碼

Merge branch 'master' of https://github.com/taylor001/crown

mikymod 12 年之前
父節點
當前提交
f57405d609
共有 3 個文件被更改,包括 20 次插入6 次删除
  1. 17 2
      src/core/mem/Allocator.h
  2. 0 1
      src/core/mem/Memory.cpp
  3. 3 3
      src/core/mem/Memory.h

+ 17 - 2
src/core/mem/Allocator.h

@@ -26,6 +26,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
+#include <new>
+
 #include "Types.h"
 #include "Memory.h"
 
@@ -37,6 +39,7 @@ class Allocator
 {
 public:
 
+						Allocator() {}
 	virtual				~Allocator() {}
 
 	/// Allocates @a size bytes of memory aligned to the specified
@@ -48,6 +51,12 @@ public:
 
 	/// Returns the total number of bytes allocated.
 	virtual size_t		allocated_size() = 0;
+
+private:
+
+	// Disable copying
+						Allocator(const Allocator&);
+	Allocator&			operator=(const Allocator&);
 };
 
 Allocator& default_allocator();
@@ -64,11 +73,17 @@ void call_destructor_and_deallocate(Allocator& a, T* 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))) 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)
 

+ 0 - 1
src/core/mem/Memory.cpp

@@ -25,7 +25,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Memory.h"
-#include "Assert.h"
 #include "MallocAllocator.h"
 
 //-----------------------------------------------------------------------------

+ 3 - 3
src/core/mem/Memory.h

@@ -26,10 +26,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-#include <stdint.h>
-#include <cstdio>
-#include <new>
 #include "Types.h"
+#include "Assert.h"
 
 namespace crown
 {
@@ -42,6 +40,8 @@ const size_t	DEFAULT_ALIGN	= 4;			//!< Default memory alignment in bytes
 /// Returns the pointer @a p aligned to the desired @a align byte
 inline void* align(void* p, size_t align)
 {
+	CE_ASSERT(align % 2 == 0, "Alignment must be a power of two");
+
 	uintptr_t ptr = (uintptr_t)p;
 
 	return (void*)(ptr + (align - ptr % align));