diff --git a/include/storage/io.hpp b/include/storage/io.hpp index 1b349c9e6..807d23f35 100644 --- a/include/storage/io.hpp +++ b/include/storage/io.hpp @@ -143,15 +143,12 @@ class FileReader // To make function calls consistent, this function returns the fixed number of properties inline std::size_t readPropertiesCount() { return 1; } -#pragma pack(push, 1) struct HSGRHeader { std::uint32_t checksum; std::uint64_t number_of_nodes; std::uint64_t number_of_edges; }; -#pragma pack(pop) -static_assert(sizeof(HSGRHeader) == 20, "HSGRHeader is not packed"); // Reads the checksum, number of nodes and number of edges written in the header file of a `.hsgr` // file and returns them in a HSGRHeader struct diff --git a/include/storage/shared_datatype.hpp b/include/storage/shared_datatype.hpp index 665020e55..e474803a1 100644 --- a/include/storage/shared_datatype.hpp +++ b/include/storage/shared_datatype.hpp @@ -4,9 +4,9 @@ #include "util/exception.hpp" #include "util/simple_logger.hpp" -#include - #include +#include +#include namespace osrm { @@ -98,21 +98,17 @@ struct DataLayout NUM_BLOCKS }; - std::array num_entries; - std::array entry_size; + std::array num_entries; + std::array entry_size; + std::array entry_align; - DataLayout() : num_entries(), entry_size() {} + DataLayout() : num_entries(), entry_size(), entry_align() {} template inline void SetBlockSize(BlockID bid, uint64_t entries) { num_entries[bid] = entries; entry_size[bid] = sizeof(T); - } - - inline uint64_t AlignBlockSize(uint64_t block_size) const - { - const uint64_t alignment = 4; - return (block_size + (alignment - 1)) & ~(alignment - 1); + entry_align[bid] = alignof(T); } inline uint64_t GetBlockSize(BlockID bid) const @@ -120,41 +116,53 @@ struct DataLayout // special bit encoding if (bid == CORE_MARKER) { - return AlignBlockSize((num_entries[bid] / 32 + 1) * entry_size[bid]); + return (num_entries[bid] / 32 + 1) * entry_size[bid]; } - return AlignBlockSize(num_entries[bid] * entry_size[bid]); + return num_entries[bid] * entry_size[bid]; } inline uint64_t GetSizeOfLayout() const { - return GetBlockOffset(NUM_BLOCKS) + NUM_BLOCKS * 2 * sizeof(CANARY); - } - - inline uint64_t GetBlockOffset(BlockID bid) const - { - uint64_t result = sizeof(CANARY); - for (auto i = 0; i < bid; i++) + uint64_t result = 0; + for (auto i = 0; i < NUM_BLOCKS; i++) { - result += GetBlockSize((BlockID)i) + 2 * sizeof(CANARY); + result += 2 * sizeof(CANARY) + GetBlockSize((BlockID)i) + entry_align[i]; } return result; } + inline void *GetAlignedBlockPtr(void *ptr, BlockID bid) const + { + for (auto i = 0; i < bid; i++) + { + ptr = static_cast(ptr) + sizeof(CANARY); + std::size_t space = 2 * entry_align[i] + entry_size[i]; + ptr = std::align(entry_align[i], entry_size[i], ptr, space); + ptr = static_cast(ptr) + GetBlockSize((BlockID)i); + ptr = static_cast(ptr) + sizeof(CANARY); + } + + ptr = static_cast(ptr) + sizeof(CANARY); + std::size_t space = 2 * entry_align[bid] + entry_size[bid]; + ptr = std::align(entry_align[bid], entry_size[bid], ptr, space); + return ptr; + } + template inline T *GetBlockPtr(char *shared_memory, BlockID bid) const { - T *ptr = (T *)(shared_memory + GetBlockOffset(bid)); + char *ptr = (char *)GetAlignedBlockPtr(shared_memory, bid); if (WRITE_CANARY) { - char *start_canary_ptr = shared_memory + GetBlockOffset(bid) - sizeof(CANARY); - char *end_canary_ptr = shared_memory + GetBlockOffset(bid) + GetBlockSize(bid); + char *start_canary_ptr = ptr - sizeof(CANARY); + char *end_canary_ptr = ptr + GetBlockSize(bid); std::copy(CANARY, CANARY + sizeof(CANARY), start_canary_ptr); std::copy(CANARY, CANARY + sizeof(CANARY), end_canary_ptr); } else { - char *start_canary_ptr = shared_memory + GetBlockOffset(bid) - sizeof(CANARY); - char *end_canary_ptr = shared_memory + GetBlockOffset(bid) + GetBlockSize(bid); + char *start_canary_ptr = ptr - sizeof(CANARY); + char *end_canary_ptr = ptr + GetBlockSize(bid); bool start_canary_alive = std::equal(CANARY, CANARY + sizeof(CANARY), start_canary_ptr); bool end_canary_alive = std::equal(CANARY, CANARY + sizeof(CANARY), end_canary_ptr); if (!start_canary_alive) @@ -169,7 +177,7 @@ struct DataLayout } } - return ptr; + return (T *)ptr; } }; diff --git a/include/storage/storage.hpp b/include/storage/storage.hpp index 53284ed6b..711dec816 100644 --- a/include/storage/storage.hpp +++ b/include/storage/storage.hpp @@ -28,8 +28,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef STORAGE_HPP #define STORAGE_HPP -#include "storage/storage_config.hpp" #include "storage/shared_datatype.hpp" +#include "storage/storage_config.hpp" #include diff --git a/include/util/io.hpp b/include/util/io.hpp index 1f58e1d46..21d46d153 100644 --- a/include/util/io.hpp +++ b/include/util/io.hpp @@ -14,8 +14,8 @@ #include #include -#include "util/fingerprint.hpp" #include "storage/io.hpp" +#include "util/fingerprint.hpp" namespace osrm { diff --git a/src/storage/storage.cpp b/src/storage/storage.cpp index 0a26e5f8c..e6b33123c 100644 --- a/src/storage/storage.cpp +++ b/src/storage/storage.cpp @@ -520,8 +520,7 @@ void Storage::PopulateData(const DataLayout &layout, char *memory_ptr) const auto name_char_ptr = layout.GetBlockPtr(memory_ptr, DataLayout::NAME_CHAR_LIST); - BOOST_ASSERT_MSG(layout.AlignBlockSize(temp_count) == - layout.GetBlockSize(DataLayout::NAME_CHAR_LIST), + BOOST_ASSERT_MSG(temp_count == layout.GetBlockSize(DataLayout::NAME_CHAR_LIST), "Name file corrupted!"); name_file.ReadInto(name_char_ptr, temp_count);