简体   繁体   English

从函数返回boost :: interprocess内存映射文件?

[英]Returning boost::interprocess memory-mapped file from function?

How can I put this code in to a function so that I pass a file path and it returns the file-mapped bytes in to a char array? 如何将这段代码放入函数中,以便传递文件路径,并将文件映射的字节返回到char数组中? Whenever I have tried I can never read the contents once the function finishes? 每当尝试完成功能后,就永远无法读取内容?

using boost::interprocess;
using boost;

boost::shared_ptr<char> getBytes(const char* FilePath){
    shared_ptr<file_mapping> fm(new file_mapping(FilePath, read_only));
    shared_ptr<mapped_region> region(new mapped_region(*fm, read_only));
    shared_ptr<char> bytes(static_cast<char*>(region->get_address()));
    return bytes;
}

You probably need to go about your objective quite differently! 您可能需要以完全不同的方式实现目标! Clearly you don't want to just delete the pointer to the memory mapped array which is what the boost::shared_ptr<char> initialized with the pointer to the base address would do. 显然,你不想只是delete指针,内存映射数组是什么boost::shared_ptr<char>与指针的基地址会做初始化。 In fact, you probably should not release that pointer at all. 实际上,您可能根本应该释放该指针。

The other two objects you create will go out of scope when getBytes() is exited but these are the objects which actually cling to the shared memory. 当退出getBytes()时,您创建的其他两个对象将超出范围,但这些对象实际上紧贴着共享内存。 What you might want to do is to put the file_mapping and the mapped_region together into an object which is put into the returned boost::shared_ptr<char> as a deleter object: this way these two objects would live long enough to keep the pointed to array alive. 可能想要做的是将file_mappingmapped_region放到一个对象中,该对象作为删除器对象放入返回的boost::shared_ptr<char>中:这样,这两个对象将活得足够长以保持指向阵列还活着。 Upon the deleter function being called the two objects would be released. 调用deleter函数后,将释放两个对象。 Here is how this would roughly look like although I haven't checked whether these are indeed the correct interfaces: 尽管我没有检查这些接口是否确实是正确的接口,但这是大致的样子:

struct shared_memory_deleter
{
    shared_memory_deleter(char const* file)
        : d_fm(new file_mapping(file, read_only))
        , d_region(new mapped_region(*fm, read_only))
    {
    }
    void operator()(char*) {
        this->d_region.reset(0);
        this->d_fm.reset(0);
    }
    shared_ptr<file_mapping>  d_fm;
    shared_ptr<mapped_region> d_region);
};

boost::shared_ptr<char> getBytes(const char* FilePath){
    shared_memory_deleter deleter(FilePath);
    shared_ptr<char> bytes(deleter.d_region->get_address(), deleter);
    return bytes;
}

I'm not sure if this would work but it has, at least, a chance. 我不确定这是否行得通,但至少有机会。 It is probably not a good idea to do something like this. 做这样的事情可能不是一个好主意。 Instead, you are probably much better off wrapping the logic into an object and not using any shared pointers at all: 相反,将逻辑包装到对象中而不使用任何共享指针可能要好得多:

class shared_memory
{
public:
    shared_memory(char const* name)
        : d_file(name, read_only)
        , d_region(d_file, read_only)
    {
    }
    char const* buffer() const { return d_region.get_address(); }
};

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

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