mmif can be const everywhere in fs_section

This commit is contained in:
Marcus Holland-Moritz 2023-05-25 15:14:17 +02:00
parent e58c80736b
commit af2fc92d12
2 changed files with 32 additions and 28 deletions

View File

@ -34,8 +34,8 @@ class mmif;
class fs_section { class fs_section {
public: public:
fs_section(mmif& mm, size_t offset, int version); fs_section(mmif const& mm, size_t offset, int version);
fs_section(std::shared_ptr<mmif> mm, section_type type, size_t offset, fs_section(std::shared_ptr<mmif const> mm, section_type type, size_t offset,
size_t size, int version); size_t size, int version);
size_t start() const { return impl_->start(); } size_t start() const { return impl_->start(); }
@ -44,9 +44,9 @@ class fs_section {
section_type type() const { return impl_->type(); } section_type type() const { return impl_->type(); }
std::string name() const { return impl_->name(); } std::string name() const { return impl_->name(); }
std::string description() const { return impl_->description(); } std::string description() const { return impl_->description(); }
bool check_fast(mmif& mm) const { return impl_->check_fast(mm); } bool check_fast(mmif const& mm) const { return impl_->check_fast(mm); }
bool verify(mmif& mm) const { return impl_->verify(mm); } bool verify(mmif const& mm) const { return impl_->verify(mm); }
folly::ByteRange data(mmif& mm) const { return impl_->data(mm); } folly::ByteRange data(mmif const& mm) const { return impl_->data(mm); }
size_t end() const { return start() + length(); } size_t end() const { return start() + length(); }
@ -60,9 +60,9 @@ class fs_section {
virtual section_type type() const = 0; virtual section_type type() const = 0;
virtual std::string name() const = 0; virtual std::string name() const = 0;
virtual std::string description() const = 0; virtual std::string description() const = 0;
virtual bool check_fast(mmif& mm) const = 0; virtual bool check_fast(mmif const& mm) const = 0;
virtual bool verify(mmif& mm) const = 0; virtual bool verify(mmif const& mm) const = 0;
virtual folly::ByteRange data(mmif& mm) const = 0; virtual folly::ByteRange data(mmif const& mm) const = 0;
}; };
private: private:

View File

@ -34,7 +34,7 @@ namespace dwarfs {
namespace { namespace {
template <typename T> template <typename T>
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) { size_t offset) {
if (offset + sizeof(T) > mm.size()) { if (offset + sizeof(T) > mm.size()) {
DWARFS_THROW(runtime_error, "truncated section header"); 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 { class fs_section_v1 : public fs_section::impl {
public: 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 start() const override { return start_; }
size_t length() const override { return hdr_.length; } 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()); return fmt::format("{}, offset={}", hdr_.to_string(), start());
} }
bool check_fast(mmif&) const override { return true; } bool check_fast(mmif const&) const override { return true; }
bool verify(mmif&) 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<uint8_t>(start_), hdr_.length); return folly::ByteRange(mm.as<uint8_t>(start_), hdr_.length);
} }
@ -102,7 +102,7 @@ class fs_section_v1 : public fs_section::impl {
class fs_section_v2 : public fs_section::impl { class fs_section_v2 : public fs_section::impl {
public: 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 start() const override { return start_; }
size_t length() const override { return hdr_.length; } 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()); 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 = auto hdr_cs_len =
sizeof(section_header_v2) - offsetof(section_header_v2, number); sizeof(section_header_v2) - offsetof(section_header_v2, number);
return checksum::verify( 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)); 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 = auto hdr_sha_len =
sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64); sizeof(section_header_v2) - offsetof(section_header_v2, xxh3_64);
return checksum::verify(checksum::algorithm::SHA2_512_256, 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)); 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<uint8_t>(start_), hdr_.length); return folly::ByteRange(mm.as<uint8_t>(start_), hdr_.length);
} }
@ -151,8 +151,8 @@ class fs_section_v2 : public fs_section::impl {
class fs_section_v2_lazy : public fs_section::impl { class fs_section_v2_lazy : public fs_section::impl {
public: public:
fs_section_v2_lazy(std::shared_ptr<mmif> mm, section_type type, size_t offset, fs_section_v2_lazy(std::shared_ptr<mmif const> mm, section_type type,
size_t size); size_t offset, size_t size);
size_t start() const override { return offset_ + sizeof(section_header_v2); } size_t start() const override { return offset_ + sizeof(section_header_v2); }
size_t length() const override { return size_ - 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(); } 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: private:
fs_section::impl const& section() const; fs_section::impl const& section() const;
std::mutex mutable mx_; std::mutex mutable mx_;
std::unique_ptr<fs_section::impl const> mutable sec_; std::unique_ptr<fs_section::impl const> mutable sec_;
std::shared_ptr<mmif> mutable mm_; std::shared_ptr<mmif const> mutable mm_;
section_type type_; section_type type_;
size_t offset_; size_t offset_;
size_t size_; 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) { switch (version) {
case 1: case 1:
impl_ = std::make_shared<fs_section_v1>(mm, offset); impl_ = std::make_shared<fs_section_v1>(mm, offset);
@ -201,7 +205,7 @@ fs_section::fs_section(mmif& mm, size_t offset, int version) {
} }
} }
fs_section::fs_section(std::shared_ptr<mmif> mm, section_type type, fs_section::fs_section(std::shared_ptr<mmif const> mm, section_type type,
size_t offset, size_t size, int version) { size_t offset, size_t size, int version) {
switch (version) { switch (version) {
case 2: case 2:
@ -216,17 +220,17 @@ fs_section::fs_section(std::shared_ptr<mmif> 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); read_section_header_common(hdr_, start_, mm, offset);
check_section(*this); 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); read_section_header_common(hdr_, start_, mm, offset);
check_section(*this); check_section(*this);
} }
fs_section_v2_lazy::fs_section_v2_lazy(std::shared_ptr<mmif> mm, fs_section_v2_lazy::fs_section_v2_lazy(std::shared_ptr<mmif const> mm,
section_type type, size_t offset, section_type type, size_t offset,
size_t size) size_t size)
: mm_{std::move(mm)} : mm_{std::move(mm)}