4444// /////////////////////////////////////////////////////////////////////////////
4545#include " PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
4646
47- #include " Common/CriticalSection.h"
48-
4947
5048// -----------------------------------------------------
5149
@@ -86,9 +84,8 @@ inline char* skipNonWhitespace(char* p)
8684// -----------------------------------------------------
8785AsciiString::AsciiString (const AsciiString& stringSrc) : m_data(stringSrc.m_data)
8886{
89- ScopedCriticalSection scopedCriticalSection (TheAsciiStringCriticalSection);
9087 if (m_data)
91- ++ m_data->m_refCount ;
88+ InterlockedIncrement (& m_data->m_refCount ) ;
9289 validate ();
9390}
9491
@@ -171,8 +168,14 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
171168 if (strToCat)
172169 strcat (newData->peek (), strToCat);
173170
174- releaseBuffer ();
175- m_data = newData;
171+ AsciiStringData* oldData = (AsciiStringData*)InterlockedExchangePointer ((PVOID*)&m_data, newData);
172+ if (oldData)
173+ {
174+ if (InterlockedDecrement (&oldData->m_refCount ) == 0 )
175+ {
176+ TheDynamicMemoryAllocator->freeBytes (oldData);
177+ }
178+ }
176179
177180 validate ();
178181}
@@ -181,18 +184,15 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
181184// -----------------------------------------------------
182185void AsciiString::releaseBuffer ()
183186{
184- ScopedCriticalSection scopedCriticalSection (TheAsciiStringCriticalSection);
185-
186187 validate ();
187- if (m_data)
188+ AsciiStringData* data = (AsciiStringData*)InterlockedExchangePointer ((PVOID*)&m_data, nullptr );
189+ if (data)
188190 {
189- if (--m_data- >m_refCount == 0 )
191+ if (InterlockedDecrement (&data- >m_refCount ) == 0 )
190192 {
191- TheDynamicMemoryAllocator->freeBytes (m_data );
193+ TheDynamicMemoryAllocator->freeBytes (data );
192194 }
193- m_data = nullptr ;
194195 }
195- validate ();
196196}
197197
198198// -----------------------------------------------------
@@ -220,15 +220,23 @@ AsciiString::AsciiString(const char* s, int len) : m_data(nullptr)
220220// -----------------------------------------------------
221221void AsciiString::set (const AsciiString& stringSrc)
222222{
223- ScopedCriticalSection scopedCriticalSection (TheAsciiStringCriticalSection);
224-
225223 validate ();
226224 if (&stringSrc != this )
227225 {
228- releaseBuffer ();
229- m_data = stringSrc.m_data ;
230- if (m_data)
231- ++m_data->m_refCount ;
226+ AsciiStringData* newData = stringSrc.m_data ;
227+ if (newData)
228+ {
229+ InterlockedIncrement (&newData->m_refCount );
230+ }
231+
232+ AsciiStringData* oldData = (AsciiStringData*)InterlockedExchangePointer ((PVOID*)&m_data, newData);
233+ if (oldData)
234+ {
235+ if (InterlockedDecrement (&oldData->m_refCount ) == 0 )
236+ {
237+ TheDynamicMemoryAllocator->freeBytes (oldData);
238+ }
239+ }
232240 }
233241 validate ();
234242}
0 commit comments