Build system almost done

This commit is contained in:
2025-06-02 19:56:18 +03:00
parent ade32c24a6
commit 3beb7aad3b
16 changed files with 191 additions and 77 deletions

View File

@@ -5,6 +5,7 @@
#include "tier0/mem.h"
#include "tier0/platform.h"
#include "tier0/lib.h"
#include "new"
template <typename T>
class CUtlBuffer;
@@ -31,8 +32,8 @@ public:
T operator []( const size_t nIndex ) const;
CUtlBuffer<T>& operator=(const CUtlBuffer<T>& other);
private:
T* m_pData;
size_t m_nSize;
T* m_pData = NULL;
size_t m_nSize = 0;
};
//-----------------------------------------------------------------------------
@@ -156,6 +157,7 @@ public:
CUtlResizableBuffer( size_t nSize );
CUtlResizableBuffer( const CUtlBuffer<T>& buffer );
CUtlResizableBuffer( const CUtlResizableBuffer<T>& buffer );
~CUtlResizableBuffer();
size_t GetSize() const;
size_t GetRealSize() const;
@@ -168,9 +170,9 @@ public:
CUtlResizableBuffer<T>& operator=(const CUtlResizableBuffer<T>& other);
private:
size_t CalculateMemorySize(size_t nSize);
T* m_pData;
size_t m_nSize;
size_t m_nAllocatedSize;
T* m_pData = NULL;
size_t m_nSize = 0;
size_t m_nAllocatedSize = 0;
};
@@ -179,24 +181,20 @@ private:
// Constructor.
//-----------------------------------------------------------------------------
template <typename T>
CUtlResizableBuffer<T>::CUtlResizableBuffer() : m_nSize(1)
CUtlResizableBuffer<T>::CUtlResizableBuffer()
{
m_pData = (T*)V_malloc(1);
m_nAllocatedSize = 1;
m_pData = NULL;
m_nSize = 0;
m_nAllocatedSize = 0;
}
//-----------------------------------------------------------------------------
// Constructor.
//-----------------------------------------------------------------------------
template <typename T>
CUtlResizableBuffer<T>::CUtlResizableBuffer( size_t nSize ) : m_nSize(nSize)
CUtlResizableBuffer<T>::CUtlResizableBuffer( size_t nSize )
{
if ( nSize == 0 )
nSize = 1;
size_t nAllocSize = CalculateMemorySize(sizeof(T)*nSize);
m_pData = (T*)V_malloc(nAllocSize);
m_nAllocatedSize = nAllocSize;
V_memset(m_pData, 0, sizeof(T)*nSize);
Resize(nSize);
}
//-----------------------------------------------------------------------------
@@ -205,10 +203,6 @@ CUtlResizableBuffer<T>::CUtlResizableBuffer( size_t nSize ) : m_nSize(nSize)
template <typename T>
CUtlResizableBuffer<T>::CUtlResizableBuffer( const CUtlBuffer<T>& buffer ) : m_nSize(buffer.nSize)
{
size_t nAllocSize = CalculateMemorySize(sizeof(T)*buffer.nSize);
m_pData = (T*)V_malloc(nAllocSize);
m_nAllocatedSize = nAllocSize;
V_memcpy(m_pData,buffer.m_pData,sizeof(T)*buffer.nSize);
}
//-----------------------------------------------------------------------------
@@ -217,11 +211,21 @@ CUtlResizableBuffer<T>::CUtlResizableBuffer( const CUtlBuffer<T>& buffer ) : m_n
template <typename T>
CUtlResizableBuffer<T>::CUtlResizableBuffer( const CUtlResizableBuffer<T>& buffer ) : m_nSize(buffer.m_nSize)
{
m_pData = (T*)V_malloc(sizeof(T)*buffer.m_nAllocatedSize);
m_nAllocatedSize = buffer.m_nAllocatedSize;
V_memcpy(m_pData,buffer.m_pData,sizeof(T)*buffer.m_nSize);
*this = buffer;
}
template <typename T>
CUtlResizableBuffer<T>::~CUtlResizableBuffer()
{
for ( size_t i = 0; i < m_nSize; i++)
m_pData[i].~T();
if (m_pData)
V_free(m_pData);
m_pData = 0;
m_nSize = 0;
m_nAllocatedSize = 0;
};
//-----------------------------------------------------------------------------
// Gets memory size.
//-----------------------------------------------------------------------------
@@ -246,25 +250,21 @@ size_t CUtlResizableBuffer<T>::GetRealSize( void ) const
template <typename T>
void CUtlResizableBuffer<T>::Resize( size_t nSize )
{
if ( nSize == 0 )
nSize = 1;
if ( m_pData == 0 )
m_pData = (T*)V_malloc(CalculateMemorySize(sizeof(T)*nSize));
else
if (nSize > m_nAllocatedSize)
{
size_t nAllocSize = CalculateMemorySize(sizeof(T)*nSize);
if (nAllocSize!=m_nAllocatedSize)
size_t nAllocationSize = CalculateMemorySize(nSize);
T *pData = (T*)V_malloc(nAllocationSize*sizeof(T));
for (size_t i = 0; i < m_nSize; i++)
{
// not ideal
T* pData = (T*)V_malloc(nAllocSize);
V_memcpy(pData, m_pData, m_nAllocatedSize>nAllocSize ? nAllocSize : m_nAllocatedSize );
m_nAllocatedSize = nAllocSize;
V_free(m_pData);
m_pData = pData;
new (&pData[i]) T(m_pData[i]);
m_pData[i].~T();
}
V_free(m_pData);
m_pData = pData;
m_nAllocatedSize = nAllocationSize;
}
for ( size_t i = m_nSize; i < nSize; ++i )
new (&m_pData[i]) T();
m_nSize = nSize;
}
@@ -320,11 +320,17 @@ CUtlResizableBuffer<T>& CUtlResizableBuffer<T>::operator=(const CUtlResizableBuf
{
if ( this != &other )
{
V_free(m_pData);
m_pData = (T*)V_malloc(other.m_nAllocatedSize);
V_memcpy(m_pData, other.m_pData, sizeof(T)*other.m_nSize);
if (m_pData)
{
for (size_t i = 0; i < m_nSize; i++)
m_pData[i].~T();
V_free(m_pData);
}
m_pData = (T*)V_malloc(other.m_nAllocatedSize*sizeof(T));
m_nAllocatedSize = other.m_nAllocatedSize;
m_nSize = other.m_nSize;
for ( size_t i = 0; i < other.m_nSize; i++)
new (&m_pData[i]) T(other.m_pData[i]);
}
return *this;
}
@@ -343,7 +349,9 @@ size_t CUtlResizableBuffer<T>::CalculateMemorySize(size_t nSize)
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
#if SIZE_MAX > UINT32_MAX
x |= x >> 32;
#endif
return x + 1;
}

View File

@@ -26,7 +26,7 @@ public:
bool operator==(CUtlString& string);
bool operator!=(CUtlString& string);
private:
CUtlVector<char> m_data;
CUtlVector<char> m_data = 0;
};
#endif

View File

@@ -75,7 +75,7 @@ private:
template<typename T>
CUtlVector<T>::CUtlVector( void )
{
m_data.Resize(0);
};
//----------------------------------------------------------------------------
@@ -85,6 +85,7 @@ template<typename T>
CUtlVector<T>::CUtlVector( size_t nSize )
{
m_data.Resize(nSize);
V_memset(m_data.GetMemory(),0,m_data.GetSize()*sizeof(T));
m_nSize = nSize;
};
@@ -95,20 +96,20 @@ template<typename T>
CUtlVector<T>::CUtlVector( CUtlInitializerList<T> initalizerList )
{
m_data.Resize(initalizerList.size());
V_memset(m_data.GetMemory(),0,m_data.GetSize()*sizeof(T));
m_nSize = m_data.GetSize();
V_memcpy(m_data.GetMemory(), initalizerList.begin(), m_data.GetSize()*sizeof(T));
for (size_t i = 0; i<m_nSize; i++)
m_data[i] = initalizerList.begin()[i];
}
template<typename T>
CUtlVector<T>::CUtlVector( const CUtlVector& vector )
{
m_data = vector.m_data;
*this = vector;
}
template<typename T>
CUtlVector<T>::~CUtlVector()
{
for ( uint32_t i = 0; i < m_nSize; i++ )
m_data[i].~T();
}
template<typename T>
@@ -133,7 +134,8 @@ template<typename T>
void CUtlVector<T>::AppendTail( const T *pData, size_t n )
{
m_data.Resize(m_data.GetSize()+n);
V_memcpy(m_data+m_nSize,pData,sizeof(T)*n);
for ( size_t i = 0; i < n; i++ )
m_data[i+m_nSize] = pData[i];
m_nSize+=n;
}
@@ -180,6 +182,7 @@ size_t CUtlVector<T>::GetSize( void )
template<typename T>
void CUtlVector<T>::Resize( size_t nSize )
{
m_data.Resize(nSize);
m_nSize = nSize;
}
template<typename T>
@@ -193,9 +196,7 @@ CUtlVector<T> &CUtlVector<T>::operator=(const CUtlVector<T> &vec)
if (this != &vec)
{
m_nSize = vec.m_nSize;
m_data.Resize(m_nSize);
for ( uint32_t i = 0; i < m_nSize; i++ )
m_data[i] = vec.m_data[i];
m_data = vec.m_data;
}
return *this;
}