folly::Range -> std::span in filesystem_v2 / mmif

This commit is contained in:
Marcus Holland-Moritz 2023-05-26 13:52:44 +02:00
parent 3a09d21d05
commit eedbed42d6
3 changed files with 21 additions and 19 deletions

View File

@ -28,6 +28,7 @@
#include <iosfwd>
#include <memory>
#include <optional>
#include <span>
#include <string>
#include <utility>
@ -70,9 +71,10 @@ class filesystem_v2 {
int detail_level = 0, size_t num_readers = 1,
bool check_integrity = false, off_t image_offset = 0);
static std::optional<folly::ByteRange> header(std::shared_ptr<mmif> mm);
static std::optional<std::span<uint8_t const>>
header(std::shared_ptr<mmif> mm);
static std::optional<folly::ByteRange>
static std::optional<std::span<uint8_t const>>
header(std::shared_ptr<mmif> mm, off_t image_offset);
void dump(std::ostream& os, int detail_level) const {
@ -150,7 +152,9 @@ class filesystem_v2 {
return impl_->readv(inode, size, offset);
}
std::optional<folly::ByteRange> header() const { return impl_->header(); }
std::optional<std::span<uint8_t const>> header() const {
return impl_->header();
}
void set_num_workers(size_t num) { return impl_->set_num_workers(num); }
void set_cache_tidy_config(cache_tidy_config const& cfg) {
@ -190,7 +194,7 @@ class filesystem_v2 {
off_t offset) const = 0;
virtual folly::Expected<std::vector<std::future<block_range>>, int>
readv(uint32_t inode, size_t size, off_t offset) const = 0;
virtual std::optional<folly::ByteRange> header() const = 0;
virtual std::optional<std::span<uint8_t const>> header() const = 0;
virtual void set_num_workers(size_t num) = 0;
virtual void set_cache_tidy_config(cache_tidy_config const& cfg) = 0;
};

View File

@ -21,13 +21,12 @@
#pragma once
#include <span>
#include <string>
#include <system_error>
#include <boost/noncopyable.hpp>
#include <folly/Range.h>
namespace dwarfs {
class mmif : public boost::noncopyable {
@ -40,8 +39,8 @@ class mmif : public boost::noncopyable {
reinterpret_cast<char const*>(this->addr()) + offset);
}
folly::ByteRange range(off_t offset, size_t length) const {
return folly::ByteRange(this->as<uint8_t>(offset), length);
std::span<uint8_t const> span(off_t offset, size_t length) const {
return std::span(this->as<uint8_t>(offset), length);
}
virtual void const* addr() const = 0;

View File

@ -28,8 +28,6 @@
#include <sys/mman.h>
#include <sys/statvfs.h>
#include <folly/Range.h>
#include <fmt/format.h>
#include "dwarfs/block_cache.h"
@ -180,11 +178,11 @@ class filesystem_parser {
return std::nullopt;
}
std::optional<folly::ByteRange> header() const {
std::optional<std::span<uint8_t const>> header() const {
if (image_offset_ == 0) {
return std::nullopt;
}
return folly::ByteRange(mm_->as<uint8_t>(), image_offset_);
return mm_->span(0, image_offset_);
}
void rewind() {
@ -250,7 +248,7 @@ get_uncompressed_section_size(std::shared_ptr<mmif> mm, fs_section const& sec) {
return bd.uncompressed_size();
}
folly::ByteRange
std::span<uint8_t const>
get_section_data(std::shared_ptr<mmif> mm, fs_section const& section,
std::vector<uint8_t>& buffer, bool force_buffer) {
auto compression = section.compression();
@ -258,7 +256,7 @@ get_section_data(std::shared_ptr<mmif> mm, fs_section const& section,
auto length = section.length();
if (!force_buffer && compression == compression_type::NONE) {
return mm->range(start, length);
return mm->span(start, length);
}
buffer = block_decompressor::decompress(compression, mm->as<uint8_t>(start),
@ -346,7 +344,7 @@ class filesystem_ final : public filesystem_v2::impl {
off_t offset) const override;
folly::Expected<std::vector<std::future<block_range>>, int>
readv(uint32_t inode, size_t size, off_t offset) const override;
std::optional<folly::ByteRange> header() const override;
std::optional<std::span<uint8_t const>> header() const override;
void set_num_workers(size_t num) override { ir_.set_num_workers(num); }
void set_cache_tidy_config(cache_tidy_config const& cfg) override {
ir_.set_cache_tidy_config(cfg);
@ -362,7 +360,7 @@ class filesystem_ final : public filesystem_v2::impl {
mutable std::mutex mx_;
mutable filesystem_parser parser_;
std::vector<uint8_t> meta_buffer_;
std::optional<folly::ByteRange> header_;
std::optional<std::span<uint8_t const>> header_;
mutable std::unique_ptr<filesystem_info const> fsinfo_;
};
@ -575,7 +573,8 @@ filesystem_<LoggerPolicy>::readv(uint32_t inode, size_t size,
}
template <typename LoggerPolicy>
std::optional<folly::ByteRange> filesystem_<LoggerPolicy>::header() const {
std::optional<std::span<uint8_t const>>
filesystem_<LoggerPolicy>::header() const {
return header_;
}
@ -743,12 +742,12 @@ int filesystem_v2::identify(logger& lgr, std::shared_ptr<mmif> mm,
return errors;
}
std::optional<folly::ByteRange>
std::optional<std::span<uint8_t const>>
filesystem_v2::header(std::shared_ptr<mmif> mm) {
return header(std::move(mm), filesystem_options::IMAGE_OFFSET_AUTO);
}
std::optional<folly::ByteRange>
std::optional<std::span<uint8_t const>>
filesystem_v2::header(std::shared_ptr<mmif> mm, off_t image_offset) {
return filesystem_parser(mm, image_offset).header();
}