- }
- } else {
- /* We are allocating memory. Make sure we have an open file
- descriptor if not working with anonymous memory. */
- if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
- THROWF(system_error,0,"mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags",mdp->fd);
- result = NULL;
- } 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) {
- /* FIXME: Test results of lseek() */
- lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
- test = write(mdp->fd, &buf, 1);
- if (test == -1)
- THROWF(system_error, 0, "write to mmap'ed fd failed! error: %s", strerror(errno));
- }
-
- /* Let's call mmap. Note that it is possible that mdp->top
- is 0. In this case mmap will choose the address for us */
- 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), foffset);
-
- if (mapto == (void *) -1/* That's MAP_FAILED */)
- THROWF(system_error,0,"mmap returned MAP_FAILED! error: %s",strerror(errno));
-
- if (mdp->top == 0)
- mdp->base = mdp->breakval = mapto;
-
- mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
- result = (void *) mdp->breakval;
- mdp->breakval = (char *) mdp->breakval + size;