- } else if ((char*)mdp->breakval + size > (char*)mdp->top) {
- /* The request would move us past the end of the currently mapped memory, so map in enough more memory to satisfy
- the request. This means we also have to grow the mapped-to file by an appropriate amount, since mmap cannot
- be used to extend a file. */
- moveto = PAGE_ALIGN((char*)mdp->breakval + size);
- mapbytes = (char*)moveto - (char*)mdp->top;
- foffset = (char*)mdp->top - (char*)mdp->base;
-
- if (mdp->fd > 0) {
- if (lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET) == -1) {
- fprintf(stderr, "Internal error: lseek into mmap'ed fd failed! error: %s", strerror(errno));
- abort();
- }
- if (write(mdp->fd, &buf, 1) == -1) {
- fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
- abort();
- }
- }
-
- /* Let's call mmap. Note that it is possible that mdp->top is 0. In this case mmap will choose the address for us.
- This call might very well overwrite an already existing memory mapping (leading to weird bugs).
- */
- mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
- MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
- MAP_FIXED, MAP_ANON_OR_FD(mdp), MAP_ANON_OR_OFFSET(mdp, foffset));
-
- if (mapto == MAP_FAILED) {
- char buff[1024];
- fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s\n",strerror(errno));
- snprintf(buff,1024,"cat /proc/%d/maps",getpid());
- int status = system(buff);
- if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
- fprintf(stderr, "Something went wrong when trying to %s\n", buff);
- sleep(1);
- abort();
- }
-
- if (mdp->top == 0)
- mdp->base = mdp->breakval = mapto;
-
- mdp->top = PAGE_ALIGN((char*)mdp->breakval + size);
- result = mdp->breakval;
- mdp->breakval = (char*)mdp->breakval + size;
- } else {
- /* Memory is already mapped, we only need to increase the breakval: */
- result = mdp->breakval;
- mdp->breakval = (char*)mdp->breakval + size;