Browse Source

Memory stuff

Panagiotis Christopoulos Charitos 13 years ago
parent
commit
f7fca51e80
2 changed files with 45 additions and 16 deletions
  1. 24 11
      include/anki/util/Memory.h
  2. 21 5
      tests/util/Memory.cpp

+ 24 - 11
include/anki/util/Memory.h

@@ -4,6 +4,7 @@
 #include "anki/util/StdTypes.h"
 #include "anki/util/Assert.h"
 #include "anki/util/NonCopyable.h"
+#include "anki/util/Functions.h"
 #include <atomic>
 #include <algorithm>
 
@@ -80,15 +81,8 @@ class Allocator;
 template<typename T, typename Alloc = Allocator<T>>
 struct New
 {
-	PtrSize n; ///< Number of elements
-	Alloc alloc; ///< The allocator
-
-	New(PtrSize n_, const Alloc& alloc_ = Alloc())
-		: n(n_), alloc(alloc_)
-	{}
-
 	template<typename... Args>
-	T* operator()(Args&&... args)
+	T* operator()(PtrSize n, Alloc alloc, Args&&... args)
 	{
 		ANKI_ASSERT(n != 0);
 		T* out;
@@ -143,9 +137,6 @@ struct Delete
 
 		if(p)
 		{
-			// Rebind allocator because the Alloc may be of another type
-			typename Alloc::template rebind<T>::other alloc(alloc);
-
 			// Call the destructor
 			alloc.destroy(p);
 
@@ -194,6 +185,28 @@ struct DeleteArray
 	}
 };
 
+/// Allocate memory using a constructor
+#define ANKI_NEW(Type_, alloc_, ...) \
+	New<Type_, decltype(alloc_)::rebind<Type_>::other>{}( \
+		1, alloc_, ## __VA_ARGS__)
+
+/// Allocate memory using a constructor
+#define ANKI_NEW_ARRAY(Type_, alloc_, n_, ...) \
+	New<Type_, decltype(alloc_)::rebind<Type_>::other>{}( \
+		n_, alloc_, ## __VA_ARGS__)
+
+/// Delete memory allocated by #ANKI_NEW
+#define ANKI_DELETE(ptr_, alloc_) \
+	Delete<RemovePointer<decltype(ptr_)>::Type, \
+		decltype(alloc_)::rebind<RemovePointer<decltype(ptr_)>::Type>::other \
+		>{alloc_}(ptr_);
+
+/// Delete memory allocated by #ANKI_NEW_ARRAY
+#define ANKI_DELETE_ARRAY(ptr_, alloc_) \
+	DeleteArray<RemovePointer<decltype(ptr_)>::Type, \
+		decltype(alloc_)::rebind<RemovePointer<decltype(ptr_)>::Type>::other \
+		>{alloc_}(ptr_);
+
 /// @}
 /// @}
 

+ 21 - 5
tests/util/Memory.cpp

@@ -1,18 +1,34 @@
 #include "tests/framework/Framework.h"
 #include "tests/util/Foo.h"
 #include "anki/util/Memory.h"
+#include <type_traits>
 
 ANKI_TEST(Memory, Test)
 {
-	/*constexpr U n = 4;
+	constexpr U n = 4;
 	StackAllocator<U8, true> alloc(sizeof(Foo) * n + sizeof(PtrSize));
 
-	Foo* f = New<Foo, StackAllocator<Foo, true>>(n, alloc)();
+	Foo* f = ANKI_NEW(Foo, alloc, 123);
 
-	ANKI_TEST_EXPECT_EQ(alloc.dump(), false);
+	ANKI_TEST_EXPECT_EQ(f->x, 123);
 
-	DeleteArray<Foo, StackAllocator<Foo, true>>{alloc}(f);
+	ANKI_TEST_EXPECT_EQ(alloc.getMemoryPool().getAllocatedSize(),
+		sizeof(Foo) + sizeof(U32));
 
-	ANKI_TEST_EXPECT_EQ(alloc.dump(), true);*/
+	ANKI_DELETE(f, alloc);
+
+	ANKI_TEST_EXPECT_EQ(alloc.getMemoryPool().getAllocatedSize(), 0);
+
+	// Array
+	f = ANKI_NEW_ARRAY(Foo, alloc, 2, 123);
+
+	ANKI_TEST_EXPECT_EQ(alloc.getMemoryPool().getAllocatedSize(),
+		2 * sizeof(Foo) + sizeof(U32) + sizeof(PtrSize));
+
+	ANKI_TEST_EXPECT_NEQ(alloc.getMemoryPool().getAllocatedSize(), 0);
+
+	ANKI_DELETE_ARRAY(f, alloc);
+
+	ANKI_TEST_EXPECT_EQ(alloc.getMemoryPool().getAllocatedSize(), 0);
 }