]> code.delx.au - gnu-emacs/blob - src/vm-limit.c
(Fvertical_motion): Be sure to set it_overshoot_expected if
[gnu-emacs] / src / vm-limit.c
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifdef emacs
21 #include <config.h>
22 #include "lisp.h"
23 #endif
24
25 #ifndef emacs
26 #include <stddef.h>
27 typedef size_t SIZE;
28 typedef void *POINTER;
29 #define EXCEEDS_LISP_PTR(x) 0
30 #endif
31
32 #include "mem-limits.h"
33
34 #ifdef HAVE_GETRLIMIT
35 #include <sys/resource.h>
36 #endif
37
38 /*
39 Level number of warnings already issued.
40 0 -- no warnings issued.
41 1 -- 75% warning already issued.
42 2 -- 85% warning already issued.
43 3 -- 95% warning issued; keep warning frequently.
44 */
45 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
46
47 static enum warnlevel warnlevel;
48
49 /* Function to call to issue a warning;
50 0 means don't issue them. */
51 static void (*warn_function) ();
52
53 /* Start of data space; can be changed by calling malloc_init. */
54 static POINTER data_space_start;
55
56 /* Number of bytes of writable memory we can expect to be able to get. */
57 static unsigned long lim_data;
58 \f
59
60 #ifdef NO_LIM_DATA
61 static void
62 get_lim_data ()
63 {
64 lim_data = -1;
65 }
66 #else /* not NO_LIM_DATA */
67
68 #if defined (HAVE_GETRLIMIT) && defined (RLIMIT_AS)
69 static void
70 get_lim_data ()
71 {
72 struct rlimit rlimit;
73
74 getrlimit (RLIMIT_AS, &rlimit);
75 if (rlimit.rlim_cur == RLIM_INFINITY)
76 lim_data = -1;
77 else
78 lim_data = rlimit.rlim_cur;
79 }
80
81 #else /* not HAVE_GETRLIMIT */
82
83 #ifdef USG
84
85 static void
86 get_lim_data ()
87 {
88 extern long ulimit ();
89
90 lim_data = -1;
91
92 /* Use the ulimit call, if we seem to have it. */
93 #if !defined (ULIMIT_BREAK_VALUE) || defined (GNU_LINUX)
94 lim_data = ulimit (3, 0);
95 #endif
96
97 /* If that didn't work, just use the macro's value. */
98 #ifdef ULIMIT_BREAK_VALUE
99 if (lim_data == -1)
100 lim_data = ULIMIT_BREAK_VALUE;
101 #endif
102
103 lim_data -= (long) data_space_start;
104 }
105
106 #else /* not USG */
107 #ifdef WINDOWSNT
108
109 static void
110 get_lim_data ()
111 {
112 extern unsigned long reserved_heap_size;
113 lim_data = reserved_heap_size;
114 }
115
116 #else
117 #if !defined (BSD4_2) && !defined (__osf__)
118
119 #ifdef MSDOS
120 void
121 get_lim_data ()
122 {
123 _go32_dpmi_meminfo info;
124 unsigned long lim1, lim2;
125
126 _go32_dpmi_get_free_memory_information (&info);
127 /* DPMI server of Windows NT and its descendants reports in
128 info.available_memory a much lower amount that is really
129 available, which causes bogus "past 95% of memory limit"
130 warnings. Try to overcome that via circumstantial evidence. */
131 lim1 = info.available_memory;
132 lim2 = info.available_physical_pages * 4096;
133 /* DPMI Spec: "Fields that are unavailable will hold -1." */
134 if ((long)lim1 == -1L)
135 lim1 = 0;
136 if ((long)lim2 == -1L)
137 lim2 = 0;
138 /* Surely, the available memory is at least what we have physically
139 available, right? */
140 if (lim1 > lim2)
141 lim_data = lim1;
142 else
143 lim_data = lim2;
144 /* Don't believe they will give us more that 0.5 GB. */
145 if (lim_data > 512 * 1024 * 1024)
146 lim_data = 512 * 1024 * 1024;
147 }
148 #else /* not MSDOS */
149 static void
150 get_lim_data ()
151 {
152 lim_data = vlimit (LIM_DATA, -1);
153 }
154 #endif /* not MSDOS */
155
156 #else /* BSD4_2 */
157
158 static void
159 get_lim_data ()
160 {
161 struct rlimit XXrlimit;
162
163 getrlimit (RLIMIT_DATA, &XXrlimit);
164 #ifdef RLIM_INFINITY
165 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
166 #else
167 lim_data = XXrlimit.rlim_cur; /* soft limit */
168 #endif
169 }
170 #endif /* BSD4_2 */
171 #endif /* not WINDOWSNT */
172 #endif /* not USG */
173 #endif /* not HAVE_GETRLIMIT */
174 #endif /* not NO_LIM_DATA */
175 \f
176 /* Verify amount of memory available, complaining if we're near the end. */
177
178 static void
179 check_memory_limits ()
180 {
181 #ifdef REL_ALLOC
182 extern POINTER (*real_morecore) ();
183 #endif
184 extern POINTER (*__morecore) ();
185
186 register POINTER cp;
187 unsigned long five_percent;
188 unsigned long data_size;
189 enum warnlevel new_warnlevel;
190
191 if (lim_data == 0)
192 get_lim_data ();
193 five_percent = lim_data / 20;
194
195 /* Find current end of memory and issue warning if getting near max */
196 #ifdef REL_ALLOC
197 if (real_morecore)
198 cp = (char *) (*real_morecore) (0);
199 else
200 #endif
201 cp = (char *) (*__morecore) (0);
202 data_size = (char *) cp - (char *) data_space_start;
203
204 if (!warn_function)
205 return;
206
207 /* What level of warning does current memory usage demand? */
208 new_warnlevel
209 = (data_size > five_percent * 19) ? warned_95
210 : (data_size > five_percent * 17) ? warned_85
211 : (data_size > five_percent * 15) ? warned_75
212 : not_warned;
213
214 /* If we have gone up a level, give the appropriate warning. */
215 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
216 {
217 warnlevel = new_warnlevel;
218 switch (warnlevel)
219 {
220 case warned_75:
221 (*warn_function) ("Warning: past 75% of memory limit");
222 break;
223
224 case warned_85:
225 (*warn_function) ("Warning: past 85% of memory limit");
226 break;
227
228 case warned_95:
229 (*warn_function) ("Warning: past 95% of memory limit");
230 }
231 }
232 /* Handle going down in usage levels, with some hysteresis. */
233 else
234 {
235 /* If we go down below 70% full, issue another 75% warning
236 when we go up again. */
237 if (data_size < five_percent * 14)
238 warnlevel = not_warned;
239 /* If we go down below 80% full, issue another 85% warning
240 when we go up again. */
241 else if (warnlevel > warned_75 && data_size < five_percent * 16)
242 warnlevel = warned_75;
243 /* If we go down below 90% full, issue another 95% warning
244 when we go up again. */
245 else if (warnlevel > warned_85 && data_size < five_percent * 18)
246 warnlevel = warned_85;
247 }
248
249 if (EXCEEDS_LISP_PTR (cp))
250 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
251 }
252 \f
253 /* Enable memory usage warnings.
254 START says where the end of pure storage is.
255 WARNFUN specifies the function to call to issue a warning. */
256
257 void
258 memory_warnings (start, warnfun)
259 POINTER start;
260 void (*warnfun) ();
261 {
262 extern void (* __after_morecore_hook) (); /* From gmalloc.c */
263
264 if (start)
265 data_space_start = start;
266 else
267 data_space_start = start_of_data ();
268
269 warn_function = warnfun;
270 __after_morecore_hook = check_memory_limits;
271
272 /* Force data limit to be recalculated on each run. */
273 lim_data = 0;
274 }
275
276 /* arch-tag: eab04eda-1f69-447a-8d9f-95f0a3983ca5
277 (do not change this comment) */