UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
CacheStorage.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "IO/IoChunkId.h"
6#include "IO/IoBuffer.h"
7#include "Containers/Array.h"
8#include "Templates/Tuple.h"
11
12#if !UE_BUILD_SHIPPING
13
14namespace StorageServer
15{
16 // Bulk storage for caching, transactions are async, non-atomic, best effort, data might be corrupted.
18 {
19 public:
20 virtual ~ICacheStorage() = default;
21
22 // returns true if cache was created/truncated this session and all data should be assumed lost
23 virtual bool IsNewlyCreatedStorage()
24 {
26 }
27
28 virtual void Flush() = 0;
29
30 virtual uint64 GetSize() const = 0;
31
32 virtual void Invalidate(const uint64 Offset, const uint64 Size) = 0;
33
34 virtual FIoBuffer Read(
35 const uint64 Offset,
36 const uint64 ReadSize,
38 ) = 0;
39
40 virtual void WriteAsync(
41 const uint64 Offset,
42 const void* Buffer,
43 const uint64 WriteSize
44 ) = 0;
45
46 protected:
48
49 // Limit cache file size to 2GB to stay below file system limitations
50 static constexpr uint64 MaxCacheFileSize = 2ull * 1024 * 1024 * 1024;
51
52 // Generate a list of filenames and sizes to be used for backing storage
54 {
57
58 const uint64 SizeOfLastFile = FileSizeTmp - MaxCacheFileSize * (BackingFiles.Num() - 1);
59
60 for (int32 i = 0; i < BackingFiles.Num(); ++i)
61 {
62 // TODO check if disk size is exhausted?
63 const FString FileName = FString::Printf(TEXT("%s%u"), FileNamePrefix, i);
64 const uint64 DesiredFileSize = i == BackingFiles.Num() - 1 ? SizeOfLastFile : MaxCacheFileSize;
65
66 BackingFiles[i] = { FileName, DesiredFileSize };
67 }
68
69 return BackingFiles;
70 }
71
72 static bool GetBackingIntervals(const uint64 Offset, const uint64 Size,
73 uint32& IndexA, uint64& OffsetA, uint64& SizeA,
74 uint32& IndexB, uint64& OffsetB, uint64& SizeB)
75 {
78
79 // we only support writes spanning maximum two backing storage files
80 if (!ensureAlways(IndexA + 1 >= IndexB))
81 {
82 return false;
83 }
84
85 if (IndexA == IndexB)
86 {
87 OffsetA = Offset - IndexA * MaxCacheFileSize;
88 SizeA = Size;
89 OffsetB = 0;
90 SizeB = 0;
91 return true;
92 }
93 else
94 {
95 OffsetA = Offset - IndexA * MaxCacheFileSize;
96 SizeA = IndexB * MaxCacheFileSize - Offset;
97 OffsetB = 0;
98 SizeB = Size - SizeA;
99 return true;
100 }
101 }
102 };
103}
104
105#endif
#define ensureAlways( InExpression)
Definition AssertionMacros.h:466
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint32 Offset
Definition VulkanMemory.cpp:4033
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition IoBuffer.h:15
Definition CacheStorage.h:18
virtual FIoBuffer Read(const uint64 Offset, const uint64 ReadSize, TOptional< FIoBuffer > OptDestination)=0
static constexpr uint64 MaxCacheFileSize
Definition CacheStorage.h:50
bool bNewlyCreatedStorage
Definition CacheStorage.h:47
virtual ~ICacheStorage()=default
virtual bool IsNewlyCreatedStorage()
Definition CacheStorage.h:23
static TArray< TTuple< FString, uint64 > > GetBackingFileNames(const TCHAR *FileNamePrefix, const uint64 FileSizeTmp)
Definition CacheStorage.h:53
virtual void Invalidate(const uint64 Offset, const uint64 Size)=0
virtual uint64 GetSize() const =0
static bool GetBackingIntervals(const uint64 Offset, const uint64 Size, uint32 &IndexA, uint64 &OffsetA, uint64 &SizeA, uint32 &IndexB, uint64 &OffsetB, uint64 &SizeB)
Definition CacheStorage.h:72
virtual void WriteAsync(const uint64 Offset, const void *Buffer, const uint64 WriteSize)=0
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void SetNum(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2308
Definition CacheJournal.h:15
static constexpr UE_FORCEINLINE_HINT T DivideAndRoundUp(T Dividend, T Divisor)
Definition UnrealMathUtility.h:694
static constexpr UE_FORCEINLINE_HINT T DivideAndRoundDown(T Dividend, T Divisor)
Definition UnrealMathUtility.h:701
Definition Optional.h:131