Z
zolli
Hi,
This question is about a piece of Linux kernel code, but is in fact a C
language question. I was looking throught some memory map init code and
ran into the following:
p = mem_map + MAP_NR(end_mem);
(*) start_mem = ((unsigned long)p + sizeof(long) - 1) &
~(sizeof(long)-1);
(*) This is the line I don't understand.
Where MAP_NR is defined (on an i386, anyway) as:
#define MAP_NR(addr) (__pa(addr) >> 12)
#define __pa(x) ((unsigned long)(x)-0xC0000000)
The first line sets p to the beginning of the heap plus the size of the
memory map which is calculated using the MAP_NR macro.
Using reasonable (?) example values if I do this:
unsigned long start_mem, end_mem, mem_map, p;
start_mem = 0xC1000000;
end_mem = 0xC2000000;
mem_map = start_mem;
p = mem_map + MAP_NR(end_mem);
start_mem = ((unsigned long)p + sizeof(long) - 1) &
~(sizeof(long)-1);
printf("p: 0x%x\n",p);
printf("start_mem: 0x%x\n",start_mem);
I get this as output:
p: 0xc1002000
start_mem: 0xc1002000
So what does the (*) line of code above do? I know that it changes
start_mem to the point where the memory allocated for the memory map
ends, but wouldn't that be more easily accomplished by:
start_mem += MAP_NR(end_mem);
My guess is that the line of code somehow sets the size of the mem_map
according to free memory minus the size of the mem_map, but I don't see how.
Thanks in advance,
zolli
This question is about a piece of Linux kernel code, but is in fact a C
language question. I was looking throught some memory map init code and
ran into the following:
p = mem_map + MAP_NR(end_mem);
(*) start_mem = ((unsigned long)p + sizeof(long) - 1) &
~(sizeof(long)-1);
(*) This is the line I don't understand.
Where MAP_NR is defined (on an i386, anyway) as:
#define MAP_NR(addr) (__pa(addr) >> 12)
#define __pa(x) ((unsigned long)(x)-0xC0000000)
The first line sets p to the beginning of the heap plus the size of the
memory map which is calculated using the MAP_NR macro.
Using reasonable (?) example values if I do this:
unsigned long start_mem, end_mem, mem_map, p;
start_mem = 0xC1000000;
end_mem = 0xC2000000;
mem_map = start_mem;
p = mem_map + MAP_NR(end_mem);
start_mem = ((unsigned long)p + sizeof(long) - 1) &
~(sizeof(long)-1);
printf("p: 0x%x\n",p);
printf("start_mem: 0x%x\n",start_mem);
I get this as output:
p: 0xc1002000
start_mem: 0xc1002000
So what does the (*) line of code above do? I know that it changes
start_mem to the point where the memory allocated for the memory map
ends, but wouldn't that be more easily accomplished by:
start_mem += MAP_NR(end_mem);
My guess is that the line of code somehow sets the size of the mem_map
according to free memory minus the size of the mem_map, but I don't see how.
Thanks in advance,
zolli