在共享内存中使用自定义分配器实例化类

发布于 2024-08-25 07:56:12 字数 2031 浏览 6 评论 0原文

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

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
    }
}

我的第一个问题:这种分配是否保证缓冲区节点分配在连续的内存位置,即当我尝试从地址 m_start_ptr + n*sizeof 访问第 n 个节点时(BufferNode) 在我的 Read() 方法中可以工作吗?如果没有,保留节点、创建链表的更好方法是什么?

我的测试工具如下:

// 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);
}

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝情姑娘 2024-09-01 07:56:13

我的第一个问题:这样的吗?
分配保证缓冲区
节点是连续分配的
记忆位置,即当我尝试
访问地址中的第n个节点
m_start_ptr + n*sizeof(BufferNode) 在
我的 Read() 方法可以工作吗?

不。原因是您只有第一个节点。您创建的所有 BufferNode 对象都不会被保存(例如,以链表方式),并且会导致内存泄漏。此外,这种分配方式不能保证连续的内存位置。随机访问(正如您稍后在问题中所述)很可能会失败。要获得连续的内存,您需要创建 BufferNode 对象的数组(可能是动态的)。

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

在不了解实际错误的情况下很难说。此外,您了解您的代码(以及 Boost::Interprocess 如何适应或分配器如何工作)?

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文