Use engine's internal memalloc singleton for KeyValues methods

This commit is contained in:
Kawe Mazidjatari 2022-08-08 00:32:25 +02:00
parent 0c583d7522
commit 8328da706a

View File

@ -6,6 +6,7 @@
//=============================================================================// //=============================================================================//
#include "core/stdafx.h" #include "core/stdafx.h"
#include "tier0/memstd.h"
#include "tier1/strtools.h" #include "tier1/strtools.h"
#include "vpc/keyvalues.h" #include "vpc/keyvalues.h"
#include "vpc/kvleaktrace.h" #include "vpc/kvleaktrace.h"
@ -143,7 +144,7 @@ void KeyValues::Init(void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void KeyValues::Clear(void) void KeyValues::Clear(void)
{ {
delete m_pSub; MemAllocSingleton()->Free(m_pSub);
m_pSub = nullptr; m_pSub = nullptr;
m_iDataType = TYPE_NONE; m_iDataType = TYPE_NONE;
} }
@ -154,7 +155,7 @@ void KeyValues::Clear(void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void KeyValues::DeleteThis(void) void KeyValues::DeleteThis(void)
{ {
delete this; MemAllocSingleton()->Free(this);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -168,19 +169,19 @@ void KeyValues::RemoveEverything(void)
{ {
datNext = dat->m_pPeer; datNext = dat->m_pPeer;
dat->m_pPeer = nullptr; dat->m_pPeer = nullptr;
delete dat; MemAllocSingleton()->Free(dat);
} }
for (dat = m_pPeer; dat && dat != this; dat = datNext) for (dat = m_pPeer; dat && dat != this; dat = datNext)
{ {
datNext = dat->m_pPeer; datNext = dat->m_pPeer;
dat->m_pPeer = nullptr; dat->m_pPeer = nullptr;
delete dat; MemAllocSingleton()->Free(dat);
} }
delete[] m_sValue; MemAllocSingleton()->Free(m_sValue);
m_sValue = nullptr; m_sValue = nullptr;
delete[] m_wsValue; MemAllocSingleton()->Free(m_wsValue);
m_wsValue = nullptr; m_wsValue = nullptr;
} }
@ -383,7 +384,7 @@ void KeyValues::ElideSubKey(KeyValues* pSubKey)
{ {
// No children, simply remove the key // No children, simply remove the key
*ppPointerToFix = pSubKey->m_pPeer; *ppPointerToFix = pSubKey->m_pPeer;
delete pSubKey; MemAllocSingleton()->Free(pSubKey);
} }
else else
{ {
@ -399,7 +400,7 @@ void KeyValues::ElideSubKey(KeyValues* pSubKey)
// Detach the node to be elided // Detach the node to be elided
pSubKey->m_pSub = nullptr; pSubKey->m_pSub = nullptr;
pSubKey->m_pPeer = nullptr; pSubKey->m_pPeer = nullptr;
delete pSubKey; MemAllocSingleton()->Free(pSubKey);
} }
return; return;
} }
@ -749,7 +750,7 @@ const wchar_t* KeyValues::GetWString(const char* pszKeyName, const wchar_t* pwsz
case TYPE_STRING: case TYPE_STRING:
{ {
size_t bufSize = strlen(pKey->m_sValue) + 1; size_t bufSize = strlen(pKey->m_sValue) + 1;
wchar_t* pWBuf = new wchar_t[bufSize]; wchar_t* pWBuf = MemAllocSingleton()->Alloc<wchar_t>(bufSize);
int result = V_UTF8ToUnicode(pKey->m_sValue, pWBuf, static_cast<int>(bufSize * sizeof(wchar_t))); int result = V_UTF8ToUnicode(pKey->m_sValue, pWBuf, static_cast<int>(bufSize * sizeof(wchar_t)));
if (result >= 0) // may be a zero length string if (result >= 0) // may be a zero length string
{ {
@ -757,10 +758,10 @@ const wchar_t* KeyValues::GetWString(const char* pszKeyName, const wchar_t* pwsz
} }
else else
{ {
delete[] pWBuf; MemAllocSingleton()->Free(pWBuf);
return pwszDefaultValue; return pwszDefaultValue;
} }
delete[] pWBuf; MemAllocSingleton()->Free(pWBuf);
break; break;
} }
default: default:
@ -859,12 +860,12 @@ void KeyValues::SetUint64(const char* pszKeyName, uint64_t nValue)
if (pKey) if (pKey)
{ {
// delete the old value // delete the old value
delete[] pKey->m_sValue; MemAllocSingleton()->Free(pKey->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[] pKey->m_wsValue; MemAllocSingleton()->Free(pKey->m_wsValue);
pKey->m_wsValue = nullptr; pKey->m_wsValue = nullptr;
pKey->m_sValue = new char[sizeof(uint64_t)]; pKey->m_sValue = MemAllocSingleton()->Alloc<char>(sizeof(uint64_t));
*(reinterpret_cast<uint64_t*>(pKey->m_sValue)) = nValue; *(reinterpret_cast<uint64_t*>(pKey->m_sValue)) = nValue;
pKey->m_iDataType = TYPE_UINT64; pKey->m_iDataType = TYPE_UINT64;
} }
@ -921,9 +922,9 @@ void KeyValues::SetPtr(const char* pszKeyName, void* pValue)
void KeyValues::SetStringValue(char const* pszValue) void KeyValues::SetStringValue(char const* pszValue)
{ {
// delete the old value // delete the old value
delete[] m_sValue; MemAllocSingleton()->Free(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; MemAllocSingleton()->Free(m_wsValue);
m_wsValue = nullptr; m_wsValue = nullptr;
if (!pszValue) if (!pszValue)
@ -934,7 +935,7 @@ void KeyValues::SetStringValue(char const* pszValue)
// allocate memory for the new value and copy it in // allocate memory for the new value and copy it in
size_t len = strlen(pszValue); size_t len = strlen(pszValue);
m_sValue = new char[len + 1]; m_sValue = MemAllocSingleton()->Alloc<char>(len + 1);
memcpy(m_sValue, pszValue, len + 1); memcpy(m_sValue, pszValue, len + 1);
m_iDataType = TYPE_STRING; m_iDataType = TYPE_STRING;
@ -973,9 +974,9 @@ void KeyValues::SetWString(const char* pszKeyName, const wchar_t* pwszValue)
if (pKey) if (pKey)
{ {
// delete the old value // delete the old value
delete[] pKey->m_wsValue; MemAllocSingleton()->Free(pKey->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[] pKey->m_sValue; MemAllocSingleton()->Free(pKey->m_sValue);
pKey->m_sValue = nullptr; pKey->m_sValue = nullptr;
if (!pwszValue) if (!pwszValue)
@ -986,7 +987,7 @@ void KeyValues::SetWString(const char* pszKeyName, const wchar_t* pwszValue)
// allocate memory for the new value and copy it in // allocate memory for the new value and copy it in
size_t len = wcslen(pwszValue); size_t len = wcslen(pwszValue);
pKey->m_wsValue = new wchar_t[len + 1]; pKey->m_wsValue = MemAllocSingleton()->Alloc<wchar_t>(len + 1);
memcpy(pKey->m_wsValue, pwszValue, (len + 1) * sizeof(wchar_t)); memcpy(pKey->m_wsValue, pwszValue, (len + 1) * sizeof(wchar_t));
pKey->m_iDataType = TYPE_WSTRING; pKey->m_iDataType = TYPE_WSTRING;
@ -1036,7 +1037,7 @@ void KeyValues::RecursiveCopyKeyValues(KeyValues& src)
if (src.m_sValue) if (src.m_sValue)
{ {
size_t len = strlen(src.m_sValue) + 1; size_t len = strlen(src.m_sValue) + 1;
m_sValue = new char[len]; m_sValue = MemAllocSingleton()->Alloc<char>(len);
strncpy(m_sValue, src.m_sValue, len); strncpy(m_sValue, src.m_sValue, len);
} }
break; break;
@ -1045,7 +1046,7 @@ void KeyValues::RecursiveCopyKeyValues(KeyValues& src)
m_iValue = src.m_iValue; m_iValue = src.m_iValue;
snprintf(buf, sizeof(buf), "%d", m_iValue); snprintf(buf, sizeof(buf), "%d", m_iValue);
size_t len = strlen(buf) + 1; size_t len = strlen(buf) + 1;
m_sValue = new char[len]; m_sValue = MemAllocSingleton()->Alloc<char>(len);
strncpy(m_sValue, buf, len); strncpy(m_sValue, buf, len);
} }
break; break;
@ -1054,7 +1055,7 @@ void KeyValues::RecursiveCopyKeyValues(KeyValues& src)
m_flValue = src.m_flValue; m_flValue = src.m_flValue;
snprintf(buf, sizeof(buf), "%f", m_flValue); snprintf(buf, sizeof(buf), "%f", m_flValue);
size_t len = strlen(buf) + 1; size_t len = strlen(buf) + 1;
m_sValue = new char[len]; m_sValue = MemAllocSingleton()->Alloc<char>(len);
strncpy(m_sValue, buf, len); strncpy(m_sValue, buf, len);
} }
break; break;
@ -1065,7 +1066,7 @@ void KeyValues::RecursiveCopyKeyValues(KeyValues& src)
break; break;
case TYPE_UINT64: case TYPE_UINT64:
{ {
m_sValue = new char[sizeof(uint64_t)]; m_sValue = MemAllocSingleton()->Alloc<char>(sizeof(uint64_t));
memcpy(m_sValue, src.m_sValue, sizeof(uint64_t)); memcpy(m_sValue, src.m_sValue, sizeof(uint64_t));
} }
break; break;
@ -1087,37 +1088,28 @@ void KeyValues::RecursiveCopyKeyValues(KeyValues& src)
} }
} }
#if 0
KeyValues* pDst = this;
for (KeyValues* pSrc = src.m_pSub; pSrc; pSrc = pSrc->m_pPeer)
{
if (pSrc->m_pSub)
{
pDst->m_pSub = new KeyValues(pSrc->m_pSub->getName());
pDst->m_pSub->RecursiveCopyKeyValues(*pSrc->m_pSub);
}
else
{
// copy non-empty keys
if (pSrc->m_sValue && *(pSrc->m_sValue))
{
pDst->m_pPeer = new KeyValues(
}
}
}
#endif
// Handle the immediate child // Handle the immediate child
if (src.m_pSub) if (src.m_pSub)
{ {
m_pSub = new KeyValues(nullptr); m_pSub = MemAllocSingleton()->Alloc<KeyValues>(sizeof(KeyValues));
TRACK_KV_ADD(m_pSub, nullptr);
m_pSub->Init();
m_pSub->SetName(nullptr);
m_pSub->RecursiveCopyKeyValues(*src.m_pSub); m_pSub->RecursiveCopyKeyValues(*src.m_pSub);
} }
// Handle the immediate peer // Handle the immediate peer
if (src.m_pPeer) if (src.m_pPeer)
{ {
m_pPeer = new KeyValues(nullptr); m_pPeer = MemAllocSingleton()->Alloc<KeyValues>(sizeof(KeyValues));
TRACK_KV_ADD(m_pPeer, nullptr);
m_pPeer->Init();
m_pPeer->SetName(nullptr);
m_pPeer->RecursiveCopyKeyValues(*src.m_pPeer); m_pPeer->RecursiveCopyKeyValues(*src.m_pPeer);
} }
} }
@ -1156,7 +1148,12 @@ void KeyValues::CopySubkeys(KeyValues* pParent) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
KeyValues* KeyValues::MakeCopy(void) const KeyValues* KeyValues::MakeCopy(void) const
{ {
KeyValues* pNewKeyValue = new KeyValues(GetName()); KeyValues* pNewKeyValue = MemAllocSingleton()->Alloc<KeyValues>(sizeof(KeyValues));
TRACK_KV_ADD(pNewKeyValue, GetName());
pNewKeyValue->Init();
pNewKeyValue->SetName(GetName());
// copy data // copy data
pNewKeyValue->m_iDataType = m_iDataType; pNewKeyValue->m_iDataType = m_iDataType;
@ -1168,7 +1165,7 @@ KeyValues* KeyValues::MakeCopy(void) const
{ {
size_t len = strlen(m_sValue); size_t len = strlen(m_sValue);
Assert(!pNewKeyValue->m_sValue); Assert(!pNewKeyValue->m_sValue);
pNewKeyValue->m_sValue = new char[len + 1]; pNewKeyValue->m_sValue = MemAllocSingleton()->Alloc<char>(len + 1);
memcpy(pNewKeyValue->m_sValue, m_sValue, len + 1); memcpy(pNewKeyValue->m_sValue, m_sValue, len + 1);
} }
} }
@ -1178,7 +1175,7 @@ KeyValues* KeyValues::MakeCopy(void) const
if (m_wsValue) if (m_wsValue)
{ {
size_t len = wcslen(m_wsValue); size_t len = wcslen(m_wsValue);
pNewKeyValue->m_wsValue = new wchar_t[len + 1]; pNewKeyValue->m_wsValue = MemAllocSingleton()->Alloc<wchar_t>(len + 1);
memcpy(pNewKeyValue->m_wsValue, m_wsValue, len + 1 * sizeof(wchar_t)); memcpy(pNewKeyValue->m_wsValue, m_wsValue, len + 1 * sizeof(wchar_t));
} }
} }
@ -1204,7 +1201,7 @@ KeyValues* KeyValues::MakeCopy(void) const
break; break;
case TYPE_UINT64: case TYPE_UINT64:
pNewKeyValue->m_sValue = new char[sizeof(uint64_t)]; pNewKeyValue->m_sValue = MemAllocSingleton()->Alloc<char>(sizeof(uint64_t));
memcpy(pNewKeyValue->m_sValue, m_sValue, sizeof(uint64_t)); memcpy(pNewKeyValue->m_sValue, m_sValue, sizeof(uint64_t));
break; break;
}; };