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 This file is part of the GNU C Library.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public
19 License along with the GNU C Library; see the file COPYING.LIB. If
20 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #if defined(HAVE_MMAP)
26 #include <unistd.h> /* Prototypes for lseek */
36 #include "mmprivate.h"
38 /* Cache the pagesize for the current host machine. Note that if the host
39 does not readily provide a getpagesize() function, we need to emulate it
40 elsewhere, not clutter up this file with lots of kluges to try to figure
43 static size_t pagesize;
44 #if NEED_DECLARATION_GETPAGESIZE
45 extern int getpagesize (void);
48 #define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \
51 /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return
54 #define MAP_PRIVATE_OR_SHARED(MDP) ((MDP -> flags & MMALLOC_DEVZERO) \
58 /* Return MAP_ANONYMOUS if MDP uses anonymous mapping. Otherwise, return 0 */
60 #define MAP_IS_ANONYMOUS(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
64 /* Return -1 if MDP uses anonymous mapping. Otherwise, return MDP->FD */
65 #define MAP_ANON_OR_FD(MDP) (((MDP) -> flags & MMALLOC_ANONYMOUS) \
69 /* Get core for the memory region specified by MDP, using SIZE as the
70 amount to either add to or subtract from the existing region. Works
71 like sbrk(), but using mmap(). */
74 __mmalloc_mmap_morecore (struct mdesc *mdp, int size)
77 off_t foffset; /* File offset at which new mapping will start */
78 size_t mapbytes; /* Number of bytes to map */
79 void* moveto; /* Address where we wish to move "break value" to */
80 void* mapto; /* Address we actually mapped to */
81 char buf = 0; /* Single byte to write to extend mapped file */
84 pagesize = getpagesize();
88 /* Just return the current "break" value. */
89 result = mdp -> breakval;
93 /* We are deallocating memory. If the amount requested would cause
94 us to try to deallocate back past the base of the mmap'd region
95 then do nothing, and return NULL. Otherwise, deallocate the
96 memory and return the old break value. */
97 if (((char*)mdp -> breakval) + size >= (char*)mdp -> base)
99 result = (void*) mdp -> breakval;
100 mdp -> breakval = (char*) mdp->breakval + size;
101 moveto = PAGE_ALIGN (mdp -> breakval);
102 munmap (moveto, (size_t) (((char*)mdp -> top) - ((char*)moveto)) - 1);
108 /* We are allocating memory. Make sure we have an open file
109 descriptor if not working with anonymous memory. */
110 if ( !(mdp->flags & MMALLOC_ANONYMOUS) && mdp -> fd < 0)
114 else if ((char*)mdp -> breakval + size > (char*)mdp -> top)
116 /* The request would move us past the end of the currently
117 mapped memory, so map in enough more memory to satisfy
118 the request. This means we also have to grow the mapped-to
119 file by an appropriate amount, since mmap cannot be used
121 moveto = PAGE_ALIGN ((char*)mdp -> breakval + size);
122 mapbytes = (char*)moveto - (char*)mdp -> top;
123 foffset = (char*)mdp -> top - (char*)mdp -> base;
126 /* FIXME: Test results of lseek() and write() */
127 lseek (mdp -> fd, foffset + mapbytes - 1, SEEK_SET);
128 write (mdp -> fd, &buf, 1);
131 /* Let's call mmap. Note that it is possible that mdp->top
132 is 0. In this case mmap will choose the address for us */
133 mapto = mmap (mdp->top, mapbytes, PROT_READ | PROT_WRITE,
134 MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) | MAP_FIXED,
135 MAP_ANON_OR_FD(mdp), foffset);
137 if (mapto != (void*) -1){
140 mdp -> base = mdp -> breakval = mapto;
142 mdp -> top = PAGE_ALIGN ((char*)mdp -> breakval + size);
143 result = (void*) mdp -> breakval;
144 mdp -> breakval = (char*)mdp->breakval + size;
149 result = (void*) mdp -> breakval;
150 mdp -> breakval = (char*)mdp->breakval + size;
157 __mmalloc_remap_core (struct mdesc *mdp)
161 /* FIXME: Quick hack, needs error checking and other attention. */
163 base = mmap (mdp -> base, (char*)mdp -> top - (char*)mdp -> base,
164 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE_OR_SHARED (mdp) | MAP_FIXED,
166 return ((void*) base);
170 mmalloc_findbase (int size)
177 flags = MAP_PRIVATE | MAP_ANONYMOUS;
181 flags = MAP_PRIVATE | MAP_FILE;
185 fd = open ("/dev/zero", O_RDWR);
188 return ((void*) NULL);
191 base = mmap (0, size, PROT_READ | PROT_WRITE, flags, fd, 0);
192 if (base != (void*) -1)
194 munmap (base, (size_t) size);
202 /* Don't allow mapping at address zero. We use that value
203 to signal an error return, and besides, it is useful to
204 catch NULL pointers if it is unmapped. Instead start
205 at the next page boundary. */
206 base = (void*) getpagesize ();
208 else if (base == (void*) -1)
212 return ((void*) base);
214 #endif /* defined(HAVE_MMAP) */