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"
26 /* Cache the pagesize for the current host machine. Note that if the host
27 does not readily provide a getpagesize() function, we need to emulate it
28 elsewhere, not clutter up this file with lots of kluges to try to figure
31 static size_t pagesize;
33 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \
36 /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return
38 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
42 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
43 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
47 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
48 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
52 /* Get core for the memory region specified by MDP, using SIZE as the
53 amount to either add to or subtract from the existing region. Works
54 like sbrk(), but using mmap(). */
56 void *mmorecore(struct mdesc *mdp, int size)
60 off_t foffset; /* File offset at which new mapping will start */
61 size_t mapbytes; /* Number of bytes to map */
62 void *moveto; /* Address where we wish to move "break value" to */
63 void *mapto; /* Address we actually mapped to */
64 char buf = 0; /* Single byte to write to extend mapped file */
67 pagesize = getpagesize();
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 do nothing, and return NULL. Otherwise, deallocate the
77 memory and return 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);
87 /* We are allocating memory. Make sure we have an open file
88 descriptor if not working with anonymous memory. */
89 if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
90 THROWF(system_error,0,"mmap file descriptor <0 (%d), without MMALLOC_ANONYMOUS being in the flags",mdp->fd);
92 } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
93 /* The request would move us past the end of the currently
94 mapped memory, so map in enough more memory to satisfy
95 the request. This means we also have to grow the mapped-to
96 file by an appropriate amount, since mmap cannot be used
98 moveto = PAGE_ALIGN((char *) mdp->breakval + size);
99 mapbytes = (char *) moveto - (char *) mdp->top;
100 foffset = (char *) mdp->top - (char *) mdp->base;
103 /* FIXME: Test results of lseek() */
104 lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
105 test = write(mdp->fd, &buf, 1);
107 THROWF(system_error, 0, "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 mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
113 MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
114 MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
116 if (mapto != (void *) -1/* That's MAP_FAILED */) {
119 mdp->base = mdp->breakval = mapto;
121 mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
122 result = (void *) mdp->breakval;
123 mdp->breakval = (char *) mdp->breakval + size;
125 THROWF(system_error,0,"mmap returned MAP_FAILED! error: %s",strerror(errno));
128 result = (void *) mdp->breakval;
129 mdp->breakval = (char *) mdp->breakval + size;
135 void *__mmalloc_remap_core(xbt_mheap_t mdp)
137 /* FIXME: Quick hack, needs error checking and other attention. */
139 return mmap(mdp->base, (char*) mdp->top - (char*) mdp->base,
140 PROT_READ | PROT_WRITE | PROT_EXEC,
141 MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);