Browse Source

Removed use of new/delete, switched to use allocator instead.

Branimir Karadžić 8 years ago
parent
commit
493c559e8a

+ 4 - 2
include/bx/inline/mpscqueue.inl

@@ -10,7 +10,8 @@
 namespace bx
 {
 	template <typename Ty>
-	inline MpScUnboundedQueueT<Ty>::MpScUnboundedQueueT()
+	inline MpScUnboundedQueueT<Ty>::MpScUnboundedQueueT(AllocatorI* _allocator)
+		: m_queue(_allocator)
 	{
 	}
 
@@ -39,7 +40,8 @@ namespace bx
 	}
 
 	template <typename Ty>
-	inline MpScUnboundedBlockingQueue<Ty>::MpScUnboundedBlockingQueue()
+	inline MpScUnboundedBlockingQueue<Ty>::MpScUnboundedBlockingQueue(AllocatorI* _allocator)
+		: m_queue(_allocator)
 	{
 	}
 

+ 12 - 8
include/bx/inline/spscqueue.inl

@@ -10,8 +10,9 @@
 namespace bx
 {
 	// http://drdobbs.com/article/print?articleId=210604448&siteSectionName=
-	inline SpScUnboundedQueue::SpScUnboundedQueue()
-		: m_first(new Node(NULL) )
+	inline SpScUnboundedQueue::SpScUnboundedQueue(AllocatorI* _allocator)
+		: m_allocator(_allocator)
+		, m_first(BX_NEW(m_allocator, Node)(NULL) )
 		, m_divider(m_first)
 		, m_last(m_first)
 	{
@@ -23,19 +24,19 @@ namespace bx
 		{
 			Node* node = m_first;
 			m_first = node->m_next;
-			delete node;
+			BX_DELETE(m_allocator, node);
 		}
 	}
 
 	inline void SpScUnboundedQueue::push(void* _ptr)
 	{
-		m_last->m_next = new Node(_ptr);
+		m_last->m_next = BX_NEW(m_allocator, Node)(_ptr);
 		atomicExchangePtr( (void**)&m_last, m_last->m_next);
 		while (m_first != m_divider)
 		{
 			Node* node = m_first;
 			m_first = m_first->m_next;
-			delete node;
+			BX_DELETE(m_allocator, node);
 		}
 	}
 
@@ -68,7 +69,8 @@ namespace bx
 	}
 
 	template<typename Ty>
-	inline SpScUnboundedQueueT<Ty>::SpScUnboundedQueueT()
+	inline SpScUnboundedQueueT<Ty>::SpScUnboundedQueueT(AllocatorI* _allocator)
+		: m_queue(_allocator)
 	{
 	}
 
@@ -96,7 +98,8 @@ namespace bx
 	}
 
 #if BX_CONFIG_SUPPORTS_THREADING
-	inline SpScBlockingUnboundedQueue::SpScBlockingUnboundedQueue()
+	inline SpScBlockingUnboundedQueue::SpScBlockingUnboundedQueue(AllocatorI* _allocator)
+		: m_queue(_allocator)
 	{
 	}
 
@@ -126,7 +129,8 @@ namespace bx
 	}
 
 	template<typename Ty>
-	inline SpScBlockingUnboundedQueueT<Ty>::SpScBlockingUnboundedQueueT()
+	inline SpScBlockingUnboundedQueueT<Ty>::SpScBlockingUnboundedQueueT(AllocatorI* _allocator)
+		: m_queue(_allocator)
 	{
 	}
 

+ 3 - 2
include/bx/mpscqueue.h

@@ -6,6 +6,7 @@
 #ifndef BX_MPSCQUEUE_H_HEADER_GUARD
 #define BX_MPSCQUEUE_H_HEADER_GUARD
 
+#include "allocator.h"
 #include "mutex.h"
 #include "spscqueue.h"
 
@@ -22,7 +23,7 @@ namespace bx
 
 	public:
 		///
-		MpScUnboundedQueueT();
+		MpScUnboundedQueueT(AllocatorI* _allocator);
 
 		///
 		~MpScUnboundedQueueT();
@@ -52,7 +53,7 @@ namespace bx
 
 	public:
 		///
-		MpScUnboundedBlockingQueue();
+		MpScUnboundedBlockingQueue(AllocatorI* _allocator);
 
 		///
 		~MpScUnboundedBlockingQueue();

+ 6 - 6
include/bx/spscqueue.h

@@ -6,7 +6,7 @@
 #ifndef BX_SPSCQUEUE_H_HEADER_GUARD
 #define BX_SPSCQUEUE_H_HEADER_GUARD
 
-#include "bx.h"
+#include "allocator.h"
 #include "cpu.h"
 #include "semaphore.h"
 
@@ -22,7 +22,7 @@ namespace bx
 
 	public:
 		///
-		SpScUnboundedQueue();
+		SpScUnboundedQueue(AllocatorI* _allocator);
 
 		///
 		~SpScUnboundedQueue();
@@ -46,6 +46,7 @@ namespace bx
 			Node* m_next;
 		};
 
+		AllocatorI* m_allocator;
 		Node* m_first;
 		Node* m_divider;
 		Node* m_last;
@@ -62,7 +63,7 @@ namespace bx
 
 	public:
 		///
-		SpScUnboundedQueueT();
+		SpScUnboundedQueueT(AllocatorI* _allocator);
 
 		///
 		~SpScUnboundedQueueT();
@@ -80,7 +81,6 @@ namespace bx
 		SpScUnboundedQueue m_queue;
 	};
 
-
 #if BX_CONFIG_SUPPORTS_THREADING
 	///
 	class SpScBlockingUnboundedQueue
@@ -92,7 +92,7 @@ namespace bx
 
 	public:
 		///
-		SpScBlockingUnboundedQueue();
+		SpScBlockingUnboundedQueue(AllocatorI* _allocator);
 
 		///
 		~SpScBlockingUnboundedQueue();
@@ -122,7 +122,7 @@ namespace bx
 
 	public:
 		///
-		SpScBlockingUnboundedQueueT();
+		SpScBlockingUnboundedQueueT(AllocatorI* _allocator);
 
 		///
 		~SpScBlockingUnboundedQueueT();

+ 1 - 1
include/bx/thread.h

@@ -6,7 +6,7 @@
 #ifndef BX_THREAD_H_HEADER_GUARD
 #define BX_THREAD_H_HEADER_GUARD
 
-#include "bx.h"
+#include "allocator.h"
 #include "mpscqueue.h"
 
 namespace bx

+ 3 - 0
src/thread.cpp

@@ -36,6 +36,8 @@ using namespace Windows::System::Threading;
 
 namespace bx
 {
+	static DefaultAllocator s_allocator;
+
 	struct ThreadInternal
 	{
 #if    BX_PLATFORM_WINDOWS \
@@ -76,6 +78,7 @@ namespace bx
 	Thread::Thread()
 		: m_fn(NULL)
 		, m_userData(NULL)
+		, m_queue(&s_allocator)
 		, m_stackSize(0)
 		, m_exitCode(kExitSuccess)
 		, m_running(false)

+ 4 - 2
tests/queue_test.cpp

@@ -21,14 +21,16 @@ uintptr_t ptrToBits(void* _ptr)
 
 TEST_CASE("SpSc", "")
 {
-	bx::SpScUnboundedQueue queue;
+	bx::DefaultAllocator allocator;
+	bx::SpScUnboundedQueue queue(&allocator);
 	queue.push(bitsToPtr(0xdeadbeef) );
 	REQUIRE(0xdeadbeef == ptrToBits(queue.pop() ) );
 }
 
 TEST_CASE("MpSc", "")
 {
-	bx::MpScUnboundedQueueT<void> queue;
+	bx::DefaultAllocator allocator;
+	bx::MpScUnboundedQueueT<void> queue(&allocator);
 	queue.push(bitsToPtr(0xdeadbeef) );
 	REQUIRE(0xdeadbeef == ptrToBits(queue.pop() ) );
 }

+ 2 - 1
tests/thread_test.cpp

@@ -6,7 +6,8 @@
 #include "test.h"
 #include <bx/thread.h>
 
-bx::MpScUnboundedBlockingQueue<void> s_mpsc;
+bx::DefaultAllocator s_allocator;
+bx::MpScUnboundedBlockingQueue<void> s_mpsc(&s_allocator);
 
 int32_t threadExit0(bx::Thread* _thread, void*)
 {