2
0
Эх сурвалжийг харах

Add API for accessing global list of proxy allocators

Daniele Bartolini 12 жил өмнө
parent
commit
5769e084ec

+ 51 - 0
engine/core/mem/ProxyAllocator.cpp

@@ -27,6 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Assert.h"
 #include "ProxyAllocator.h"
 #include "Allocator.h"
+#include "StringUtils.h"
 
 namespace crown
 {
@@ -67,4 +68,54 @@ const char* ProxyAllocator::name() const
 	return m_name;
 }
 
+//-----------------------------------------------------------------------------
+uint32_t ProxyAllocator::count()
+{
+	const ProxyAllocator* head = g_proxy_allocators_head;
+	uint32_t count = 0;
+
+	while (head != NULL)
+	{
+		++count;
+		head = head->m_next;
+	}
+
+	return count;
+}
+
+//-----------------------------------------------------------------------------
+ProxyAllocator* ProxyAllocator::find(const char* name)
+{
+	ProxyAllocator* head = g_proxy_allocators_head;
+
+	while (head != NULL)
+	{
+		if (string::strcmp(name, head->name()) == 0)
+		{
+			return head;
+		}
+
+		head = head->m_next;
+	}
+
+	return NULL;
+}
+
+//-----------------------------------------------------------------------------
+ProxyAllocator* ProxyAllocator::begin()
+{
+	return g_proxy_allocators_head;
+}
+
+//-----------------------------------------------------------------------------
+ProxyAllocator* ProxyAllocator::next(ProxyAllocator* a)
+{
+	if (a == NULL)
+	{
+		return NULL;
+	}
+
+	return a->m_next;
+}
+
 } // namespace crown

+ 24 - 7
engine/core/mem/ProxyAllocator.h

@@ -42,23 +42,40 @@ class ProxyAllocator
 public:
 
 	/// Tag all allocations made with @a allocator by the given @a name
-					ProxyAllocator(const char* name, Allocator& allocator);
+							ProxyAllocator(const char* name, Allocator& allocator);
 
 	/// @copydoc Allocator::allocate()
-	void*			allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+	void*					allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
 
 	/// @copydoc Allocator::deallocate()
-	void			deallocate(void* data);
+	void					deallocate(void* data);
 
 	/// Returns the name of the proxy allocator
-	const char*		name() const;
+	const char*				name() const;
+
+public:
+
+	/// Returns the total number of proxy allocators.
+	/// in the global list.
+	static uint32_t			count();
+
+	/// Returns the proxy allocator @name or NULL if not found.
+	static ProxyAllocator*	find(const char* name);
+
+	/// Returns the first proxy allocator in the global list or
+	/// NULL if the list is empty.
+	static ProxyAllocator*	begin();
+
+	/// Returns the next proxy allocator to @a a in the global list
+	/// or NULL if end-of-list is reached.
+	static ProxyAllocator*	next(ProxyAllocator* a);
 
 private:
 
-	Allocator&		m_allocator;
-	const char*		m_name;
+	Allocator&				m_allocator;
+	const char*				m_name;
 
-	ProxyAllocator*	m_next;
+	ProxyAllocator*			m_next;
 };
 
 } // namespace crown