将虚拟地址与下一个页面边界对齐 [英] Aligning virtual address to immediate next page boundary

查看:95
本文介绍了将虚拟地址与下一个页面边界对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下算法,该算法将虚拟地址与下一个页面边界对齐.

I came across following algorithm that aligns virtual address to immediate next page bounday.

VirtualAddr = (VirtualAddr & ~(PageSize-1));

此外,给定字节长度,将长度(四舍五入)对齐到页面边界上

Also, given a length of bytes aligns length (rounds it) to be on the page boundary

len = ((PageSize-1)&len) ? ((len+PageSize) & ~(PageSize-1)):len;

我发现很难解释它是如何工作的. 有人可以帮我分解一下吗?

I am finding it hard to decipher how this works. Can someone help me out to break it down?

推荐答案

这些计算假设页面大小是2的次幂(em为2). 我所知道的所有系统),例如

Those calculations assume that the page size is a power of 2 (which is the case for all systems that I know of), for example

PageSize = 4096 = 2^12 = 1000000000000 (binary)

然后(写为二进制数字)

Then (written as binary numbers)

PageSize-1    = 00...00111111111111
~(PageSize-1) = 11...11000000000000

这意味着

(VirtualAddr & ~(PageSize-1))

VirtualAddr,其低12位设置为零,换句话说, VirtualAddr向下舍入到2^12 = PageSize的下一个倍数.

is VirtualAddr with the lower 12 bits set to zero or, in other words, VirtualAddr rounded down to the next multiple of 2^12 = PageSize.

现在,您可以(希望)在

Now you can (hopefully) see that in

len = ((PageSize-1)&len) ? ((len+PageSize) & ~(PageSize-1)):len;

第一个表达式

 ((PageSize-1)&len)

如果lenPageSize的倍数,则

确切为零.在这种情况下,剩下len 不变.否则,(len + PageSize)会四舍五入到下一个整数倍 PageSize.

is zero exactly if len is a multiple of PageSize. In that case, len is left unchanged. Otherwise (len + PageSize) is rounded down to the next multiple of PageSize.

因此,在任何情况下,len都会四舍五入为向上PageSize的下一个倍数.

So in any case, len is rounded up to the next multiple of PageSize.

这篇关于将虚拟地址与下一个页面边界对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆