2016-01-07 13:19:55 -05:00
|
|
|
#ifndef SHARED_MEMORY_HPP
|
|
|
|
#define SHARED_MEMORY_HPP
|
2013-09-17 08:23:06 -04:00
|
|
|
|
2016-01-28 08:27:05 -05:00
|
|
|
#include "util/exception.hpp"
|
2016-12-06 15:30:46 -05:00
|
|
|
#include "util/exception_utils.hpp"
|
|
|
|
#include "util/log.hpp"
|
2013-09-17 08:23:06 -04:00
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2013-09-26 05:28:51 -04:00
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2013-09-17 08:23:06 -04:00
|
|
|
#include <boost/interprocess/mapped_region.hpp>
|
2016-05-05 07:24:27 -04:00
|
|
|
#ifndef _WIN32
|
2013-09-17 08:23:06 -04:00
|
|
|
#include <boost/interprocess/xsi_shared_memory.hpp>
|
2014-04-07 07:07:27 -04:00
|
|
|
#else
|
|
|
|
#include <boost/interprocess/shared_memory_object.hpp>
|
|
|
|
#endif
|
2013-09-17 08:23:06 -04:00
|
|
|
|
2013-11-14 12:33:09 -05:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-07 08:46:46 -04:00
|
|
|
#include <cstdint>
|
2013-11-19 04:38:59 -05:00
|
|
|
|
2013-09-17 08:23:06 -04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <exception>
|
2017-01-05 17:38:48 -05:00
|
|
|
#include <thread>
|
2013-09-17 08:23:06 -04:00
|
|
|
|
2017-04-04 03:52:00 -04:00
|
|
|
#include "storage/shared_memory_ownership.hpp"
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
2016-01-07 13:19:55 -05:00
|
|
|
namespace storage
|
2016-01-05 10:51:13 -05:00
|
|
|
{
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
struct OSRMLockFile
|
|
|
|
{
|
2018-08-28 21:12:19 -04:00
|
|
|
template <typename IdentifierT> boost::filesystem::path operator()(const IdentifierT &id)
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
|
|
|
boost::filesystem::path temp_dir = boost::filesystem::temp_directory_path();
|
2018-08-28 21:12:19 -04:00
|
|
|
boost::filesystem::path lock_file = temp_dir / ("osrm-" + std::to_string(id) + ".lock");
|
2014-05-07 12:39:16 -04:00
|
|
|
return lock_file;
|
|
|
|
}
|
2013-09-17 08:23:06 -04:00
|
|
|
};
|
|
|
|
|
2016-05-05 07:24:27 -04:00
|
|
|
#ifndef _WIN32
|
2014-05-07 12:39:16 -04:00
|
|
|
class SharedMemory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void *Ptr() const { return region.get_address(); }
|
2018-03-26 11:01:48 -04:00
|
|
|
std::size_t Size() const { return region.get_size(); }
|
2014-05-07 12:39:16 -04:00
|
|
|
|
|
|
|
SharedMemory(const SharedMemory &) = delete;
|
2016-01-13 04:57:05 -05:00
|
|
|
SharedMemory &operator=(const SharedMemory &) = delete;
|
2014-05-07 12:39:16 -04:00
|
|
|
|
|
|
|
template <typename IdentifierT>
|
|
|
|
SharedMemory(const boost::filesystem::path &lock_file,
|
|
|
|
const IdentifierT id,
|
2016-12-25 10:53:10 -05:00
|
|
|
const uint64_t size = 0)
|
2014-05-07 12:39:16 -04:00
|
|
|
: key(lock_file.string().c_str(), id)
|
|
|
|
{
|
2016-10-11 19:09:20 -04:00
|
|
|
// open only
|
2014-05-07 12:39:16 -04:00
|
|
|
if (0 == size)
|
2016-10-11 19:09:20 -04:00
|
|
|
{
|
2014-05-07 12:39:16 -04:00
|
|
|
shm = boost::interprocess::xsi_shared_memory(boost::interprocess::open_only, key);
|
|
|
|
|
2018-04-04 18:56:47 -04:00
|
|
|
util::Log(logDEBUG) << "opening " << (int)shm.get_shmid() << " from id " << (int)id;
|
2016-10-11 19:09:20 -04:00
|
|
|
|
2016-12-25 10:53:10 -05:00
|
|
|
region = boost::interprocess::mapped_region(shm, boost::interprocess::read_only);
|
2014-05-07 12:39:16 -04:00
|
|
|
}
|
2016-10-11 19:09:20 -04:00
|
|
|
// open or create
|
2014-05-07 12:39:16 -04:00
|
|
|
else
|
2016-10-11 19:09:20 -04:00
|
|
|
{
|
2016-05-27 15:05:04 -04:00
|
|
|
shm = boost::interprocess::xsi_shared_memory(
|
|
|
|
boost::interprocess::open_or_create, key, size);
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log(logDEBUG) << "opening/creating " << shm.get_shmid() << " from id " << id
|
|
|
|
<< " with size " << size;
|
2013-11-14 12:33:09 -05:00
|
|
|
#ifdef __linux__
|
2015-08-18 06:56:34 -04:00
|
|
|
if (-1 == shmctl(shm.get_shmid(), SHM_LOCK, nullptr))
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
|
|
|
if (ENOMEM == errno)
|
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log(logWARNING) << "could not lock shared memory to RAM";
|
2014-05-07 12:39:16 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-14 12:33:09 -05:00
|
|
|
#endif
|
2016-12-25 10:53:10 -05:00
|
|
|
region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write);
|
2014-05-07 12:39:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IdentifierT> static bool RegionExists(const IdentifierT id)
|
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
OSRMLockFile lock_file;
|
2018-08-28 21:12:19 -04:00
|
|
|
boost::interprocess::xsi_key key(lock_file(id).string().c_str(), id);
|
2014-05-07 12:39:16 -04:00
|
|
|
result = RegionExists(key);
|
|
|
|
}
|
2015-01-27 11:44:46 -05:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
2014-05-07 12:39:16 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename IdentifierT> static bool Remove(const IdentifierT id)
|
|
|
|
{
|
|
|
|
OSRMLockFile lock_file;
|
2018-08-28 21:12:19 -04:00
|
|
|
boost::interprocess::xsi_key key(lock_file(id).string().c_str(), id);
|
2014-05-07 12:39:16 -04:00
|
|
|
return Remove(key);
|
|
|
|
}
|
|
|
|
|
2017-01-13 16:33:32 -05:00
|
|
|
#ifdef __linux__
|
2017-01-05 17:38:48 -05:00
|
|
|
void WaitForDetach()
|
|
|
|
{
|
|
|
|
auto shmid = shm.get_shmid();
|
|
|
|
::shmid_ds xsi_ds;
|
2017-01-13 10:56:36 -05:00
|
|
|
const auto errorToMessage = [](int error) -> std::string {
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case EPERM:
|
|
|
|
return "EPERM";
|
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
return "ACCESS";
|
|
|
|
break;
|
|
|
|
case EINVAL:
|
|
|
|
return "EINVAL";
|
|
|
|
break;
|
|
|
|
case EFAULT:
|
|
|
|
return "EFAULT";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "Unknown Error " + std::to_string(error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-05 17:38:48 -05:00
|
|
|
do
|
|
|
|
{
|
2017-01-13 16:33:32 -05:00
|
|
|
// On OSX this returns EINVAL for whatever reason, hence we need to disable it
|
2017-01-05 17:38:48 -05:00
|
|
|
int ret = ::shmctl(shmid, IPC_STAT, &xsi_ds);
|
2017-01-13 10:56:36 -05:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-01-13 16:33:32 -05:00
|
|
|
auto error_code = errno;
|
|
|
|
throw util::exception("shmctl encountered an error: " + errorToMessage(error_code) +
|
2017-01-13 10:56:36 -05:00
|
|
|
SOURCE_REF);
|
|
|
|
}
|
2017-01-05 17:38:48 -05:00
|
|
|
BOOST_ASSERT(ret >= 0);
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
|
|
|
} while (xsi_ds.shm_nattch > 1);
|
|
|
|
}
|
2017-01-13 16:33:32 -05:00
|
|
|
#else
|
|
|
|
void WaitForDetach()
|
|
|
|
{
|
2017-03-14 12:39:04 -04:00
|
|
|
util::Log(logDEBUG)
|
2017-01-13 16:33:32 -05:00
|
|
|
<< "Shared memory support for non-Linux systems does not wait for clients to "
|
|
|
|
"dettach. Going to sleep for 50ms.";
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-05 17:38:48 -05:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
private:
|
|
|
|
static bool RegionExists(const boost::interprocess::xsi_key &key)
|
|
|
|
{
|
|
|
|
bool result = true;
|
2015-01-27 11:44:46 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
boost::interprocess::xsi_shared_memory shm(boost::interprocess::open_only, key);
|
|
|
|
}
|
2016-10-11 19:09:20 -04:00
|
|
|
catch (const boost::interprocess::interprocess_exception &e)
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
2016-10-11 19:09:20 -04:00
|
|
|
if (e.get_error_code() != boost::interprocess::not_found_error)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
2015-01-27 11:44:46 -05:00
|
|
|
result = false;
|
|
|
|
}
|
2016-10-11 19:09:20 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool Remove(const boost::interprocess::xsi_key &key)
|
|
|
|
{
|
2016-10-11 19:09:20 -04:00
|
|
|
boost::interprocess::xsi_shared_memory xsi(boost::interprocess::open_only, key);
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log(logDEBUG) << "deallocating prev memory " << xsi.get_shmid();
|
2016-10-11 19:09:20 -04:00
|
|
|
return boost::interprocess::xsi_shared_memory::remove(xsi.get_shmid());
|
2014-05-07 12:39:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::interprocess::xsi_key key;
|
|
|
|
boost::interprocess::xsi_shared_memory shm;
|
|
|
|
boost::interprocess::mapped_region region;
|
2013-09-17 08:23:06 -04:00
|
|
|
};
|
2014-04-07 07:07:27 -04:00
|
|
|
#else
|
|
|
|
// Windows - specific code
|
2014-10-20 04:20:58 -04:00
|
|
|
class SharedMemory
|
2014-04-07 07:07:27 -04:00
|
|
|
{
|
2015-01-27 11:44:46 -05:00
|
|
|
SharedMemory(const SharedMemory &) = delete;
|
2016-01-13 04:57:05 -05:00
|
|
|
SharedMemory &operator=(const SharedMemory &) = delete;
|
2014-04-07 07:07:27 -04:00
|
|
|
|
|
|
|
public:
|
2014-06-11 09:22:51 -04:00
|
|
|
void *Ptr() const { return region.get_address(); }
|
2018-03-26 11:01:48 -04:00
|
|
|
std::size_t Size() const { return region.get_size(); }
|
2014-04-07 07:07:27 -04:00
|
|
|
|
2017-01-05 17:38:48 -05:00
|
|
|
SharedMemory(const boost::filesystem::path &lock_file, const int id, const uint64_t size = 0)
|
2014-04-07 07:07:27 -04:00
|
|
|
{
|
2014-06-11 09:22:51 -04:00
|
|
|
sprintf(key, "%s.%d", "osrm.lock", id);
|
|
|
|
if (0 == size)
|
|
|
|
{ // read_only
|
2014-04-07 07:07:27 -04:00
|
|
|
shm = boost::interprocess::shared_memory_object(
|
2017-01-05 17:38:48 -05:00
|
|
|
boost::interprocess::open_only, key, boost::interprocess::read_only);
|
2016-12-25 19:00:37 -05:00
|
|
|
region = boost::interprocess::mapped_region(shm, boost::interprocess::read_only);
|
2014-06-11 09:22:51 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // writeable pointer
|
2016-05-27 15:05:04 -04:00
|
|
|
shm = boost::interprocess::shared_memory_object(
|
|
|
|
boost::interprocess::open_or_create, key, boost::interprocess::read_write);
|
2014-04-07 07:07:27 -04:00
|
|
|
shm.truncate(size);
|
2016-12-25 19:00:37 -05:00
|
|
|
region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write);
|
2014-04-07 07:07:27 -04:00
|
|
|
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log(logDEBUG) << "writeable memory allocated " << size << " bytes";
|
2014-04-07 07:07:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool RegionExists(const int id)
|
|
|
|
{
|
|
|
|
bool result = true;
|
2014-06-11 09:22:51 -04:00
|
|
|
try
|
2014-04-07 07:07:27 -04:00
|
|
|
{
|
|
|
|
char k[500];
|
|
|
|
build_key(id, k);
|
|
|
|
result = RegionExists(k);
|
|
|
|
}
|
2015-01-27 11:44:46 -05:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
result = false;
|
|
|
|
}
|
2014-04-07 07:07:27 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool Remove(const int id)
|
|
|
|
{
|
|
|
|
char k[500];
|
|
|
|
build_key(id, k);
|
|
|
|
return Remove(k);
|
|
|
|
}
|
|
|
|
|
2017-01-12 17:12:45 -05:00
|
|
|
void WaitForDetach()
|
|
|
|
{
|
|
|
|
// FIXME this needs an implementation for Windows
|
2017-03-14 12:39:04 -04:00
|
|
|
util::Log(logDEBUG) << "Shared memory support for Windows does not wait for clients to "
|
|
|
|
"dettach. Going to sleep for 50ms.";
|
2017-01-13 04:49:02 -05:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
2017-01-12 17:12:45 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 07:07:27 -04:00
|
|
|
private:
|
2015-01-27 11:44:46 -05:00
|
|
|
static void build_key(int id, char *key) { sprintf(key, "%s.%d", "osrm.lock", id); }
|
2014-06-17 09:48:47 -04:00
|
|
|
|
2014-06-11 09:22:51 -04:00
|
|
|
static bool RegionExists(const char *key)
|
2014-04-07 07:07:27 -04:00
|
|
|
{
|
|
|
|
bool result = true;
|
2014-06-11 09:22:51 -04:00
|
|
|
try
|
2014-04-07 07:07:27 -04:00
|
|
|
{
|
2016-05-27 15:05:04 -04:00
|
|
|
boost::interprocess::shared_memory_object shm(
|
|
|
|
boost::interprocess::open_only, key, boost::interprocess::read_write);
|
2015-01-27 11:44:46 -05:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
result = false;
|
2014-04-07 07:07:27 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-06-11 09:22:51 -04:00
|
|
|
static bool Remove(char *key)
|
2014-04-07 07:07:27 -04:00
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log(logDEBUG) << "deallocating prev memory for key " << key;
|
2016-10-11 19:09:20 -04:00
|
|
|
return boost::interprocess::shared_memory_object::remove(key);
|
2014-04-07 07:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char key[500];
|
|
|
|
boost::interprocess::shared_memory_object shm;
|
|
|
|
boost::interprocess::mapped_region region;
|
|
|
|
};
|
|
|
|
#endif
|
2013-09-17 08:23:06 -04:00
|
|
|
|
2016-01-07 13:19:55 -05:00
|
|
|
template <typename IdentifierT, typename LockFileT = OSRMLockFile>
|
2017-01-05 17:38:48 -05:00
|
|
|
std::unique_ptr<SharedMemory> makeSharedMemory(const IdentifierT &id, const uint64_t size = 0)
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
2018-08-28 21:12:19 -04:00
|
|
|
static_assert(sizeof(id) == sizeof(std::uint16_t), "Key type is not 16 bits");
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
try
|
2014-05-07 12:39:16 -04:00
|
|
|
{
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
LockFileT lock_file;
|
2018-08-28 21:12:19 -04:00
|
|
|
if (!boost::filesystem::exists(lock_file(id)))
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
{
|
|
|
|
if (0 == size)
|
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
throw util::exception("lock file does not exist, exiting" + SOURCE_REF);
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-28 21:12:19 -04:00
|
|
|
boost::filesystem::ofstream ofs(lock_file(id));
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
}
|
|
|
|
}
|
2018-08-28 21:12:19 -04:00
|
|
|
return std::make_unique<SharedMemory>(lock_file(id), id, size);
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
}
|
|
|
|
catch (const boost::interprocess::interprocess_exception &e)
|
|
|
|
{
|
2016-12-06 15:30:46 -05:00
|
|
|
util::Log(logERROR) << "Error while attempting to allocate shared memory: " << e.what()
|
|
|
|
<< ", code " << e.get_error_code();
|
|
|
|
throw util::exception(e.what() + SOURCE_REF);
|
2014-05-07 12:39:16 -04:00
|
|
|
}
|
2016-01-07 13:19:55 -05:00
|
|
|
}
|
2018-08-28 21:12:19 -04:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace osrm
|
2016-01-05 10:51:13 -05:00
|
|
|
|
2016-01-07 13:19:55 -05:00
|
|
|
#endif // SHARED_MEMORY_HPP
|