add ability to _not_ destroy prev allocated memory

This commit is contained in:
Dennis Luxen 2013-10-25 14:00:34 +02:00
parent 86433ab3a8
commit 3e63b14192

View File

@ -82,7 +82,9 @@ public:
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,
bool read_write = false,
bool remove_prev = true
) : key( ) : key(
lock_file.string().c_str(), lock_file.string().c_str(),
id id
@ -94,13 +96,19 @@ public:
); );
region = boost::interprocess::mapped_region ( region = boost::interprocess::mapped_region (
shm, shm,
boost::interprocess::read_only (
read_write ?
boost::interprocess::read_write :
boost::interprocess::read_only
)
); );
} else { //writeable pointer } else { //writeable pointer
//remove previously allocated mem //remove previously allocated mem
RemoveSharedMemory(key); if( remove_prev ) {
Remove(key);
}
shm = boost::interprocess::xsi_shared_memory ( shm = boost::interprocess::xsi_shared_memory (
boost::interprocess::create_only, boost::interprocess::open_or_create,
key, key,
size size
); );
@ -115,24 +123,28 @@ public:
} }
} }
void Swap(SharedMemory & other) { // void Swap(SharedMemory & other) {
SimpleLogger().Write() << "prev: " << shm.get_shmid(); // SimpleLogger().Write() << "&prev: " << shm.get_shmid();
shm.swap(other.shm); // shm.swap(other.shm);
region.swap(other.region); // region.swap(other.region);
boost::interprocess::xsi_key temp_key = other.key; // boost::interprocess::xsi_key temp_key = other.key;
other.key = key; // other.key = key;
key = temp_key; // key = temp_key;
SimpleLogger().Write() << "after: " << shm.get_shmid(); // SimpleLogger().Write() << "&after: " << shm.get_shmid();
} // }
void Swap(SharedMemory * other) { void Swap(SharedMemory * other) {
SimpleLogger().Write() << "prev: " << shm.get_shmid(); SimpleLogger().Write() << "this key: " << key.get_key() << ", other: " << other->key.get_key();
SimpleLogger().Write() << "this id: " << shm.get_shmid() << ", other: " << other->shm.get_shmid();
shm.swap(other->shm); shm.swap(other->shm);
region.swap(other->region); region.swap(other->region);
boost::interprocess::xsi_key temp_key = other->key; boost::interprocess::xsi_key temp_key = other->key;
other->key = key; other->key = key;
key = temp_key; key = temp_key;
SimpleLogger().Write() << "after: " << shm.get_shmid(); SimpleLogger().Write() << "swap done";
SimpleLogger().Write() << "this key: " << key.get_key() << ", other: " << other->key.get_key();
SimpleLogger().Write() << "this id: " << shm.get_shmid() << ", other: " << other->shm.get_shmid();
} }
template<typename IdentifierT > template<typename IdentifierT >
@ -151,12 +163,12 @@ public:
} }
template<typename IdentifierT > template<typename IdentifierT >
static void RemoveSharedMemory( static void Remove(
const IdentifierT id const IdentifierT id
) { ) {
OSRMLockFile lock_file; OSRMLockFile lock_file;
boost::interprocess::xsi_key key( lock_file().string().c_str(), id ); boost::interprocess::xsi_key key( lock_file().string().c_str(), id );
RemoveSharedMemory(key); Remove(key);
} }
private: private:
@ -173,7 +185,7 @@ private:
return result; return result;
} }
static void RemoveSharedMemory( static void Remove(
const boost::interprocess::xsi_key &key const boost::interprocess::xsi_key &key
) { ) {
try{ try{
@ -199,10 +211,21 @@ private:
template<class LockFileT = OSRMLockFile> template<class LockFileT = OSRMLockFile>
class SharedMemoryFactory_tmpl : boost::noncopyable { class SharedMemoryFactory_tmpl : boost::noncopyable {
public: public:
template<typename IdentifierT >
static void Swap(const IdentifierT & id1, const IdentifierT & id2) {
SharedMemory * memory_1 = Get(id1);
SharedMemory * memory_2 = Get(id2);
memory_1->Swap(memory_2);
}
template<typename IdentifierT > template<typename IdentifierT >
static SharedMemory * Get( static SharedMemory * Get(
const IdentifierT & id, const IdentifierT & id,
const unsigned size = 0 const unsigned size = 0,
bool read_write = false,
bool remove_prev = true
) { ) {
try { try {
LockFileT lock_file; LockFileT lock_file;
@ -214,7 +237,7 @@ public:
ofs.close(); ofs.close();
} }
} }
return new SharedMemory(lock_file(), id, size); return new SharedMemory(lock_file(), id, size, read_write, remove_prev);
} catch(const boost::interprocess::interprocess_exception &e){ } catch(const boost::interprocess::interprocess_exception &e){
SimpleLogger().Write(logWARNING) << SimpleLogger().Write(logWARNING) <<
"caught exception: " << e.what() << "caught exception: " << e.what() <<