3

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

Now I am trying to figure how the memory is implemented and I am stuck on two C stupid problems:

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

--> The -1 parameter stands for a file descriptor, what does that mean? (I have aleady read the mmap man, but haven't found it, maybe I misunderstood...).

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

--> What is 1& ? I don't find it in the C specification...

Thanks,

Yann

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • If this were two separate questions, then you'd be able to give each one a useful title. – Steve Jessop Jun 17 '11 at 13:30
  • `-1&)~(` is not even legal C (a compiler should complain about a missing token), are you sure its not `-1)&~(`? which means AND NOT, (usually used to turn a specific bit off) – Wiz Jun 17 '11 at 13:31
  • @Wiz: In this case, OBJECT_GRAIN is probably a power of 2, so if the code were `-1)&~` then it would round `mem+HEADER_SIZE` up to the next multiple of `OBJECT_GRAIN`. – Steve Jessop Jun 17 '11 at 13:33
  • Yeah I am sure, I copied the source code! If it was a mistake, there maybe a warning a compile no? – scoobyclown Jun 17 '11 at 13:33
  • @Steve: Yes OBJECT_GRAIN is for data alignment, it's equal to 8 – scoobyclown Jun 17 '11 at 13:34
  • @scoobyclown i beg to differ: (line 212) http://read.pudn.com/downloads144/sourcecode/others/628545/jamvm-1.4.2/jamvm-1.4.2/src/alloc.c__.htm (I guess it must be a typo in the source you are viewing) – Wiz Jun 17 '11 at 13:36
  • 1
    @scoobyclown: please accept an answer that solves your question instead of editing that into the title. Stackoverflow.com is not a forum. – rubenvb Jun 17 '11 at 13:43

2 Answers2

4

In answer to your first question. From the man page.

fd should be a valid file descriptor, unless MAP_ANONYMOUS is set. If MAP_ANONYMOUS is set, then fd is ignored on Linux. However, some implementations require fd to be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this.

So it's -1 because MAP_ANONYMOUS is being used.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
  • 1
    Thanks! That why I said it was a stupid question in the first place, I hadn't read the good manual in the first place (I read the 3p) – scoobyclown Jun 17 '11 at 13:36
2

You use the file descriptor when you have an open file that you want to map into memory. In this case, you're creating an anonymous map (one not backed by a file) so the file descriptor isn't needed. Some implementations ignore fd for anonymous maps, some require it to be -1.

The second question is a syntax error (probably a typo). It probably should be something like:

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

In that case, OBJECT_GRAIN will be a power of two and it's a way to get alignment to that power. For example, if it were 8, then ~(OBJECT_GRAIN-1) would be ~7 (~00...001112, which is ~11...110002) which, when ANDed with a value, could be used to force that value to the multiple-of-8 less than or equal to it.

In fact, it's definitely a transcription error somewhere (not necessarily you) because, when I download the JamVM from here and look in src/alloc.c, I get:

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;

(note that your version is also missing the - immediately before HEADER_SIZE, something else that points to transcription problems).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Thank you, That's it! But it's strange because I have the latest version and it works fine... – scoobyclown Jun 17 '11 at 13:39
  • After downloading a clean copy of JamVM and apply a diff, the only difference seems to be the line with heapbase... I maybe have made bad edits while reading... Thanks for your answer and sorry ! – scoobyclown Jun 17 '11 at 13:59
  • @scooby, no need to apologise, helping people is why we are here. Cheers. – paxdiablo Jun 17 '11 at 14:01