Skip to content

Commit

Permalink
Fixed TArray
Browse files Browse the repository at this point in the history
  • Loading branch information
AdventureT committed Dec 8, 2023
1 parent 05873ad commit 57ef274
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
44 changes: 18 additions & 26 deletions Toshi/Include/TKernel/TArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <TKernel/TDebug.h>
#include "TMemory.h"
#include "TSystemTools.h"
#include "TMath.h"

TOSHI_NAMESPACE_BEGIN

Expand Down Expand Up @@ -59,8 +60,9 @@ class TArray
m_iIndex++;
TASSERT(m_poArray);

if (m_poArray->m_iNumElements <= m_iIndex || m_iIndex == 0)
if (m_poArray->m_iNumElements <= m_iIndex || m_iIndex == 0) {
m_iIndex = -1;
}

return m_iIndex;
}
Expand Down Expand Up @@ -148,31 +150,26 @@ class TArray
m_iNumAllocElements = a_iSize;
m_iNumElements = 0;

if (m_iNumAllocElements > 0)
{
m_pData = TSTATICCAST(T*, Tmemalign(alignof(T), m_iNumAllocElements * sizeof(T)));
if (m_iNumAllocElements > 0) {
m_pData = TSTATICCAST(T*, tmemalign(alignof(T), m_iNumAllocElements * sizeof(T)));
}
else
{
else {
TASSERT(m_iGrowSize != 0);
m_pData = TNULL;
}
}

~TArray()
{
if (m_pData)
{
if (m_pData) {
tfree(m_pData);
}
}

void Clear()
{
if (m_iNumAllocElements < 0)
{
if (m_pData)
{
if (m_iNumAllocElements < 0) {
if (m_pData) {
tfree(m_pData);
m_pData = TNULL;
}
Expand Down Expand Up @@ -242,35 +239,30 @@ class TArray
private:
void GrowBy(TINT a_iGrowBy)
{
if (m_iNumAllocElements < m_iNumElements + a_iGrowBy)
{
if (m_iNumAllocElements < m_iNumElements + a_iGrowBy) {
TASSERT(m_iGrowSize != 0);



auto iNewSize = m_iNumAllocElements + m_iGrowSize < m_iNumElements + a_iGrowBy ? TMath::Max(m_iNumAllocElements + m_iGrowSize, m_iNumElements + a_iGrowBy);
TINT iNewSize = TMAX(m_iNumAllocElements + m_iGrowSize, m_iNumElements + a_iGrowBy);
Resize(iNewSize);
}
}

void Resize(TINT a_iNewSize)
{
if (a_iNewSize != 0)
{
T* pNewBuffer = TSTATICCAST(T*, TMemalign(alignof(T), a_iNewSize * sizeof(T)));
size_t uiCopySize = TMath::Min(m_iNumElements, a_iNewSize);
if (a_iNewSize != 0) {
T* pNewBuffer = TSTATICCAST(T*, tmemalign(alignof(T), a_iNewSize * sizeof(T)));
size_t uiCopySize = TMIN(m_iNumElements, a_iNewSize);

TUtil::MemCopy(pNewBuffer, m_pData, sizeof(T) * uiCopySize);
TSystem::MemCopy(pNewBuffer, m_pData, sizeof(T) * uiCopySize);

m_iNumAllocElements = a_iNewSize;
TASSERT(m_iNumElements <= m_iNumAllocElements);

if (m_pData) TFree(m_pData);
if (m_pData) tfree(m_pData);
m_pData = pNewBuffer;
}
else
{
if (m_pData) TFree(m_pData);
else {
if (m_pData) tfree(m_pData);
m_pData = TNULL;
m_iNumAllocElements = 0;
m_iNumElements = 0;
Expand Down
4 changes: 4 additions & 0 deletions Toshi/Include/TKernel/TMath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define TMIN(x,y) (((a)<(b))?(a):(b))
#define TMAX(x,y) (((a)>(b))?(a):(b))

0 comments on commit 57ef274

Please sign in to comment.