0

I am working with boost multi index in shared memory

I create a struct SrHpackHeaderElement that contain shared strings

struct SrHpackHeaderElement 
{
    SrHpackHeaderElement(char_allocator* pCharAllocator):m_sName("",pCharAllocator),m_sValue("",pCharAllocator){}
    
    shm_string m_sName;
    shm_string m_sValue;
};

The same struct is used in a shared vector

typedef bip::managed_shared_memory::allocator<SrHpackHeaderElement>::type HpackHeader_allocator;
typedef bip::allocator<SrHpackHeaderElement, HpackHeader_allocator>  HpackHeader_ShmemAllocator;
typedef bip::vector<SrHpackHeaderElement,  HpackHeader_ShmemAllocator> HpackTableType;

struct CrHttp2HpackValue
{
    HpackTableType m_table;

    CrHttp2HpackValue(HpackHeader_ShmemAllocator * pHpackHeaderShmemAllocator,char_allocator* pCharAllocator):m_table(pHpackHeaderShmemAllocator)
    {
    }
};

I want to pass the allocator to the struct constructor CrHttp2HpackValue, and to initialize the shared/interprocess vector Is this possible?

I put the code in the following link https://coliru.stacked-crooked.com/a/46b96c0a70a165b9

Hemda
  • 1

1 Answers1

0

I think the code is excessively complicated due to some specifics of C++ and allocators you're seemingly not taking advantage of:

  • Many (most) objects in C++ are designed to be constructed in the stack (i.e. without new) and passed around by ref or value, in particular this is the case for segment managers and allocators (even though they're managing memory that is definitely not in the stack, but this they deal with internally).
  • If you have an object x of type whatever_allocator<T> and need to construct an object y of type whatever_allocator<Q> you can simply write whatever_allocator<Q> y = x. That is, allocators out of the same class template are interconvertible.

With these features in mind, the code can we just rewritten as follows:

Live Coliru Demo

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/fusion/include/pair.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/bind/bind.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/creation_tags.hpp>

namespace bip=boost::interprocess;

typedef bip::managed_shared_memory::allocator<char>::type char_allocator;
typedef bip::basic_string<char, std::char_traits<char>, char_allocator>shm_string;

using bip::shared_memory_object;
using bip::managed_shared_memory;
using bip::open_or_create;
using namespace boost::container;

struct SrHpackHeaderElement 
{
    SrHpackHeaderElement(char_allocator charAllocator):m_sName("",charAllocator),m_sValue("",charAllocator){}
    
    shm_string m_sName;
    shm_string m_sValue;
};
typedef bip::managed_shared_memory::allocator<SrHpackHeaderElement>::type HpackHeader_ShmemAllocator;
typedef bip::vector<SrHpackHeaderElement,  HpackHeader_ShmemAllocator> HpackTableType;

struct CrHttp2HpackValue
{
    HpackTableType m_table;

    CrHttp2HpackValue(HpackHeader_ShmemAllocator hpackHeaderShmemAllocator):m_table(hpackHeaderShmemAllocator)
    {
    }
};

int main()
{
    //shared mem
    bip::managed_shared_memory segment_shared_mem( bip::create_only, "MySharedMem"  , 1000000);
    
    HpackHeader_ShmemAllocator hpackAlloc=segment_shared_mem.get_allocator<SrHpackHeaderElement>();
    
    //create a sing element;
    SrHpackHeaderElement element(hpackAlloc);
    
    CrHttp2HpackValue value(hpackAlloc);
}
Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20