In xv6 mmu.h
file, there are these 2 lines of code
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
What do they do?
In xv6 mmu.h
file, there are these 2 lines of code
#define PGROUNDUP(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
What do they do?
PGROUNDUP
and PGROUNDDOWN
are macros to round the address sent to a multiple of the PGSIZE
. These are generally used to obtained page aligned address. PGROUNDUP
will round the address to the higher multiple of PGSIZE
while PGROUNDDOWN
will round it to the lower multiple of PGSIZE
.
Let us take an example if PGROUNDUP
is called on a system with PGSIZE
1KB with the address 620:
Similarly for PGROUNDDOWN
consider:
First you should know (PGSIZE-1)) is the max remainder (addr/page_size)
Essentially we only need to abandon the remainder part,and & ~(PGSIZE-1)) makes the thing