memcpy/memmove是将数据从源复制到目的地的函数。是否存在一种方法可以将页面从一个虚拟地址移动到另一个虚拟地址,而不需要实际逐字节复制源数据?我认为这完全有可能,但任何操作系统实际上允许这样做吗?对我来说,动态数组是如此广泛和流行的概念,但通过物理复制来扩展它们却是一种浪费的操作,当你开始谈论以GB为单位的数组大小时,这种效果就变得不可接受了(例如,想象一下将100GB数组扩展为200GB数组。现在在低于10K美元的服务器上完全有可能出现这个问题)。
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)