How to concatenate pages of memory without copying the memory (by eg.: memcpy()
) ?
Take this example in which 3 pages of memory are allocated:
char* GetStuff(char* Dest, size_t MaximumSize);
char* p1 = VirtualAlloc(NULL, PAGE_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
GetStuff(p1, PAGE_SIZE);
char* p2 = VirtualAlloc(NULL, PAGE_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
GetStuff(p2, PAGE_SIZE);
char* p3 = VirtualAlloc(NULL, PAGE_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
GetStuff(p3, PAGE_SIZE);
It is possible to concatenate these 3 pages of memory into a contiguous address space, without destroying their contents, like this:
char* pAll = VirtualAlloc(NULL, 3*PAGE_SIZE, MEM_RESERVE, PAGE_READWRITE);
VirtualAlloc(pAll, 3*PAGE_SIZE, MEM_COMMIT, PAGE_READWRITE);
memcpy(pAll+(0*PAGE_SIZE), p1, PAGE_SIZE);
memcpy(pAll+(1*PAGE_SIZE), p2, PAGE_SIZE);
memcpy(pAll+(2*PAGE_SIZE), p3, PAGE_SIZE);
…but without the memcpy()
– by just maping these 3 pages of memory into a consecutive address space, like it is possible to do in kernel mode with MDLs.