I'm using boost::interprocess::vector to share some strings between processes, and I want to make sure I do not overflow the shared memory segment it lives in.
How do I find how much space the vector takes in memory, and how much memory a special segment-allocated string will take?
typedef boost::interprocess::managed_shared_memory::segment_manager SegmentManager;
typedef boost::interprocess::allocator<char, SegmentManager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> ShmString;
typedef boost::interprocess::allocator<ShmString, SegmentManager> StringAllocator;
typedef boost::interprocess::vector<ShmString, StringAllocator> ShmStringVector;
const size_t SEGMENT_SIZE = ...;
addToSharedVector(std::string localString){
using namespace boost::interprocess;
managed_shared_memory segment(open_only, kSharedMemorySegmentName);
ShmStringVector *shmvector = segment.find<ShmStringVector>(kSharedMemoryVectorName).first;
size_t currentVectorSizeInShm = ?????(shmvector); <-------- HALP!
size_t sizeOfNewStringInSharedMemory = ?????(localString); <--------
//shared mutex not shown for clarity
if (currentVectorSizeInShm + sizeOfNewStringInSharedMemory < SEGMENT_SIZE) {
CharAllocator charAllocator(segment.get_segment_manager());
ShmString shmString(charAllocator);
shmFunctionName = localString.c_str();
shmvector->push_back(shmString);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…