openMSX
DeltaBlock.hh
Go to the documentation of this file.
1 #ifndef DELTA_BLOCK_HH
2 #define DELTA_BLOCK_HH
3 
4 #define STATISTICS 0
5 
6 #include "MemBuffer.hh"
7 #include <cstdint>
8 #include <memory>
9 #include <vector>
10 #ifdef DEBUG
11 #include "sha1.hh"
12 #endif
13 
14 namespace openmsx {
15 
17 {
18 public:
19 #if STATISTICS
20  virtual ~DeltaBlock();
21 #else
22  virtual ~DeltaBlock() = default;
23 #endif
24  virtual void apply(uint8_t* dst, size_t size) const = 0;
25 
26 protected:
27  DeltaBlock() = default;
28 
29 #ifdef DEBUG
30 public:
31  Sha1Sum sha1;
32 #endif
33 
34 #if STATISTICS
35 protected:
36  static size_t globalAllocSize;
37  size_t allocSize;
38 #endif
39 };
40 
41 
42 class DeltaBlockCopy final : public DeltaBlock
43 {
44 public:
45  DeltaBlockCopy(const uint8_t* data, size_t size);
46  void apply(uint8_t* dst, size_t size) const override;
47  void compress(size_t size);
48  const uint8_t* getData();
49 
50 private:
51  bool compressed() const { return compressedSize != 0; }
52 
53  MemBuffer<uint8_t> block;
54  size_t compressedSize;
55 };
56 
57 
58 class DeltaBlockDiff final : public DeltaBlock
59 {
60 public:
61  DeltaBlockDiff(std::shared_ptr<DeltaBlockCopy> prev_,
62  const uint8_t* data, size_t size);
63  void apply(uint8_t* dst, size_t size) const override;
64  size_t getDeltaSize() const;
65 
66 private:
67  const std::shared_ptr<DeltaBlockCopy> prev;
68  const std::vector<uint8_t> delta; // TODO could be tweaked to use OutputBuffer
69 };
70 
71 
73 {
74 public:
75  std::shared_ptr<DeltaBlock> createNew(
76  const void* id, const uint8_t* data, size_t size);
77  std::shared_ptr<DeltaBlock> createNullDiff(
78  const void* id, const uint8_t* data, size_t size);
79  void clear();
80 
81 private:
82  struct Info {
83  Info(const void* id_, size_t size_)
84  : id(id_), size(size_), accSize(0) {}
85 
86  const void* id;
87  size_t size;
88  std::weak_ptr<DeltaBlockCopy> ref;
89  std::weak_ptr<DeltaBlock> last;
90  size_t accSize;
91  };
92 
93  std::vector<Info> infos;
94 };
95 
96 } // namespace openmsx
97 
98 #endif
void compress(const char *input, size_t inLen, char *output, size_t &outLen)
Definition: snappy.cc:603
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.hh:69
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:19
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
virtual void apply(uint8_t *dst, size_t size) const =0
constexpr auto size(const C &c) -> decltype(c.size())
Definition: span.hh:62
virtual ~DeltaBlock()=default