如何确定要分配多少共享内存?

时间:2017-07-27 09:27:07

标签: c++ memory-management ipc stdvector boost-interprocess

我正在使用boost::interprocess创建进程间通信方案。我希望我的共享内存区域包含一些std::vector,并根据提供的here示例执行此操作。

为了进行通信,我计划在共享内存中放置如下结构(甚至没有尝试编译这个btw):

// Define an STL compatible allocator of doubles that allocates from the managed_shared_memory.
// This allocator will allow placing stl containers in the segment
typedef allocator<doublereal, boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator_doublereal;

// Alias a vector that uses the previous STL-like allocator so that it
// allocates its values from the segment
typedef vector<doublereal, ShmemAllocator_doublereal> shmVector_doublereal;

// Define an STL compatible allocator for uint32_t in shared memory
typedef allocator<uint32_t, boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator_uint32_t;

// Shared memory vector for uint32_t type for lables
typedef vector<uint32_t, ShmemAllocator_uint32_t> shmVector_uint32_t;


struct shared_memory_buffer {
    // Server initialized with one to start, client must wait for it to be released
    shared_memory_buffer(int nnodes, boost::interprocess::managed_shared_memory segment): server(1), client(0){

        // Initialize shared memory STL-compatible allocators
        const ShmemAllocator_doublereal alloc_inst_d (segment.get_segment_manager());
        const ShmemAllocator_uint32_t alloc_inst_u (segment.get_segment_manager());

        // Construct the data vectors
        motion_data = segment.construct<shmVector_doublereal>("motion_data_vector")(alloc_inst_d);

        force_data = segment.construct<shmVector_doublereal>("force_data_vector")(alloc_inst_d);

        labels = segment.construct<shmVector_uint32_t>("label_vector")(alloc_inst_u);

        // position, velocity and acceleration in 6dof
        motion_data.resize (nnodes * 6  * 3);

        // force in 6dof
        force_data.resize (nnodes * 6);

        // n node labels
        labels.resize (nnodes);

    }

    // convenience function to get the motion data vector from the shared
    // memory segment
    shmVector_doublereal<doublereal, ShmemAllocator_doublereal> * getMotionData (boost::interprocess::managed_shared_memory segment) {
        return segment.find<shmVector_doublereal>("motion_data_vector").first;
    }

    // convenience function to get the force data vector from the shared
    // memory segment
    shmVector_doublereal<doublereal, ShmemAllocator_doublereal> * getMotionData (boost::interprocess::managed_shared_memory segment) {
        return segment.find<shmVector_doublereal>("force_data_vector").first;
    }

    // convenience function to get the buffer from the shared
    // memory segment
    shmVector_doublereal<doublereal, shmMemAllocator> * getLabels (boost::interprocess::managed_shared_memory segment) {
        return segment.find<shmVector_uint32_t>("label_vector").first;
    }

    // semaphores for read/write control
    boost::interprocess::interprocess_semaphore server, client;

    // buffer for motion data
    shmVector_doublereal<doublereal, shmMemAllocator> *motion_data;

    // buffer for force data
    shmVector_doublereal<doublereal, shmMemAllocator> *force_data;

    // buffer for label data
    shmVector_uint32_t<uint32_t, shmMemAllocator> *labels;

    //doublereal time;

};

我的问题是,我怎么知道在创建共享内存时要分配多少内存,例如:与

managed_shared_memory segment(create_only, "MySharedMemory", 65536);

因此,我想知道/估计必须分配多少内存来保存共享内存区域中包含stl向量的整个结构。

在运行时,在共享内存分配之前,我将始终知道向量中将有多少元素(请参阅结构构造函数以了解这一点),但不是在编译时,这是我&#39;使用矢量。根据我认为人们可能会如何使用它,我可以分配我认为相当多的东西,但毫无疑问,有一天会有人使用太多节点并且会遇到困难。

0 个答案:

没有答案