From bf89e9876f50487ba87752f0a0cd48a041e08102 Mon Sep 17 00:00:00 2001 From: Jendrik Weise Date: Fri, 25 Aug 2023 20:51:12 +0200 Subject: [PATCH] Allow not copying the dictionary --- src/google/vcencoder.h | 3 ++- src/vcdiffengine.cc | 9 +++++---- src/vcdiffengine.h | 4 +++- src/vcencoder.cc | 5 +++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/google/vcencoder.h b/src/google/vcencoder.h index 37d6fd6..b04df5b 100644 --- a/src/google/vcencoder.h +++ b/src/google/vcencoder.h @@ -38,7 +38,8 @@ class CodeTableWriterInterface; class HashedDictionary { public: HashedDictionary(const char* dictionary_contents, - size_t dictionary_size); + size_t dictionary_size, + bool copy = true); ~HashedDictionary(); // Init() must be called before using the HashedDictionary as an argument diff --git a/src/vcdiffengine.cc b/src/vcdiffengine.cc index 0060867..5a5a052 100644 --- a/src/vcdiffengine.cc +++ b/src/vcdiffengine.cc @@ -23,20 +23,21 @@ namespace open_vcdiff { -VCDiffEngine::VCDiffEngine(const char* dictionary, size_t dictionary_size) +VCDiffEngine::VCDiffEngine(const char* dictionary, size_t dictionary_size, bool copy) // If dictionary_size == 0, then dictionary could be NULL. Guard against // using a NULL value. - : dictionary_((dictionary_size > 0) ? new char[dictionary_size] : ""), + : dictionary_((dictionary_size > 0) ? (copy ? new char[dictionary_size] : dictionary) : ""), + owns_dictionary_(copy), dictionary_size_(dictionary_size), hashed_dictionary_(NULL) { - if (dictionary_size > 0) { + if (dictionary_size > 0 && copy) { memcpy(const_cast(dictionary_), dictionary, dictionary_size); } } VCDiffEngine::~VCDiffEngine() { delete hashed_dictionary_; - if (dictionary_size_ > 0) { + if (dictionary_size_ > 0 && owns_dictionary_) { delete[] dictionary_; } } diff --git a/src/vcdiffengine.h b/src/vcdiffengine.h index 02ede2a..78ed326 100644 --- a/src/vcdiffengine.h +++ b/src/vcdiffengine.h @@ -37,7 +37,7 @@ class VCDiffEngine { // aligned on block boundaries in the dictionary text. static const size_t kMinimumMatchSize = 32; - VCDiffEngine(const char* dictionary, size_t dictionary_size); + VCDiffEngine(const char* dictionary, size_t dictionary_size, bool copy = true); ~VCDiffEngine(); @@ -107,6 +107,8 @@ class VCDiffEngine { const char* dictionary_; // A copy of the dictionary contents + bool owns_dictionary_; + const size_t dictionary_size_; // A hash that contains one element for every kBlockSize bytes of dictionary_. diff --git a/src/vcencoder.cc b/src/vcencoder.cc index 7fe5a6a..8ad7201 100644 --- a/src/vcencoder.cc +++ b/src/vcencoder.cc @@ -57,8 +57,9 @@ CodeTableWriterInterface* create_writer( } // namespace HashedDictionary::HashedDictionary(const char* dictionary_contents, - size_t dictionary_size) - : engine_(new VCDiffEngine(dictionary_contents, dictionary_size)) { } + size_t dictionary_size, + bool copy) + : engine_(new VCDiffEngine(dictionary_contents, dictionary_size, copy)) { } HashedDictionary::~HashedDictionary() { delete engine_; }