1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-09-19 12:06:07 +08:00

Separate common method from kv3 headers

This commit is contained in:
GAMMACASE
2025-06-05 16:34:12 +03:00
parent e2731b48a1
commit 10aaf6c8ec
3 changed files with 27 additions and 21 deletions

View File

@ -169,5 +169,28 @@ T ClampedArrayElement( const T (&buffer)[N], unsigned int uIndex )
return buffer[ 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 #endif // COMMONMACROS_H

View File

@ -7,6 +7,7 @@
#include "tier0/platform.h" #include "tier0/platform.h"
#include "tier0/dbg.h" #include "tier0/dbg.h"
#include "tier0/commonmacros.h"
#include "tier1/bufferstring.h" #include "tier1/bufferstring.h"
#include "tier1/generichash.h" #include "tier1/generichash.h"
#include "tier1/strtools.h" #include "tier1/strtools.h"
@ -299,24 +300,6 @@ namespace KV3Helpers
{ {
return ((ALIGN_VALUE( size * sizeof( Ts ), ALIGN )) + ... + 0); 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 struct KV3MetaData_t
@ -1569,7 +1552,7 @@ inline void CKeyValues3ContextBase::NodeList<NODE>::EnsureByteSize( int bytes_ne
if(bytes_needed < m_nAllocatedBytes) if(bytes_needed < m_nAllocatedBytes)
return; 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_pData = (ListEntry *)realloc( m_pData, new_alloc_size );
m_nAllocatedBytes = new_alloc_size; m_nAllocatedBytes = new_alloc_size;

View File

@ -1384,7 +1384,7 @@ void CKeyValues3Array::EnsureElementCapacity( int count, bool force, bool dont_m
DebuggerBreak(); 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 ); const int new_byte_size = TotalSizeOfData( new_count );
Element_t *new_base = nullptr; Element_t *new_base = nullptr;
@ -1591,7 +1591,7 @@ void CKeyValues3Table::EnsureMemberCapacity( int count, bool force, bool dont_mo
DebuggerBreak(); 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 ); const int new_byte_size = TotalSizeOfData( new_count );
void *new_base = nullptr; void *new_base = nullptr;