R
RazvanD
Hi!
I am trying to get the page-aligned address for an ordinary memory
address.
As I am using an x86-based CPU the page size is 4KB[1]. Thus, assuming
addr is the memory address, I could get the page-aligned address by
using
the following methods:
unsigned long uaddr = (unsigned long) addr;
char *page_addr = (char *) (uaddr - uaddr % 4096);
char *page_addr = (char *) (uaddr / 4096 * 4096);
char *page_addr = (char *) ((uaddr >> 12) << 12);
char *page_addr = (char *) (uaddr & (~0xfff));
My humble hardware knowledge tells me the last method (bitwise AND) is
the
fastest.
Its only problem is that it's not page-size portable. If the page size
is
8KB or 16KB or 64KB I would be using a different mask.
In a megalomaniacal attempt to achieve "ultimate" portability I
devised a
macro-based solution[2]. I'm not sure about three things: * how
portable
is this solution? (using unsigned long etc.) * does it make sense to
use
this? From my knowledge, operating systems and other low level
applications would have portability layers and one could define a
specific
mask according to the CPU architecture. * are there other real world
applications that would use a similar solution to get a proper bit
mask?
I'm considering any possible use, not only for page sizes.
I've been reading the c.l.c posts for some time but this is my first
post.
I apologize in advance for any mistakes or problems regarding this
message.
Razvan
[1] I am intentionally ignoring the possibility of using the
granularity
bit in the segment descriptor
[2] http://rafb.net/p/UZwEfH47.html
I am trying to get the page-aligned address for an ordinary memory
address.
As I am using an x86-based CPU the page size is 4KB[1]. Thus, assuming
addr is the memory address, I could get the page-aligned address by
using
the following methods:
unsigned long uaddr = (unsigned long) addr;
char *page_addr = (char *) (uaddr - uaddr % 4096);
char *page_addr = (char *) (uaddr / 4096 * 4096);
char *page_addr = (char *) ((uaddr >> 12) << 12);
char *page_addr = (char *) (uaddr & (~0xfff));
My humble hardware knowledge tells me the last method (bitwise AND) is
the
fastest.
Its only problem is that it's not page-size portable. If the page size
is
8KB or 16KB or 64KB I would be using a different mask.
In a megalomaniacal attempt to achieve "ultimate" portability I
devised a
macro-based solution[2]. I'm not sure about three things: * how
portable
is this solution? (using unsigned long etc.) * does it make sense to
use
this? From my knowledge, operating systems and other low level
applications would have portability layers and one could define a
specific
mask according to the CPU architecture. * are there other real world
applications that would use a similar solution to get a proper bit
mask?
I'm considering any possible use, not only for page sizes.
I've been reading the c.l.c posts for some time but this is my first
post.
I apologize in advance for any mistakes or problems regarding this
message.
Razvan
[1] I am intentionally ignoring the possibility of using the
granularity
bit in the segment descriptor
[2] http://rafb.net/p/UZwEfH47.html