共享内存中的映射 [英] Map in Shared memory

查看:62
本文介绍了共享内存中的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在共享内存中创建unordered_map.我正在使用分配器来达到目的.

I am trying to create an unordered_map in shared memory. I am using allocator to server the purpose.

代码

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;
}

我正在为共享内存地址上的映射分配内存.我猜想对于元素,将使用分配器类的allocate()函数.但是我不知道如何使用分配功能为地图的元素分配内存.我要在地图中插入的行上出现算术异常.

I am allocating memory for the map on shared memory address. And I suppose for the elements the allocator class' allocate() function will be used. But I am not able to figure out how to use allocate function to allocate memory for the elements of the map. I am getting Arithmetic Exception at the line where I am trying to insert in the map.

有人可以帮助您了解内存布局如何吗?

Could someone please help to understand how the memory layout should be?

谢谢.

推荐答案

正如我所说的上次您问这个问题,您不能将指针存储在共享内存中并期望它们可以在多个进程中使用.和以前一样,我建议使用

As I said last time you asked about this, you can't store pointers in shared memory and expect them to be usable in more than one process. As before, I suggest using boost::unordered_map in conjunction with boost::interprocess::allocator.

如果您确实想编写自己的分配器,则需要某种自定义指针,该指针将偏移量存储到共享内存块中,并能够从中重建正确的地址.我不建议您自己尝试这样做:而且很奇怪,您可能会发现 std :: unordered_map 的某些实现不适用于非常规指针类型.实际上,我怀疑不可能为共享内存创建一个符合标准的分配器.否则,Boost分配器肯定会在所有标准容器中使用,而不仅仅是特殊的Boost版本.

If you really want to write your own allocator, you'll need some kind of custom pointer that stores the offset into the shared memory block, and is able to reconstruct the correct address from that. I wouldn't recommend trying to do this yourself: as well as it being quite fiddly, you may find that some implementations of std::unordered_map don't work correctly with unconventional pointer types. In fact, I suspect that it might be impossible to make a standard-compliant allocator for shared memory; otherwise, the Boost allocator would surely be usable by all standard containers, not just special Boost versions.

顺便说一句,您不应在自己的代码中使用诸如 __ p 之类的保留名称(包含双下划线).它们只能由标准库实现使用.

By the way, you shouldn't use reserved names like __p (containing a double underscore) in your own code; they should only be used by the Standard Library implementation.

这篇关于共享内存中的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆