From 10aaf6c8ec428be25fe85b2f0bfdb6ded80449ff Mon Sep 17 00:00:00 2001 From: GAMMACASE <31375974+GAMMACASE@users.noreply.github.com> Date: Thu, 5 Jun 2025 16:34:12 +0300 Subject: [PATCH] Separate common method from kv3 headers --- public/tier0/commonmacros.h | 23 +++++++++++++++++++++++ public/tier1/keyvalues3.h | 21 ++------------------- tier1/keyvalues3.cpp | 4 ++-- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/public/tier0/commonmacros.h b/public/tier0/commonmacros.h index 8701e711..fd1fcc6f 100644 --- a/public/tier0/commonmacros.h +++ b/public/tier0/commonmacros.h @@ -169,5 +169,28 @@ T ClampedArrayElement( const T (&buffer)[N], unsigned int uIndex ) return buffer[ uIndex ]; } +// Would double old_count until it's greater or equal the requested_count and is clamped to min/max bounds. +inline int CalcNewDoublingCount( int old_count, int requested_count, int min_count, int max_count ) +{ + int new_count = old_count; + + while(new_count < requested_count) + { + if(new_count < max_count / 2) + { + new_count *= 2; + if(new_count < min_count) + new_count = min_count; + } + else + { + new_count = max_count; + break; + } + } + + return new_count; +} + #endif // COMMONMACROS_H diff --git a/public/tier1/keyvalues3.h b/public/tier1/keyvalues3.h index 443a9827..42d49b64 100644 --- a/public/tier1/keyvalues3.h +++ b/public/tier1/keyvalues3.h @@ -7,6 +7,7 @@ #include "tier0/platform.h" #include "tier0/dbg.h" +#include "tier0/commonmacros.h" #include "tier1/bufferstring.h" #include "tier1/generichash.h" #include "tier1/strtools.h" @@ -299,24 +300,6 @@ namespace KV3Helpers { return ((ALIGN_VALUE( size * sizeof( Ts ), ALIGN )) + ... + 0); } - - inline int CalcNewBufferSize( int old_size, int requested_size, int min_size, int max_size ) - { - int new_size = MAX( old_size, min_size ); - - while(new_size < requested_size) - { - if(new_size < max_size / 2) - new_size *= 2; - else - { - new_size = max_size; - break; - } - } - - return new_size; - } } struct KV3MetaData_t @@ -1569,7 +1552,7 @@ inline void CKeyValues3ContextBase::NodeList::EnsureByteSize( int bytes_ne if(bytes_needed < m_nAllocatedBytes) return; - int new_alloc_size = KV3Helpers::CalcNewBufferSize( m_nAllocatedBytes, bytes_needed, ALLOC_CONTEXT_NODELIST_MIN, ALLOC_CONTEXT_NODELIST_MAX ); + int new_alloc_size = CalcNewDoublingCount( m_nAllocatedBytes, bytes_needed, ALLOC_CONTEXT_NODELIST_MIN, ALLOC_CONTEXT_NODELIST_MAX ); m_pData = (ListEntry *)realloc( m_pData, new_alloc_size ); m_nAllocatedBytes = new_alloc_size; diff --git a/tier1/keyvalues3.cpp b/tier1/keyvalues3.cpp index 49788cf2..7fe4eb8a 100644 --- a/tier1/keyvalues3.cpp +++ b/tier1/keyvalues3.cpp @@ -1384,7 +1384,7 @@ void CKeyValues3Array::EnsureElementCapacity( int count, bool force, bool dont_m DebuggerBreak(); } - const int new_count = force ? count : KV3Helpers::CalcNewBufferSize( m_nAllocatedChunks, count, ALLOC_KV3ARRAY_MIN, ALLOC_KV3ARRAY_MAX ); + const int new_count = force ? count : CalcNewDoublingCount( m_nAllocatedChunks, count, ALLOC_KV3ARRAY_MIN, ALLOC_KV3ARRAY_MAX ); const int new_byte_size = TotalSizeOfData( new_count ); Element_t *new_base = nullptr; @@ -1591,7 +1591,7 @@ void CKeyValues3Table::EnsureMemberCapacity( int count, bool force, bool dont_mo DebuggerBreak(); } - const int new_count = force ? count : KV3Helpers::CalcNewBufferSize( m_nAllocatedChunks, count, ALLOC_KV3TABLE_MIN, ALLOC_KV3TABLE_MAX ); + const int new_count = force ? count : CalcNewDoublingCount( m_nAllocatedChunks, count, ALLOC_KV3TABLE_MIN, ALLOC_KV3TABLE_MAX ); const int new_byte_size = TotalSizeOfData( new_count ); void *new_base = nullptr;