Does VirtualAlloc alignment fit the distribution size?

When using the API VirtualAllocto distribute and fix a region of virtual memory with a capacity of two page border sizes, for example:

void* address = VirtualAlloc(0, 0x10000, MEM_COMMIT, PAGE_READWRITE); // Get 64KB

It seems to be addressin alignment of 64 KB, and not just the page border, which in my case is 4 KB.

Question: Is this alignment reliable and prescribed, or is it just a coincidence? Documents claim that it is guaranteed to be on the page border, but does not affect the behavior that I see. I ask because later I wanted to take an arbitrary pointer (provided by the pool allocator that uses this piece), and determine which piece of 64 KB it belongs to, something like:

void* chunk = (void*)((uintptr_t)ptr & 0xFFFF0000);
+2
source share
2 answers

The documentation for VirtualAllocdescribes the behavior for two scenarios: 1) backup memory and 2) memory commit:

If the memory is reserved , the specified address is rounded to the nearest multiple distribution gradation .

If the memory is already reserved and fixed , the address is rounded to the next page border .

, () . , . .

, GetSystemInfo. SYSTEM_INFO dwPageSize dwAllocationGranularity .

+3

. 64KB - SYSTEM_INFO.dwAllocationGranularity. , 4 . - 64 , .

HeapAlloc() . .

+3

All Articles