Remove canary to get a consistent interface for getting a block pointer

This commit is contained in:
Patrick Niklaus
2018-04-03 09:43:43 +00:00
committed by Patrick Niklaus
parent 39effb8f7e
commit 3af3e06e75
3 changed files with 123 additions and 149 deletions
+2 -56
View File
@@ -7,7 +7,6 @@
#include "util/exception.hpp"
#include "util/exception_utils.hpp"
#include "util/log.hpp"
#include "util/vector_view.hpp"
#include <boost/assert.hpp>
@@ -55,9 +54,6 @@ inline std::string trimName(const std::string &name_prefix, const std::string &n
}
}
// Added at the start and end of each block as sanity check
const constexpr char CANARY[4] = {'O', 'S', 'R', 'M'};
class DataLayout
{
public:
@@ -82,55 +78,17 @@ class DataLayout
uint64_t result = 0;
for (const auto &name_and_block : blocks)
{
result += 2 * sizeof(CANARY) + GetBlockSize(name_and_block.first) + BLOCK_ALIGNMENT;
result += GetBlockSize(name_and_block.first) + BLOCK_ALIGNMENT;
}
return result;
}
template <typename T>
util::vector_view<T> GetVector(char *shared_memory, const std::string &name) const
{
return util::vector_view<T>(GetBlockPtr<T>(shared_memory, name), GetBlockEntries(name));
}
template <typename T>
util::vector_view<T> GetWritableVector(char *shared_memory, const std::string &name) const
{
return util::vector_view<T>(GetBlockPtr<T, true>(shared_memory, name),
GetBlockEntries(name));
}
template <typename T, bool WRITE_CANARY = false>
inline T *GetBlockPtr(char *shared_memory, const std::string &name) const
template <typename T> inline T *GetBlockPtr(char *shared_memory, const std::string &name) const
{
static_assert(BLOCK_ALIGNMENT % std::alignment_of<T>::value == 0,
"Datatype does not fit alignment constraints.");
char *ptr = (char *)GetAlignedBlockPtr(shared_memory, name);
if (WRITE_CANARY)
{
char *start_canary_ptr = ptr - sizeof(CANARY);
char *end_canary_ptr = ptr + GetBlockSize(name);
std::copy(CANARY, CANARY + sizeof(CANARY), start_canary_ptr);
std::copy(CANARY, CANARY + sizeof(CANARY), end_canary_ptr);
}
else
{
char *start_canary_ptr = ptr - sizeof(CANARY);
char *end_canary_ptr = ptr + GetBlockSize(name);
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)
{
throw util::exception("Start canary of block corrupted. (" + name + ")" +
SOURCE_REF);
}
if (!end_canary_alive)
{
throw util::exception("End canary of block corrupted. (" + name + ")" + SOURCE_REF);
}
}
return (T *)ptr;
}
@@ -190,13 +148,10 @@ class DataLayout
for (auto iter = blocks.begin(); iter != block_iter; ++iter)
{
ptr = static_cast<char *>(ptr) + sizeof(CANARY);
ptr = align(ptr);
ptr = static_cast<char *>(ptr) + iter->second.byte_size;
ptr = static_cast<char *>(ptr) + sizeof(CANARY);
}
ptr = static_cast<char *>(ptr) + sizeof(CANARY);
ptr = align(ptr);
return ptr;
}
@@ -205,15 +160,6 @@ class DataLayout
std::map<std::string, Block> blocks;
};
template <>
inline util::vector_view<bool> DataLayout::GetWritableVector<bool>(char *shared_memory,
const std::string &name) const
{
return util::vector_view<bool>(
GetBlockPtr<util::vector_view<bool>::Word, true>(shared_memory, name),
GetBlockEntries(name));
}
enum SharedDataType
{
REGION_NONE,
+31
View File
@@ -0,0 +1,31 @@
#ifndef OSRM_STOARGE_VIEW_FACTORY_HPP
#define OSRM_STOARGE_VIEW_FACTORY_HPP
#include "storage/shared_datatype.hpp"
#include "util/vector_view.hpp"
namespace osrm
{
namespace storage
{
template <typename T>
util::vector_view<T> make_vector_view(char *memory_ptr, DataLayout layout, const std::string &name)
{
return util::vector_view<T>(layout.GetBlockPtr<T>(memory_ptr, name),
layout.GetBlockEntries(name));
}
template <>
inline util::vector_view<bool>
make_vector_view(char *memory_ptr, DataLayout layout, const std::string &name)
{
return util::vector_view<bool>(
layout.GetBlockPtr<util::vector_view<bool>::Word>(memory_ptr, name),
layout.GetBlockEntries(name));
}
}
}
#endif