Browse Source

TinySTL: Fixed bug when string container is used with multiple different allocators.

bkaradzic 12 years ago
parent
commit
05ca00212e

+ 0 - 38
include/bx/mutex.h

@@ -104,45 +104,7 @@ namespace bx
 		Mutex& m_mutex;
 		Mutex& m_mutex;
 	};
 	};
 
 
-#if 1
 	typedef Mutex LwMutex;
 	typedef Mutex LwMutex;
-#else
-	class LwMutex
-	{
-	public:
-		LwMutex()
-			: m_count(0)
-		{
-		}
-
-		~LwMutex()
-		{
-		}
-
-		void lock()
-		{
-			if (atomicIncr(&m_count) > 1)
-			{
-				m_sem.wait();
-			}
-		}
-
-		void unlock()
-		{
-			if (atomicDecr(&m_count) > 0)
-			{
-				m_sem.post();
-			}
-		}
-
-	private:
-		LwMutex(const LwMutex& _rhs); // no copy constructor
-		LwMutex& operator=(const LwMutex& _rhs); // no assignment operator
-
-		Semaphore m_sem;
-		volatile int32_t m_count;
-	};
-#endif // 0
 
 
 	class LwMutexScope
 	class LwMutexScope
 	{
 	{

+ 3 - 2
include/tinystl/allocator.h

@@ -29,6 +29,8 @@
 
 
 #include "stddef.h"
 #include "stddef.h"
 
 
+#ifndef TINYSTL_ALLOCATOR
+
 namespace tinystl {
 namespace tinystl {
 
 
 	struct allocator {
 	struct allocator {
@@ -42,8 +44,7 @@ namespace tinystl {
 	};
 	};
 }
 }
 
 
-#ifndef TINYSTL_ALLOCATOR
 #	define TINYSTL_ALLOCATOR ::tinystl::allocator
 #	define TINYSTL_ALLOCATOR ::tinystl::allocator
-#endif
+#endif // TINYSTL_ALLOCATOR
 
 
 #endif
 #endif

+ 0 - 1
include/tinystl/buffer.h

@@ -27,7 +27,6 @@
 #ifndef TINYSTL_BUFFER_H
 #ifndef TINYSTL_BUFFER_H
 #define TINYSTL_BUFFER_H
 #define TINYSTL_BUFFER_H
 
 
-#include "allocator.h"
 #include "new.h"
 #include "new.h"
 #include "traits.h"
 #include "traits.h"
 
 

+ 59 - 31
include/tinystl/string.h

@@ -27,24 +27,25 @@
 #ifndef TINYSTL_STRING_H
 #ifndef TINYSTL_STRING_H
 #define TINYSTL_STRING_H
 #define TINYSTL_STRING_H
 
 
-#include "allocator.h"
 #include "stddef.h"
 #include "stddef.h"
 #include "hash.h"
 #include "hash.h"
 
 
 namespace tinystl {
 namespace tinystl {
 
 
-	class string {
+	template<typename Alloc>
+	class stringT {
 	public:
 	public:
-		string();
-		string(const string& other);
-		string(const char* sz);
-		string(const char* sz, size_t len);
-		~string();
+		stringT();
+		stringT(const stringT<Alloc>& other);
+		stringT(const char* sz);
+		stringT(const char* sz, size_t len);
+		~stringT();
 
 
-		string& operator=(const string& other);
+		stringT<Alloc>& operator=(const stringT<Alloc>& other);
 
 
 		const char* c_str() const;
 		const char* c_str() const;
 		size_t size() const;
 		size_t size() const;
+		bool empty() const;
 
 
 		void reserve(size_t size);
 		void reserve(size_t size);
 		void resize(size_t size);
 		void resize(size_t size);
@@ -52,7 +53,7 @@ namespace tinystl {
 		void append(const char* first, const char* last);
 		void append(const char* first, const char* last);
 		void append(const char* str);
 		void append(const char* str);
 
 
-		void swap(string& other);
+		void swap(stringT<Alloc>& other);
 
 
 	private:
 	private:
 		typedef char* pointer;
 		typedef char* pointer;
@@ -64,7 +65,10 @@ namespace tinystl {
 		char m_buffer[12];
 		char m_buffer[12];
 	};
 	};
 
 
-	inline string::string()
+	typedef stringT<TINYSTL_ALLOCATOR> string;
+
+	template<typename Alloc>
+	inline stringT<Alloc>::stringT()
 		: m_first(m_buffer)
 		: m_first(m_buffer)
 		, m_last(m_buffer)
 		, m_last(m_buffer)
 		, m_capacity(m_buffer + c_nbuffer)
 		, m_capacity(m_buffer + c_nbuffer)
@@ -72,7 +76,8 @@ namespace tinystl {
 		resize(0);
 		resize(0);
 	}
 	}
 
 
-	inline string::string(const string& other)
+	template<typename Alloc>
+	inline stringT<Alloc>::stringT(const stringT<Alloc>& other)
 		: m_first(m_buffer)
 		: m_first(m_buffer)
 		, m_last(m_buffer)
 		, m_last(m_buffer)
 		, m_capacity(m_buffer + c_nbuffer)
 		, m_capacity(m_buffer + c_nbuffer)
@@ -81,7 +86,8 @@ namespace tinystl {
 		append(other.m_first, other.m_last);
 		append(other.m_first, other.m_last);
 	}
 	}
 
 
-	inline string::string(const char* sz)
+	template<typename Alloc>
+	inline stringT<Alloc>::stringT(const char* sz)
 		: m_first(m_buffer)
 		: m_first(m_buffer)
 		, m_last(m_buffer)
 		, m_last(m_buffer)
 		, m_capacity(m_buffer + c_nbuffer)
 		, m_capacity(m_buffer + c_nbuffer)
@@ -94,7 +100,8 @@ namespace tinystl {
 		append(sz, sz + len);
 		append(sz, sz + len);
 	}
 	}
 
 
-	inline string::string(const char* sz, size_t len)
+	template<typename Alloc>
+	inline stringT<Alloc>::stringT(const char* sz, size_t len)
 		: m_first(m_buffer)
 		: m_first(m_buffer)
 		, m_last(m_buffer)
 		, m_last(m_buffer)
 		, m_capacity(m_buffer + c_nbuffer)
 		, m_capacity(m_buffer + c_nbuffer)
@@ -103,43 +110,59 @@ namespace tinystl {
 		append(sz, sz + len);
 		append(sz, sz + len);
 	}
 	}
 
 
-	inline string::~string() {
+	template<typename Alloc>
+	inline stringT<Alloc>::~stringT() {
 		if (m_first != m_buffer)
 		if (m_first != m_buffer)
-			TINYSTL_ALLOCATOR::static_deallocate(m_first, m_capacity - m_first);
+			Alloc::static_deallocate(m_first, m_capacity - m_first);
 	}
 	}
 
 
-	inline string& string::operator=(const string& other) {
-		string(other).swap(*this);
+	template<typename Alloc>
+	inline stringT<Alloc>& stringT<Alloc>::operator=(const stringT<Alloc>& other) {
+		stringT<Alloc>(other).swap(*this);
 		return *this;
 		return *this;
 	}
 	}
 
 
-	inline const char* string::c_str() const {
+	template<typename Alloc>
+	inline const char* stringT<Alloc>::c_str() const {
 		return m_first;
 		return m_first;
 	}
 	}
 
 
-	inline size_t string::size() const
+	template<typename Alloc>
+	inline size_t stringT<Alloc>::size() const
 	{
 	{
 		return (size_t)(m_last - m_first);
 		return (size_t)(m_last - m_first);
 	}
 	}
 
 
-	inline void string::reserve(size_t capacity) {
-		if (m_first + capacity + 1 <= m_capacity)
+	template<typename Alloc>
+	inline bool stringT<Alloc>::empty() const
+	{
+		return 0 == size();
+	}
+
+	template<typename Alloc>
+	inline void stringT<Alloc>::reserve(size_t capacity) {
+		if (m_first + capacity + 1 <= m_capacity) {
 			return;
 			return;
+		}
 
 
 		const size_t size = (size_t)(m_last - m_first);
 		const size_t size = (size_t)(m_last - m_first);
 
 
-		pointer newfirst = (pointer)TINYSTL_ALLOCATOR::static_allocate(capacity + 1);
-		for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit)
+		pointer newfirst = (pointer)Alloc::static_allocate(capacity + 1);
+		for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit) {
 			*newit = *it;
 			*newit = *it;
-		if (m_first != m_buffer)
-			TINYSTL_ALLOCATOR::static_deallocate(m_first, m_capacity - m_first);
+		}
+
+		if (m_first != m_buffer) {
+			Alloc::static_deallocate(m_first, m_capacity - m_first);
+		}
 
 
 		m_first = newfirst;
 		m_first = newfirst;
 		m_last = newfirst + size;
 		m_last = newfirst + size;
 		m_capacity = m_first + capacity;
 		m_capacity = m_first + capacity;
 	}
 	}
 
 
-	inline void string::resize(size_t size) {
+	template<typename Alloc>
+	inline void stringT<Alloc>::resize(size_t size) {
 		reserve(size);
 		reserve(size);
 		for (pointer it = m_last, end = m_first + size + 1; it < end; ++it)
 		for (pointer it = m_last, end = m_first + size + 1; it < end; ++it)
 			*it = 0;
 			*it = 0;
@@ -147,7 +170,8 @@ namespace tinystl {
 		m_last += size;
 		m_last += size;
 	}
 	}
 
 
-	inline void string::append(const char* first, const char* last) {
+	template<typename Alloc>
+	inline void stringT<Alloc>::append(const char* first, const char* last) {
 		const size_t newsize = (size_t)((m_last - m_first) + (last - first) + 1);
 		const size_t newsize = (size_t)((m_last - m_first) + (last - first) + 1);
 		if (m_first + newsize > m_capacity)
 		if (m_first + newsize > m_capacity)
 			reserve((newsize * 3) / 2);
 			reserve((newsize * 3) / 2);
@@ -157,11 +181,13 @@ namespace tinystl {
 		*m_last = 0;
 		*m_last = 0;
 	}
 	}
 
 
-	inline void string::append(const char* str) {
+	template<typename Alloc>
+	inline void stringT<Alloc>::append(const char* str) {
 		append(str, str + strlen(str) );
 		append(str, str + strlen(str) );
 	}
 	}
 
 
-	inline void string::swap(string& other) {
+	template<typename Alloc>
+	inline void stringT<Alloc>::swap(stringT<Alloc>& other) {
 		const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity;
 		const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity;
 		m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity;
 		m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity;
 		other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity;
 		other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity;
@@ -193,7 +219,8 @@ namespace tinystl {
 		}
 		}
 	}
 	}
 
 
-	inline bool operator==(const string& lhs, const string& rhs) {
+	template<typename Alloc>
+	inline bool operator==(const stringT<Alloc>& lhs, const stringT<Alloc>& rhs) {
 		typedef const char* pointer;
 		typedef const char* pointer;
 
 
 		const size_t lsize = lhs.size(), rsize = rhs.size();
 		const size_t lsize = lhs.size(), rsize = rhs.size();
@@ -209,7 +236,8 @@ namespace tinystl {
 		return true;
 		return true;
 	}
 	}
 
 
-	static inline size_t hash(const string& value) {
+	template<typename Alloc>
+	static inline size_t hash(const stringT<Alloc>& value) {
 		return hash_string(value.c_str(), value.size());
 		return hash_string(value.c_str(), value.size());
 	}
 	}
 }
 }

+ 1 - 2
include/tinystl/unordered_map.h

@@ -27,7 +27,6 @@
 #ifndef TINYSTL_UNORDERED_MAP_H
 #ifndef TINYSTL_UNORDERED_MAP_H
 #define TINYSTL_UNORDERED_MAP_H
 #define TINYSTL_UNORDERED_MAP_H
 
 
-#include "allocator.h"
 #include "buffer.h"
 #include "buffer.h"
 #include "hash.h"
 #include "hash.h"
 #include "hash_base.h"
 #include "hash_base.h"
@@ -73,7 +72,7 @@ namespace tinystl {
 		typedef unordered_hash_node<Key, Value>* pointer;
 		typedef unordered_hash_node<Key, Value>* pointer;
 
 
 		size_t m_size;
 		size_t m_size;
-		tinystl::buffer<pointer, Alloc> m_buckets;
+		buffer<pointer, Alloc> m_buckets;
 	};
 	};
 
 
 	template<typename Key, typename Value, typename Alloc>
 	template<typename Key, typename Value, typename Alloc>

+ 1 - 2
include/tinystl/unordered_set.h

@@ -27,7 +27,6 @@
 #ifndef TINYSTL_UNORDERED_SET_H
 #ifndef TINYSTL_UNORDERED_SET_H
 #define TINYSTL_UNORDERED_SET_H
 #define TINYSTL_UNORDERED_SET_H
 
 
-#include "allocator.h"
 #include "buffer.h"
 #include "buffer.h"
 #include "hash.h"
 #include "hash.h"
 #include "hash_base.h"
 #include "hash_base.h"
@@ -65,7 +64,7 @@ namespace tinystl {
 		typedef unordered_hash_node<Key, void>* pointer;
 		typedef unordered_hash_node<Key, void>* pointer;
 
 
 		size_t m_size;
 		size_t m_size;
-		tinystl::buffer<pointer, Alloc> m_buckets;
+		buffer<pointer, Alloc> m_buckets;
 	};
 	};
 
 
 	template<typename Key, typename Alloc>
 	template<typename Key, typename Alloc>

+ 0 - 1
include/tinystl/vector.h

@@ -27,7 +27,6 @@
 #ifndef TINYSTL_VECTOR_H
 #ifndef TINYSTL_VECTOR_H
 #define TINYSTL_VECTOR_H
 #define TINYSTL_VECTOR_H
 
 
-#include "allocator.h"
 #include "buffer.h"
 #include "buffer.h"
 #include "new.h"
 #include "new.h"
 #include "stddef.h"
 #include "stddef.h"

+ 0 - 0
test/main.cpp → tests/main.cpp


+ 0 - 0
test/test.h → tests/test.h


+ 3 - 0
test/vector_complex.cpp → tests/vector_complex.cpp

@@ -25,7 +25,10 @@
  */
  */
 
 
 #include "test.h"
 #include "test.h"
+
+#include <tinystl/allocator.h>
 #include <tinystl/vector.h>
 #include <tinystl/vector.h>
+
 #include <algorithm>
 #include <algorithm>
 #include <string.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdlib.h>

+ 0 - 0
test/vector_header.cpp → tests/vector_header.cpp


+ 3 - 0
test/vector_nodefault.cpp → tests/vector_nodefault.cpp

@@ -25,7 +25,10 @@
  */
  */
 
 
 #include "test.h"
 #include "test.h"
+
+#include <tinystl/allocator.h>
 #include <tinystl/vector.h>
 #include <tinystl/vector.h>
+
 #include <algorithm>
 #include <algorithm>
 #include <string.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdlib.h>

+ 3 - 0
test/vector_primitive.cpp → tests/vector_primitive.cpp

@@ -25,7 +25,10 @@
  */
  */
 
 
 #include "test.h"
 #include "test.h"
+
+#include <tinystl/allocator.h>
 #include <tinystl/vector.h>
 #include <tinystl/vector.h>
+
 #include <algorithm>
 #include <algorithm>
 
 
 TEST(vector_constructor) {
 TEST(vector_constructor) {