How to merge discontinuous memory

45 Views Asked by At

In my driver, I need to merge some discontinuous memory to one entire continuous memory area, and I don't want to allocate a new buffer to copy. So I need to map memory to a specific address. Do I need to probe the memory which is unused and unallocated ?

I have tried this code, but it doesn't work correctly as I expected.

g_p = ExAllocatePool(NonPagedPool, sizeof(DRIVER_OBJECT));
g_pMdl = IoAllocateMdl(pDriverObject, sizeof(DRIVER_OBJECT), FALSE, FALSE, NULL);
MmBuildMdlForNonPagedPool(g_pMdl);
MmProbeAndLockPages(g_pMdl, KernelMode, IoModifyAccess);
g_pMaped = MmMapLockedPagesSpecifyCache(g_pMdl, KernelMode, MmCached, g_p, FALSE, NormalPagePriority);

DbgPrint("DriverObject: 0x%p\n", pDriverObject);
DbgPrint("g_p: 0x%p\n", g_p);
DbgPrint("g_pMdl: 0x%p\n", g_pMdl);
DbgPrint("g_pMaped: 0x%p\n", g_pMaped);

And here is the result I get from windbg:

DriverObject: 0xFFFFAB0C56868E30
g_p: 0xFFFFAB0C50B07AC0
g_pMdl: 0xFFFFAB0C5696B6E0
g_pMaped: 0xFFFFD3812F2FFE30
0

There are 0 best solutions below