2017-05-19 18:28:01 -04:00
|
|
|
#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 <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/iostreams/device/mapped_file.hpp>
|
|
|
|
|
2022-12-11 04:10:26 -05:00
|
|
|
namespace osrm::util
|
2017-05-19 18:28:01 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
template <typename T, typename MmapContainerT>
|
|
|
|
util::vector_view<T> mmapFile(const boost::filesystem::path &file, MmapContainerT &mmap_container)
|
2017-05-19 18:28:01 -04:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
mmap_container.open(file);
|
|
|
|
std::size_t num_objects = mmap_container.size() / sizeof(T);
|
|
|
|
auto data_ptr = mmap_container.data();
|
2017-05-19 18:28:01 -04:00
|
|
|
BOOST_ASSERT(reinterpret_cast<uintptr_t>(data_ptr) % alignof(T) == 0);
|
|
|
|
return util::vector_view<T>(reinterpret_cast<T *>(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);
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 10:36:19 -05:00
|
|
|
|
2018-10-27 02:48:51 -04:00
|
|
|
template <typename T, typename MmapContainerT>
|
|
|
|
util::vector_view<T> mmapFile(const boost::filesystem::path &file,
|
|
|
|
MmapContainerT &mmap_container,
|
|
|
|
const std::size_t size)
|
2018-02-13 10:36:19 -05:00
|
|
|
{
|
|
|
|
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;
|
2018-10-27 02:48:51 -04:00
|
|
|
mmap_container.open(params);
|
2018-02-13 10:36:19 -05:00
|
|
|
|
|
|
|
std::size_t num_objects = size / sizeof(T);
|
2018-10-27 02:48:51 -04:00
|
|
|
auto data_ptr = mmap_container.data();
|
2018-02-13 10:36:19 -05:00
|
|
|
BOOST_ASSERT(reinterpret_cast<uintptr_t>(data_ptr) % alignof(T) == 0);
|
|
|
|
return util::vector_view<T>(reinterpret_cast<T *>(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);
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 10:21:39 -05:00
|
|
|
} // namespace detail
|
2017-05-19 18:28:01 -04:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
util::vector_view<const T> mmapFile(const boost::filesystem::path &file,
|
2018-10-27 02:48:51 -04:00
|
|
|
boost::iostreams::mapped_file_source &mmap_container)
|
2017-05-19 18:28:01 -04:00
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
return detail::mmapFile<const T>(file, mmap_container);
|
2017-05-19 18:28:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
util::vector_view<T> mmapFile(const boost::filesystem::path &file,
|
2018-10-27 02:48:51 -04:00
|
|
|
boost::iostreams::mapped_file &mmap_container)
|
2017-05-19 18:28:01 -04:00
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
return detail::mmapFile<T>(file, mmap_container);
|
2017-05-19 18:28:01 -04:00
|
|
|
}
|
2018-02-13 10:36:19 -05:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
util::vector_view<T> mmapFile(const boost::filesystem::path &file,
|
2018-10-27 02:48:51 -04:00
|
|
|
boost::iostreams::mapped_file &mmap_container,
|
2018-02-13 10:36:19 -05:00
|
|
|
std::size_t size)
|
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
return detail::mmapFile<T>(file, mmap_container, size);
|
2018-02-13 10:36:19 -05:00
|
|
|
}
|
2022-12-20 12:00:11 -05:00
|
|
|
} // namespace osrm::util
|
2017-05-19 18:28:01 -04:00
|
|
|
|
|
|
|
#endif
|