From 05ca00212e7100d0efa238a595707628ae9d1756 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 6 Oct 2013 19:28:41 -0700 Subject: [PATCH] TinySTL: Fixed bug when string container is used with multiple different allocators. --- include/bx/mutex.h | 38 ------------ include/tinystl/allocator.h | 5 +- include/tinystl/buffer.h | 1 - include/tinystl/string.h | 90 ++++++++++++++++++---------- include/tinystl/unordered_map.h | 3 +- include/tinystl/unordered_set.h | 3 +- include/tinystl/vector.h | 1 - {test => tests}/main.cpp | 0 {test => tests}/test.h | 0 {test => tests}/vector_complex.cpp | 3 + {test => tests}/vector_header.cpp | 0 {test => tests}/vector_nodefault.cpp | 3 + {test => tests}/vector_primitive.cpp | 3 + 13 files changed, 73 insertions(+), 77 deletions(-) rename {test => tests}/main.cpp (100%) rename {test => tests}/test.h (100%) rename {test => tests}/vector_complex.cpp (99%) rename {test => tests}/vector_header.cpp (100%) rename {test => tests}/vector_nodefault.cpp (99%) rename {test => tests}/vector_primitive.cpp (99%) diff --git a/include/bx/mutex.h b/include/bx/mutex.h index c12e062..e7fcf1a 100644 --- a/include/bx/mutex.h +++ b/include/bx/mutex.h @@ -104,45 +104,7 @@ namespace bx Mutex& m_mutex; }; -#if 1 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 { diff --git a/include/tinystl/allocator.h b/include/tinystl/allocator.h index 0eb18d2..a0cce7b 100644 --- a/include/tinystl/allocator.h +++ b/include/tinystl/allocator.h @@ -29,6 +29,8 @@ #include "stddef.h" +#ifndef TINYSTL_ALLOCATOR + namespace tinystl { struct allocator { @@ -42,8 +44,7 @@ namespace tinystl { }; } -#ifndef TINYSTL_ALLOCATOR # define TINYSTL_ALLOCATOR ::tinystl::allocator -#endif +#endif // TINYSTL_ALLOCATOR #endif diff --git a/include/tinystl/buffer.h b/include/tinystl/buffer.h index ce7470b..fae9ba4 100644 --- a/include/tinystl/buffer.h +++ b/include/tinystl/buffer.h @@ -27,7 +27,6 @@ #ifndef TINYSTL_BUFFER_H #define TINYSTL_BUFFER_H -#include "allocator.h" #include "new.h" #include "traits.h" diff --git a/include/tinystl/string.h b/include/tinystl/string.h index c878531..ba338c3 100644 --- a/include/tinystl/string.h +++ b/include/tinystl/string.h @@ -27,24 +27,25 @@ #ifndef TINYSTL_STRING_H #define TINYSTL_STRING_H -#include "allocator.h" #include "stddef.h" #include "hash.h" namespace tinystl { - class string { + template + class stringT { public: - string(); - string(const string& other); - string(const char* sz); - string(const char* sz, size_t len); - ~string(); + stringT(); + stringT(const stringT& other); + stringT(const char* sz); + stringT(const char* sz, size_t len); + ~stringT(); - string& operator=(const string& other); + stringT& operator=(const stringT& other); const char* c_str() const; size_t size() const; + bool empty() const; void reserve(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* str); - void swap(string& other); + void swap(stringT& other); private: typedef char* pointer; @@ -64,7 +65,10 @@ namespace tinystl { char m_buffer[12]; }; - inline string::string() + typedef stringT string; + + template + inline stringT::stringT() : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -72,7 +76,8 @@ namespace tinystl { resize(0); } - inline string::string(const string& other) + template + inline stringT::stringT(const stringT& other) : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -81,7 +86,8 @@ namespace tinystl { append(other.m_first, other.m_last); } - inline string::string(const char* sz) + template + inline stringT::stringT(const char* sz) : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -94,7 +100,8 @@ namespace tinystl { append(sz, sz + len); } - inline string::string(const char* sz, size_t len) + template + inline stringT::stringT(const char* sz, size_t len) : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -103,43 +110,59 @@ namespace tinystl { append(sz, sz + len); } - inline string::~string() { + template + inline stringT::~stringT() { 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 + inline stringT& stringT::operator=(const stringT& other) { + stringT(other).swap(*this); return *this; } - inline const char* string::c_str() const { + template + inline const char* stringT::c_str() const { return m_first; } - inline size_t string::size() const + template + inline size_t stringT::size() const { return (size_t)(m_last - m_first); } - inline void string::reserve(size_t capacity) { - if (m_first + capacity + 1 <= m_capacity) + template + inline bool stringT::empty() const + { + return 0 == size(); + } + + template + inline void stringT::reserve(size_t capacity) { + if (m_first + capacity + 1 <= m_capacity) { return; + } 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; - 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_last = newfirst + size; m_capacity = m_first + capacity; } - inline void string::resize(size_t size) { + template + inline void stringT::resize(size_t size) { reserve(size); for (pointer it = m_last, end = m_first + size + 1; it < end; ++it) *it = 0; @@ -147,7 +170,8 @@ namespace tinystl { m_last += size; } - inline void string::append(const char* first, const char* last) { + template + inline void stringT::append(const char* first, const char* last) { const size_t newsize = (size_t)((m_last - m_first) + (last - first) + 1); if (m_first + newsize > m_capacity) reserve((newsize * 3) / 2); @@ -157,11 +181,13 @@ namespace tinystl { *m_last = 0; } - inline void string::append(const char* str) { + template + inline void stringT::append(const char* str) { append(str, str + strlen(str) ); } - inline void string::swap(string& other) { + template + inline void stringT::swap(stringT& other) { 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; 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 + inline bool operator==(const stringT& lhs, const stringT& rhs) { typedef const char* pointer; const size_t lsize = lhs.size(), rsize = rhs.size(); @@ -209,7 +236,8 @@ namespace tinystl { return true; } - static inline size_t hash(const string& value) { + template + static inline size_t hash(const stringT& value) { return hash_string(value.c_str(), value.size()); } } diff --git a/include/tinystl/unordered_map.h b/include/tinystl/unordered_map.h index 025b57f..64f8457 100644 --- a/include/tinystl/unordered_map.h +++ b/include/tinystl/unordered_map.h @@ -27,7 +27,6 @@ #ifndef TINYSTL_UNORDERED_MAP_H #define TINYSTL_UNORDERED_MAP_H -#include "allocator.h" #include "buffer.h" #include "hash.h" #include "hash_base.h" @@ -73,7 +72,7 @@ namespace tinystl { typedef unordered_hash_node* pointer; size_t m_size; - tinystl::buffer m_buckets; + buffer m_buckets; }; template diff --git a/include/tinystl/unordered_set.h b/include/tinystl/unordered_set.h index 6c3336c..a4caddd 100644 --- a/include/tinystl/unordered_set.h +++ b/include/tinystl/unordered_set.h @@ -27,7 +27,6 @@ #ifndef TINYSTL_UNORDERED_SET_H #define TINYSTL_UNORDERED_SET_H -#include "allocator.h" #include "buffer.h" #include "hash.h" #include "hash_base.h" @@ -65,7 +64,7 @@ namespace tinystl { typedef unordered_hash_node* pointer; size_t m_size; - tinystl::buffer m_buckets; + buffer m_buckets; }; template diff --git a/include/tinystl/vector.h b/include/tinystl/vector.h index 7bbff18..81d9f67 100644 --- a/include/tinystl/vector.h +++ b/include/tinystl/vector.h @@ -27,7 +27,6 @@ #ifndef TINYSTL_VECTOR_H #define TINYSTL_VECTOR_H -#include "allocator.h" #include "buffer.h" #include "new.h" #include "stddef.h" diff --git a/test/main.cpp b/tests/main.cpp similarity index 100% rename from test/main.cpp rename to tests/main.cpp diff --git a/test/test.h b/tests/test.h similarity index 100% rename from test/test.h rename to tests/test.h diff --git a/test/vector_complex.cpp b/tests/vector_complex.cpp similarity index 99% rename from test/vector_complex.cpp rename to tests/vector_complex.cpp index c3965d7..97716fb 100644 --- a/test/vector_complex.cpp +++ b/tests/vector_complex.cpp @@ -25,7 +25,10 @@ */ #include "test.h" + +#include #include + #include #include #include diff --git a/test/vector_header.cpp b/tests/vector_header.cpp similarity index 100% rename from test/vector_header.cpp rename to tests/vector_header.cpp diff --git a/test/vector_nodefault.cpp b/tests/vector_nodefault.cpp similarity index 99% rename from test/vector_nodefault.cpp rename to tests/vector_nodefault.cpp index 332dd40..319fd55 100644 --- a/test/vector_nodefault.cpp +++ b/tests/vector_nodefault.cpp @@ -25,7 +25,10 @@ */ #include "test.h" + +#include #include + #include #include #include diff --git a/test/vector_primitive.cpp b/tests/vector_primitive.cpp similarity index 99% rename from test/vector_primitive.cpp rename to tests/vector_primitive.cpp index 850d141..06fd8fd 100644 --- a/test/vector_primitive.cpp +++ b/tests/vector_primitive.cpp @@ -25,7 +25,10 @@ */ #include "test.h" + +#include #include + #include TEST(vector_constructor) {