mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-14 06:48:39 -04:00
Switch dwarfs::mmap implementation to boost::iostreams::mapped_file
This commit is contained in:
parent
61fba32a3a
commit
2f26ceafb4
@ -160,7 +160,7 @@ else()
|
|||||||
find_package(fmt 10.0 REQUIRED CONFIG PATHS "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install" NO_DEFAULT_PATH)
|
find_package(fmt 10.0 REQUIRED CONFIG PATHS "${CMAKE_CURRENT_BINARY_DIR}/fmtlib-install" NO_DEFAULT_PATH)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
list(APPEND DWARFS_BOOST_MODULES chrono date_time filesystem program_options system)
|
list(APPEND DWARFS_BOOST_MODULES chrono date_time filesystem iostreams program_options system)
|
||||||
|
|
||||||
if(WITH_PYTHON)
|
if(WITH_PYTHON)
|
||||||
# TODO: would be nicer to be able to support a range of python versions
|
# TODO: would be nicer to be able to support a range of python versions
|
||||||
|
@ -24,6 +24,8 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <boost/iostreams/device/mapped_file.hpp>
|
||||||
|
|
||||||
#include "dwarfs/mmif.h"
|
#include "dwarfs/mmif.h"
|
||||||
|
|
||||||
namespace dwarfs {
|
namespace dwarfs {
|
||||||
@ -33,8 +35,6 @@ class mmap : public mmif {
|
|||||||
explicit mmap(const std::string& path);
|
explicit mmap(const std::string& path);
|
||||||
mmap(const std::string& path, size_t size);
|
mmap(const std::string& path, size_t size);
|
||||||
|
|
||||||
~mmap() noexcept override;
|
|
||||||
|
|
||||||
void const* addr() const override;
|
void const* addr() const override;
|
||||||
size_t size() const override;
|
size_t size() const override;
|
||||||
|
|
||||||
@ -43,9 +43,9 @@ class mmap : public mmif {
|
|||||||
std::error_code release_until(off_t offset) override;
|
std::error_code release_until(off_t offset) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int fd_;
|
boost::iostreams::mapped_file mutable mf_;
|
||||||
size_t size_;
|
#ifndef _WIN32
|
||||||
void* addr_;
|
|
||||||
off_t const page_size_;
|
off_t const page_size_;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
} // namespace dwarfs
|
} // namespace dwarfs
|
||||||
|
@ -19,109 +19,84 @@
|
|||||||
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
|
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
|
|
||||||
#include <fcntl.h>
|
#ifndef _WIN32
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/stat.h>
|
#endif
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include <fmt/format.h>
|
|
||||||
|
|
||||||
#include "dwarfs/error.h"
|
#include "dwarfs/error.h"
|
||||||
#include "dwarfs/mmap.h"
|
#include "dwarfs/mmap.h"
|
||||||
|
|
||||||
namespace dwarfs {
|
namespace dwarfs {
|
||||||
|
|
||||||
namespace {
|
std::error_code
|
||||||
|
mmap::lock(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
|
||||||
int safe_open(const std::string& path) {
|
|
||||||
int fd = ::open(path.c_str(), O_RDONLY);
|
|
||||||
|
|
||||||
if (fd == -1) {
|
|
||||||
DWARFS_THROW(system_error, fmt::format("open('{}')", path));
|
|
||||||
}
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t safe_size(int fd) {
|
|
||||||
struct stat st;
|
|
||||||
if (::fstat(fd, &st) == -1) {
|
|
||||||
DWARFS_THROW(system_error, "fstat");
|
|
||||||
}
|
|
||||||
return st.st_size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* safe_mmap(int fd, size_t size) {
|
|
||||||
if (size == 0) {
|
|
||||||
DWARFS_THROW(runtime_error, "empty file");
|
|
||||||
}
|
|
||||||
|
|
||||||
void* addr = ::mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
||||||
|
|
||||||
if (addr == MAP_FAILED) {
|
|
||||||
DWARFS_THROW(system_error, "mmap");
|
|
||||||
}
|
|
||||||
|
|
||||||
return addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
std::error_code mmap::lock(off_t offset, size_t size) {
|
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
auto addr = reinterpret_cast<uint8_t*>(addr_) + offset;
|
|
||||||
if (::mlock(addr, size) != 0) {
|
#ifndef _WIN32
|
||||||
|
if (::mlock(mf_.const_data() + offset, size) != 0) {
|
||||||
ec.assign(errno, std::generic_category());
|
ec.assign(errno, std::generic_category());
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return ec;
|
return ec;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code mmap::release(off_t offset, size_t size) {
|
std::error_code
|
||||||
|
mmap::release(off_t offset [[maybe_unused]], size_t size [[maybe_unused]]) {
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
auto misalign = offset % page_size_;
|
auto misalign = offset % page_size_;
|
||||||
|
|
||||||
offset -= misalign;
|
offset -= misalign;
|
||||||
size += misalign;
|
size += misalign;
|
||||||
size -= size % page_size_;
|
size -= size % page_size_;
|
||||||
|
|
||||||
auto addr = reinterpret_cast<uint8_t*>(addr_) + offset;
|
if (::madvise(mf_.data() + offset, size, MADV_DONTNEED) != 0) {
|
||||||
if (::madvise(addr, size, MADV_DONTNEED) != 0) {
|
|
||||||
ec.assign(errno, std::generic_category());
|
ec.assign(errno, std::generic_category());
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return ec;
|
return ec;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::error_code mmap::release_until(off_t offset) {
|
std::error_code mmap::release_until(off_t offset [[maybe_unused]]) {
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
offset -= offset % page_size_;
|
offset -= offset % page_size_;
|
||||||
|
|
||||||
if (::madvise(addr_, offset, MADV_DONTNEED) != 0) {
|
if (::madvise(mf_.data(), offset, MADV_DONTNEED) != 0) {
|
||||||
ec.assign(errno, std::generic_category());
|
ec.assign(errno, std::generic_category());
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return ec;
|
return ec;
|
||||||
}
|
}
|
||||||
|
|
||||||
void const* mmap::addr() const { return addr_; }
|
void const* mmap::addr() const { return mf_.const_data(); }
|
||||||
|
|
||||||
size_t mmap::size() const { return size_; }
|
size_t mmap::size() const { return mf_.size(); }
|
||||||
|
|
||||||
mmap::mmap(const std::string& path)
|
mmap::mmap(const std::string& path)
|
||||||
: fd_(safe_open(path))
|
: mf_(path, boost::iostreams::mapped_file::readonly)
|
||||||
, size_(safe_size(fd_))
|
#ifndef _WIN32
|
||||||
, addr_(safe_mmap(fd_, size_))
|
, page_size_(::sysconf(_SC_PAGESIZE))
|
||||||
, page_size_(::sysconf(_SC_PAGESIZE)) {}
|
#endif
|
||||||
|
{
|
||||||
|
assert(mf_.is_open());
|
||||||
|
}
|
||||||
|
|
||||||
mmap::mmap(const std::string& path, size_t size)
|
mmap::mmap(const std::string& path, size_t size)
|
||||||
: fd_(safe_open(path))
|
: mf_(path, boost::iostreams::mapped_file::readonly, size)
|
||||||
, size_(size)
|
#ifndef _WIN32
|
||||||
, addr_(safe_mmap(fd_, size_))
|
, page_size_(::sysconf(_SC_PAGESIZE))
|
||||||
, page_size_(::sysconf(_SC_PAGESIZE)) {}
|
#endif
|
||||||
|
{
|
||||||
mmap::~mmap() noexcept {
|
assert(mf_.is_open());
|
||||||
::munmap(addr_, size_);
|
|
||||||
::close(fd_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dwarfs
|
} // namespace dwarfs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user