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 */
13 #include "src/internal_config.h"
15 #include <unistd.h> /* Prototypes for lseek */
23 #include "mmprivate.h"
26 #define MAP_ANONYMOUS MAP_ANON
29 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + xbt_pagesize - 1) & \
30 ~((long)xbt_pagesize - 1))
32 /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return
34 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
38 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
39 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
43 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
44 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
48 /** @brief Add memoty to this heap
50 * Get core for the memory region specified by MDP, using SIZE as the
51 * amount to either add to or subtract from the existing region. Works
52 * like sbrk(), but using mmap().
54 * It never returns NULL. Instead, it dies verbosely on errors.
57 * @param size Bytes to allocate for this heap (or <0 to free memory from this heap)
59 void *mmorecore(struct mdesc *mdp, ssize_t size)
62 void *result; // please keep it uninitialized to track issues
63 off_t foffset; /* File offset at which new mapping will start */
64 size_t mapbytes; /* Number of bytes to map */
65 void *moveto; /* Address where we wish to move "break value" to */
66 void *mapto; /* Address we actually mapped to */
67 char buf = 0; /* Single byte to write to extend mapped file */
70 /* Just return the current "break" value. */
71 result = mdp->breakval;
73 } else if (size < 0) {
74 /* We are deallocating memory. If the amount requested would cause
75 us to try to deallocate back past the base of the mmap'd region
76 then die verbosely. Otherwise, deallocate the memory and return
77 the old break value. */
78 if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
79 result = (void *) mdp->breakval;
80 mdp->breakval = (char *) mdp->breakval + size;
81 moveto = PAGE_ALIGN(mdp->breakval);
83 (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
86 fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
90 /* We are allocating memory. Make sure we have an open file
91 descriptor if not working with anonymous memory. */
92 if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
93 fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
95 } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
96 /* The request would move us past the end of the currently
97 mapped memory, so map in enough more memory to satisfy
98 the request. This means we also have to grow the mapped-to
99 file by an appropriate amount, since mmap cannot be used
101 moveto = PAGE_ALIGN((char *) mdp->breakval + size);
102 mapbytes = (char *) moveto - (char *) mdp->top;
103 foffset = (char *) mdp->top - (char *) mdp->base;
106 /* FIXME: Test results of lseek() */
107 lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
108 test = write(mdp->fd, &buf, 1);
110 fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
115 /* Let's call mmap. Note that it is possible that mdp->top
116 is 0. In this case mmap will choose the address for us.
117 This call might very well overwrite an already existing memory mapping
118 (leading to weird bugs).
120 mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
121 MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
122 MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
124 if (mapto == MAP_FAILED) {
126 fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s\n",strerror(errno));
127 snprintf(buff,1024,"cat /proc/%d/maps",getpid());
128 int status = system(buff);
129 if (status == -1 || !(WIFEXITED(status) && WEXITSTATUS(status) == 0))
130 fprintf(stderr, "Something went wrong when trying to %s\n", buff);
136 mdp->base = mdp->breakval = mapto;
138 mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
139 result = (void *) mdp->breakval;
140 mdp->breakval = (char *) mdp->breakval + size;
142 /* Memory is already mapped, we only need to increase the breakval: */
143 result = (void *) mdp->breakval;
144 mdp->breakval = (char *) mdp->breakval + size;
150 void *__mmalloc_remap_core(xbt_mheap_t mdp)
152 /* FIXME: Quick hack, needs error checking and other attention. */
154 return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
155 PROT_READ | PROT_WRITE | PROT_EXEC,
156 MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);