OpenBarnyard
 
Loading...
Searching...
No Matches
T2DynamicVector.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "T2DynamicVector.h"
3
4//-----------------------------------------------------------------------------
5// Enables memory debugging.
6// Note: Should be the last include!
7//-----------------------------------------------------------------------------
9
11
12void T2GenericDynamicVector::Reallocate( TINT a_iNewSize, TINT a_iElementSize )
13{
14 if ( a_iNewSize != m_iAllocSize )
15 {
16 TASSERT( m_pAllocator != TNULL, "Cannot reallocate a T2DynamicVector with no allocator specified!" );
17
18 void* elements = TNULL;
19
20 if ( 0 < a_iNewSize )
21 {
22 elements = m_pAllocator->Malloc( a_iNewSize * a_iElementSize );
23 TUtil::MemCopy( elements, m_poElements, m_iNumElements <= a_iNewSize ? m_iNumElements * a_iElementSize : a_iNewSize * a_iElementSize );
24 }
25
26 m_iAllocSize = a_iNewSize;
28
29 if ( m_poElements )
30 {
32 }
33
34 m_poElements = elements;
35 }
36}
37
38void T2GenericDynamicVector::Grow( TINT a_iNumElements, TINT a_iElementSize )
39{
40 TINT newSize = m_iNumElements + a_iNumElements;
41 TINT curSize = m_iAllocSize;
42
43 if ( curSize < newSize )
44 {
45 TASSERT( m_iGrowSize != 0 );
46
47 while ( curSize < newSize )
48 {
49 if ( m_iGrowSize == -1 )
50 {
51 curSize = m_iAllocSize * 2 < 3 ? 2 : m_iAllocSize * 2;
52 }
53 else
54 {
55 curSize += m_iGrowSize;
56 }
57 }
58
59 Reallocate( curSize, a_iElementSize );
60 }
61}
62
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
Dynamic vector container for the Toshi engine.
TFORCEINLINE const T & Min(const T &a, const T &b)
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90
void Grow(TINT a_iNumElements, TINT a_iElementSize)
void Reallocate(TINT a_iNewSize, TINT a_iElementSize)