1 /* Support for an sbrk-like function that uses mmap. */
3 /* Copyright (c) 2010-2014. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
9 /* Copyright 1992, 2000 Free Software Foundation, Inc.
11 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com */
14 #include <unistd.h> /* Prototypes for lseek */
21 #include "mmprivate.h"
24 #define MAP_ANONYMOUS MAP_ANON
27 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + xbt_pagesize - 1) & \
28 ~((long)xbt_pagesize - 1))
30 /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return
32 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
36 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
37 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
41 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
42 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
46 /* Get core for the memory region specified by MDP, using SIZE as the
47 amount to either add to or subtract from the existing region. Works
48 like sbrk(), but using mmap().
50 It never returns NULL. Instead, it dies verbosely on errors. */
52 void *mmorecore(struct mdesc *mdp, ssize_t size)
55 void *result; // please keep it uninitialized to track issues
56 off_t foffset; /* File offset at which new mapping will start */
57 size_t mapbytes; /* Number of bytes to map */
58 void *moveto; /* Address where we wish to move "break value" to */
59 void *mapto; /* Address we actually mapped to */
60 char buf = 0; /* Single byte to write to extend mapped file */
62 // fprintf(stderr,"increase %p by %u\n",mdp,size);
65 /* Just return the current "break" value. */
66 result = mdp->breakval;
68 } else if (size < 0) {
69 /* We are deallocating memory. If the amount requested would cause
70 us to try to deallocate back past the base of the mmap'd region
71 then die verbosely. Otherwise, deallocate the memory and return
72 the old break value. */
73 if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
74 result = (void *) mdp->breakval;
75 mdp->breakval = (char *) mdp->breakval + size;
76 moveto = PAGE_ALIGN(mdp->breakval);
78 (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
81 fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
85 /* We are allocating memory. Make sure we have an open file
86 descriptor if not working with anonymous memory. */
87 if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
88 fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
90 } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
91 /* The request would move us past the end of the currently
92 mapped memory, so map in enough more memory to satisfy
93 the request. This means we also have to grow the mapped-to
94 file by an appropriate amount, since mmap cannot be used
96 moveto = PAGE_ALIGN((char *) mdp->breakval + size);
97 mapbytes = (char *) moveto - (char *) mdp->top;
98 foffset = (char *) mdp->top - (char *) mdp->base;
101 /* FIXME: Test results of lseek() */
102 lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
103 test = write(mdp->fd, &buf, 1);
105 fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
110 /* Let's call mmap. Note that it is possible that mdp->top
111 is 0. In this case mmap will choose the address for us */
112 if(mdp->base==mdp->top)
113 mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
114 MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
115 MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
117 size_t old_size = (char*)mdp->top - (char*)mdp->base;
118 mapto = mremap(mdp->base, old_size, old_size+size, 0);
121 if (mapto == (void *) -1/* That's MAP_FAILED */) {
123 fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s\n",strerror(errno));
124 sprintf(buff,"cat /proc/%d/maps",getpid());
125 int status = system(buff);
126 if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
127 fprintf(stderr, "Something went wrong when trying to %s\n", buff);
132 mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
133 result = (void *) mdp->breakval;
134 mdp->breakval = (char *) mdp->breakval + size;
136 result = (void *) mdp->breakval;
137 mdp->breakval = (char *) mdp->breakval + size;
143 void *__mmalloc_remap_core(xbt_mheap_t mdp)
145 /* FIXME: Quick hack, needs error checking and other attention. */
147 return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
148 PROT_READ | PROT_WRITE | PROT_EXEC,
149 MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);