Introduction

In Windows Internals, I came across the following statement as I was reading (chapter 5, page 485):

Finally, when a region of address space is reserved, Windows ensures that the size and base of the region is a multiple of the system page size, whatever that might be. For example, because x86 systems use 4 KB pages, if you tried to reserve a region of memory 18 KB in size, the actual amount reserved on an x86 system would be 20 KB. If you specified a base address of 3 KB for an 18 KB region, the actual amount reserved would be 24 KB.

I had a hard time figuring out the math and picturing this in my head, so I made a diagram and that solidified my understanding. I am sharing that diagram and explanation here with you.

Diagram and Explanation

Virtual Memory Allocate Page Padding Diagram

The top figure in the diagram above shows how an 18 KB allocation, starting at 0, fits into 5 4 KB pages. The virtual memory allocated for the process has access only to the 18 KB in its virtual address space which is shown in green. The extra 2 KB at the end are not accessible. 18 KB + 2 KB = 20 KB of total space that must be used for this 18 KB virtual memory allocation.

The bottom figure was the hard one for me to figure out just trying to picture it in my head. Notice that the 18 KB of allocated virtual memory starts at the 3 KB base address. So, the first 3 KB of the paged memory that will be used for this virtual memory allocation is inaccessible. 18 KB + 3 KB = 21 KB, so the end of the green 18 KB virtual memory allocation sits at the 21 KB mark. We are now into a sixth 4 KB page by 1 KB, so we waste 3 KB at the end, for a total of 6 KB inaccessible (3 KB at the beginning, 3 KB at the end). 18 KB + 6 KB = 24 KB total memory needed.

But wait! There’s more!

I did not include the following last sentence of the paragraph I quoted above:

Note that the VAD for the allocation would then also be rounded to 64 KB alignment/length, thus making the remainder of it inaccessible.

The VAD is the Virtual Address Descriptor, where virtual address information for allocations is stored. Since the system automatically rounds many allocation sizes up to 64 KB, in reality, 64 KB - 18 KB = 46 KB would be inaccessible for those cases. In the diagram above, we would have to tack on enough 4 KB pages at the ends to reach 64 KB (maybe - see paragraph below diagram below).

This makes the entire exercise above a little more confusing. However, just note that the same two diagrams could be used with the page size left at 4 KB, a “boundary size” of 64 KB indicated by the vertical lines, and a larger allocation size to vet out running over the larger boundary sizes.

For instance, if you simply multiplied the pages sizes (which would then indicate 64 KB boundary sizes), base offsets, and allocation sizes by 16, you would have the exact same diagrams, just with larger numbers. Everything you learned from his discussion and the previous diagram easily fits, except for leaving the notation of x86 architecture page size below each figure at 4 KB:

Virtual Memory Allocate Page Padding 64 KB Diagram

In the future, as noted by the book, if the page sizes of an architecture are larger [in multiples of 2] up to 64 KB [i.e., 8 KB, 16 KB, 32 KB, 64 KB], relatively little work would need to be done to accomodate that change in those cases where memory allocations are rounded up to 64 KB boundaries by the operating system currently. This is a potential benefit of the larger boundary size.

Conclusion

There are a lot of great diagrams in the book, but not one for this, which would have helped me picture it better. So, here it is best I can do. I hope if others have difficulty picturing this without a diagram, it will help them as well.

Thanks to Pexels for the free image