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);
buffer = malloc(PAGESIZE*2);
printf("%p\n", (void *) &buffer);
res = is_page_aligned(&buffer);
fputs(res ? "true\n" : "false\n", stdout);
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);