2019-12-26 15:32:37 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2019-06-03 15:32:00 -04:00
|
|
|
|
|
|
|
|
#include "Common/StringStore.h"
|
2021-01-28 12:04:39 -04:00
|
|
|
#include "Misc/ScopeLock.h"
|
|
|
|
|
|
2019-06-03 15:32:00 -04:00
|
|
|
|
2020-11-13 05:29:37 -04:00
|
|
|
namespace TraceServices
|
2019-06-03 15:32:00 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
FStringStore::FStringStore(FSlabAllocator& InAllocator)
|
|
|
|
|
: Allocator(InAllocator)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TCHAR* FStringStore::Store(const TCHAR* String)
|
2020-09-24 00:43:27 -04:00
|
|
|
{
|
|
|
|
|
return Store(FStringView(String));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const TCHAR* FStringStore::Store(const FStringView& String)
|
2019-06-03 15:32:00 -04:00
|
|
|
{
|
2021-01-22 05:36:41 -04:00
|
|
|
FScopeLock _(&Cs);
|
2019-06-03 15:32:00 -04:00
|
|
|
uint32 Hash = GetTypeHash(String);
|
|
|
|
|
const TCHAR** AlreadyStored = StoredStrings.Find(Hash);
|
2020-09-24 00:43:27 -04:00
|
|
|
if (AlreadyStored && !String.Compare(FStringView(*AlreadyStored)))
|
2019-06-03 15:32:00 -04:00
|
|
|
{
|
|
|
|
|
return *AlreadyStored;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 00:43:27 -04:00
|
|
|
int32 StringLength = String.Len() + 1;
|
2019-06-03 15:32:00 -04:00
|
|
|
if (BufferLeft < StringLength)
|
|
|
|
|
{
|
|
|
|
|
BufferPtr = reinterpret_cast<TCHAR*>(Allocator.Allocate(BlockSize * sizeof(TCHAR)));
|
|
|
|
|
++BlockCount;
|
|
|
|
|
BufferLeft = BlockSize;
|
|
|
|
|
}
|
|
|
|
|
const TCHAR* Stored = BufferPtr;
|
2020-09-24 00:43:27 -04:00
|
|
|
memcpy(BufferPtr, String.GetData(), (StringLength - 1) * sizeof(TCHAR));
|
|
|
|
|
BufferPtr[StringLength - 1] = TEXT('\0');
|
2019-06-03 15:32:00 -04:00
|
|
|
BufferLeft -= StringLength;
|
|
|
|
|
BufferPtr += StringLength;
|
|
|
|
|
if (!AlreadyStored)
|
|
|
|
|
{
|
|
|
|
|
StoredStrings.Add(Hash, Stored);
|
|
|
|
|
}
|
|
|
|
|
return Stored;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 00:43:27 -04:00
|
|
|
|
2020-11-13 05:29:37 -04:00
|
|
|
} // namespace TraceServices
|