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;
}