在共享内存中使用自定义分配器实例化类 [英] Instantiating class with custom allocator in shared memory

查看:465
本文介绍了在共享内存中使用自定义分配器实例化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下问题,我正在拉我的头发:我正在关注在boost.interprocess文档中给出的示例来实例化我在共享内存中写入的固定大小的环形缓冲区缓冲区类。我的类的骨架构造函数是:

  template< typename ItemType,class Allocator> 
SharedMemoryBuffer< ItemType,Allocator> :: SharedMemoryBuffer(unsigned long capacity){

m_capacity = capacity;

//创建缓冲节点。
m_start_ptr = this-> allocator-> allocate(); //分配第一个缓冲节点
BufferNode * ptr = m_start_ptr;
for(int i = 0; i capacity() - 1; i ++){
BufferNode * p = this-> allocator-> allocate(); //分配缓冲节点
}
}

我的第一个问题:这种分配保证缓冲节点分配在连续的存储器位置,即当我尝试从地址 m_start_ptr + n * sizeof(BufferNode)访问第n个节点时,在我的 Read()方法会工作吗?



我的测试工具如下:

  //定义从managed_shared_memory分配的int类型的STL兼容分配器。 
//这个分配器将允许在段中放置容器
typedef allocator< int,managed_shared_memory :: segment_manager> ShmemAllocator;

//使用上一个类似STL的分配器的向量,以便从段
分配
//的值typedef SharedMemoryBuffer< int,ShmemAllocator> MyBuf;

int main(int argc,char * argv [])
{
shared_memory_object :: remove(MySharedMemory);

//创建一个具有给定名称和大小的新段。
managed_shared_memory段(create_only,MySharedMemory,65536);

//初始化共享内存STL兼容分配器
const ShmemAllocator alloc_inst(segment.get_segment_manager());

//在共享内存中使用参数alloc_inst构造一个名为MyBuffer的缓冲区
MyBuf * pBuf = segment.construct< MyBuf>(MyBuffer)(100,alloc_inst);
}

这给出了与最后一个语句的模板相关的各种编译错误。我做错了什么? segment.construct< MyBuf>(MyBuffer)(100,alloc_inst)正确的方式提供两个模板参数?

分配是否保证缓冲区
节点分配在连续的$ b中?

$ b内存位置,即当我尝试
访问第n个节点从
m_start_ptr + n * sizeof(BufferNode)在
我的Read()方法将工作? p>

否。原因是你只有第一个节点。您创建的所有 BufferNode 对象未保存(例如,以链接列表方式),并导致内存泄漏。此外,这种分配方式不指定连续的存储器位置。随机访问(如你在问题中后面所述)很可能会失败。要获取连续的内存,您需要创建 BufferNode 对象的数组(也许是动态的)。


这给出了与最后一条语句的模板相关的各种编译错误。我做错了什么?


很难说,不知道实际的错误。此外,你是否理解你的代码(以及 Boost :: Interprocess 如何适合或如何分配器工作)?



请注意,您引用的示例创建了一个向量,它保证为其包含的对象具有连续的内存。唯一的区别是,对象是在共享内存段上创建的,而不是自由存储,这是在没有指定分配器作为第二个参数并使用默认值时通常发生的情况。


I'm pulling my hair due to the following problem: I am following the example given in boost.interprocess documentation to instantiate a fixed-size ring buffer buffer class that I wrote in shared memory. The skeleton constructor for my class is:

template<typename ItemType, class Allocator >
SharedMemoryBuffer<ItemType, Allocator>::SharedMemoryBuffer( unsigned long capacity ){

    m_capacity = capacity;

    // Create the buffer nodes.
    m_start_ptr = this->allocator->allocate();  // allocate first buffer node
    BufferNode* ptr = m_start_ptr;
    for( int i = 0 ; i < this->capacity()-1; i++ ) {
        BufferNode* p = this->allocator->allocate();    // allocate a buffer node
    }
}

My first question: Does this sort of allocation guarantee that the buffer nodes are allocated in contiguous memory locations, i.e. when I try to access the n'th node from address m_start_ptr + n*sizeof(BufferNode) in my Read() method would it work? If not, what's a better way to keep the nodes, creating a linked list?

My test harness is the following:

// Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
// This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager>  ShmemAllocator;

//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef SharedMemoryBuffer<int, ShmemAllocator> MyBuf;

int main(int argc, char *argv[]) 
{
    shared_memory_object::remove("MySharedMemory");

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

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

    //Construct a buffer named "MyBuffer" in shared memory with argument alloc_inst
    MyBuf *pBuf = segment.construct<MyBuf>("MyBuffer")(100, alloc_inst);
}

This gives me all kinds of compilation errors related to templates for the last statement. What am I doing wrong? Is segment.construct<MyBuf>("MyBuffer")(100, alloc_inst) the right way to provide the two template parameters?

解决方案

My first question: Does this sort of allocation guarantee that the buffer nodes are allocated in contiguous memory locations, i.e. when I try to access the n'th node from address m_start_ptr + n*sizeof(BufferNode) in my Read() method would it work?

No. The reason being that you have the first node only. All BufferNode objects that you create are not being saved (say, in a linked-list fashion) and contribute to memory leaks. Further, this style of allocation does not gurantee contiguous memory locations. Random access (as you state later in your question) will most likely fail. To get contiguous memory, you need to create an array (perhaps dynamic) of BufferNode objects.

This gives me all kinds of compilation errors related to templates for the last statement. What am I doing wrong?

Difficult to say without knowing about the actual errors. Further, do you understand your code (and how Boost::Interprocess fits in or how the allocator works)?

Note that the example you cite creates a vector which is guaranteed to have contiguous memory for its contained objects. The only difference here is that the objects are created on a shared memory segment rather than the free store which is what typically happens when you don't specify an allocator as the second parameter and the default one is used.

这篇关于在共享内存中使用自定义分配器实例化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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