Files
UnrealEngineUWP/Engine/Source/Programs/Unsync/Private/UnsyncBuffer.h
Yuriy ODonnell b5709042fb Import Unsync into the main source tree
This is a binary patching and incremental downloading tool, similar to rsync or zsync. It aims to improve the large binary download processes that previously were served by robocopy (i.e. full packages produced by the build farm).

The original code can be found in `//depot/usr/yuriy.odonnell/unsync`. This commit is a branch from the original location to preserve history.

While the codebase is designed to be self-contained and does not depend on any engine libraries, it mostly follows the UE coding guidelines and can be built with UBT.

Currently only Windows is supported, however the tool is expected to also work on Mac and Linux in the future.

#rb Martin.Ridgers
#preflight skip

[CL 18993571 by Yuriy ODonnell in ue5-main branch]
2022-02-15 04:30:27 -05:00

99 lines
2.2 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include <mutex>
#include "UnsyncLog.h"
#include "UnsyncUtil.h"
namespace unsync {
struct FBufferView
{
const uint8* Data = nullptr;
uint64 Size = 0;
};
struct FMutBufferView
{
uint8* Data = nullptr;
uint64 Size = 0;
};
class FBuffer
{
public:
FBuffer() = default;
FBuffer(uint64 InitialSize);
FBuffer(uint64 InitialSize, uint8 Value);
FBuffer(std::initializer_list<uint8> Values);
FBuffer(const FBuffer&) = delete;
FBuffer& operator=(const FBuffer&) = delete;
FBuffer(FBuffer&& Rhs);
FBuffer& operator=(FBuffer&& Rhs);
~FBuffer();
void Reserve(uint64 RequiredCapacity);
uint64 Capacity() const { return BufferCapacity; }
uint64 Size() const { return BufferSize; }
bool Empty() const { return Size() == 0; }
uint8* Data() { return Buffer; }
const uint8* Data() const { return Buffer; }
uint8* begin() { return Buffer; } // NOLINT
uint8* end() { return Buffer + BufferSize; } // NOLINT
const uint8* begin() const { return Buffer; } // NOLINT
const uint8* end() const { return Buffer + BufferSize; } // NOLINT
void Append(const uint8* AppendData, uint64 AppendSize);
void Append(const FBuffer& Other);
void PushBack(uint8 Value) { Append(&Value, 1); }
void Resize(uint64 NewSize);
void Clear() { Resize(0); }
void Shrink();
uint8& operator[](size_t I)
{
UNSYNC_ASSERT(I < BufferSize);
return Buffer[I];
}
const uint8& operator[](size_t I) const
{
UNSYNC_ASSERT(I < BufferSize);
return Buffer[I];
}
FBufferView View() const { return FBufferView{.Data = Data(), .Size = Size()}; }
FMutBufferView MutView() { return FMutBufferView{.Data = Data(), .Size = Size()}; }
operator FBufferView() const { return View(); }
private:
void EnsureCapacity(uint64 RequiredCapacity);
uint8* Buffer = nullptr;
uint64 BufferSize = 0;
uint64 BufferCapacity = 0;
};
struct FBufferPool
{
UNSYNC_DISALLOW_COPY_ASSIGN(FBufferPool);
FBufferPool(uint64 InMaxBufferSize);
~FBufferPool();
FBuffer* Acquire();
void Release(FBuffer* Buffer);
uint64 MaxBufferSize;
uint64 AllocatedBuffers = 0;
std::mutex Mutex;
std::vector<FBuffer*> Pool;
};
} // namespace unsync