Skip to content

Commit

Permalink
!Broken!
Browse files Browse the repository at this point in the history
  • Loading branch information
AdventureT committed Dec 8, 2023
1 parent d7d2af4 commit 05873ad
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 8 deletions.
287 changes: 287 additions & 0 deletions Toshi/Include/TKernel/TArray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
#pragma once
#include <TKernel/TDebug.h>
#include "TMemory.h"
#include "TSystemTools.h"

TOSHI_NAMESPACE_BEGIN

// Borrowed from OpenBarnyard (T2GenericDynamicArray)

template <class T>
class TArray
{
public:
class Iterator
{
public:
friend TArray;
using Storage = TArray;

public:
Iterator()
{
m_iIndex = 0;
m_poArray = TNULL;
}

Iterator(const Iterator& other)
{
m_poArray = other.m_poArray;
m_iIndex = 0;
}

Iterator(Storage& a_poArray)
{
m_iIndex = 0;
m_poArray = &a_poArray;
}

Iterator(TINT a_iIndex, Storage& a_poArray)
{
m_iIndex = a_iIndex;
m_poArray = &a_poArray;
}

void SetCurrentIndex(TINT a_iIndex) const
{
TASSERT(m_poArray);
TASSERT(a_iIndex < m_poArray->GetNumElements());
m_iIndex = a_iIndex;
}

TINT GetCurrentIndex() const
{
return m_iIndex;
}

TINT& IncrementSafe()
{
m_iIndex++;
TASSERT(m_poArray);

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

return m_iIndex;
}

void Push(const T& element)
{
TASSERT(m_poArray);
m_poArray->Push(element);
}

T* operator->() const
{
TASSERT(m_iIndex >= 0);
TASSERT(m_poArray);
TASSERT(m_iIndex < m_poArray->m_iNumElements);
return &m_poArray->m_pData[m_iIndex];
}

T& Get()
{
TASSERT(m_iIndex >= 0);
TASSERT(m_poArray);
TASSERT(m_iIndex < m_poArray->m_iNumElements);
return m_poArray->m_pData[m_iIndex];
}

bool IsOver() const
{
return (m_iIndex >= m_poArray->GetNumElements() || m_iIndex == -1);
}

const T& Get() const
{
TASSERT(m_iIndex >= 0);
TASSERT(m_poArray);
TASSERT(m_iIndex < m_poArray->m_iNumElements);
return m_poArray->m_pData[m_iIndex];
}

Iterator& operator++()
{
m_iIndex++;
return *this;
}

Iterator operator++(TINT)
{
Iterator temp = *this;
m_iIndex++;
return temp;
}

Iterator& operator--()
{
m_iIndex--;
return *this;
}

Iterator operator--(TINT)
{
Iterator temp = *this;
m_iIndex--;
return temp;
}

Iterator operator+(TINT a_iValue) const
{
return Iterator(m_iIndex + a_iValue, m_poArray);
}

Iterator operator-(TINT a_iValue) const
{
return Iterator(m_iIndex - a_iValue, m_poArray);
}

private:
TINT m_iIndex; // 0x0
Storage* m_poArray; // 0x4
};

public:
TArray(TINT a_iGrowSize, TINT a_iSize)
{
m_iGrowSize = a_iGrowSize;
m_iNumAllocElements = a_iSize;
m_iNumElements = 0;

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

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

void Clear()
{
if (m_iNumAllocElements < 0)
{
if (m_pData)
{
tfree(m_pData);
m_pData = TNULL;
}

m_iNumAllocElements = 0;
}

m_iNumElements = 0;
}

Iterator Begin()
{
return Iterator((m_iNumElements == 0) ? -1 : 0, *this);
}

Iterator End()
{
return Iterator(-1, *this);
}

TINT GetNumElements() const
{
return m_iNumElements;
}

TINT GetNumAllocElements() const
{
return m_iNumAllocElements;
}

TINT GetGrowSize() const
{
return m_iGrowSize;
}

void SetGrowSize(TINT a_iGrowSize)
{
m_iGrowSize = a_iGrowSize;
}

T* Push(const T& element)
{
GrowBy(1);
return new (&m_pData[m_iNumElements++]) T(element);
}

T& Pop()
{
TASSERT(m_iNumElements >= 1);
return m_pData[--m_iNumElements];
}

T& operator[](TINT a_iIndex)
{
TASSERT(a_iIndex >= 0);
TASSERT(a_iIndex < m_iNumElements);
return m_pData[a_iIndex];
}

const T& operator[](TINT a_iIndex) const
{
TASSERT(a_iIndex >= 0);
TASSERT(a_iIndex < m_iNumElements);
return m_pData[a_iIndex];
}

private:
void GrowBy(TINT 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);
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);

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

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

if (m_pData) TFree(m_pData);
m_pData = pNewBuffer;
}
else
{
if (m_pData) TFree(m_pData);
m_pData = TNULL;
m_iNumAllocElements = 0;
m_iNumElements = 0;
}
}

private:
TINT m_iGrowSize; // 0x0
TINT m_iNumElements; // 0x4
TINT m_iNumAllocElements; // 0x8
T* m_pData; // 0xC
};

TOSHI_NAMESPACE_END
34 changes: 27 additions & 7 deletions Toshi/Include/TKernel/TFreeList.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,44 @@ TOSHI_NAMESPACE_BEGIN
class TOSHI_EXPORT TFreeList
{
public:

struct Node
{
Node* pNext = TNULL;
friend class TFreeList;
public:
Node& operator=(const Node& a_rNode)
{
m_pNext = a_rNode.m_pNext;
return *this;
}
protected:
Node* m_pNext = TNULL;
};

TFreeList();
TFreeList(TUINT a_uiItemSize, TINT a_iInitialSize, TINT a_iGrowSize, TPCHAR a_pcName);
~TFreeList();

protected:

Node* Allocate(TINT a_iNumber, TUINT a_uiSize);

public:
TPVOID New(TUINT a_uiSize);
void Delete(TPVOID a_pData);

void SetCapacity(TINT a_iCapacity);
void SetGrowSize(TINT a_iGrowSize);

TINT GetCapacity() const { return m_iCapacity; }
TINT GetGrowSize() const { return m_iGrowSize; }
TINT GetFreeCount() { return m_iFreeCount; }
private:
TFreeList* m_pPrevList;
TINT m_iFreeCount; // 0x0
TUINT m_uiItemSize;
Node m_LastNode;
TINT m_iMaxFreeCount; // 0x4
TPCHAR m_pcName; // 0x8
TINT m_iCapacity; // 0xC
Node m_RootNode;
TUINT m_uiItemSize; // 0x10
Node m_oLastNode; // 0x14
Node m_oRootNode; // 0x18
TINT m_iGrowSize; // 0x1C
};

Expand Down
1 change: 1 addition & 0 deletions Toshi/Include/TKernel/TMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ inline static TMemory g_oMemManager;
TOSHI_NAMESPACE_END

TPVOID TOSHI_EXPORT __stdcall tmalloc(TINT a_iSize, TPCHAR a_pBuffer, TINT a_iUnk);
TPVOID TOSHI_EXPORT __stdcall tmemalign(TINT a_iAlign, TINT a_iSize);
void TOSHI_EXPORT __stdcall tfree(TPVOID a_pMem);
11 changes: 11 additions & 0 deletions Toshi/Include/TKernel/TPCString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "TDebug.h"

TOSHI_NAMESPACE_BEGIN

class TOSHI_EXPORT TPCString
{

};

TOSHI_NAMESPACE_END
Loading

0 comments on commit 05873ad

Please sign in to comment.