简体   繁体   English

在内存映射文件中存储矢量

[英]Storing vector in memory mapped file

I am attempting to store a vector of arbitrary elements in a memory mapped file (for now I'm trying to succeed with a vector of ints but it should work with vector of arbitrary objects). 我试图在一个内存映射文件中存储一个任意元素的向量(现在我试图用一个int向量成功,但它应该与任意对象的向量一起工作)。 I have found plenty of documentation on doing so with shared memory, but not with memory mapped files proper. 我已经找到了大量关于使用共享内存的文档,但没有找到适当的内存映射文件。 Since I have successfully made and used R-trees in memory mapped file (like in that example ), I tried to replicate the process with vectors, but I guess I am missing some crucial element because it doesn't work. 由于我已经成功地在内存映射文件中制作和使用了R-tree(就像在那个例子中 ),我试图用向量复制过程,但我想我错过了一些关键元素,因为它不起作用。 Here is my code: 这是我的代码:

namespace bi = boost::interprocess;
typedef bi::allocator<std::vector<int>, bi::managed_mapped_file::segment_manager> allocator_vec;
std::string vecFile = "/path/to/my/file/vector.dat";
bi::managed_mapped_file file_vec(bi::open_or_create,vecFile.c_str(), 1000);
allocator_vec alloc_vec(file_vec.get_segment_manager());
std::vector<int> * vecptr = file_vec.find_or_construct<std::vector<int> >("myvector")(alloc_vec);

Probably my last line is wrong, because "alloc_vec" is passed as an argument to the vector constructor, which doesn't expect it (I get among others the error /usr/include/c++/4.8/bits/stl_vector.h:248:7: note: candidate expects 0 arguments, 1 provided ). 可能我的最后一行是错误的,因为“alloc_vec”作为参数传递给向量构造函数,它不期望它(我得到的错误是/usr/include/c++/4.8/bits/stl_vector.h:248:7: note: candidate expects 0 arguments, 1 provided )。 However, I don't know then how to pass the allocator to find_or_construc(), which I assume is crucial for the vector to be properly created in the memory mapped file. 但是,我不知道如何将分配器传递给find_or_construc(),我认为这对于在内存映射文件中正确创建向量至关重要。 Removing (alloc_vec) at the end of the last line leads to another error that I have more trouble to solve : 在最后一行的末尾删除(alloc_vec)导致另一个错误,我(alloc_vec)解决:

error: cannot convert ‘boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>::construct_proxy<std::vector<int> >::type {aka boost::interprocess::ipcdetail::named_proxy<boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>, std::vector<int>, false>}’ to ‘std::vector<int>*’ in initialization
std::vector<int> * vecptr = file_vec.find_or_construct<std::vector<int> >("myvector");

Any help will be greatly appreciated.` 任何帮助将不胜感激

Like the samples show, tell the vector class about your custom allocator, so instead of 就像示例展示一样,告诉vector类关于你的自定义分配器,而不是

typedef std::vector<int>  MyVec;
MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(alloc_vec);

Use 采用

typedef bi::allocator<int, bi::managed_mapped_file::segment_manager> int_alloc;
typedef std::vector<int, int_alloc>  MyVec;

int_alloc alloc(file_vec.get_segment_manager());
MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(alloc);

Note that 注意

  • vector uses an allocator for the element types (not for the vector; segment_manager allocates that) vector使用分配器作为元素类型(不适用于vector; segment_manager分配)
  • since the constructor for allocator<> is implicit, you can also just pass the segment_manager : 因为allocator<>的构造函数是隐式的,所以你也可以只传递segment_manager

Live On Coliru 住在Coliru

#include <boost/interprocess/managed_mapped_file.hpp>

namespace bi = boost::interprocess;

int main() {
    std::string vecFile = "vector.dat";
    bi::managed_mapped_file file_vec(bi::open_or_create,vecFile.c_str(), 1000);

    typedef bi::allocator<int, bi::managed_mapped_file::segment_manager> int_alloc;
    typedef std::vector<int, int_alloc>  MyVec;

    MyVec * vecptr = file_vec.find_or_construct<MyVec>("myvector")(file_vec.get_segment_manager());

    vecptr->push_back(rand());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM