2013-09-17 08:23:06 -04:00
|
|
|
/*
|
2013-10-21 05:45:16 -04:00
|
|
|
|
|
|
|
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
Redistributions of source code must retain the above copyright notice, this list
|
|
|
|
of conditions and the following disclaimer.
|
|
|
|
Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
2013-09-17 08:23:06 -04:00
|
|
|
|
|
|
|
#ifndef SHARED_MEMORY_VECTOR_WRAPPER_H
|
|
|
|
#define SHARED_MEMORY_VECTOR_WRAPPER_H
|
|
|
|
|
2013-09-23 12:01:30 -04:00
|
|
|
#include "../Util/SimpleLogger.h"
|
|
|
|
|
2013-09-17 08:23:06 -04:00
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/type_traits.hpp>
|
|
|
|
|
2013-09-23 12:01:30 -04:00
|
|
|
#include <algorithm>
|
2013-09-17 08:23:06 -04:00
|
|
|
#include <iterator>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
template<typename DataT>
|
|
|
|
class ShMemIterator : public std::iterator<std::input_iterator_tag, DataT> {
|
2013-09-23 13:00:08 -04:00
|
|
|
DataT * p;
|
2013-09-17 08:23:06 -04:00
|
|
|
public:
|
2013-09-23 13:00:08 -04:00
|
|
|
ShMemIterator(DataT * x) : p(x) {}
|
2013-09-17 08:23:06 -04:00
|
|
|
ShMemIterator(const ShMemIterator & mit) : p(mit.p) {}
|
|
|
|
ShMemIterator& operator++() {
|
|
|
|
++p;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ShMemIterator operator++(int) {
|
|
|
|
ShMemIterator tmp(*this);
|
|
|
|
operator++();
|
|
|
|
return tmp;
|
|
|
|
}
|
2013-09-24 09:10:06 -04:00
|
|
|
ShMemIterator operator+(std::ptrdiff_t diff) {
|
|
|
|
ShMemIterator tmp(p+diff);
|
|
|
|
return tmp;
|
|
|
|
}
|
2013-09-17 08:23:06 -04:00
|
|
|
bool operator==(const ShMemIterator& rhs) {
|
|
|
|
return p==rhs.p;
|
|
|
|
}
|
|
|
|
bool operator!=(const ShMemIterator& rhs) {
|
|
|
|
return p!=rhs.p;
|
|
|
|
}
|
|
|
|
DataT& operator*() {
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename DataT>
|
|
|
|
class SharedMemoryWrapper {
|
|
|
|
private:
|
2013-09-23 13:00:08 -04:00
|
|
|
DataT * m_ptr;
|
2013-09-17 08:23:06 -04:00
|
|
|
std::size_t m_size;
|
|
|
|
|
|
|
|
public:
|
2013-09-23 12:01:30 -04:00
|
|
|
SharedMemoryWrapper() :
|
|
|
|
m_size(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
SharedMemoryWrapper(DataT * ptr, std::size_t size) :
|
2013-09-17 08:23:06 -04:00
|
|
|
m_ptr(ptr),
|
|
|
|
m_size(size)
|
|
|
|
{ }
|
|
|
|
|
2013-09-23 13:00:08 -04:00
|
|
|
void swap( SharedMemoryWrapper<DataT> & other ) {
|
2013-09-30 09:50:26 -04:00
|
|
|
BOOST_ASSERT_MSG(m_size != 0 || other.size() != 0, "size invalid");
|
2013-09-23 12:01:30 -04:00
|
|
|
std::swap( m_size, other.m_size);
|
|
|
|
std::swap( m_ptr , other.m_ptr );
|
|
|
|
}
|
|
|
|
|
|
|
|
// void SetData(const DataT * ptr, const std::size_t size) {
|
|
|
|
// BOOST_ASSERT_MSG( 0 == m_size, "vector not empty");
|
|
|
|
// BOOST_ASSERT_MSG( 0 < size , "new vector empty");
|
|
|
|
// m_ptr.reset(ptr);
|
|
|
|
// m_size = size;
|
|
|
|
// }
|
|
|
|
|
2013-09-23 07:30:21 -04:00
|
|
|
DataT & at(const std::size_t index) {
|
|
|
|
return m_ptr[index];
|
|
|
|
}
|
|
|
|
|
2013-09-23 12:01:30 -04:00
|
|
|
const DataT & at(const std::size_t index) const {
|
|
|
|
return m_ptr[index];
|
|
|
|
}
|
|
|
|
|
2013-09-17 08:23:06 -04:00
|
|
|
ShMemIterator<DataT> begin() const {
|
|
|
|
return ShMemIterator<DataT>(m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
ShMemIterator<DataT> end() const {
|
|
|
|
return ShMemIterator<DataT>(m_ptr+m_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const { return m_size; }
|
|
|
|
|
2013-09-26 12:19:51 -04:00
|
|
|
DataT & operator[](const unsigned index) {
|
2013-09-17 08:23:06 -04:00
|
|
|
BOOST_ASSERT_MSG(index < m_size, "invalid size");
|
|
|
|
return m_ptr[index];
|
|
|
|
}
|
2013-09-23 12:01:30 -04:00
|
|
|
|
2013-09-26 12:19:51 -04:00
|
|
|
const DataT & operator[](const unsigned index) const {
|
2013-09-23 12:01:30 -04:00
|
|
|
BOOST_ASSERT_MSG(index < m_size, "invalid size");
|
|
|
|
return m_ptr[index];
|
|
|
|
}
|
2013-09-17 08:23:06 -04:00
|
|
|
};
|
|
|
|
|
2013-09-23 12:01:30 -04:00
|
|
|
template<typename DataT, bool UseSharedMemory>
|
|
|
|
struct ShM {
|
|
|
|
typedef typename boost::conditional<
|
|
|
|
UseSharedMemory,
|
|
|
|
SharedMemoryWrapper<DataT>,
|
|
|
|
std::vector<DataT>
|
|
|
|
>::type vector;
|
|
|
|
};
|
2013-09-17 08:23:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
#endif //SHARED_MEMORY_VECTOR_WRAPPER_H
|