Victor Schubert Victor Schubert - 28 days ago 7
C Question

Is it safe to assume memory page size is a power of two?

I am currently working on a portable C program which makes use of the memory page size (as returned by

sysconf(_SC_PAGESIZE)
). I know most often page size is a power of two to allow efficient management using bitwise operations; I found no guarantee of this anywhere, though. So how safe is it to assume page size is a power of two? Are there any examples of architectures not satisfying this condition?

R.. R..
Answer

While I can't find any explicit statement that the page size is required to be a power of two, I think it's a very reasonable assumption. The POSIX concept of "alignment" (see posix_memalign) is defined in terms of powers of two, not arbitrary divisors - specifically, the alignment argument must be a power-of-two multiple of sizeof(void*) - and it seems that you should be able to achieve page-aligned memory with this interface. Beyond that, all real-world architectures use power-of-two page sizes, and have no reasonable justification for ever changing that; anything else is significantly more costly in hardware.