From af2fc92d12388491bd6ea9264ddba992da6d34bb Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Thu, 25 May 2023 15:14:17 +0200 Subject: [PATCH] mmif can be const everywhere in fs_section --- include/dwarfs/fs_section.h | 16 +++++++------- src/dwarfs/fs_section.cpp | 44 ++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/include/dwarfs/fs_section.h b/include/dwarfs/fs_section.h index 0a283204..46d38737 100644 --- a/include/dwarfs/fs_section.h +++ b/include/dwarfs/fs_section.h @@ -34,8 +34,8 @@ class mmif; class fs_section { public: - fs_section(mmif& mm, size_t offset, int version); - fs_section(std::shared_ptr mm, section_type type, size_t offset, + fs_section(mmif const& mm, size_t offset, int version); + fs_section(std::shared_ptr mm, section_type type, size_t offset, size_t size, int version); size_t start() const { return impl_->start(); } @@ -44,9 +44,9 @@ class fs_section { section_type type() const { return impl_->type(); } std::string name() const { return impl_->name(); } std::string description() const { return impl_->description(); } - bool check_fast(mmif& mm) const { return impl_->check_fast(mm); } - bool verify(mmif& mm) const { return impl_->verify(mm); } - folly::ByteRange data(mmif& mm) const { return impl_->data(mm); } + bool check_fast(mmif const& mm) const { return impl_->check_fast(mm); } + bool verify(mmif const& mm) const { return impl_->verify(mm); } + folly::ByteRange data(mmif const& mm) const { return impl_->data(mm); } size_t end() const { return start() + length(); } @@ -60,9 +60,9 @@ class fs_section { virtual section_type type() const = 0; virtual std::string name() const = 0; virtual std::string description() const = 0; - virtual bool check_fast(mmif& mm) const = 0; - virtual bool verify(mmif& mm) const = 0; - virtual folly::ByteRange data(mmif& mm) const = 0; + virtual bool check_fast(mmif const& mm) const = 0; + virtual bool verify(mmif const& mm) const = 0; + virtual folly::ByteRange data(mmif const& mm) const = 0; }; private: diff --git a/src/dwarfs/fs_section.cpp b/src/dwarfs/fs_section.cpp index c12bc5bc..5a882268 100644 --- a/src/dwarfs/fs_section.cpp +++ b/src/dwarfs/fs_section.cpp @@ -34,7 +34,7 @@ namespace dwarfs { namespace { template -void read_section_header_common(T& header, size_t& start, mmif& mm, +void read_section_header_common(T& header, size_t& start, mmif const& mm, size_t offset) { if (offset + sizeof(T) > mm.size()) { DWARFS_THROW(runtime_error, "truncated section header"); @@ -75,7 +75,7 @@ void check_section(T const& sec) { class fs_section_v1 : public fs_section::impl { public: - fs_section_v1(mmif& mm, size_t offset); + fs_section_v1(mmif const& mm, size_t offset); size_t start() const override { return start_; } size_t length() const override { return hdr_.length; } @@ -88,10 +88,10 @@ class fs_section_v1 : public fs_section::impl { return fmt::format("{}, offset={}", hdr_.to_string(), start()); } - bool check_fast(mmif&) const override { return true; } - bool verify(mmif&) const override { return true; } + bool check_fast(mmif const&) const override { return true; } + bool verify(mmif const&) const override { return true; } - folly::ByteRange data(mmif& mm) const override { + folly::ByteRange data(mmif const& mm) const override { return folly::ByteRange(mm.as(start_), hdr_.length); } @@ -102,7 +102,7 @@ class fs_section_v1 : public fs_section::impl { class fs_section_v2 : public fs_section::impl { public: - fs_section_v2(mmif& mm, size_t offset); + fs_section_v2(mmif const& mm, size_t offset); size_t start() const override { return start_; } size_t length() const override { return hdr_.length; } @@ -123,7 +123,7 @@ class fs_section_v2 : public fs_section::impl { return fmt::format("{}, offset={}", hdr_.to_string(), start()); } - bool check_fast(mmif& mm) const override { + bool check_fast(mmif const& mm) const override { auto hdr_cs_len = sizeof(section_header_v2) - offsetof(section_header_v2, number); return checksum::verify( @@ -131,7 +131,7 @@ class fs_section_v2 : public fs_section::impl { hdr_.length + hdr_cs_len, &hdr_.xxh3_64, sizeof(hdr_.xxh3_64)); } - bool verify(mmif& mm) const override { + bool verify(mmif const& mm) const override { auto hdr_sha_len = sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64); return checksum::verify(checksum::algorithm::SHA2_512_256, @@ -140,7 +140,7 @@ class fs_section_v2 : public fs_section::impl { sizeof(hdr_.sha2_512_256)); } - folly::ByteRange data(mmif& mm) const override { + folly::ByteRange data(mmif const& mm) const override { return folly::ByteRange(mm.as(start_), hdr_.length); } @@ -151,8 +151,8 @@ class fs_section_v2 : public fs_section::impl { class fs_section_v2_lazy : public fs_section::impl { public: - fs_section_v2_lazy(std::shared_ptr mm, section_type type, size_t offset, - size_t size); + fs_section_v2_lazy(std::shared_ptr mm, section_type type, + size_t offset, size_t size); size_t start() const override { return offset_ + sizeof(section_header_v2); } size_t length() const override { return size_ - sizeof(section_header_v2); } @@ -167,24 +167,28 @@ class fs_section_v2_lazy : public fs_section::impl { std::string description() const override { return section().description(); } - bool check_fast(mmif& mm) const override { return section().check_fast(mm); } + bool check_fast(mmif const& mm) const override { + return section().check_fast(mm); + } - bool verify(mmif& mm) const override { return section().verify(mm); } + bool verify(mmif const& mm) const override { return section().verify(mm); } - folly::ByteRange data(mmif& mm) const override { return section().data(mm); } + folly::ByteRange data(mmif const& mm) const override { + return section().data(mm); + } private: fs_section::impl const& section() const; std::mutex mutable mx_; std::unique_ptr mutable sec_; - std::shared_ptr mutable mm_; + std::shared_ptr mutable mm_; section_type type_; size_t offset_; size_t size_; }; -fs_section::fs_section(mmif& mm, size_t offset, int version) { +fs_section::fs_section(mmif const& mm, size_t offset, int version) { switch (version) { case 1: impl_ = std::make_shared(mm, offset); @@ -201,7 +205,7 @@ fs_section::fs_section(mmif& mm, size_t offset, int version) { } } -fs_section::fs_section(std::shared_ptr mm, section_type type, +fs_section::fs_section(std::shared_ptr mm, section_type type, size_t offset, size_t size, int version) { switch (version) { case 2: @@ -216,17 +220,17 @@ fs_section::fs_section(std::shared_ptr mm, section_type type, } } -fs_section_v1::fs_section_v1(mmif& mm, size_t offset) { +fs_section_v1::fs_section_v1(mmif const& mm, size_t offset) { read_section_header_common(hdr_, start_, mm, offset); check_section(*this); } -fs_section_v2::fs_section_v2(mmif& mm, size_t offset) { +fs_section_v2::fs_section_v2(mmif const& mm, size_t offset) { read_section_header_common(hdr_, start_, mm, offset); check_section(*this); } -fs_section_v2_lazy::fs_section_v2_lazy(std::shared_ptr mm, +fs_section_v2_lazy::fs_section_v2_lazy(std::shared_ptr mm, section_type type, size_t offset, size_t size) : mm_{std::move(mm)}