简体   繁体   中英

How do I tell if two addresses are in the same page file?

涉及什么数学,如何判断两个地址是否在同一个4千字节的页面中?

Well, assuming 4 KiB pages,

#include <stdint.h>
bool same_page(const void *x, const void *y)
{
    uintptr_t mask = ~(uintptr_t) 4095;
    return ((uintptr_t) x & mask) == ((uintptr_t) y & mask);
}

This will get ugly quickly since pages have a variable size on common architectures, and the page size of a particular region of memory can and will be changed by the operating system on the fly depending on application memory usage patters.

(Note that memory pages are virtual memory and not physical memory. Strictly speaking, it does not make sense to talk about physical pages, although we usually understand when someone says "physical page" they mean "physical memory corresponding to a page".)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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