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 */
27 #include "mmprivate.h"
29 /* Cache the pagesize for the current host machine. Note that if the host
30 does not readily provide a getpagesize() function, we need to emulate it
31 elsewhere, not clutter up this file with lots of kluges to try to figure
34 static size_t pagesize;
35 #if NEED_DECLARATION_GETPAGESIZE
36 extern int getpagesize(void);
39 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \
42 /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return
45 #define MAP_PRIVATE_OR_SHARED(MDP) (( MDP -> flags & MMALLOC_ANONYMOUS) \
49 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
51 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
55 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
56 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
60 /* Get core for the memory region specified by MDP, using SIZE as the
61 amount to either add to or subtract from the existing region. Works
62 like sbrk(), but using mmap(). */
64 void *__mmalloc_mmap_morecore(struct mdesc *mdp, int size)
68 off_t foffset; /* File offset at which new mapping will start */
69 size_t mapbytes; /* Number of bytes to map */
70 void *moveto; /* Address where we wish to move "break value" to */
71 void *mapto; /* Address we actually mapped to */
72 char buf = 0; /* Single byte to write to extend mapped file */
75 pagesize = getpagesize();
78 /* Just return the current "break" value. */
79 result = mdp->breakval;
80 } else if (size < 0) {
81 /* We are deallocating memory. If the amount requested would cause
82 us to try to deallocate back past the base of the mmap'd region
83 then do nothing, and return NULL. Otherwise, deallocate the
84 memory and return the old break value. */
85 if (((char *) mdp->breakval) + size >= (char *) mdp->base) {
86 result = (void *) mdp->breakval;
87 mdp->breakval = (char *) mdp->breakval + size;
88 moveto = PAGE_ALIGN(mdp->breakval);
90 (size_t) (((char *) mdp->top) - ((char *) moveto)) - 1);
94 /* We are allocating memory. Make sure we have an open file
95 descriptor if not working with anonymous memory. */
96 if (!(mdp->flags & MMALLOC_ANONYMOUS) && mdp->fd < 0) {
98 } else if ((char *) mdp->breakval + size > (char *) mdp->top) {
99 /* The request would move us past the end of the currently
100 mapped memory, so map in enough more memory to satisfy
101 the request. This means we also have to grow the mapped-to
102 file by an appropriate amount, since mmap cannot be used
104 moveto = PAGE_ALIGN((char *) mdp->breakval + size);
105 mapbytes = (char *) moveto - (char *) mdp->top;
106 foffset = (char *) mdp->top - (char *) mdp->base;
109 /* FIXME: Test results of lseek() and write() */
110 lseek(mdp->fd, foffset + mapbytes - 1, SEEK_SET);
111 test = write(mdp->fd, &buf, 1);
114 /* Let's call mmap. Note that it is possible that mdp->top
115 is 0. In this case mmap will choose the address for us */
116 mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
117 MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
118 MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
120 if (mapto != (void *) -1) {
123 mdp->base = mdp->breakval = mapto;
125 mdp->top = PAGE_ALIGN((char *) mdp->breakval + size);
126 result = (void *) mdp->breakval;
127 mdp->breakval = (char *) mdp->breakval + size;
130 result = (void *) mdp->breakval;
131 mdp->breakval = (char *) mdp->breakval + size;
137 void *__mmalloc_remap_core(struct mdesc *mdp)
141 /* FIXME: Quick hack, needs error checking and other attention. */
143 base = mmap(mdp->base, (char *) mdp->top - (char *) mdp->base,
144 PROT_READ | PROT_WRITE | PROT_EXEC,
145 MAP_PRIVATE_OR_SHARED(mdp) | MAP_FIXED, mdp->fd, 0);
146 return ((void *) base);
149 void *mmalloc_findbase(int size)
156 flags = MAP_PRIVATE | MAP_ANONYMOUS;
160 flags = MAP_PRIVATE | MAP_FILE;
164 fd = open("/dev/zero", O_RDWR);
166 return ((void *) NULL);
169 base = mmap(0, size, PROT_READ | PROT_WRITE, flags, fd, 0);
170 if (base != (void *) -1) {
171 munmap(base, (size_t) size);
177 /* Don't allow mapping at address zero. We use that value
178 to signal an error return, and besides, it is useful to
179 catch NULL pointers if it is unmapped. Instead start
180 at the next page boundary. */
181 base = (void *) (long) getpagesize();
182 } else if (base == (void *) -1) {
185 return ((void *) base);