#ifndef OSRM_UTIL_MMAP_FILE_HPP #define OSRM_UTIL_MMAP_FILE_HPP #include "util/exception.hpp" #include "util/exception_utils.hpp" #include "util/vector_view.hpp" #include #include namespace osrm { namespace util { namespace detail { template util::vector_view mmapFile(const boost::filesystem::path &file, RegionT ®ion) { try { region.open(file); std::size_t num_objects = region.size() / sizeof(T); auto data_ptr = region.data(); BOOST_ASSERT(reinterpret_cast(data_ptr) % alignof(T) == 0); return util::vector_view(reinterpret_cast(data_ptr), num_objects); } catch (const std::exception &exc) { throw exception( boost::str(boost::format("File %1% mapping failed: %2%") % file % exc.what()) + SOURCE_REF); } } template util::vector_view mmapFile(const boost::filesystem::path &file, RegionT ®ion, const std::size_t size) { try { // Create a new file with the given size in bytes boost::iostreams::mapped_file_params params; params.path = file.string(); params.flags = boost::iostreams::mapped_file::readwrite; params.new_file_size = size; region.open(params); std::size_t num_objects = size / sizeof(T); auto data_ptr = region.data(); BOOST_ASSERT(reinterpret_cast(data_ptr) % alignof(T) == 0); return util::vector_view(reinterpret_cast(data_ptr), num_objects); } catch (const std::exception &exc) { throw exception( boost::str(boost::format("File %1% mapping failed: %2%") % file % exc.what()) + SOURCE_REF); } } } template util::vector_view mmapFile(const boost::filesystem::path &file, boost::iostreams::mapped_file_source ®ion) { return detail::mmapFile(file, region); } template util::vector_view mmapFile(const boost::filesystem::path &file, boost::iostreams::mapped_file ®ion) { return detail::mmapFile(file, region); } template util::vector_view mmapFile(const boost::filesystem::path &file, boost::iostreams::mapped_file ®ion, std::size_t size) { return detail::mmapFile(file, region, size); } } } #endif