Card in shared memory

I am trying to create unordered_map in shared memory. I use a dispenser for the purpose of the server.

Code

void *addr; void *pool; int shmid; template<class T> class MyPoolAlloc { private: public: typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; template<class X> struct rebind { typedef MyPoolAlloc<X> other; }; MyPoolAlloc() throw() { } MyPoolAlloc(const MyPoolAlloc&) throw() { } template<class X> MyPoolAlloc(const MyPoolAlloc<X>&) throw() { } ~MyPoolAlloc() throw() { } pointer address(reference __x) const { return &__x; } const_pointer address(const_reference __x) const { return &__x; } pointer allocate(size_type __n, const void * hint = 0) { pointer tmp = static_cast<T*>(addr); addr = (void*)((char*)addr + __n); return tmp; } void deallocate(pointer __p, size_type __n) { // pMyPool->Free(reinterpret_cast<void *>(__p)); } size_type max_size() const throw() { //return size_t(-1) / sizeof(T); } void construct(pointer __p, const T& __val) { ::new(__p) T(__val); } void destroy(pointer __p) { //__p->~T(); } }; typedef std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, MyPoolAlloc<std::pair<const int,int>> > Map; int main () { shmid = shmget(shm_key, 1000, IPC_CREAT | IPC_EXCL | 644); if(shmid == -1){ std::cerr << "Failed to create the shared segment." << strerror(errno)<< std::endl; exit(-1); } addr = shmat(shmid, NULL, 0); pool = addr; if(addr == (void*)-1){ std::cerr << "Failed to attach the segment to the process." << std::endl; shmctl(shmid, IPC_RMID, 0); exit(-1); } Map *m = new(pool) Map; m->insert(std::pair<int, int>(2,4)); shmdt(addr); shmctl(shmid, IPC_RMID, 0); return 0; } 

I allocate memory for the card at the shared memory address. And I believe that the allocate function of the allocator () class will be used for elements. But I can’t understand how to use the allocate function to allocate memory for map elements. I get an arithmetic exception on the line where I am trying to insert on a card.

Can someone help to understand what a memory layout should look like?

Thanks.

+4
source share
1 answer

As I said the last time you asked about this , you cannot store pointers in shared memory and expect that they can be used in several processes. As before, I suggest using boost::unordered_map in combination with boost::interprocess::allocator .

If you really want to write your own allocator, you will need some kind of custom pointer that stores the offset in the shared memory block and can recover the correct address from this. I would not recommend trying this on your own: since it is quite complicated, you may find that some implementations of std::unordered_map do not work correctly with non-traditional pointer types. In fact, I suspect that it may not be possible to create a standard compatible allocator for shared memory; otherwise, the Boost dispenser can certainly be used by all standard containers, and not just special versions of Boost.

By the way, you should not use reserved names, for example __p (containing double underscores) in your own code; they should only be used when implementing the standard library.

+6
source

All Articles