mirror of
https://github.com/alliedmodders/hl2sdk.git
synced 2024-12-23 01:59:43 +08:00
Force KeyValues to use g_pMemAlloc where applicable for string alloc.
This commit is contained in:
parent
b4418b3409
commit
ac0eb9cebe
Binary file not shown.
@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include "tier0/mem.h"
|
#include "tier0/mem.h"
|
||||||
|
#include <tier0/commonmacros.h>
|
||||||
|
|
||||||
struct _CrtMemState;
|
struct _CrtMemState;
|
||||||
|
|
||||||
@ -113,6 +114,24 @@ MEM_INTERFACE IMemAlloc *g_pMemAlloc;
|
|||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
inline void *MemAlloc_Alloc( size_t nSize )
|
||||||
|
{
|
||||||
|
return g_pMemAlloc->Alloc( nSize );
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void *MemAlloc_Alloc( size_t nSize, const char *pFileName, int nLine )
|
||||||
|
{
|
||||||
|
return g_pMemAlloc->Alloc( nSize, pFileName, nLine );
|
||||||
|
}
|
||||||
|
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 );
|
||||||
|
}
|
||||||
|
|
||||||
inline void *MemAlloc_AllocAligned( size_t size, size_t align )
|
inline void *MemAlloc_AllocAligned( size_t size, size_t align )
|
||||||
{
|
{
|
||||||
unsigned char *pAlloc, *pResult;
|
unsigned char *pAlloc, *pResult;
|
||||||
|
@ -27,6 +27,17 @@
|
|||||||
// memdbgon must be the last include file in a .cpp file!!!
|
// memdbgon must be the last include file in a .cpp file!!!
|
||||||
#include <tier0/memdbgon.h>
|
#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
|
static const char * s_LastFileLoadingFrom = "unknown"; // just needed for error messages
|
||||||
|
|
||||||
#define KEYVALUES_TOKEN_SIZE 1024
|
#define KEYVALUES_TOKEN_SIZE 1024
|
||||||
@ -323,9 +334,9 @@ void KeyValues::RemoveEverything()
|
|||||||
delete dat;
|
delete dat;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete [] m_sValue;
|
KVStringDelete(m_sValue);
|
||||||
m_sValue = NULL;
|
m_sValue = NULL;
|
||||||
delete [] m_wsValue;
|
KVStringDelete(m_wsValue);
|
||||||
m_wsValue = NULL;
|
m_wsValue = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1280,9 +1291,9 @@ void KeyValues::SetColor( const char *keyName, Color value)
|
|||||||
void KeyValues::SetStringValue( char const *strValue )
|
void KeyValues::SetStringValue( char const *strValue )
|
||||||
{
|
{
|
||||||
// delete the old value
|
// 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
|
// make sure we're not storing the WSTRING - as we're converting over to STRING
|
||||||
delete [] m_wsValue;
|
KVStringDelete(m_wsValue);
|
||||||
m_wsValue = NULL;
|
m_wsValue = NULL;
|
||||||
|
|
||||||
if (!strValue)
|
if (!strValue)
|
||||||
@ -1293,7 +1304,7 @@ void KeyValues::SetStringValue( char const *strValue )
|
|||||||
|
|
||||||
// allocate memory for the new value and copy it in
|
// allocate memory for the new value and copy it in
|
||||||
int len = Q_strlen( strValue );
|
int len = Q_strlen( strValue );
|
||||||
m_sValue = new char[len + 1];
|
m_sValue = KVStringAlloc<char>(len + 1);
|
||||||
Q_memcpy( m_sValue, strValue, len+1 );
|
Q_memcpy( m_sValue, strValue, len+1 );
|
||||||
|
|
||||||
m_iDataType = TYPE_STRING;
|
m_iDataType = TYPE_STRING;
|
||||||
@ -1309,9 +1320,9 @@ void KeyValues::SetString( const char *keyName, const char *value )
|
|||||||
if ( dat )
|
if ( dat )
|
||||||
{
|
{
|
||||||
// delete the old 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
|
// 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_wsValue = NULL;
|
||||||
|
|
||||||
if (!value)
|
if (!value)
|
||||||
@ -1322,7 +1333,7 @@ void KeyValues::SetString( const char *keyName, const char *value )
|
|||||||
|
|
||||||
// allocate memory for the new value and copy it in
|
// allocate memory for the new value and copy it in
|
||||||
int len = Q_strlen( value );
|
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 );
|
Q_memcpy( dat->m_sValue, value, len+1 );
|
||||||
|
|
||||||
dat->m_iDataType = TYPE_STRING;
|
dat->m_iDataType = TYPE_STRING;
|
||||||
@ -1338,9 +1349,9 @@ void KeyValues::SetWString( const char *keyName, const wchar_t *value )
|
|||||||
if ( dat )
|
if ( dat )
|
||||||
{
|
{
|
||||||
// delete the old value
|
// 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
|
// 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;
|
dat->m_sValue = NULL;
|
||||||
|
|
||||||
if (!value)
|
if (!value)
|
||||||
@ -1351,7 +1362,7 @@ void KeyValues::SetWString( const char *keyName, const wchar_t *value )
|
|||||||
|
|
||||||
// allocate memory for the new value and copy it in
|
// allocate memory for the new value and copy it in
|
||||||
int len = wcslen( value );
|
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) );
|
Q_memcpy( dat->m_wsValue, value, (len+1) * sizeof(wchar_t) );
|
||||||
|
|
||||||
dat->m_iDataType = TYPE_WSTRING;
|
dat->m_iDataType = TYPE_WSTRING;
|
||||||
@ -1382,12 +1393,12 @@ void KeyValues::SetUint64( const char *keyName, uint64 value )
|
|||||||
if ( dat )
|
if ( dat )
|
||||||
{
|
{
|
||||||
// delete the old 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
|
// 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_wsValue = NULL;
|
||||||
|
|
||||||
dat->m_sValue = new char[sizeof(uint64)];
|
dat->m_sValue = KVStringAlloc<char>(sizeof(uint64));
|
||||||
*((uint64 *)dat->m_sValue) = value;
|
*((uint64 *)dat->m_sValue) = value;
|
||||||
dat->m_iDataType = TYPE_UINT64;
|
dat->m_iDataType = TYPE_UINT64;
|
||||||
}
|
}
|
||||||
@ -1444,7 +1455,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
|
|||||||
if( src.m_sValue )
|
if( src.m_sValue )
|
||||||
{
|
{
|
||||||
int len = Q_strlen(src.m_sValue) + 1;
|
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 );
|
Q_strncpy( m_sValue, src.m_sValue, len );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -1453,7 +1464,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
|
|||||||
m_iValue = src.m_iValue;
|
m_iValue = src.m_iValue;
|
||||||
Q_snprintf( buf,sizeof(buf), "%d", m_iValue );
|
Q_snprintf( buf,sizeof(buf), "%d", m_iValue );
|
||||||
int len = Q_strlen(buf) + 1;
|
int len = Q_strlen(buf) + 1;
|
||||||
m_sValue = new char[len];
|
m_sValue = KVStringAlloc<char>(len);
|
||||||
Q_strncpy( m_sValue, buf, len );
|
Q_strncpy( m_sValue, buf, len );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -1462,7 +1473,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
|
|||||||
m_flValue = src.m_flValue;
|
m_flValue = src.m_flValue;
|
||||||
Q_snprintf( buf,sizeof(buf), "%f", m_flValue );
|
Q_snprintf( buf,sizeof(buf), "%f", m_flValue );
|
||||||
int len = Q_strlen(buf) + 1;
|
int len = Q_strlen(buf) + 1;
|
||||||
m_sValue = new char[len];
|
m_sValue = KVStringAlloc<char>(len);
|
||||||
Q_strncpy( m_sValue, buf, len );
|
Q_strncpy( m_sValue, buf, len );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -1473,7 +1484,7 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
|
|||||||
break;
|
break;
|
||||||
case TYPE_UINT64:
|
case TYPE_UINT64:
|
||||||
{
|
{
|
||||||
m_sValue = new char[sizeof(uint64)];
|
m_sValue = KVStringAlloc<char>(sizeof(uint64));
|
||||||
Q_memcpy( m_sValue, src.m_sValue, sizeof(uint64) );
|
Q_memcpy( m_sValue, src.m_sValue, sizeof(uint64) );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -1584,7 +1595,7 @@ KeyValues *KeyValues::MakeCopy( void ) const
|
|||||||
{
|
{
|
||||||
int len = Q_strlen( m_sValue );
|
int len = Q_strlen( m_sValue );
|
||||||
Assert( !newKeyValue->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 );
|
Q_memcpy( newKeyValue->m_sValue, m_sValue, len+1 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1594,7 +1605,7 @@ KeyValues *KeyValues::MakeCopy( void ) const
|
|||||||
if ( m_wsValue )
|
if ( m_wsValue )
|
||||||
{
|
{
|
||||||
int len = wcslen( 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));
|
Q_memcpy( newKeyValue->m_wsValue, m_wsValue, (len+1)*sizeof(wchar_t));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1620,7 +1631,7 @@ KeyValues *KeyValues::MakeCopy( void ) const
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case TYPE_UINT64:
|
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) );
|
Q_memcpy( newKeyValue->m_sValue, m_sValue, sizeof(uint64) );
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
@ -2062,7 +2073,7 @@ void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &b
|
|||||||
|
|
||||||
if (dat->m_sValue)
|
if (dat->m_sValue)
|
||||||
{
|
{
|
||||||
delete[] dat->m_sValue;
|
KVStringDelete(dat->m_sValue);
|
||||||
dat->m_sValue = NULL;
|
dat->m_sValue = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2094,7 +2105,7 @@ void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &b
|
|||||||
digit -= 'A' - ( '9' + 1 );
|
digit -= 'A' - ( '9' + 1 );
|
||||||
retVal = ( retVal * 16 ) + ( digit - '0' );
|
retVal = ( retVal * 16 ) + ( digit - '0' );
|
||||||
}
|
}
|
||||||
dat->m_sValue = new char[sizeof(uint64)];
|
dat->m_sValue = KVStringAlloc<char>(sizeof(uint64));
|
||||||
*((uint64 *)dat->m_sValue) = retVal;
|
*((uint64 *)dat->m_sValue) = retVal;
|
||||||
dat->m_iDataType = TYPE_UINT64;
|
dat->m_iDataType = TYPE_UINT64;
|
||||||
}
|
}
|
||||||
@ -2116,7 +2127,7 @@ void KeyValues::RecursiveLoadFromBuffer( char const *resourceName, CUtlBuffer &b
|
|||||||
if (dat->m_iDataType == TYPE_STRING)
|
if (dat->m_iDataType == TYPE_STRING)
|
||||||
{
|
{
|
||||||
// copy in the string information
|
// 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 );
|
Q_memcpy( dat->m_sValue, value, len+1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2274,7 +2285,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer )
|
|||||||
token[KEYVALUES_TOKEN_SIZE-1] = 0;
|
token[KEYVALUES_TOKEN_SIZE-1] = 0;
|
||||||
|
|
||||||
int len = Q_strlen( token );
|
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 );
|
Q_memcpy( dat->m_sValue, token, len+1 );
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@ -2293,7 +2304,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer )
|
|||||||
|
|
||||||
case TYPE_UINT64:
|
case TYPE_UINT64:
|
||||||
{
|
{
|
||||||
dat->m_sValue = new char[sizeof(uint64)];
|
dat->m_sValue = KVStringAlloc<char>(sizeof(uint64));
|
||||||
*((double *)dat->m_sValue) = buffer.GetDouble();
|
*((double *)dat->m_sValue) = buffer.GetDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user