简体   繁体   English

对碎片内存进行虚拟碎片整理,就好像它在 C++ 中是连续的一样

[英]Virtually defragment a fragmented memory as if it was contiguous in c++

is there a way or is it possible to take eg 10 memory regions (eg pointers with given size) and create a sort of overlay such that they can be handled/treated as contiguous?有没有办法或者是否可以采用例如 10 个内存区域(例如具有给定大小的指针)并创建一种覆盖,以便它们可以被处理/视为连续的?

The use case would be something like reconstruct a message out of "n" frames without copying them around.用例类似于从“n”帧中重建消息而不复制它们。 Of course the "n" frames are appended/prependend with a header which should be stripped in order to reconstruct the information.当然,“n”帧附加/前置一个标题,该标题应该被剥离以重建信息。 Moreover a variable could be eg splitted across two consecutive frames.此外,变量可以例如跨两个连续帧分割。

Few more details for future help.为将来的帮助提供更多详细信息。

Otter solution is quite nice but it lacks the possibility to lay a structure on top of multiple boost::join-ed block. Otter 解决方案非常好,但它缺乏在多个 boost::join-ed 块之上放置结构的可能性。 Of course a std::copy of the joined block will create a contiguous copy of all the interested and fragmented regions but in my case i would like it to be "virtual" due to performance constraints.当然,连接块的 std::copy 将创建所有感兴趣和碎片区域的连续副本,但在我的情况下,由于性能限制,我希望它是“虚拟的”。

Regards,问候,

boost::range::join is a great helper here - link . boost::range::join是这里的好帮手 - link When working with random access ranges it will also produce random access range with quick access to elements.使用随机访问范围时,它还将生成具有快速访问元素的随机访问范围。 As the manual tells The resultant range will have the lowest common traversal of the two ranges supplied as parameters正如手册所说The resultant range will have the lowest common traversal of the two ranges supplied as parameters

Also when working with plain memory boost::make_iterator_range cound help.同样在使用普通内存boost::make_iterator_range也有帮助。

Take a look at this short example.看看这个简短的例子。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM