| 1 |
#ifndef DELTA_BLOCK_HH |
| 2 |
#define DELTA_BLOCK_HH |
| 3 |
|
| 4 |
#define STATISTICS 0 |
| 5 |
|
| 6 |
#include "MemBuffer.hh" |
| 7 |
|
| 8 |
#include <cstdint> |
| 9 |
#include <memory> |
| 10 |
#include <span> |
| 11 |
#include <vector> |
| 12 |
#ifdef DEBUG |
| 13 |
#include "sha1.hh" |
| 14 |
#endif |
| 15 |
|
| 16 |
namespace openmsx { |
| 17 |
|
| 18 |
class DeltaBlock |
| 19 |
{ |
| 20 |
public: |
| 21 |
#if STATISTICS |
| 22 |
virtual ~DeltaBlock(); |
| 23 |
#else |
| 24 |
virtual ~DeltaBlock() = default; |
| 25 |
#endif |
| 26 |
virtual void apply(std::span<uint8_t> dst) const = 0; |
| 27 |
|
| 28 |
protected: |
| 29 |
DeltaBlock() = default; |
| 30 |
|
| 31 |
#ifdef DEBUG |
| 32 |
public: |
| 33 |
Sha1Sum sha1; |
| 34 |
#endif |
| 35 |
|
| 36 |
#if STATISTICS |
| 37 |
protected: |
| 38 |
static inline size_t globalAllocSize = 0; |
| 39 |
size_t allocSize; |
| 40 |
#endif |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
class DeltaBlockCopy final : public DeltaBlock |
| 45 |
{ |
| 46 |
public: |
| 47 |
explicit DeltaBlockCopy(std::span<const uint8_t> data); |
| 48 |
void apply(std::span<uint8_t> dst) const override; |
| 49 |
void compress(size_t size); |
| 50 |
[[nodiscard]] const uint8_t* getData(); |
| 51 |
|
| 52 |
private: |
| 53 |
[[nodiscard]] bool compressed() const { return compressedSize != 0; } |
| 54 |
|
| 55 |
MemBuffer<uint8_t> block; |
| 56 |
size_t compressedSize = 0; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
class DeltaBlockDiff final : public DeltaBlock |
| 61 |
{ |
| 62 |
public: |
| 63 |
DeltaBlockDiff(std::shared_ptr<DeltaBlockCopy> prev_, |
| 64 |
std::span<const uint8_t> data); |
| 65 |
void apply(std::span<uint8_t> dst) const override; |
| 66 |
[[nodiscard]] size_t getDeltaSize() const; |
| 67 |
|
| 68 |
private: |
| 69 |
const std::shared_ptr<DeltaBlockCopy> prev; |
| 70 |
const std::vector<uint8_t> delta; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
class LastDeltaBlocks |
| 75 |
{ |
| 76 |
public: |
| 77 |
[[nodiscard]] std::shared_ptr<DeltaBlock> createNew( |
| 78 |
const void* id, std::span<const uint8_t> data); |
| 79 |
[[nodiscard]] std::shared_ptr<DeltaBlock> createNullDiff( |
| 80 |
const void* id, std::span<const uint8_t> data); |
| 81 |
void clear(); |
| 82 |
|
| 83 |
private: |
| 84 |
struct Info { |
| 85 |
Info(const void* id_, size_t size_) |
| 86 |
: id(id_), size(size_) {} |
| 87 |
|
| 88 |
const void* id; |
| 89 |
size_t size; |
| 90 |
std::weak_ptr<DeltaBlockCopy> ref; |
| 91 |
std::weak_ptr<DeltaBlock> last; |
| 92 |
size_t accSize = 0; |
| 93 |
}; |
| 94 |
|
| 95 |
std::vector<Info> infos; |
| 96 |
}; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
#endif |