From 2000-02-17 RodneyBrown@pmsc.com:
[binutils-gdb.git] / mmalloc / mmap-sup.c
1 /* Support for an sbrk-like function that uses mmap.
2 Copyright 1992, 2000 Free Software Foundation, Inc.
3
4 Contributed by Fred Fish at Cygnus Support. fnf@cygnus.com
5
6 This file is part of the GNU C Library.
7
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.
12
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.
17
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. */
22
23 #if defined(HAVE_MMAP)
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h> /* Prototypes for lseek */
27 #endif
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31
32 #ifndef SEEK_SET
33 #define SEEK_SET 0
34 #endif
35
36 #include "mmprivate.h"
37
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
41 it out. */
42
43 static size_t pagesize;
44 extern int getpagesize PARAMS ((void));
45
46 #define PAGE_ALIGN(addr) (caddr_t) (((long)(addr) + pagesize - 1) & \
47 ~(pagesize - 1))
48
49 /* Get core for the memory region specified by MDP, using SIZE as the
50 amount to either add to or subtract from the existing region. Works
51 like sbrk(), but using mmap(). */
52
53 PTR
54 __mmalloc_mmap_morecore (mdp, size)
55 struct mdesc *mdp;
56 int size;
57 {
58 PTR result = NULL;
59 off_t foffset; /* File offset at which new mapping will start */
60 size_t mapbytes; /* Number of bytes to map */
61 caddr_t moveto; /* Address where we wish to move "break value" to */
62 caddr_t mapto; /* Address we actually mapped to */
63 char buf = 0; /* Single byte to write to extend mapped file */
64
65 if (pagesize == 0)
66 {
67 pagesize = getpagesize ();
68 }
69 if (size == 0)
70 {
71 /* Just return the current "break" value. */
72 result = mdp -> breakval;
73 }
74 else if (size < 0)
75 {
76 /* We are deallocating memory. If the amount requested would cause
77 us to try to deallocate back past the base of the mmap'd region
78 then do nothing, and return NULL. Otherwise, deallocate the
79 memory and return the old break value. */
80 if (mdp -> breakval + size >= mdp -> base)
81 {
82 result = (PTR) mdp -> breakval;
83 mdp -> breakval += size;
84 moveto = PAGE_ALIGN (mdp -> breakval);
85 munmap (moveto, (size_t) (mdp -> top - moveto));
86 mdp -> top = moveto;
87 }
88 }
89 else
90 {
91 /* We are allocating memory. Make sure we have an open file
92 descriptor and then go on to get the memory. */
93 if (mdp -> fd < 0)
94 {
95 result = NULL;
96 }
97 else if (mdp -> breakval + size > mdp -> top)
98 {
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
103 to extend a file. */
104 moveto = PAGE_ALIGN (mdp -> breakval + size);
105 mapbytes = moveto - mdp -> top;
106 foffset = mdp -> top - mdp -> base;
107 /* FIXME: Test results of lseek() and write() */
108 lseek (mdp -> fd, foffset + mapbytes - 1, SEEK_SET);
109 write (mdp -> fd, &buf, 1);
110 if (mdp -> base == 0)
111 {
112 /* Let mmap pick the map start address */
113 mapto = mmap (0, mapbytes, PROT_READ | PROT_WRITE,
114 MAP_SHARED, mdp -> fd, foffset);
115 if (mapto != (caddr_t) -1)
116 {
117 mdp -> base = mdp -> breakval = mapto;
118 mdp -> top = mdp -> base + mapbytes;
119 result = (PTR) mdp -> breakval;
120 mdp -> breakval += size;
121 }
122 }
123 else
124 {
125 mapto = mmap (mdp -> top, mapbytes, PROT_READ | PROT_WRITE,
126 MAP_SHARED | MAP_FIXED, mdp -> fd, foffset);
127 if (mapto == mdp -> top)
128 {
129 mdp -> top = moveto;
130 result = (PTR) mdp -> breakval;
131 mdp -> breakval += size;
132 }
133 }
134 }
135 else
136 {
137 result = (PTR) mdp -> breakval;
138 mdp -> breakval += size;
139 }
140 }
141 return (result);
142 }
143
144 PTR
145 __mmalloc_remap_core (mdp)
146 struct mdesc *mdp;
147 {
148 caddr_t base;
149
150 /* FIXME: Quick hack, needs error checking and other attention. */
151
152 base = mmap (mdp -> base, mdp -> top - mdp -> base,
153 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
154 mdp -> fd, 0);
155 return ((PTR) base);
156 }
157
158 PTR
159 mmalloc_findbase (size)
160 int size;
161 {
162 int fd;
163 int flags;
164 caddr_t base = NULL;
165
166 #ifdef MAP_ANONYMOUS
167 flags = MAP_SHARED | MAP_ANONYMOUS;
168 fd = -1;
169 #else
170 #ifdef MAP_FILE
171 flags = MAP_SHARED | MAP_FILE;
172 #else
173 flags = MAP_SHARED;
174 #endif
175 fd = open ("/dev/zero", O_RDWR);
176 if (fd != -1)
177 {
178 return ((PTR) NULL);
179 }
180 #endif
181 base = mmap (0, size, PROT_READ | PROT_WRITE, flags, fd, 0);
182 if (base != (caddr_t) -1)
183 {
184 munmap (base, (size_t) size);
185 }
186 if (fd != -1)
187 {
188 close (fd);
189 }
190 if (base == 0)
191 {
192 /* Don't allow mapping at address zero. We use that value
193 to signal an error return, and besides, it is useful to
194 catch NULL pointers if it is unmapped. Instead start
195 at the next page boundary. */
196 base = (caddr_t) getpagesize ();
197 }
198 else if (base == (caddr_t) -1)
199 {
200 base = NULL;
201 }
202 return ((PTR) base);
203 }
204
205 #else /* defined(HAVE_MMAP) */
206 /* Prevent "empty translation unit" warnings from the idiots at X3J11. */
207 static char ansi_c_idiots = 69;
208 #endif /* defined(HAVE_MMAP) */