C / Posix Questions

I have been working for two weeks on JamVM, a small but powerful Java virtual machine.

Now I'm trying to figure out how memory is implemented, and I'm stuck on two C silly problems:

char *mem = (char*)mmap(0, args->max_heap, PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON, -1, 0);

-> The -1 parameter indicates the file descriptor, what does this mean? (I have aleady who read manap but did not find it, maybe I misunderstood ...).

heapbase = (char*)(((uintptr_t)mem+HEADER_SIZE+OBJECT_GRAIN-1&)~(OBJECT_GRAIN-1)) HEADER_SIZE;

-> What is 1 &? I did not find it in the C specification ...

Thank,

Yann

+3
source share
2 answers

, , . ( ), . fd , , -1.

- (, ). , - :

heapbase = (char*)(((uintptr_t)mem+HEADER_SIZE+OBJECT_GRAIN-1)
    &~(OBJECT_GRAIN-1)) - HEADER_SIZE;

OBJECT_GRAIN , . , 8, ~(OBJECT_GRAIN-1) ~7 (~00...001112, ~11...110002), , ANDed , 8 .

- ( ), , JamVM src/alloc.c, :

void initialiseAlloc(InitArgs *args) {
    char *mem = (char*)mmap(0, args->max_heap, PROT_READ|PROT_WRITE,
                                               MAP_PRIVATE|MAP_ANON, -1, 0);
    :
    << a couple of irrelevant lines >>
    :    
    /* Align heapbase so that start of heap + HEADER_SIZE is object aligned */
    heapbase = (char*)(((uintptr_t)mem+HEADER_SIZE+OBJECT_GRAIN-1)&
               ~(OBJECT_GRAIN-1))-HEADER_SIZE;

( , - HEADER_SIZE, - , ).

+2

. man.

fd , MAP_ANONYMOUS. MAP_ANONYMOUS , fd Linux. , fd -1, MAP_ANONYMOUS ( MAP_ANON), .

, -1, MAP_ANONYMOUS .

+4
source

All Articles