mirror of
https://github.com/rehlds/rehlds.git
synced 2025-01-16 08:38:10 +03:00
Fix for range loop in CUtlVector (#697)
* Fix for range loop * Reallocate memory only if needed
This commit is contained in:
parent
3f809d018a
commit
0c8d3d76ac
@ -262,6 +262,7 @@ void CUtlMemory<T, I>::Grow(int num)
|
|||||||
// Make sure we have at least numallocated + num allocations.
|
// Make sure we have at least numallocated + num allocations.
|
||||||
// Use the grow rules specified for this memory (in m_nGrowSize)
|
// Use the grow rules specified for this memory (in m_nGrowSize)
|
||||||
int nAllocationRequested = m_nAllocationCount + num;
|
int nAllocationRequested = m_nAllocationCount + num;
|
||||||
|
bool needToReallocate = false;
|
||||||
while (m_nAllocationCount < nAllocationRequested)
|
while (m_nAllocationCount < nAllocationRequested)
|
||||||
{
|
{
|
||||||
if (m_nAllocationCount != 0)
|
if (m_nAllocationCount != 0)
|
||||||
@ -274,6 +275,7 @@ void CUtlMemory<T, I>::Grow(int num)
|
|||||||
{
|
{
|
||||||
m_nAllocationCount += m_nAllocationCount;
|
m_nAllocationCount += m_nAllocationCount;
|
||||||
}
|
}
|
||||||
|
needToReallocate = true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -283,11 +285,11 @@ void CUtlMemory<T, I>::Grow(int num)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_pMemory)
|
if (m_pMemory && needToReallocate)
|
||||||
{
|
{
|
||||||
m_pMemory = (T *)realloc(m_pMemory, m_nAllocationCount * sizeof(T));
|
m_pMemory = (T *)realloc(m_pMemory, m_nAllocationCount * sizeof(T));
|
||||||
}
|
}
|
||||||
else
|
else if (!m_pMemory)
|
||||||
{
|
{
|
||||||
m_pMemory = (T *)malloc(m_nAllocationCount * sizeof(T));
|
m_pMemory = (T *)malloc(m_nAllocationCount * sizeof(T));
|
||||||
}
|
}
|
||||||
|
@ -43,10 +43,10 @@ public:
|
|||||||
|
|
||||||
// features C++11 ranged based for
|
// features C++11 ranged based for
|
||||||
T *begin() { return &m_Memory[0]; }
|
T *begin() { return &m_Memory[0]; }
|
||||||
T *end() { return &m_Memory[m_Size - 1]; }
|
T *end() { return &m_Memory[m_Size]; }
|
||||||
|
|
||||||
T const *begin() const { return &m_Memory[0]; }
|
T const *begin() const { return &m_Memory[0]; }
|
||||||
T const *end() const { return &m_Memory[m_Size - 1]; }
|
T const *end() const { return &m_Memory[m_Size]; }
|
||||||
|
|
||||||
// Copy the array.
|
// Copy the array.
|
||||||
CUtlVector<T> &operator=(const CUtlVector<T> &other);
|
CUtlVector<T> &operator=(const CUtlVector<T> &other);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user