2019-12-26 15:32:37 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2019-11-25 12:03:09 -05:00
|
|
|
|
|
|
|
|
#include "StreamReader.h"
|
|
|
|
|
#include "Math/UnrealMath.h"
|
|
|
|
|
|
2020-11-17 06:54:28 -04:00
|
|
|
namespace UE {
|
|
|
|
|
namespace Trace {
|
2019-11-25 12:03:09 -05:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
const uint8* FStreamReader::GetPointer(uint32 Size)
|
|
|
|
|
{
|
|
|
|
|
if (Cursor + Size > End)
|
|
|
|
|
{
|
|
|
|
|
DemandHint = FMath::Max(DemandHint, Size);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Buffer + Cursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
void FStreamReader::Advance(uint32 Size)
|
|
|
|
|
{
|
|
|
|
|
Cursor += Size;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 03:36:35 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
int32 FStreamReader::GetRemaining() const
|
|
|
|
|
{
|
|
|
|
|
return int32(PTRINT(End - Cursor));
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 09:34:58 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
bool FStreamReader::CanMeetDemand() const
|
|
|
|
|
{
|
|
|
|
|
return (GetRemaining() >= int32(DemandHint));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 12:03:09 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
bool FStreamReader::IsEmpty() const
|
|
|
|
|
{
|
|
|
|
|
return Cursor >= End;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 07:57:36 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
bool FStreamReader::Backtrack(const uint8* To)
|
|
|
|
|
{
|
|
|
|
|
uint32 BacktrackedCursor = uint32(UPTRINT(To - Buffer));
|
|
|
|
|
if (BacktrackedCursor > End)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cursor = BacktrackedCursor;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 12:03:09 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
struct FMark* FStreamReader::SaveMark() const
|
|
|
|
|
{
|
|
|
|
|
return (FMark*)(UPTRINT(Cursor));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
void FStreamReader::RestoreMark(struct FMark* Mark)
|
|
|
|
|
{
|
|
|
|
|
Cursor = uint32(UPTRINT(Mark));
|
|
|
|
|
if (Cursor > End)
|
|
|
|
|
{
|
|
|
|
|
Cursor = End;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 03:53:13 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
FStreamBuffer::FStreamBuffer(uint32 InitialBufferSize)
|
|
|
|
|
: BufferSize(InitialBufferSize)
|
|
|
|
|
{
|
|
|
|
|
Buffer = (uint8*)FMemory::Malloc(BufferSize);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 12:03:09 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
void FStreamBuffer::Append(const uint8* Data, uint32 Size)
|
|
|
|
|
{
|
|
|
|
|
uint8* Out = Append(Size);
|
|
|
|
|
FMemory::Memcpy(Out, Data, Size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
uint8* FStreamBuffer::Append(uint32 Size)
|
|
|
|
|
{
|
|
|
|
|
DemandHint = FMath::Max(Size, DemandHint);
|
|
|
|
|
Consolidate();
|
|
|
|
|
uint8* Out = Buffer + End;
|
|
|
|
|
End += Size;
|
|
|
|
|
check(End <= BufferSize);
|
|
|
|
|
return Out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
void FStreamBuffer::Consolidate()
|
|
|
|
|
{
|
|
|
|
|
int32 Remaining = End - Cursor;
|
|
|
|
|
DemandHint += Remaining;
|
|
|
|
|
|
|
|
|
|
if (DemandHint >= BufferSize)
|
|
|
|
|
{
|
2020-11-12 04:23:44 -04:00
|
|
|
const uint32 GrowthSizeMask = (64 << 10) - 1;
|
2019-11-25 12:03:09 -05:00
|
|
|
BufferSize = (DemandHint + GrowthSizeMask + 1) & ~GrowthSizeMask;
|
|
|
|
|
Buffer = (uint8*)FMemory::Realloc(Buffer, BufferSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Remaining)
|
|
|
|
|
{
|
|
|
|
|
Cursor = 0;
|
|
|
|
|
End = 0;
|
|
|
|
|
}
|
|
|
|
|
else if (Cursor)
|
|
|
|
|
{
|
|
|
|
|
memmove(Buffer, Buffer + Cursor, Remaining);
|
|
|
|
|
Cursor = 0;
|
|
|
|
|
End = Remaining;
|
|
|
|
|
check(End <= BufferSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DemandHint = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Trace
|
2020-11-17 06:54:28 -04:00
|
|
|
} // namespace UE
|