OpenBarnyard
 
Loading...
Searching...
No Matches
PTRBSections::MemoryStream Class Reference

#include <PTRB.h>

Classes

class  Ptr
 
struct  RelcPtr
 

Public Member Functions

 MemoryStream (TUINT32 index, Endianess eEndianess)
 
 MemoryStream (const MemoryStream &other)
 
 ~MemoryStream ()
 
TUINT Tell ()
 
void Seek (TUINT newPos)
 
TUINT32 SetIndex (TUINT32 index)
 
void SetExpectedSize (TUINT32 expectedSize)
 
TUINT32 GetExpectedSize () const
 
TUINT32 GetIndex () const
 
TUINT GetPointerCount () const
 
TCHARGetBuffer () const
 
TCHARGetBufferPos () const
 
TUINT GetBufferSize () const
 
TUINT GetUsedSize () const
 
TUINT GetOffset (const void *ptr) const
 
template<class T>
void SetCrossPointer (T **outPtr, Ptr< T > ptr)
 
template<class T>
T * Write (TUINT offset, const T &value)
 
void Write (TUINT offset, TCHAR *value, TINT size)
 
template<class T>
Ptr< T > Alloc ()
 
template<class T>
Ptr< T > Alloc (TUINT count)
 
Ptr< TCHARAllocBytes (TUINT Size)
 
template<class T>
void WritePointer (T **outPtr, const T *ptr)
 
template<class T>
void WritePointer (T **outPtr, const Ptr< T > &ptr)
 
template<class T>
Ptr< T > Alloc (T **outPtr, TUINT count)
 
template<class T, TUINT Count = 1>
Ptr< T > Alloc (T **outPtr)
 
void Link ()
 
void Unlink ()
 
std::vector< RelcPtr >::iterator begin ()
 
std::vector< RelcPtr >::iterator end ()
 
void GrowBuffer (TUINT requiredSize)
 

Public Attributes

friend PTRBSections
 
friend PTRBSymbols
 
friend PTRBRelocations
 

Static Public Attributes

static constexpr TUINT BUFFER_GROW_SIZE = 4096
 

Detailed Description

Definition at line 56 of file PTRB.h.

Constructor & Destructor Documentation

◆ MemoryStream() [1/2]

PTRBSections::MemoryStream::MemoryStream ( TUINT32 index,
Endianess eEndianess )
inline

Definition at line 140 of file PTRB.h.

141 {
142 m_Index = index;
143 m_eEndianess = eEndianess;
144 m_Buffer = TNULL;
145 m_BufferPos = TNULL;
146 m_BufferSize = 0;
147 m_ExpectedSize = 0;
148 m_PtrList.reserve( 32 );
149 GrowBuffer( 0 );
150 }
#define TNULL
Definition Typedefs.h:23
void GrowBuffer(TUINT requiredSize)
Definition PTRB.h:973

◆ MemoryStream() [2/2]

PTRBSections::MemoryStream::MemoryStream ( const MemoryStream & other)
inline

Definition at line 152 of file PTRB.h.

153 {
154 TASSERT( other.m_DependentStacks.size() == 0, "Cross pointers are not supported" );
155
156 m_Index = other.m_Index;
157 m_eEndianess = other.m_eEndianess;
158 m_Buffer = TNULL;
159 m_BufferSize = 0;
160 m_ExpectedSize = other.m_ExpectedSize;
161 Resize( other.m_BufferSize );
162 m_BufferPos = m_Buffer + other.GetUsedSize();
163 Toshi::TUtil::MemCopy( m_Buffer, other.m_Buffer, other.GetUsedSize() );
164
165 m_PtrList.reserve( other.m_PtrList.size() );
166 for ( TUINT i = 0; i < other.m_PtrList.size(); i++ )
167 {
168 m_PtrList.emplace_back(
169 other.m_PtrList[ i ].Offset,
170 other.m_PtrList[ i ].DataPtr,
171 this // TODO: support cross pointers
172 );
173 }
174 }
#define TASSERT(X,...)
Definition Defines.h:138
unsigned int TUINT
Definition Typedefs.h:8

◆ ~MemoryStream()

PTRBSections::MemoryStream::~MemoryStream ( )
inline

Definition at line 176 of file PTRB.h.

177 {
178 if ( m_Buffer != TNULL )
179 {
180 delete[] m_Buffer;
181 }
182 }

Member Function Documentation

◆ Alloc() [1/4]

template<class T>
PTRBSections::MemoryStream::Ptr< T > PTRBSections::MemoryStream::Alloc ( )
inline

Definition at line 861 of file PTRB.h.

862{
863 m_BufferPos = TREINTERPRETCAST( TCHAR*, TAlignPointerUp( m_BufferPos ) );
864
865 constexpr TUINT TSize = sizeof( T );
866 GrowBuffer( GetUsedSize() + TSize );
867
868 T* allocated = TREINTERPRETCAST( T*, m_BufferPos );
869 m_BufferPos += TSize;
870
871 return { this, allocated };
872}
#define TREINTERPRETCAST(TYPE, VALUE)
Definition Defines.h:68
TFORCEINLINE constexpr T * TAlignPointerUp(T *a_pMem, TSIZE a_uiAlignment=sizeof(T *))
Definition Helpers.h:41
char TCHAR
Definition Typedefs.h:20
TUINT GetUsedSize() const
Definition PTRB.h:213

◆ Alloc() [2/4]

template<class T, TUINT Count = 1>
PTRBSections::MemoryStream::Ptr< T > PTRBSections::MemoryStream::Alloc ( T ** outPtr)
inline

Definition at line 908 of file PTRB.h.

909{
910 TASSERT( (TUINT)outPtr >= (TUINT)m_Buffer && (TUINT)outPtr < (TUINT)m_Buffer + (TUINT)m_BufferSize, "Out pointer is out of buffer" );
911 m_BufferPos = TREINTERPRETCAST( TCHAR*, TAlignPointerUp( m_BufferPos ) );
912
913 constexpr TUINT TSize = sizeof( T ) * Count;
914 TUINT outPtrOffset = GetOffset( outPtr );
915 GrowBuffer( GetUsedSize() + TSize );
916
917 T* allocated = TREINTERPRETCAST( T*, m_BufferPos );
918 m_BufferPos += TSize;
919
920 Write<T*>( outPtrOffset, allocated );
921 AddRelocationPtr( outPtrOffset, CONVERTENDIANESS( m_eEndianess, GetOffset( allocated ) ) );
922
923 return { this, allocated };
924}
T * Write(TUINT offset, const T &value)
Definition PTRB.h:225
TUINT GetOffset(const void *ptr) const
Definition PTRB.h:215

◆ Alloc() [3/4]

template<class T>
PTRBSections::MemoryStream::Ptr< T > PTRBSections::MemoryStream::Alloc ( T ** outPtr,
TUINT count )
inline

Definition at line 889 of file PTRB.h.

890{
891 TASSERT( (TUINT)outPtr >= (TUINT)m_Buffer && (TUINT)outPtr < (TUINT)m_Buffer + (TUINT)m_BufferSize, "Out pointer is out of buffer" );
892 m_BufferPos = TREINTERPRETCAST( TCHAR*, TAlignPointerUp( m_BufferPos ) );
893
894 const TUINT TSize = sizeof( T ) * count;
895 TUINT outPtrOffset = GetOffset( outPtr );
896 GrowBuffer( GetUsedSize() + TSize );
897
898 T* allocated = TREINTERPRETCAST( T*, m_BufferPos );
899 m_BufferPos += TSize;
900
901 Write<T*>( outPtrOffset, allocated );
902 AddRelocationPtr( outPtrOffset, CONVERTENDIANESS( m_eEndianess, GetOffset( allocated ) ) );
903
904 return { this, allocated };
905}

◆ Alloc() [4/4]

template<class T>
PTRBSections::MemoryStream::Ptr< T > PTRBSections::MemoryStream::Alloc ( TUINT count)
inline

Definition at line 875 of file PTRB.h.

876{
877 m_BufferPos = TREINTERPRETCAST( TCHAR*, TAlignPointerUp( m_BufferPos ) );
878
879 const TUINT TSize = sizeof( T ) * count;
880 GrowBuffer( GetUsedSize() + TSize );
881
882 T* allocated = TREINTERPRETCAST( T*, m_BufferPos );
883 m_BufferPos += TSize;
884
885 return { this, allocated };
886}

◆ AllocBytes()

PTRBSections::MemoryStream::Ptr< TCHAR > PTRBSections::MemoryStream::AllocBytes ( TUINT Size)
inline

Definition at line 947 of file PTRB.h.

948{
949 GrowBuffer( GetUsedSize() + Size );
950
951 TCHAR* allocated = reinterpret_cast<TCHAR*>( m_BufferPos );
952 m_BufferPos += Size;
953
954 return { this, allocated };
955}

◆ begin()

std::vector< RelcPtr >::iterator PTRBSections::MemoryStream::begin ( )
inline

Definition at line 261 of file PTRB.h.

261{ return m_PtrList.begin(); }

◆ end()

std::vector< RelcPtr >::iterator PTRBSections::MemoryStream::end ( )
inline

Definition at line 262 of file PTRB.h.

262{ return m_PtrList.end(); }

◆ GetBuffer()

TCHAR * PTRBSections::MemoryStream::GetBuffer ( ) const
inline

Definition at line 207 of file PTRB.h.

207{ return m_Buffer; }

◆ GetBufferPos()

TCHAR * PTRBSections::MemoryStream::GetBufferPos ( ) const
inline

Definition at line 209 of file PTRB.h.

209{ return m_BufferPos; }

◆ GetBufferSize()

TUINT PTRBSections::MemoryStream::GetBufferSize ( ) const
inline

Definition at line 211 of file PTRB.h.

211{ return m_BufferSize; }

◆ GetExpectedSize()

TUINT32 PTRBSections::MemoryStream::GetExpectedSize ( ) const
inline

Definition at line 201 of file PTRB.h.

201{ return m_ExpectedSize; }

◆ GetIndex()

TUINT32 PTRBSections::MemoryStream::GetIndex ( ) const
inline

Definition at line 203 of file PTRB.h.

203{ return m_Index; }

◆ GetOffset()

TUINT PTRBSections::MemoryStream::GetOffset ( const void * ptr) const
inline

Definition at line 215 of file PTRB.h.

216 {
217 TASSERT( (TUINT)ptr >= (TUINT)m_Buffer && (TUINT)ptr < (TUINT)m_Buffer + m_BufferSize, "Pointer is out of buffer" );
218 return (TUINT)ptr - (TUINT)m_Buffer;
219 }

◆ GetPointerCount()

TUINT PTRBSections::MemoryStream::GetPointerCount ( ) const
inline

Definition at line 205 of file PTRB.h.

205{ return m_PtrList.size(); }

◆ GetUsedSize()

TUINT PTRBSections::MemoryStream::GetUsedSize ( ) const
inline

Definition at line 213 of file PTRB.h.

213{ return (TUINT)m_BufferPos - (TUINT)m_Buffer; }

◆ GrowBuffer()

void PTRBSections::MemoryStream::GrowBuffer ( TUINT requiredSize)
inline

Definition at line 973 of file PTRB.h.

974{
975 TUINT newSize = ( ( requiredSize / BUFFER_GROW_SIZE ) + 1 ) * BUFFER_GROW_SIZE;
976
977 if ( newSize != m_BufferSize )
978 {
979 Resize( newSize );
980 }
981}
static constexpr TUINT BUFFER_GROW_SIZE
Definition PTRB.h:63

◆ Link()

void PTRBSections::MemoryStream::Link ( )
inline

Definition at line 957 of file PTRB.h.

958{
959 for ( auto& ptr : m_PtrList )
960 {
961 Write<void*>( ptr.Offset, ptr.DataStack->GetBuffer() + CONVERTENDIANESS( m_eEndianess, ptr.DataPtr ) );
962 }
963}

◆ Seek()

void PTRBSections::MemoryStream::Seek ( TUINT newPos)
inline

Definition at line 186 of file PTRB.h.

187 {
188 TASSERT( newPos < m_BufferSize, "Trying to seek out of buffer" );
189 m_BufferPos = m_Buffer + newPos;
190 }

◆ SetCrossPointer()

template<class T>
void PTRBSections::MemoryStream::SetCrossPointer ( T ** outPtr,
Ptr< T > ptr )
inline

Definition at line 838 of file PTRB.h.

839{
840 TASSERT( (TUINT)outPtr >= (TUINT)m_Buffer && (TUINT)outPtr < (TUINT)m_Buffer + (TUINT)m_BufferSize, "Out pointer is out of buffer" );
841 TASSERT( TNULL != ptr.stack() );
842 TASSERT( this != ptr.stack() );
843
844 TUINT outPtrOffset = GetOffset( outPtr );
845 Write<T*>( outPtrOffset, ptr.get() );
846
847 AddRelocationPtr( outPtrOffset, ptr.offset(), ptr.stack() );
848
849 // Set current stack as dependent from ptr.stack()
850 auto crossStack = ptr.stack();
851 auto it = std::find( crossStack->m_DependentStacks.begin(), crossStack->m_DependentStacks.end(), this );
852
853 if ( it == crossStack->m_DependentStacks.end() )
854 {
855 // Add this stack
856 crossStack->m_DependentStacks.push_back( this );
857 }
858}

◆ SetExpectedSize()

void PTRBSections::MemoryStream::SetExpectedSize ( TUINT32 expectedSize)
inline

Definition at line 199 of file PTRB.h.

199{ m_ExpectedSize = expectedSize; }

◆ SetIndex()

TUINT32 PTRBSections::MemoryStream::SetIndex ( TUINT32 index)
inline

Definition at line 192 of file PTRB.h.

193 {
194 TUINT32 oldIndex = m_Index;
195 m_Index = index;
196 return oldIndex;
197 }
uint32_t TUINT32
Definition Typedefs.h:13

◆ Tell()

TUINT PTRBSections::MemoryStream::Tell ( )
inline

Definition at line 184 of file PTRB.h.

184{ return GetUsedSize(); }

◆ Unlink()

void PTRBSections::MemoryStream::Unlink ( )
inline

Definition at line 965 of file PTRB.h.

966{
967 for ( auto& ptr : m_PtrList )
968 {
969 Write<void*>( ptr.Offset, (void*)ptr.DataPtr );
970 }
971}

◆ Write() [1/2]

template<class T>
T * PTRBSections::MemoryStream::Write ( TUINT offset,
const T & value )
inline

Definition at line 225 of file PTRB.h.

226 {
227 TASSERT( offset >= 0 && offset < m_BufferSize, "Offset is out of buffer" );
228 *(T*)( &m_Buffer[ offset ] ) = value;
229 return TREINTERPRETCAST( T*, &m_Buffer[ offset ] );
230 }

◆ Write() [2/2]

void PTRBSections::MemoryStream::Write ( TUINT offset,
TCHAR * value,
TINT size )
inline

Definition at line 232 of file PTRB.h.

233 {
234 TASSERT( offset >= 0 && offset < m_BufferSize, "Offset is out of buffer" );
235 Toshi::TUtil::MemCopy( &m_Buffer[ offset ], value, size );
236 }

◆ WritePointer() [1/2]

template<class T>
void PTRBSections::MemoryStream::WritePointer ( T ** outPtr,
const Ptr< T > & ptr )
inline

Definition at line 938 of file PTRB.h.

939{
940 TASSERT( (TUINT)outPtr >= (TUINT)m_Buffer && (TUINT)outPtr < (TUINT)m_Buffer + (TUINT)m_BufferSize, "Out pointer is out of buffer" );
941
942 TUINT outPtrOffset = GetOffset( outPtr );
943 Write<TUINT32>( outPtrOffset, TREINTERPRETCAST( TUINT32, ptr.get() ) );
944 AddRelocationPtr( outPtrOffset, CONVERTENDIANESS( m_eEndianess, ptr.offset() ) );
945}

◆ WritePointer() [2/2]

template<class T>
void PTRBSections::MemoryStream::WritePointer ( T ** outPtr,
const T * ptr )
inline

Definition at line 927 of file PTRB.h.

928{
929 TASSERT( (TUINT)outPtr >= (TUINT)m_Buffer && (TUINT)outPtr < (TUINT)m_Buffer + (TUINT)m_BufferSize, "Out pointer is out of buffer" );
930 TASSERT( (TUINT)ptr >= (TUINT)m_Buffer && (TUINT)ptr < (TUINT)m_Buffer + (TUINT)m_BufferSize, "Pointer is out of buffer" );
931
932 TUINT outPtrOffset = GetOffset( outPtr );
933 Write<TUINT32>( outPtrOffset, TREINTERPRETCAST( TUINT32, ptr ) );
934 AddRelocationPtr( outPtrOffset, CONVERTENDIANESS( m_eEndianess, GetOffset( ptr ) ) );
935}

Member Data Documentation

◆ BUFFER_GROW_SIZE

TUINT PTRBSections::MemoryStream::BUFFER_GROW_SIZE = 4096
staticconstexpr

Definition at line 63 of file PTRB.h.

◆ PTRBRelocations

friend PTRBSections::MemoryStream::PTRBRelocations

Definition at line 61 of file PTRB.h.

◆ PTRBSections

friend PTRBSections::MemoryStream::PTRBSections

Definition at line 59 of file PTRB.h.

◆ PTRBSymbols

friend PTRBSections::MemoryStream::PTRBSymbols

Definition at line 60 of file PTRB.h.


The documentation for this class was generated from the following file: