Browse Source

Scene allocator

Panagiotis Christopoulos Charitos 13 years ago
parent
commit
cee1e8d88d

+ 4 - 0
include/anki/scene/Common.h

@@ -9,6 +9,10 @@ namespace anki {
 template<typename T>
 using SceneAllocator = StackAllocator<T, false>;
 
+/// Scene string
+typedef std::basic_string<char, std::char_traits<char>,
+	SceneAllocator<char>> SceneString;
+
 } // end namespace anki
 
 #endif

+ 4 - 4
include/anki/scene/Scene.h

@@ -43,7 +43,7 @@ public:
 
 	/// @name Accessors
 	/// @{
-	const SceneAllocator<U8> getAllocator() const
+	const SceneAllocator<U8>& getAllocator() const
 	{
 		return alloc;
 	}
@@ -151,10 +151,10 @@ private:
 	template<typename T>
 	void addDict(typename Types<T>::NameToItemMap& d, T* ptr)
 	{
-		ANKI_ASSERT(d.find(ptr->getName().c_str()) == d.end()
+		ANKI_ASSERT(d.find(ptr->getName()) == d.end()
 			&& "Item with same name already exists");
 
-		d[ptr->getName().c_str()] = ptr;
+		d[ptr->getName()] = ptr;
 	}
 
 	/// Remove from a container
@@ -179,7 +179,7 @@ private:
 	void removeDict(typename Types<T>::NameToItemMap& d, T* ptr)
 	{
 		typename Types<T>::NameToItemMap::iterator it =
-			d.find(ptr->getName().c_str());
+			d.find(ptr->getName());
 
 		ANKI_ASSERT(it != d.end());
 		d.erase(it);

+ 3 - 3
include/anki/scene/SceneNode.h

@@ -40,9 +40,9 @@ public:
 
 	/// @name Accessors
 	/// @{
-	const std::string& getName() const
+	const char* getName() const
 	{
-		return name;
+		return name.c_str();
 	}
 
 	Scene& getScene()
@@ -100,7 +100,7 @@ public:
 	U32 getLastUpdateFrame();
 
 private:
-	std::string name; ///< A unique name
+	SceneString name; ///< A unique name
 	Scene* scene; ///< Keep it here for unregistering
 };
 /// @}

+ 11 - 16
include/anki/util/Allocator.h

@@ -9,7 +9,11 @@
 #include <utility> // For forward
 
 #define ANKI_DEBUG_ALLOCATORS ANKI_DEBUG
-#define ANKI_PRINT_ALLOCATOR_MESSAGES 0
+#define ANKI_PRINT_ALLOCATOR_MESSAGES 1
+
+#if ANKI_PRINT_ALLOCATOR_MESSAGES
+#	include <iostream> // Never include it on release
+#endif
 
 namespace anki {
 
@@ -191,6 +195,9 @@ protected:
 
 	/// Deinit the memory pool
 	void deinit();
+
+	/// Copy
+	void copy(const StackAllocatorInternal& b);
 };
 
 } // end namespace detail
@@ -255,13 +262,7 @@ public:
 	/// Copy
 	StackAllocator& operator=(const StackAllocator& b)
 	{
-		deinit();
-		mpool = b.mpool;
-		if(mpool)
-		{
-			// Retain the mpool
-			++mpool->refCounter;
-		}
+		copy(b);
 		return *this;
 	}
 	/// Copy
@@ -269,13 +270,7 @@ public:
 	StackAllocator& operator=(
 		const StackAllocator<U, deallocationFlag, alignmentBits>& b)
 	{
-		deinit();
-		mpool = b.mpool;
-		if(mpool)
-		{
-			// Retain the mpool
-			++mpool->refCounter;
-		}
+		copy(b);
 		return *this;
 	}
 
@@ -303,7 +298,7 @@ public:
 #if ANKI_PRINT_ALLOCATOR_MESSAGES
 			std::cout << "Allocating: size: " << size
 				<< ", size after alignment: " << alignedSize
-				<< ", returned address: " << out
+				<< ", returned address: " << (void*)out
 				<< ", hint: " << hint <<std::endl;
 #endif
 

+ 2 - 2
src/scene/SceneNode.cpp

@@ -8,13 +8,13 @@ namespace anki {
 
 //==============================================================================
 SceneNode::SceneNode(const char* name_, Scene* scene_)
-	: name(name_), scene(scene_)
+	: name(name_, scene_->getAllocator()), scene(scene_)
 {
 	name.shrink_to_fit(); // Do that first
 	scene->registerNode(this);
 
 	/// Add the first property
-	addNewProperty(new ReadPointerProperty<std::string>("name", &name));
+	//addNewProperty(new ReadPointerProperty<std::string>("name", &name));
 }
 
 //==============================================================================

+ 15 - 5
src/util/Allocator.cpp

@@ -86,7 +86,7 @@ void StackAllocatorInternal::init(const PtrSize size)
 #if ANKI_PRINT_ALLOCATOR_MESSAGES
 			std::cout << "New MemoryPool created: Address: " << mpool
 				<< ", size: " << size
-				<< ", pool address: " << mpool->memory << std::endl;
+				<< ", pool address: " << (void*)mpool->memory << std::endl;
 #endif
 			return;
 		}
@@ -106,15 +106,13 @@ void StackAllocatorInternal::deinit()
 {
 	if (mpool)
 	{
-		I32 refCounter = mpool->refCounter.fetch_sub(1);
+		I32 refCounterPrev = mpool->refCounter.fetch_sub(1);
 
-		if(refCounter == 1)
+		if(refCounterPrev == 1)
 		{
 #if ANKI_PRINT_ALLOCATOR_MESSAGES
 			std::cout << "Deleting MemoryPool " << mpool << std::endl;
 #endif
-			// It is safe to delete the memory pool
-			ANKI_ASSERT(mpool->refCounter == 0);
 
 			::free(mpool->memory);
 			delete mpool;
@@ -123,6 +121,18 @@ void StackAllocatorInternal::deinit()
 	}
 }
 
+//==============================================================================
+void StackAllocatorInternal::copy(const StackAllocatorInternal& b)
+{
+	deinit();
+	mpool = b.mpool;
+	if(mpool)
+	{
+		// Retain the mpool
+		++mpool->refCounter;
+	}
+}
+
 } // end namespace detail
 
 } // end namespace anki

+ 3 - 2
tests/util/Allocator.cpp

@@ -5,14 +5,15 @@
 
 ANKI_TEST(Allocator, StackAllocator)
 {
-	StackAllocator<char> alloc(12);
+	StackAllocator<char, false> alloc(128);
 	typedef std::basic_string<char, std::char_traits<char>, 
-		StackAllocator<char>> Str;
+		StackAllocator<char, false>> Str;
 
 	{
 		Str str(alloc);
 
 		str = "lalala";
+		str = "lalalalo";
 	}
 }