1

次の問題が原因で髪を引っ張っています。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(BufferNode)Read()メソッドでアドレスから n 番目のノードにアクセスしようとすると、機能しますか? そうでない場合、リンクされたリストを作成して、ノードを保持するためのより良い方法は何ですか?

私のテストハーネスは次のとおりです。

// 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<MyBuf>("MyBuffer")(100, alloc_inst)2 つのテンプレート パラメータを提供する正しい方法はありますか?

4

1 に答える 1