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

View File

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

View File

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