2018-04-04 19:05:34 -04:00
|
|
|
#ifndef OSRM_STORAGE_SHARED_DATA_INDEX_HPP
|
|
|
|
#define OSRM_STORAGE_SHARED_DATA_INDEX_HPP
|
|
|
|
|
|
|
|
#include "storage/shared_datatype.hpp"
|
|
|
|
|
2022-11-16 15:09:54 -05:00
|
|
|
#include <boost/iterator/function_output_iterator.hpp>
|
2018-04-04 19:05:34 -04:00
|
|
|
|
2018-10-27 02:48:51 -04:00
|
|
|
#include <type_traits>
|
2018-04-04 19:05:34 -04:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2022-12-11 04:10:26 -05:00
|
|
|
namespace osrm::storage
|
2018-04-04 19:05:34 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
// This class wraps one or more shared memory regions with the associated data layout
|
|
|
|
// to abstract away in which region a block of memory is stored.
|
|
|
|
class SharedDataIndex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
struct AllocatedRegion
|
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
void *memory_ptr;
|
|
|
|
std::unique_ptr<BaseDataLayout> layout;
|
2018-04-04 19:05:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SharedDataIndex() = default;
|
|
|
|
SharedDataIndex(std::vector<AllocatedRegion> regions_) : regions(std::move(regions_))
|
|
|
|
{
|
|
|
|
// Build mapping from block name to region
|
|
|
|
for (auto index : util::irange<std::uint32_t>(0, regions.size()))
|
|
|
|
{
|
2024-05-06 03:14:46 -04:00
|
|
|
regions[index].layout->List(
|
|
|
|
"",
|
|
|
|
boost::make_function_output_iterator([&](const auto &name)
|
|
|
|
{ block_to_region[name] = index; }));
|
2018-04-04 19:05:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename OutIter> void List(const std::string &name_prefix, OutIter out) const
|
|
|
|
{
|
|
|
|
for (const auto ®ion : regions)
|
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
region.layout->List(name_prefix, out);
|
2018-04-04 19:05:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> auto GetBlockPtr(const std::string &name) const
|
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
|
|
|
// is_tivially_copyable only exists in GCC >=5
|
|
|
|
static_assert(std::is_trivially_copyable<T>::value,
|
|
|
|
"Block-based data must be a trivially copyable type");
|
|
|
|
static_assert(sizeof(T) % alignof(T) == 0, "aligned T* can't be used as an array pointer");
|
|
|
|
#endif
|
2018-04-21 07:12:55 -04:00
|
|
|
const auto ®ion = GetBlockRegion(name);
|
2018-10-27 02:48:51 -04:00
|
|
|
return reinterpret_cast<T *>(region.layout->GetBlockPtr(region.memory_ptr, name));
|
2018-04-04 19:05:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> auto GetBlockPtr(const std::string &name)
|
|
|
|
{
|
2018-10-27 02:48:51 -04:00
|
|
|
#if !defined(__GNUC__) || (__GNUC__ > 4)
|
|
|
|
// is_tivially_copyable only exists in GCC >=5
|
|
|
|
static_assert(std::is_trivially_copyable<T>::value,
|
|
|
|
"Block-based data must be a trivially copyable type");
|
|
|
|
static_assert(sizeof(T) % alignof(T) == 0, "aligned T* can't be used as an array pointer");
|
|
|
|
#endif
|
2018-04-21 07:12:55 -04:00
|
|
|
const auto ®ion = GetBlockRegion(name);
|
2018-10-27 02:48:51 -04:00
|
|
|
return reinterpret_cast<T *>(region.layout->GetBlockPtr(region.memory_ptr, name));
|
2018-04-04 19:05:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetBlockEntries(const std::string &name) const
|
|
|
|
{
|
2018-04-21 07:12:55 -04:00
|
|
|
const auto ®ion = GetBlockRegion(name);
|
2018-10-27 02:48:51 -04:00
|
|
|
return region.layout->GetBlockEntries(name);
|
2018-04-04 19:05:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetBlockSize(const std::string &name) const
|
|
|
|
{
|
2018-04-21 07:12:55 -04:00
|
|
|
const auto ®ion = GetBlockRegion(name);
|
2018-10-27 02:48:51 -04:00
|
|
|
return region.layout->GetBlockSize(name);
|
2018-04-04 19:05:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-04-21 07:12:55 -04:00
|
|
|
const AllocatedRegion &GetBlockRegion(const std::string &name) const
|
|
|
|
{
|
|
|
|
const auto index_iter = block_to_region.find(name);
|
|
|
|
if (index_iter == block_to_region.end())
|
|
|
|
{
|
|
|
|
throw util::exception("data block " + name + " not found " + SOURCE_REF);
|
|
|
|
}
|
|
|
|
return regions[index_iter->second];
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:05:34 -04:00
|
|
|
std::vector<AllocatedRegion> regions;
|
|
|
|
std::unordered_map<std::string, std::uint32_t> block_to_region;
|
|
|
|
};
|
2022-12-20 12:00:11 -05:00
|
|
|
} // namespace osrm::storage
|
2018-04-04 19:05:34 -04:00
|
|
|
|
|
|
|
#endif
|