问题描述
在MSDN中,我发现以下内容
In MSDN i find following`
BaseAddress-指向页面区域的基地址的指针.
BaseAddress - A pointer to the base address of the region of pages.
AllocationBase-指向VirtualAlloc函数分配的页面范围的基地址的指针. BaseAddress成员指向的页面包含在此分配范围内.
AllocationBase - A pointer to the base address of a range of pages allocated by the VirtualAlloc function. The page pointed to by the BaseAddress member is contained within this allocation range.
但是我不明白真正的区别是什么.谁能告诉我与众不同? (不像在MSDN中那样:))
But i don't understand what is difference really. Can anyone tell me difference? (not like in MSDN :) )
推荐答案
Windows上的虚拟内存分配的粒度为64 KB,即SYSTEM_INFO.dwAllocationGranularity的值.但是虚拟内存页为4096字节,即SYSTEM_INFO.dwPageSize的值.
Virtual memory allocations on Windows are made with a granularity of 64 kilobytes, the value of SYSTEM_INFO.dwAllocationGranularity. But virtual memory pages are 4096 bytes, the value of SYSTEM_INFO.dwPageSize.
当您使用VirtualAlloc分配虚拟内存时,您将始终获得一个块,其BaseAddress等于AllocationBase.但是,如果您随后更改了该块中一个或多个页面的页面保护,则可以观察到该块被不同的BaseAddress细分了.最好与示例程序一起显示,在MSVC ++上运行此程序:
When you allocate virtual memory with VirtualAlloc, you'll always get a chunk back whose BaseAddress equals AllocationBase. But if you then alter the page protection of one or more of the pages within this chunk then you can observe this chunk being subdivided with a different BaseAddress. Best shown with a sample program, run this on MSVC++:
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
void showmem(void* mem) {
MEMORY_BASIC_INFORMATION info = {};
VirtualQuery(mem, &info, sizeof info);
printf("Alloc = %p, base = %p, size = %d, protect = %d\n",
info.AllocationBase, info.BaseAddress, info.RegionSize, info.Protect);
}
int main() {
BYTE* mem = (BYTE*)VirtualAlloc(0, 65536, MEM_COMMIT, PAGE_READWRITE);
printf("%s", "Initial allocation:\n");
showmem(mem);
DWORD oldprotect;
BOOL ok = VirtualProtect(mem + 4096, 4096, PAGE_NOACCESS, &oldprotect);
printf("%s", "\nAfter protection changes:\n");
showmem(mem);
showmem(mem + 4096);
showmem(mem + 4096 + 4096);
_getch();
return 0;
}
该程序的示例输出:
Initial allocation:
Alloc = 00ED0000, base = 00ED0000, size = 65536, protect = 4
After protection changes:
Alloc = 00ED0000, base = 00ED0000, size = 4096, protect = 4
Alloc = 00ED0000, base = 00ED1000, size = 4096, protect = 1
Alloc = 00ED0000, base = 00ED2000, size = 57344, protect = 4
请注意,VirtualProtect()调用是如何要求将原始块拆分为3个区域的,这些区域具有不同的BaseAddress但具有相同的AllocationBase.
And note how the VirtualProtect() call required the original chunk to be split in 3 regions with different BaseAddress but the same AllocationBase.
这篇关于MEMORY_BASIC_INFORMATION结构中的BaseAddress和AllocationBase有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!