From 82f99ea9d23e3594dd3637bd6e664d3e0d75255a Mon Sep 17 00:00:00 2001 From: castano Date: Sat, 13 Jun 2009 13:54:04 +0000 Subject: [PATCH] Fix non-thread-safe code. --- src/nvcore/Debug.h | 1 + src/nvcore/StrLib.cpp | 14 +++++------- src/nvcore/StrLib.h | 52 +++++++++++++++++++++---------------------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/nvcore/Debug.h b/src/nvcore/Debug.h index c0c8df3..c1f0ca5 100644 --- a/src/nvcore/Debug.h +++ b/src/nvcore/Debug.h @@ -115,6 +115,7 @@ namespace nv { NVCORE_API void dumpInfo(); + // These functions are not thread safe. NVCORE_API void setMessageHandler( MessageHandler * messageHandler ); NVCORE_API void resetMessageHandler(); diff --git a/src/nvcore/StrLib.cpp b/src/nvcore/StrLib.cpp index 21456e1..34cf992 100644 --- a/src/nvcore/StrLib.cpp +++ b/src/nvcore/StrLib.cpp @@ -545,8 +545,6 @@ const char * Path::extension(const char * str) } -// static -String String::s_null(String::null); /// Clone this string String String::clone() const @@ -557,13 +555,13 @@ String String::clone() const void String::setString(const char * str) { - if( str == NULL ) { - data = s_null.data; + if (str == NULL) { + data = NULL; } else { allocString( str ); + addRef(); } - addRef(); } void String::setString(const char * str, int length) @@ -576,11 +574,11 @@ void String::setString(const char * str, int length) void String::setString(const StringBuilder & str) { - if( str.str() == NULL ) { - data = s_null.data; + if (str.str() == NULL) { + data = NULL; } else { allocString(str); + addRef(); } - addRef(); } diff --git a/src/nvcore/StrLib.h b/src/nvcore/StrLib.h index e425bb6..6b00b8a 100644 --- a/src/nvcore/StrLib.h +++ b/src/nvcore/StrLib.h @@ -151,15 +151,14 @@ namespace nv /// Constructs a null string. @sa isNull() String() { - data = s_null.data; - addRef(); + data = NULL; } /// Constructs a shared copy of str. String(const String & str) { data = str.data; - addRef(); + if (data != NULL) addRef(); } /// Constructs a shared string from a standard string. @@ -256,7 +255,7 @@ namespace nv } /// Returns true if this string is the null string. - bool isNull() const { nvDebugCheck(data != NULL); return data == s_null.data; } + bool isNull() const { return data == NULL; } /// Return the exact length. uint length() const { nvDebugCheck(data != NULL); return uint(strlen(data)); } @@ -265,44 +264,45 @@ namespace nv uint hash() const { nvDebugCheck(data != NULL); return strHash(data); } /// const char * cast operator. - operator const char * () const { nvDebugCheck(data != NULL); return data; } + operator const char * () const { return data; } /// Get string pointer. - const char * str() const { nvDebugCheck(data != NULL); return data; } + const char * str() const { return data; } private: - enum null_t { null }; - - // Private constructor for null string. - String(null_t) { - setString(""); - } - // Add reference count. - void addRef() { - nvDebugCheck(data != NULL); - setRefCount(getRefCount() + 1); + void addRef() + { + if (data != NULL) + { + setRefCount(getRefCount() + 1); + } } // Decrease reference count. - void release() { - nvDebugCheck(data != NULL); - - const uint16 count = getRefCount(); - setRefCount(count - 1); - if( count - 1 == 0 ) { - mem::free(data - 2); - data = NULL; + void release() + { + if (data != NULL) + { + const uint16 count = getRefCount(); + setRefCount(count - 1); + if (count - 1 == 0) { + mem::free(data - 2); + data = NULL; + } } } - uint16 getRefCount() const { + uint16 getRefCount() const + { + nvDebugCheck(data != NULL); return *reinterpret_cast(data - 2); } void setRefCount(uint16 count) { + nvDebugCheck(data != NULL); nvCheck(count < 0xFFFF); *reinterpret_cast(const_cast(data - 2)) = uint16(count); } @@ -341,8 +341,6 @@ namespace nv private: - NVCORE_API static String s_null; - const char * data; };