Skip to content

Commit

Permalink
mmif can be const everywhere in fs_section
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed May 25, 2023
1 parent e58c807 commit af2fc92
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
16 changes: 8 additions & 8 deletions include/dwarfs/fs_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class mmif;

class fs_section {
public:
fs_section(mmif& mm, size_t offset, int version);
fs_section(std::shared_ptr<mmif> mm, section_type type, size_t offset,
fs_section(mmif const& mm, size_t offset, int version);
fs_section(std::shared_ptr<mmif const> mm, section_type type, size_t offset,
size_t size, int version);

size_t start() const { return impl_->start(); }
Expand All @@ -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(); }

Expand All @@ -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:
Expand Down
44 changes: 24 additions & 20 deletions src/dwarfs/fs_section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace dwarfs {
namespace {

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) {
if (offset + sizeof(T) > mm.size()) {
DWARFS_THROW(runtime_error, "truncated section header");
Expand Down Expand Up @@ -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; }
Expand All @@ -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<uint8_t>(start_), hdr_.length);
}

Expand All @@ -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; }
Expand All @@ -123,15 +123,15 @@ 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(
checksum::algorithm::XXH3_64, mm.as<void>(start_ - hdr_cs_len),
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,
Expand All @@ -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<uint8_t>(start_), hdr_.length);
}

Expand All @@ -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<mmif> mm, section_type type, size_t offset,
size_t size);
fs_section_v2_lazy(std::shared_ptr<mmif const> 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); }
Expand All @@ -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<fs_section::impl const> mutable sec_;
std::shared_ptr<mmif> mutable mm_;
std::shared_ptr<mmif const> 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<fs_section_v1>(mm, offset);
Expand All @@ -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) {
switch (version) {
case 2:
Expand All @@ -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);
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<mmif> mm,
fs_section_v2_lazy::fs_section_v2_lazy(std::shared_ptr<mmif const> mm,
section_type type, size_t offset,
size_t size)
: mm_{std::move(mm)}
Expand Down

0 comments on commit af2fc92

Please sign in to comment.