std :: vector是否满足Boost.Interprocess分配器的容器要求?

Gab*_*ton 8 c++ vector boost-interprocess gcc4.7

boost::interprocess文档中,据说要将容器存储在共享内存中:

  1. STL容器可能不会假定分配器分配的内存可以与其他相同类型的分配器一起释放.只有当分配了一个对象的内存可以与另一个对象解除分配时,所有分配器对象必须比较相等,并且这只能operator==()在运行时进行测试.
  2. 容器的内部指针应该是类型allocator::pointer,容器可能不会假定allocator::pointer是原始指针.
  3. 必须通过allocator::constructallocator::destroy函数构造所有对象.

我正在使用gcc 4.7.1和-std = c ++ 11(和boost 1.53).使用下面定义的ShmVector类型是否安全?

typedef boost::interprocess::allocator<int,
    boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;
Run Code Online (Sandbox Code Playgroud)

我尝试了一个使用这种类型的虚拟进程,看起来它正在工作,但我仍然不确定gcc4.7.1中的向量是否满足所有要求.我对第一个要求特别不确定.

#include <iostream>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <vector>
#include <cstdlib> //std::system

typedef boost::interprocess::allocator<int,
        boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef std::vector<int, ShmemAllocator> ShmVector;

int main(int argc, char *argv[])
{
    if(argc == 1){ //Parent process

        struct shm_remove
        {
            shm_remove() { boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
            ~shm_remove(){ boost::interprocess::shared_memory_object::remove("MySharedMemory"); }
        } remover;

        //Create a new segment with given name and size
        boost::interprocess::managed_shared_memory segment(boost::interprocess::create_only,
                "MySharedMemory", 65536);

        //Initialize shared memory STL-compatible allocator
        const ShmemAllocator allocator(segment.get_segment_manager());

        ShmVector* v = segment.construct<ShmVector>("ShmVector")(allocator);
        v->push_back(1); v->push_back(2); v->push_back(3);

        //Launch child process
        std::string s(argv[0]); s += " child ";
        if(0 != std::system(s.c_str()))
            return 1;

    } else { // Child process

        //Open the managed segment
        boost::interprocess::managed_shared_memory segment(
                boost::interprocess::open_only, "MySharedMemory");

        //Find the vector using the c-string name
        ShmVector *v = segment.find<ShmVector>("ShmVector").first;

        for (const auto& i : *v) {
            std::cout << i << " ";
        }
        std::cout << std::endl;

    }
}
Run Code Online (Sandbox Code Playgroud)

Art*_*kov 0

在 C++ 11 分配器规则中略有变化,但我认为它不会影响你的问题。

您可能想首先了解标准对此有何规定。但您实际上想要检查您的特定 STL 实现是否符合标准并且不包含错误。

对于第二部分,我强烈建议您查看来源并进行检查,实际上并不那么难。

另外,您可以编写测试来查看它是否确实正常工作:

  • 创建自定义分配器:
    • 使用一些自定义类型作为指针,const指针;
    • 在 中construct()destruct()统计调用次数;
  • 创建YourCustomType与分配器一起使用,分配器还计算构造/销毁的数量。
  • 现在,创建std::vetor<YourCustomType, YourCustomAllocator<YourCustomType>>实例,插入一些元素,清除向量,销毁它,看看是否:
    • 调用次数construct() destruct()等于 的构造破坏次数YourCustomType
    • typeid(YourCustomAllocator::pointer) == typeid(std::vetor<YourCustomType, YourCustomAllocator<YourCustomType>>::pointer)

这样您就可以确保所有限制均适用。

至于问题的第一部分,这是一个旧的 C++ 标准(不是 C++ 11)。

1(正确实现的)向量不可能凭空获取分配器。它将使用您提供的任何分配器,并将其用于所有事情。至于operator==,它是在boost的分配器中实现的,因此让operator==按要求工作是boost的问题。尽管我无法在标准中找到确认。

2除非有错误,否则std::vector<T, YourAllocator>::pointer应该是分配器的指针。cppreference.com 是这样说的,标准也是这样说的(查找“模板类向量”):

    typedef typename Allocator::pointer               pointer;
    typedef typename Allocator::const_pointer         const_pointer;
Run Code Online (Sandbox Code Playgroud)

尽管同一标准对分配器也有这样的规定:本国际标准中描述的容器的实现允许假设它们的分配器模板参数除了表 6 中的要求外还满足以下两个附加要求。

--给定分配器类型的所有实例都必须是可互换的,并且彼此比较时始终相等。

--typedef 成员指针、const_pointer、size_type 和 Difference_type 要求分别为 T*、T const*、size_t 和 ptrdiff_t。

因此,实际上标准不允许使用任何指针类型,但我的猜测是实际的 STL 实现会起作用。

3只需检查std::vector<T>::clear()方法实现,看看是否调用了 allocator::destroy。检查std::vector<T>::resize()方法的实现以查看是否使用了 allocator::construct。我在标准中找不到调用 destroy 和 construct 的要求。