简体   繁体   English

如何获得指向页面开头的指针

[英]How do I get pointer to beginning of a page

How do I get a pointer to the beginning of a page?如何获得指向页面开头的指针?

I tried the following to no success:我尝试了以下没有成功:

#define PAGESIZE 4096

 bool is_page_aligned(void *p)
 {
    return !((long int)p & 0xFFF);
 }

 int main(void)
 {
     bool res;
     void *buffer;

     buffer = malloc(PAGESIZE*2);
     printf("%p\n", (void *) &buffer);
     res = is_page_aligned(&buffer);
     fputs(res ? "true\n" : "false\n", stdout);
     return 0;
}

I'm trying to mitigate TLB misses.我正在尝试减少 TLB 未命中。 Any possible assistance is greatly appreciated.非常感谢任何可能的帮助。

To get the address of the beginning of the page containing the address a , divide by the page size and then multiply by the page size.要获得包含地址a的页面开头的地址,请除以页面大小,然后乘以页面大小。

long int page_beginning = PAGESIZE * (a / PAGESIZE);

This works because of the truncation performed during integer division.这是因为在整数除法期间执行的截断。

You can also subtract the modulus:您还可以减去模数:

long int page_beginning = a - (a % PAGESIZE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM