make shmem swappable by ref and ptr

This commit is contained in:
Dennis Luxen 2013-10-08 15:26:19 +02:00
parent cf7e107ad1
commit 98f9f0cd48

View File

@ -67,53 +67,73 @@ class SharedMemory : boost::noncopyable {
}; };
public: public:
void * Ptr() const { void * Ptr() const {
return region.get_address(); return region.get_address();
} }
template<typename IdentifierT > template<typename IdentifierT >
SharedMemory( SharedMemory(
const boost::filesystem::path & lock_file, const boost::filesystem::path & lock_file,
const IdentifierT id, const IdentifierT id,
const unsigned size = 0 const unsigned size = 0
) : key( ) : key(
lock_file.string().c_str(), lock_file.string().c_str(),
id id
) { ) {
if( 0 == size ){ //read_only if( 0 == size ){ //read_only
shm = boost::interprocess::xsi_shared_memory ( shm = boost::interprocess::xsi_shared_memory (
boost::interprocess::open_only, boost::interprocess::open_only,
key key
); );
region = boost::interprocess::mapped_region ( region = boost::interprocess::mapped_region (
shm, shm,
boost::interprocess::read_only boost::interprocess::read_only
); );
} else { //writeable pointer } else { //writeable pointer
//remove previously allocated mem //remove previously allocated mem
RemoveSharedMemory(key); RemoveSharedMemory(key);
shm = boost::interprocess::xsi_shared_memory ( shm = boost::interprocess::xsi_shared_memory (
boost::interprocess::create_only, boost::interprocess::create_only,
key, key,
size size
); );
region = boost::interprocess::mapped_region ( region = boost::interprocess::mapped_region (
shm, shm,
boost::interprocess::read_write boost::interprocess::read_write
); );
remover.SetID( shm.get_shmid() ); remover.SetID( shm.get_shmid() );
SimpleLogger().Write(logDEBUG) << SimpleLogger().Write(logDEBUG) <<
"writeable memory allocated " << size << " bytes"; "writeable memory allocated " << size << " bytes";
} }
} }
void Swap(SharedMemory & other) {
SimpleLogger().Write() << "prev: " << shm.get_shmid();
shm.swap(other.shm);
region.swap(other.region);
boost::interprocess::xsi_key temp_key = other.key;
other.key = key;
key = temp_key;
SimpleLogger().Write() << "after: " << shm.get_shmid();
}
void Swap(SharedMemory * other) {
SimpleLogger().Write() << "prev: " << shm.get_shmid();
shm.swap(other->shm);
region.swap(other->region);
boost::interprocess::xsi_key temp_key = other->key;
other->key = key;
key = temp_key;
SimpleLogger().Write() << "after: " << shm.get_shmid();
}
template<typename IdentifierT > template<typename IdentifierT >
static bool RegionExists( static bool RegionExists(
const boost::filesystem::path & lock_file,
const IdentifierT id const IdentifierT id
) { ) {
boost::interprocess::xsi_key key( lock_file.string().c_str(), id ); OSRMLockFile lock_file;
boost::interprocess::xsi_key key( lock_file().string().c_str(), id );
return RegionExists(key); return RegionExists(key);
} }