1 /* Support for an sbrk-like function that uses mmap.
2 Copyright 1992, 2000 Free Software Foundation, Inc.
4 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com */
6 /* Copyright (c) 2010. The SimGrid Team.
7 * All rights reserved. */
9 /* This program is free software; you can redistribute it and/or modify it
10 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #define MAP_ANONYMOUS MAP_ANON
17 #include <unistd.h> /* Prototypes for lseek */
23 #include "mmprivate.h"
25 /* Cache the pagesize for the current host machine. Note that if the host
26 does not readily provide a getpagesize() function, we need to emulate it
27 elsewhere, not clutter up this file with lots of kluges to try to figure
30 static size_t pagesize;
32 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \
35 /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return
37 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
41 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
42 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
46 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
47 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
51 /* Get core for the memory region specified by MDP, using SIZE as the
52 amount to either add to or subtract from the existing region. Works
53 like sbrk(), but using mmap().
55 It never returns NULL. Instead, it dies verbosely on errors. */
57 void *mmorecore(struct mdesc *mdp, int size)
60 void *result; // please keep it uninitialized to track issues
61 off_t foffset; /* File offset at which new mapping will start */
62 size_t mapbytes; /* Number of bytes to map */
63 void *moveto; /* Address where we wish to move "break value" to */
64 void *mapto; /* Address we actually mapped to */
65 char buf = 0; /* Single byte to write to extend mapped file */
68 pagesize = getpagesize();
71 /* Just return the current "break" value. */
72 result = mdp->breakval;
74 } else if (size < 0) {
75 /* We are deallocating memory. If the amount requested would cause
76 us to try to deallocate back past the base of the mmap'd region
77 then die verbosely. Otherwise, deallocate the memory and return
78 the old break value. */
79 if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
80 result = (void *) mdp->breakval;
81 mdp->breakval = (char *) mdp->breakval + size;
82 moveto = PAGE_ALIGN(mdp->breakval);
84 (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
87 fprintf(stderr,"Internal error: mmap was asked to deallocate more memory than it previously allocated. Bailling out now!\n");
91 /* We are allocating memory. Make sure we have an open file
92 descriptor if not working with anonymous memory. */
93 if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
94 fprintf(stderr,"Internal error: mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags.\n",mdp->fd);
96 } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
97 /* The request would move us past the end of the currently
98 mapped memory, so map in enough more memory to satisfy
99 the request. This means we also have to grow the mapped-to
100 file by an appropriate amount, since mmap cannot be used
102 moveto = PAGE_ALIGN((char *) mdp->breakval + size);
103 mapbytes = (char *) moveto - (char *) mdp->top;
104 foffset = (char *) mdp->top - (char *) mdp->base;
107 /* FIXME: Test results of lseek() */
108 lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
109 test = write(mdp->fd, &buf, 1);
111 fprintf(stderr,"Internal error: write to mmap'ed fd failed! error: %s", strerror(errno));
116 /* Let's call mmap. Note that it is possible that mdp->top
117 is 0. In this case mmap will choose the address for us */
118 mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
119 MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
120 MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
122 if (mapto == (void *) -1/* That's MAP_FAILED */) {
123 fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s",strerror(errno));
128 mdp->base = mdp->breakval = mapto;
130 mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
131 result = (void *) mdp->breakval;
132 mdp->breakval = (char *) mdp->breakval + size;
134 result = (void *) mdp->breakval;
135 mdp->breakval = (char *) mdp->breakval + size;
141 void *__mmalloc_remap_core(xbt_mheap_t mdp)
143 /* FIXME: Quick hack, needs error checking and other attention. */
145 return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
146 PROT_READ | PROT_WRITE | PROT_EXEC,
147 MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);