Skip to content

Commit c8eb72c

Browse files
authored
[ZH] Unify code of AsciiString (#799)
1 parent 91b5eb8 commit c8eb72c

File tree

4 files changed

+91
-110
lines changed

4 files changed

+91
-110
lines changed

GeneralsMD/Code/GameEngine/Include/Common/AsciiString.h

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@
5757

5858
class UnicodeString;
5959

60-
#include "windows.h"
61-
6260
// -----------------------------------------------------
6361
/**
6462
AsciiString is the fundamental single-byte string type used in the Generals
@@ -107,8 +105,6 @@ class AsciiString
107105
inline void validate() const { }
108106
#endif
109107

110-
void freeBytes(void);
111-
112108
protected:
113109
AsciiStringData* m_data; // pointer to ref counted string data
114110

@@ -359,46 +355,6 @@ inline AsciiString::AsciiString() : m_data(0)
359355
validate();
360356
}
361357

362-
// -----------------------------------------------------
363-
inline AsciiString::AsciiString(const char* s) : m_data(0)
364-
{
365-
//DEBUG_ASSERTCRASH(isMemoryManagerOfficiallyInited(), ("Initializing AsciiStrings prior to main (ie, as static vars) can cause memory leak reporting problems. Are you sure you want to do this?\n"));
366-
int len = (s)?strlen(s):0;
367-
if (len)
368-
{
369-
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
370-
}
371-
validate();
372-
}
373-
374-
// -----------------------------------------------------
375-
inline AsciiString::AsciiString(const AsciiString& stringSrc) : m_data(stringSrc.m_data)
376-
{
377-
// don't need this if we're using InterlockedIncrement
378-
// FastCriticalSectionClass::LockClass lock(TheAsciiStringCriticalSection);
379-
if (m_data)
380-
// ++m_data->m_refCount;
381-
// yes, I know it's not a DWord but we're incrementing so we're safe
382-
InterlockedIncrement((long *)&m_data->m_refCount);
383-
validate();
384-
}
385-
386-
// -----------------------------------------------------
387-
inline void AsciiString::releaseBuffer()
388-
{
389-
// FastCriticalSectionClass::LockClass lock(TheAsciiStringCriticalSection);
390-
391-
validate();
392-
if (m_data)
393-
{
394-
InterlockedDecrement((long *)&m_data->m_refCount);
395-
if (!m_data->m_refCount)
396-
freeBytes();
397-
m_data = 0;
398-
}
399-
validate();
400-
}
401-
402358
// -----------------------------------------------------
403359
inline AsciiString::~AsciiString()
404360
{
@@ -444,49 +400,6 @@ inline const char* AsciiString::str() const
444400
return m_data ? peek() : &TheNullChr;
445401
}
446402

447-
// -----------------------------------------------------
448-
inline void AsciiString::set(const AsciiString& stringSrc)
449-
{
450-
//FastCriticalSectionClass::LockClass lock(TheAsciiStringCriticalSection);
451-
452-
validate();
453-
if (&stringSrc != this)
454-
{
455-
// do not call releaseBuffer(); here, it locks the CS twice
456-
// from the same thread which is illegal using fast CS's
457-
if (m_data)
458-
{
459-
InterlockedDecrement((long *)&m_data->m_refCount);
460-
if (!m_data->m_refCount)
461-
freeBytes();
462-
}
463-
464-
m_data = stringSrc.m_data;
465-
if (m_data)
466-
InterlockedIncrement((long *)&m_data->m_refCount);
467-
}
468-
validate();
469-
}
470-
471-
// -----------------------------------------------------
472-
inline void AsciiString::set(const char* s)
473-
{
474-
validate();
475-
if (!m_data || s != peek())
476-
{
477-
int len = s ? strlen(s) : 0;
478-
if (len)
479-
{
480-
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
481-
}
482-
else
483-
{
484-
releaseBuffer();
485-
}
486-
}
487-
validate();
488-
}
489-
490403
// -----------------------------------------------------
491404
inline AsciiString& AsciiString::operator=(const AsciiString& stringSrc)
492405
{
@@ -505,25 +418,6 @@ inline AsciiString& AsciiString::operator=(const char* s)
505418
return *this;
506419
}
507420

508-
// -----------------------------------------------------
509-
inline void AsciiString::concat(const char* s)
510-
{
511-
validate();
512-
int addlen = strlen(s);
513-
if (addlen == 0)
514-
return; // my, that was easy
515-
516-
if (m_data)
517-
{
518-
ensureUniqueBufferOfSize(getLength() + addlen + 1, true, NULL, s);
519-
}
520-
else
521-
{
522-
set(s);
523-
}
524-
validate();
525-
}
526-
527421
// -----------------------------------------------------
528422
inline void AsciiString::concat(const AsciiString& stringSrc)
529423
{

GeneralsMD/Code/GameEngine/Include/Common/CriticalSection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ScopedCriticalSection
9999

100100
// These should be NULL on creation then non-NULL in WinMain or equivalent.
101101
// This allows us to be silently non-threadsafe for WB and other single-threaded apps.
102-
extern FastCriticalSectionClass TheAsciiStringCriticalSection;
102+
extern CriticalSection *TheAsciiStringCriticalSection;
103103
extern CriticalSection *TheUnicodeStringCriticalSection;
104104
extern CriticalSection *TheDmaCriticalSection;
105105
extern CriticalSection *TheMemoryPoolCriticalSection;

GeneralsMD/Code/GameEngine/Source/Common/System/AsciiString.cpp

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ inline char* skipNonWhitespace(char* p)
8383
return p;
8484
}
8585

86-
void AsciiString::freeBytes(void)
86+
// -----------------------------------------------------
87+
AsciiString::AsciiString(const AsciiString& stringSrc) : m_data(stringSrc.m_data)
8788
{
88-
TheDynamicMemoryAllocator->freeBytes(m_data);
89+
ScopedCriticalSection scopedCriticalSection(TheAsciiStringCriticalSection);
90+
if (m_data)
91+
++m_data->m_refCount;
92+
validate();
8993
}
9094

9195
// -----------------------------------------------------
@@ -163,6 +167,70 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
163167
}
164168

165169

170+
// -----------------------------------------------------
171+
void AsciiString::releaseBuffer()
172+
{
173+
ScopedCriticalSection scopedCriticalSection(TheAsciiStringCriticalSection);
174+
175+
validate();
176+
if (m_data)
177+
{
178+
if (--m_data->m_refCount == 0)
179+
{
180+
TheDynamicMemoryAllocator->freeBytes(m_data);
181+
}
182+
m_data = 0;
183+
}
184+
validate();
185+
}
186+
187+
// -----------------------------------------------------
188+
AsciiString::AsciiString(const char* s) : m_data(0)
189+
{
190+
//DEBUG_ASSERTCRASH(isMemoryManagerOfficiallyInited(), ("Initializing AsciiStrings prior to main (ie, as static vars) can cause memory leak reporting problems. Are you sure you want to do this?\n"));
191+
int len = (s)?strlen(s):0;
192+
if (len)
193+
{
194+
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
195+
}
196+
validate();
197+
}
198+
199+
// -----------------------------------------------------
200+
void AsciiString::set(const AsciiString& stringSrc)
201+
{
202+
ScopedCriticalSection scopedCriticalSection(TheAsciiStringCriticalSection);
203+
204+
validate();
205+
if (&stringSrc != this)
206+
{
207+
releaseBuffer();
208+
m_data = stringSrc.m_data;
209+
if (m_data)
210+
++m_data->m_refCount;
211+
}
212+
validate();
213+
}
214+
215+
// -----------------------------------------------------
216+
void AsciiString::set(const char* s)
217+
{
218+
validate();
219+
if (!m_data || s != peek())
220+
{
221+
int len = s ? strlen(s) : 0;
222+
if (len)
223+
{
224+
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
225+
}
226+
else
227+
{
228+
releaseBuffer();
229+
}
230+
}
231+
validate();
232+
}
233+
166234
// -----------------------------------------------------
167235
char* AsciiString::getBufferForRead(Int len)
168236
{
@@ -185,6 +253,25 @@ void AsciiString::translate(const UnicodeString& stringSrc)
185253
validate();
186254
}
187255

256+
// -----------------------------------------------------
257+
void AsciiString::concat(const char* s)
258+
{
259+
validate();
260+
int addlen = strlen(s);
261+
if (addlen == 0)
262+
return; // my, that was easy
263+
264+
if (m_data)
265+
{
266+
ensureUniqueBufferOfSize(getLength() + addlen + 1, true, NULL, s);
267+
}
268+
else
269+
{
270+
set(s);
271+
}
272+
validate();
273+
}
274+
188275
// -----------------------------------------------------
189276
void AsciiString::trim()
190277
{

GeneralsMD/Code/GameEngine/Source/Common/System/CriticalSection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "Common/CriticalSection.h"
2828

2929
// Definitions.
30-
FastCriticalSectionClass TheAsciiStringCriticalSection;
30+
CriticalSection *TheAsciiStringCriticalSection = NULL;
3131
CriticalSection *TheUnicodeStringCriticalSection = NULL;
3232
CriticalSection *TheDmaCriticalSection = NULL;
3333
CriticalSection *TheMemoryPoolCriticalSection = NULL;

0 commit comments

Comments
 (0)