更好地设计SharedMemory分配器

时间:2014-09-14 22:41:16

标签: c++

gcc中的分配器是不可移动的。它们仅在我与std::vector一起使用时才会被复制。所以我设计了分配器以保持"引用计数"设计何时应删除/取消映射内存。

另一个问题是我必须手动将发送矢量的大小存储到内存中,以便接收矢量可以计算出分配的数量。

我所拥有的内容如下:

#include <windows.h>
#include <cstdint>
#include <new>
#include <iostream>
#include <stdexcept>


template<typename T>
class SHMAllocator
{
    private:    
        MemoryMap info; //structure..
        MemoryMap** refptr; //needed so the destructor doesn't delete when copying this allocator..

        void* CreateMemoryMap(MemoryMap* info, const char* MapName, unsigned int size);
        void* OpenMemoryMap(MemoryMap* info, const char* MapName, unsigned int size);
        void CloseMemoryMap(MemoryMap* data);


    public:
        typedef T* pointer;
        typedef const T* const_pointer;

        typedef T& reference;
        typedef const T& const_reference;

        typedef size_t size_type;
        typedef ptrdiff_t difference_type;

        typedef T value_type;


        SHMAllocator() noexcept : info{0} {}

        SHMAllocator(const char* name, size_type max_size_in_bytes) noexcept : info{0}
        {
            MemoryMap* m = &info;
            refptr = &m; //not sure if legal. Taking address of local var "m".

            if (name && !OpenMemoryMap(&info, name, max_size_in_bytes * sizeof(T)))
            {
                if (!CreateMemoryMap(&info, name, max_size_in_bytes * sizeof(T)))
                {
                    throw std::bad_alloc();
                }
            }
        };

        ~SHMAllocator() {CloseMemoryMap(*refptr);} //if (*refptr) != NULL, unmap the memory.

        template<typename U>
        SHMAllocator(const SHMAllocator<U>& other) noexcept {};

        //turn the copy constructor into a move constructor with refptr..
        SHMAllocator(const SHMAllocator &other) : info(other.info), refptr(other.refptr) {*other.refptr = NULL;}

        template<typename U>
        struct rebind {typedef SHMAllocator<U> other;};

        pointer allocate(size_type n, const void* hint = 0) {return static_cast<pointer>(info.pData);}
        void deallocate(void* ptr, size_type n) {}

        size_type max_size() const {return info.size;}
};


//implementation of the open, create, close functions here..



/** Test case **/
#include <vector>
#include <iostream>
#include <stdexcept>

int main()
{
    /** Sender **/
    std::vector<int, SHMAllocator<int>> shared_sender_vector(SHMAllocator<int>("Local//Map", 100));
    shared_sender_vector.push_back(10); //add the size of this vector to the memory.

    for (int i = 0; i < 10; ++i)
    {
        shared_sender_vector.push_back(i + 1);
    }



    /** Receiver **/
    std::vector<int, SHMAllocator<int>> shared_receiver_vector(SHMAllocator<int>("Local//Map", 100));
    shared_receiver_vector.reserve(1); //get the size.
    shared_receiver_vector.reserve(shared_receiver_vector.front()); //reserve "size" amount.

    for (int i = 1; i < shared_receiver_vector.capacity(); ++i)
    {
        std::cout<<shared_receiver_vector[i]<<" ";
    }
}

有没有更好的方法来设计上面的分配器(第一优先级)并可能自动将大小写入内存(不重要但很好)?

0 个答案:

没有答案