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. 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.
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);