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

Force KeyValues to use g_pMemAlloc where applicable for string alloc.

This commit is contained in:
Nicholas Hastings 2015-07-12 19:08:04 -04:00
parent 94467900cc
commit db2dc1a2a0
3 changed files with 45 additions and 26 deletions

Binary file not shown.

View File

@ -174,6 +174,14 @@ inline void *MemAlloc_Alloc( size_t nSize, const char *pFileName, int nLine )
return g_pMemAlloc->IndirectAlloc( nSize, pFileName, nLine );
}
#endif
inline void MemAlloc_Free( void *ptr )
{
g_pMemAlloc->Free( ptr );
}
inline void MemAlloc_Free( void *ptr, const char *pFileName, int nLine )
{
g_pMemAlloc->Free( ptr, pFileName, nLine );
}
//-----------------------------------------------------------------------------

View File

@ -27,6 +27,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
#define KEYVALUES_TOKEN_SIZE 1024
@ -322,9 +333,9 @@ void KeyValues::RemoveEverything()
delete dat;
}
delete [] m_sValue;
KVStringDelete(m_sValue);
m_sValue = NULL;
delete [] m_wsValue;
KVStringDelete(m_wsValue);
m_wsValue = NULL;
}
@ -1279,9 +1290,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)
@ -1292,7 +1303,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;
@ -1308,9 +1319,9 @@ void KeyValues::SetString( const char *keyName, const char *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;
if (!value)
@ -1321,7 +1332,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;
@ -1337,9 +1348,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)
@ -1350,7 +1361,7 @@ void KeyValues::SetWString( const char *keyName, const wchar_t *value )
// allocate memory for the new value and copy it in
int len = 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;
@ -1381,12 +1392,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;
}
@ -1443,7 +1454,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;
@ -1452,7 +1463,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;
@ -1461,7 +1472,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;
@ -1472,7 +1483,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;
@ -1583,7 +1594,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 );
}
}
@ -1593,7 +1604,7 @@ KeyValues *KeyValues::MakeCopy( void ) const
if ( m_wsValue )
{
int len = 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));
}
}
@ -1619,7 +1630,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;
};
@ -2062,7 +2073,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;
}
@ -2094,7 +2105,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;
}
@ -2116,7 +2127,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 );
}
@ -2274,7 +2285,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer )
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;
@ -2293,7 +2304,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer )
case TYPE_UINT64:
{
dat->m_sValue = new char[sizeof(uint64)];
dat->m_sValue = KVStringAlloc<char>(sizeof(uint64));
*((double *)dat->m_sValue) = buffer.GetDouble();
}