c++ - 在不取消映射所有指针的情况下增加 managed_shared_memory
问题描述
是否可以managed_shared_memory
在不必重新分配使用allocate
of 函数创建的指针地址的情况下增长segment_manager
?理想情况下,我希望下面的代码能够安全地工作:
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib> //std::system
#include <sstream>
int main (int argc, char *argv[])
{
using namespace boost::interprocess;
char *shptr;
{
//Create a managed shared memory segment
managed_shared_memory segment(create_only, "MySharedMemory", 1024);
//Allocate a portion of the segment (raw memory)
managed_shared_memory::size_type free_memory = segment.get_free_memory();
shptr = (char *) segment.allocate(1024/*bytes to allocate*/);
shptr[1023] = 1;
//Check invariant
if(free_memory <= segment.get_free_memory())
return 1;
}
{
managed_shared_memory::grow("MySharedMemory", 500);
//I don't intend to use the additionally allocated bytes in shptr because
//it is not possible to support it from OS side.
assert(shptr[1023] == 1);
shptr[1023] = 2;
managed_shared_memory segment(create_only, "MySharedMemory", 1024);
//ability to allocate additional pointers without overriding
//previous allocations
char *shptr2 = (char *) segment.allocate(500/*bytes to allocate*/);
assert(shptr[1023] == 2);
shptr2[499] = 1;
}
}
解决方案
推荐阅读
- php - Vimeo API 获取结果为空的请求
- c# - 保存前设置值
- string-formatting - 字符串末尾的百分比-S (%s) 有什么用?(不是字符串格式)
- swift - 在 Swift 4.0 中 - 从代码添加时不会出现圆形
- javascript - 从数据库中获取月份并使用 javascript / ajax 发送到另一个页面?
- php - 计算多个表的行并单独返回结果?
- hibernate - java.lang.NoSuchMethodError: org.hibernate.StatelessSession.createQuery(Ljava/lang/String;)Lorg/hibernate/query/Query;
- java - 如何在Java中添加一系列数字
- python - Pandas 散点图 TypeError
- r - 在闪亮的 R 中绘制每月百分比