[英]Virtually defragment a fragmented memory as if it was contiguous in c++
有没有办法或者是否可以采用例如 10 个内存区域(例如具有给定大小的指针)并创建一种覆盖,以便它们可以被处理/视为连续的?
用例类似于从“n”帧中重建消息而不复制它们。 当然,“n”帧附加/前置一个标题,该标题应该被剥离以重建信息。 此外,变量可以例如跨两个连续帧分割。
为将来的帮助提供更多详细信息。
Otter 解决方案非常好,但它缺乏在多个 boost::join-ed 块之上放置结构的可能性。 当然,连接块的 std::copy 将创建所有感兴趣和碎片区域的连续副本,但在我的情况下,由于性能限制,我希望它是“虚拟的”。
问候,
boost::range::join
是这里的好帮手 - link 。 使用随机访问范围时,它还将生成具有快速访问元素的随机访问范围。 正如手册所说The resultant range will have the lowest common traversal of the two ranges supplied as parameters
同样在使用普通内存boost::make_iterator_range
也有帮助。
看看这个简短的例子。
int arr1[] = { 0, 1, 2, 3, 4, 5 }; // let's join these 3 plain memory arrays
int arr2[] = { 6, 7, 8, 9 };
int arr3[] = { 10, 11, 12};
int* mem1 = arr1; // let's make the example more complicated
int* mem2 = arr2; // because int arr1[] with known size will be recognized
int* mem3 = arr3; // as a range by boost::join
auto res1 = boost::range::join(boost::make_iterator_range(mem1, mem1 + 6),
boost::make_iterator_range(mem2, mem2 + 4)); // join 2 ranges by pointer arithmetics
auto res2 = boost::range::join(res1, // join previously joined range
boost::make_iterator_range(mem3, mem3 + 3));
for (auto& r : res2) // the resulted range is iterable
{
std::cout << r << "\n";
}
std::cout << res2[12]; // outputs '12', note that this result
// was eventually got by pointer arithmetics
// applyed to mem3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.