1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2024-12-22 09:38:56 +08:00

Force KeyValues to use g_pMemAlloc where applicable for string alloc.

This commit is contained in:
Nicholas Hastings 2015-07-12 15:25:35 -04:00
parent c6a0af0f3c
commit 98fe5b5a34
2 changed files with 41 additions and 30 deletions

Binary file not shown.

View File

@ -32,6 +32,17 @@
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
template<typename T>
T *KVStringAlloc(size_t nLength)
{
return reinterpret_cast<T*>(MemAlloc_Alloc(sizeof(T) * nLength));
}
void KVStringDelete(void* pMem)
{
MemAlloc_Free(pMem);
}
static const char * s_LastFileLoadingFrom = "unknown"; // just needed for error messages
// Statics for the growable string table
@ -484,9 +495,9 @@ void KeyValues::RemoveEverything()
delete dat;
}
delete [] m_sValue;
KVStringDelete(m_sValue);
m_sValue = NULL;
delete [] m_wsValue;
KVStringDelete(m_wsValue);
m_wsValue = NULL;
}
@ -1425,7 +1436,7 @@ const wchar_t *KeyValues::GetWString( const char *keyName, const wchar_t *defaul
case TYPE_STRING:
{
int bufSize = Q_strlen(dat->m_sValue) + 1;
wchar_t *pWBuf = new wchar_t[ bufSize ];
wchar_t *pWBuf = KVStringAlloc<wchar_t>( bufSize );
int result = Q_UTF8ToUnicode(dat->m_sValue, pWBuf, bufSize * sizeof( wchar_t ) );
if ( result >= 0 ) // may be a zero length string
{
@ -1433,10 +1444,10 @@ const wchar_t *KeyValues::GetWString( const char *keyName, const wchar_t *defaul
}
else
{
delete [] pWBuf;
KVStringDelete(pWBuf);
return defaultValue;
}
delete [] pWBuf;
KVStringDelete(pWBuf);
break;
}
default:
@ -1524,9 +1535,9 @@ void KeyValues::SetColor( const char *keyName, Color value)
void KeyValues::SetStringValue( char const *strValue )
{
// delete the old value
delete [] m_sValue;
KVStringDelete(m_sValue);
// make sure we're not storing the WSTRING - as we're converting over to STRING
delete [] m_wsValue;
KVStringDelete(m_wsValue);
m_wsValue = NULL;
if (!strValue)
@ -1537,7 +1548,7 @@ void KeyValues::SetStringValue( char const *strValue )
// allocate memory for the new value and copy it in
int len = Q_strlen( strValue );
m_sValue = new char[len + 1];
m_sValue = KVStringAlloc<char>(len + 1);
Q_memcpy( m_sValue, strValue, len+1 );
m_iDataType = TYPE_STRING;
@ -1558,9 +1569,9 @@ void KeyValues::SetString( const char *keyName, const char *value )
}
// delete the old value
delete [] dat->m_sValue;
KVStringDelete(dat->m_sValue);
// make sure we're not storing the WSTRING - as we're converting over to STRING
delete [] dat->m_wsValue;
KVStringDelete(dat->m_wsValue);
dat->m_wsValue = NULL;
if (!value)
@ -1571,7 +1582,7 @@ void KeyValues::SetString( const char *keyName, const char *value )
// allocate memory for the new value and copy it in
int len = Q_strlen( value );
dat->m_sValue = new char[len + 1];
dat->m_sValue = KVStringAlloc<char>(len + 1);
Q_memcpy( dat->m_sValue, value, len+1 );
dat->m_iDataType = TYPE_STRING;
@ -1587,9 +1598,9 @@ void KeyValues::SetWString( const char *keyName, const wchar_t *value )
if ( dat )
{
// delete the old value
delete [] dat->m_wsValue;
KVStringDelete(dat->m_wsValue);
// make sure we're not storing the STRING - as we're converting over to WSTRING
delete [] dat->m_sValue;
KVStringDelete(dat->m_sValue);
dat->m_sValue = NULL;
if (!value)
@ -1600,7 +1611,7 @@ void KeyValues::SetWString( const char *keyName, const wchar_t *value )
// allocate memory for the new value and copy it in
int len = Q_wcslen( value );
dat->m_wsValue = new wchar_t[len + 1];
dat->m_wsValue = KVStringAlloc<wchar_t>(len + 1);
Q_memcpy( dat->m_wsValue, value, (len+1) * sizeof(wchar_t) );
dat->m_iDataType = TYPE_WSTRING;
@ -1631,12 +1642,12 @@ void KeyValues::SetUint64( const char *keyName, uint64 value )
if ( dat )
{
// delete the old value
delete [] dat->m_sValue;
KVStringDelete(dat->m_sValue);
// make sure we're not storing the WSTRING - as we're converting over to STRING
delete [] dat->m_wsValue;
KVStringDelete(dat->m_wsValue);
dat->m_wsValue = NULL;
dat->m_sValue = new char[sizeof(uint64)];
dat->m_sValue = KVStringAlloc<char>(sizeof(uint64));
*((uint64 *)dat->m_sValue) = value;
dat->m_iDataType = TYPE_UINT64;
}
@ -1693,7 +1704,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
if( src.m_sValue )
{
int len = Q_strlen(src.m_sValue) + 1;
m_sValue = new char[len];
m_sValue = KVStringAlloc<char>(len);
Q_strncpy( m_sValue, src.m_sValue, len );
}
break;
@ -1702,7 +1713,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
m_iValue = src.m_iValue;
Q_snprintf( buf,sizeof(buf), "%d", m_iValue );
int len = Q_strlen(buf) + 1;
m_sValue = new char[len];
m_sValue = KVStringAlloc<char>(len);
Q_strncpy( m_sValue, buf, len );
}
break;
@ -1711,7 +1722,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
m_flValue = src.m_flValue;
Q_snprintf( buf,sizeof(buf), "%f", m_flValue );
int len = Q_strlen(buf) + 1;
m_sValue = new char[len];
m_sValue = KVStringAlloc<char>(len);
Q_strncpy( m_sValue, buf, len );
}
break;
@ -1722,7 +1733,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
break;
case TYPE_UINT64:
{
m_sValue = new char[sizeof(uint64)];
m_sValue = KVStringAlloc<char>(sizeof(uint64));
Q_memcpy( m_sValue, src.m_sValue, sizeof(uint64) );
}
break;
@ -1836,7 +1847,7 @@ KeyValues *KeyValues::MakeCopy( void ) const
{
int len = Q_strlen( m_sValue );
Assert( !newKeyValue->m_sValue );
newKeyValue->m_sValue = new char[len + 1];
newKeyValue->m_sValue = KVStringAlloc<char>(len + 1);
Q_memcpy( newKeyValue->m_sValue, m_sValue, len+1 );
}
}
@ -1846,7 +1857,7 @@ KeyValues *KeyValues::MakeCopy( void ) const
if ( m_wsValue )
{
int len = Q_wcslen( m_wsValue );
newKeyValue->m_wsValue = new wchar_t[len+1];
newKeyValue->m_wsValue = KVStringAlloc<wchar_t>(len + 1);
Q_memcpy( newKeyValue->m_wsValue, m_wsValue, (len+1)*sizeof(wchar_t));
}
}
@ -1872,7 +1883,7 @@ KeyValues *KeyValues::MakeCopy( void ) const
break;
case TYPE_UINT64:
newKeyValue->m_sValue = new char[sizeof(uint64)];
newKeyValue->m_sValue = KVStringAlloc<char>(sizeof(uint64));
Q_memcpy( newKeyValue->m_sValue, m_sValue, sizeof(uint64) );
break;
};
@ -2254,7 +2265,7 @@ bool KeyValues::LoadFromBuffer( char const *resourceName, const char *pBuffer, I
if ( nLen > 2 && (uint8)pBuffer[0] == 0xFF && (uint8)pBuffer[1] == 0xFE )
{
int nUTF8Len = V_UnicodeToUTF8( (wchar_t*)(pBuffer+2), NULL, 0 );
char *pUTF8Buf = new char[nUTF8Len];
char *pUTF8Buf = KVStringAlloc<char>(nUTF8Len);
V_UnicodeToUTF8( (wchar_t*)(pBuffer+2), pUTF8Buf, nUTF8Len );
buf.AssumeMemory( pUTF8Buf, nUTF8Len, nUTF8Len, CUtlBuffer::READ_ONLY | CUtlBuffer::TEXT_BUFFER );
}
@ -2347,7 +2358,7 @@ void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &b
if (dat->m_sValue)
{
delete[] dat->m_sValue;
KVStringDelete(dat->m_sValue);
dat->m_sValue = NULL;
}
@ -2389,7 +2400,7 @@ void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &b
digit -= 'A' - ( '9' + 1 );
retVal = ( retVal * 16 ) + ( digit - '0' );
}
dat->m_sValue = new char[sizeof(uint64)];
dat->m_sValue = KVStringAlloc<char>(sizeof(uint64));
*((uint64 *)dat->m_sValue) = retVal;
dat->m_iDataType = TYPE_UINT64;
}
@ -2411,7 +2422,7 @@ void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &b
if (dat->m_iDataType == TYPE_STRING)
{
// copy in the string information
dat->m_sValue = new char[len+1];
dat->m_sValue = KVStringAlloc<char>(len + 1);
Q_memcpy( dat->m_sValue, value, len+1 );
}
@ -2594,7 +2605,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer, int nStackDepth )
token[KEYVALUES_TOKEN_SIZE-1] = 0;
int len = Q_strlen( token );
dat->m_sValue = new char[len + 1];
dat->m_sValue = KVStringAlloc<char>(len + 1);
Q_memcpy( dat->m_sValue, token, len+1 );
break;
@ -2613,7 +2624,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer, int nStackDepth )
case TYPE_UINT64:
{
dat->m_sValue = new char[sizeof(uint64)];
dat->m_sValue = KVStringAlloc<char>(sizeof(uint64));
*((uint64 *)dat->m_sValue) = buffer.GetInt64();
break;
}