Init
This commit is contained in:
commit
950e7b7e2d
350 files changed
+38599
No files matched your search
@@ -0,0 +1,201 @@
|
||||
#ifndef NO_CPP0X
|
||||
|
||||
#include "SharedMemory.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/logging/logging.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#include <boost/interprocess/shared_memory_object.hpp>
|
||||
#include <boost/interprocess/mapped_region.hpp>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace boost::interprocess;
|
||||
|
||||
namespace bts {
|
||||
namespace utils {
|
||||
|
||||
struct BoostLowLevelSharedMemoryImpl;
|
||||
class BoostLowLevelSharedMemory : public LowLevelSharedMemory
|
||||
{
|
||||
std::unique_ptr<BoostLowLevelSharedMemoryImpl> _me;
|
||||
public:
|
||||
BoostLowLevelSharedMemory();
|
||||
|
||||
// Inherited via LowLevelSharedMemory
|
||||
virtual Segment create(const std::string & name, size_t size) override;
|
||||
virtual const Segment open(const std::string& name, bool read_only) override;
|
||||
virtual bool release(const std::string & name) override;
|
||||
};
|
||||
|
||||
struct PrivateSharedMemoryImpl;
|
||||
struct RcSegment
|
||||
{
|
||||
RcSegment() : name(""), size(0), buffer() {}
|
||||
RcSegment(string n, size_t s) :
|
||||
name(n), size(s), buffer(make_shared<vector<byte>>(s))
|
||||
{}
|
||||
string name;
|
||||
size_t size;
|
||||
std::shared_ptr<vector<byte>> buffer;
|
||||
byte* data() const { return buffer ? &(*buffer)[0] : nullptr; }
|
||||
|
||||
operator Segment() const {
|
||||
Segment s = { size, data() };
|
||||
return s;
|
||||
}
|
||||
};
|
||||
class PrivateSharedMemory : public LowLevelSharedMemory
|
||||
{
|
||||
vector<pair<bool,RcSegment>> _segments;
|
||||
public:
|
||||
PrivateSharedMemory();
|
||||
~PrivateSharedMemory();
|
||||
|
||||
// Inherited via LowLevelSharedMemory
|
||||
virtual Segment create(const std::string & name, size_t size) override;
|
||||
virtual const Segment open(const std::string& name, bool read_only) override;
|
||||
virtual bool release(const std::string & name) override;
|
||||
};
|
||||
|
||||
/*
|
||||
* Boot shared memory impl
|
||||
*/
|
||||
|
||||
Segment make_segment(size_t size, void* ptr) {
|
||||
Segment s = {size, (byte*)ptr };
|
||||
return s;
|
||||
}
|
||||
|
||||
struct BoostLowLevelSharedMemoryImpl {
|
||||
BoostLowLevelSharedMemoryImpl() :
|
||||
_mappings()
|
||||
{ }
|
||||
|
||||
void add_region(shared_memory_object block) {
|
||||
mapped_region region(block, block.get_mode());
|
||||
_mappings.push_back(make_pair(move(block), move(region)));
|
||||
}
|
||||
|
||||
Segment create(const string & name, size_t size) {
|
||||
try {
|
||||
shared_memory_object block(create_only, name.c_str(), read_write);
|
||||
block.truncate(size);
|
||||
add_region(move(block));
|
||||
return make_segment(size, _mappings.back().second.get_address());
|
||||
}
|
||||
catch (...) {
|
||||
return make_segment(0, nullptr);
|
||||
}
|
||||
}
|
||||
Segment open(const string& name, bool ro) {
|
||||
try {
|
||||
add_region(shared_memory_object(open_only, name.c_str(), ro ? read_only : read_write));
|
||||
return make_segment(_mappings.back().second.get_size(), _mappings.back().second.get_address());
|
||||
}
|
||||
catch (...) {
|
||||
return make_segment(0, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
bool release(const string& segment_name) {
|
||||
_mappings.erase(
|
||||
remove_if(_mappings.begin(), _mappings.end(), [&](const pair<shared_memory_object, mapped_region>& p) {
|
||||
return p.first.get_name() == segment_name;
|
||||
}), _mappings.end());
|
||||
LOG(MarketData,Diag, "Remove shared memory segment: {}" , std::string(segment_name));
|
||||
return boost::interprocess::shared_memory_object::remove(segment_name.c_str());
|
||||
}
|
||||
vector<pair<shared_memory_object, mapped_region>> _mappings;
|
||||
};
|
||||
|
||||
BoostLowLevelSharedMemory::BoostLowLevelSharedMemory() :
|
||||
_me(std::make_unique<BoostLowLevelSharedMemoryImpl>())
|
||||
{}
|
||||
Segment utils::BoostLowLevelSharedMemory::create(const string & name, size_t size) {
|
||||
return _me->create(name, size);
|
||||
}
|
||||
const Segment BoostLowLevelSharedMemory::open(const std::string & name, bool read_only) {
|
||||
return _me->open(name, read_only);
|
||||
}
|
||||
bool utils::BoostLowLevelSharedMemory::release(const string & name) {
|
||||
return _me->release(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* Non-shared memory implementation
|
||||
*/
|
||||
struct PrivateSharedMemoryImpl {
|
||||
PrivateSharedMemoryImpl() :
|
||||
_mutex(), _mallocs()
|
||||
{}
|
||||
std::mutex _mutex;
|
||||
map<string, RcSegment> _mallocs;
|
||||
|
||||
RcSegment create(const string & name, size_t size) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
RcSegment segment(name, size);
|
||||
// If an old segment existed it is discarded
|
||||
_mallocs[name] = segment;
|
||||
return segment;
|
||||
}
|
||||
RcSegment open(const string& name, bool) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto find_result = _mallocs.find(name);
|
||||
if (find_result != _mallocs.end()) {
|
||||
return find_result->second;
|
||||
}
|
||||
return RcSegment();
|
||||
}
|
||||
bool release(const string& name) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
auto find_result = _mallocs.find(name);
|
||||
if (find_result != _mallocs.end()) {
|
||||
_mallocs.erase(name);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static PrivateSharedMemoryImpl& instance() {
|
||||
static PrivateSharedMemoryImpl impl;
|
||||
return impl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
PrivateSharedMemory::PrivateSharedMemory() : _segments() {}
|
||||
PrivateSharedMemory::~PrivateSharedMemory() {
|
||||
for (auto& pair : _segments) {
|
||||
bool owned = pair.first;
|
||||
if (owned) {
|
||||
PrivateSharedMemoryImpl::instance().release(pair.second.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Segment PrivateSharedMemory::create(const string & name, size_t size) {
|
||||
auto result = PrivateSharedMemoryImpl::instance().create(name, size);
|
||||
if (result.buffer) _segments.push_back(make_pair(true, result));
|
||||
return (Segment)result;
|
||||
}
|
||||
const Segment PrivateSharedMemory::open(const std::string & name, bool read_only) {
|
||||
auto result = PrivateSharedMemoryImpl::instance().open(name, read_only);
|
||||
if (result.buffer) _segments.push_back(make_pair(false, result));
|
||||
return (Segment)result;
|
||||
}
|
||||
bool PrivateSharedMemory::release(const string & name) {
|
||||
return PrivateSharedMemoryImpl::instance().release(name);
|
||||
}
|
||||
|
||||
std::unique_ptr<LowLevelSharedMemory> create_low_level_shared_memory(bool shared) {
|
||||
return unique_ptr<LowLevelSharedMemory>(shared ?
|
||||
static_cast<LowLevelSharedMemory*>(new BoostLowLevelSharedMemory()) :
|
||||
static_cast<LowLevelSharedMemory*>(new PrivateSharedMemory()));
|
||||
}
|
||||
|
||||
}}//namespace
|
||||
#endif
|
||||
Reference in new issue
Block a user