Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid unsafe things
[simgrid.git] / src / xbt / mmalloc / mmorecore.c
index 4a9cbb5..e876dd2 100644 (file)
 
    Contributed by Fred Fish at Cygnus Support.   fnf@cygnus.com */
 
-#ifdef HAVE_UNISTD_H
+#include "src/internal_config.h"
+#if HAVE_UNISTD_H
 #include <unistd.h>             /* Prototypes for lseek */
 #endif
 #include <stdio.h>
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <sys/wait.h>
+#include <errno.h>
 
 #include "mmprivate.h"
 
                              ? -1                                 \
                              : (MDP) -> fd)
 
+/* Return 0if MDP uses anonymous mapping. Otherwise, return off */
+#define MAP_ANON_OR_OFFSET(MDP, off) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
+                             ? 0                                           \
+                             : off)
+
 /** @brief Add memoty to this heap
  *
  *  Get core for the memory region specified by MDP, using SIZE as the
@@ -117,12 +124,12 @@ void *mmorecore(struct mdesc *mdp, ssize_t size)
        */
       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);
+                   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));
-        sprintf(buff,"cat /proc/%d/maps",getpid());
+        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);