【发布时间】:2012-01-26 23:27:36
【问题描述】:
memcpy/memmove 从源到目标重复(复制数据)。是否存在将页面从一个虚拟地址移动到另一个而不对源数据进行实际逐字节复制的方法?对我来说这似乎是完全可能的,但是任何操作系统实际上都允许这样做吗?在我看来,动态数组是一个如此广泛和流行的概念,但通过物理复制来增长它们是一种非常浪费的操作,这对我来说似乎很奇怪。当您开始谈论以千兆字节为单位的数组大小时,它只是无法扩展(例如,想象将一个 100GB 的数组增加到一个 200GB 的数组。现在在
void* very_large_buffer = VirtualAlloc(NULL, 2GB, MEM_COMMIT);
// Populate very_large_buffer, run out of space.
// Allocate buffer twice as large, but don't actually allocate
// physical memory, just reserve the address space.
void* even_bigger_buffer = VirtualAlloc(NULL, 4GB, MEM_RESERVE);
// Remap the physical memory from very_large_buffer to even_bigger_buffer without copying
// (i.e. don't copy 2GB of data, just copy the mapping of virtual pages to physical pages)
// Does any OS provide support for an operation like this?
MoveMemory(very_large_buffer, even_bigger_buffer, 2GB)
// Now very_large_buffer no longer has any physical memory pages associated with it
VirtualFree(very_large_buffer)
【问题讨论】:
标签: c++ c windows linux operating-system