* lisp/vc/log-view.el (log-view-mode-map): Bind revert-buffer.
[emacs.git] / src / vm-limit.c
blob63f0f47e2d002f14788d7b17ecf419336500c848
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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/>. */
20 #ifdef emacs
21 #include <config.h>
22 #include <setjmp.h>
23 #include "lisp.h"
24 #endif
26 #include "mem-limits.h"
29 Level number of warnings already issued.
30 0 -- no warnings issued.
31 1 -- 75% warning already issued.
32 2 -- 85% warning already issued.
33 3 -- 95% warning issued; keep warning frequently.
35 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
36 static enum warnlevel warnlevel;
38 typedef POINTER_TYPE *POINTER;
40 /* Function to call to issue a warning;
41 0 means don't issue them. */
42 static void (*warn_function) (const char *);
44 /* Start of data space; can be changed by calling malloc_init. */
45 static POINTER data_space_start;
47 /* Number of bytes of writable memory we can expect to be able to get. */
48 static unsigned long lim_data;
51 #if defined (HAVE_GETRLIMIT) && defined (RLIMIT_AS)
52 static void
53 get_lim_data (void)
55 struct rlimit rlimit;
57 getrlimit (RLIMIT_AS, &rlimit);
58 if (rlimit.rlim_cur == RLIM_INFINITY)
59 lim_data = -1;
60 else
61 lim_data = rlimit.rlim_cur;
64 #else /* not HAVE_GETRLIMIT */
66 #ifdef USG
68 static void
69 get_lim_data (void)
71 extern long ulimit ();
73 lim_data = -1;
75 /* Use the ulimit call, if we seem to have it. */
76 #if !defined (ULIMIT_BREAK_VALUE) || defined (GNU_LINUX)
77 lim_data = ulimit (3, 0);
78 #endif
80 /* If that didn't work, just use the macro's value. */
81 #ifdef ULIMIT_BREAK_VALUE
82 if (lim_data == -1)
83 lim_data = ULIMIT_BREAK_VALUE;
84 #endif
86 lim_data -= (long) data_space_start;
89 #else /* not USG */
90 #ifdef WINDOWSNT
92 static void
93 get_lim_data (void)
95 extern unsigned long reserved_heap_size;
96 lim_data = reserved_heap_size;
99 #else
100 #if !defined (BSD4_2) && !defined (CYGWIN)
102 #ifdef MSDOS
103 void
104 get_lim_data (void)
106 _go32_dpmi_meminfo info;
107 unsigned long lim1, lim2;
109 _go32_dpmi_get_free_memory_information (&info);
110 /* DPMI server of Windows NT and its descendants reports in
111 info.available_memory a much lower amount that is really
112 available, which causes bogus "past 95% of memory limit"
113 warnings. Try to overcome that via circumstantial evidence. */
114 lim1 = info.available_memory;
115 lim2 = info.available_physical_pages;
116 /* DPMI Spec: "Fields that are unavailable will hold -1." */
117 if ((long)lim1 == -1L)
118 lim1 = 0;
119 if ((long)lim2 == -1L)
120 lim2 = 0;
121 else
122 lim2 *= 4096;
123 /* Surely, the available memory is at least what we have physically
124 available, right? */
125 if (lim1 >= lim2)
126 lim_data = lim1;
127 else
128 lim_data = lim2;
129 /* Don't believe they will give us more that 0.5 GB. */
130 if (lim_data > 512U * 1024U * 1024U)
131 lim_data = 512U * 1024U * 1024U;
134 unsigned long
135 ret_lim_data (void)
137 get_lim_data ();
138 return lim_data;
140 #else /* not MSDOS */
141 static void
142 get_lim_data (void)
144 lim_data = vlimit (LIM_DATA, -1);
146 #endif /* not MSDOS */
148 #else /* BSD4_2 || CYGWIN */
150 static void
151 get_lim_data (void)
153 struct rlimit XXrlimit;
155 getrlimit (RLIMIT_DATA, &XXrlimit);
156 #ifdef RLIM_INFINITY
157 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
158 #else
159 lim_data = XXrlimit.rlim_cur; /* soft limit */
160 #endif
162 #endif /* BSD4_2 */
163 #endif /* not WINDOWSNT */
164 #endif /* not USG */
165 #endif /* not HAVE_GETRLIMIT */
167 /* Verify amount of memory available, complaining if we're near the end. */
169 static void
170 check_memory_limits (void)
172 #ifdef REL_ALLOC
173 extern POINTER (*real_morecore) (SIZE);
174 #endif
175 extern POINTER (*__morecore) (SIZE);
177 register POINTER cp;
178 unsigned long five_percent;
179 unsigned long data_size;
180 enum warnlevel new_warnlevel;
182 if (lim_data == 0)
183 get_lim_data ();
184 five_percent = lim_data / 20;
186 /* Find current end of memory and issue warning if getting near max */
187 #ifdef REL_ALLOC
188 if (real_morecore)
189 cp = (char *) (*real_morecore) (0);
190 else
191 #endif
192 cp = (char *) (*__morecore) (0);
193 data_size = (char *) cp - (char *) data_space_start;
195 if (!warn_function)
196 return;
198 /* What level of warning does current memory usage demand? */
199 new_warnlevel
200 = (data_size > five_percent * 19) ? warned_95
201 : (data_size > five_percent * 17) ? warned_85
202 : (data_size > five_percent * 15) ? warned_75
203 : not_warned;
205 /* If we have gone up a level, give the appropriate warning. */
206 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
208 warnlevel = new_warnlevel;
209 switch (warnlevel)
211 case warned_75:
212 (*warn_function) ("Warning: past 75% of memory limit");
213 break;
215 case warned_85:
216 (*warn_function) ("Warning: past 85% of memory limit");
217 break;
219 case warned_95:
220 (*warn_function) ("Warning: past 95% of memory limit");
223 /* Handle going down in usage levels, with some hysteresis. */
224 else
226 /* If we go down below 70% full, issue another 75% warning
227 when we go up again. */
228 if (data_size < five_percent * 14)
229 warnlevel = not_warned;
230 /* If we go down below 80% full, issue another 85% warning
231 when we go up again. */
232 else if (warnlevel > warned_75 && data_size < five_percent * 16)
233 warnlevel = warned_75;
234 /* If we go down below 90% full, issue another 95% warning
235 when we go up again. */
236 else if (warnlevel > warned_85 && data_size < five_percent * 18)
237 warnlevel = warned_85;
240 if (EXCEEDS_LISP_PTR (cp))
241 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
244 #if !defined(CANNOT_DUMP) || !defined(SYSTEM_MALLOC)
245 /* Some systems that cannot dump also cannot implement these. */
248 * Return the address of the start of the data segment prior to
249 * doing an unexec. After unexec the return value is undefined.
250 * See crt0.c for further information and definition of data_start.
252 * Apparently, on BSD systems this is etext at startup. On
253 * USG systems (swapping) this is highly mmu dependent and
254 * is also dependent on whether or not the program is running
255 * with shared text. Generally there is a (possibly large)
256 * gap between end of text and start of data with shared text.
260 char *
261 start_of_data (void)
263 #ifdef BSD_SYSTEM
264 extern char etext;
265 return (POINTER)(&etext);
266 #elif defined DATA_START
267 return ((POINTER) DATA_START);
268 #elif defined ORDINARY_LINK
270 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
271 * data_start isn't defined. We take the address of environ, which
272 * is known to live at or near the start of the system crt0.c, and
273 * we don't sweat the handful of bytes that might lose.
275 extern char **environ;
276 return ((POINTER) &environ);
277 #else
278 extern int data_start;
279 return ((POINTER) &data_start);
280 #endif
282 #endif /* (not CANNOT_DUMP or not SYSTEM_MALLOC) */
284 /* Enable memory usage warnings.
285 START says where the end of pure storage is.
286 WARNFUN specifies the function to call to issue a warning. */
288 void
289 memory_warnings (POINTER start, void (*warnfun) (const char *))
291 extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
293 if (start)
294 data_space_start = start;
295 else
296 data_space_start = start_of_data ();
298 warn_function = warnfun;
299 __after_morecore_hook = check_memory_limits;
301 /* Force data limit to be recalculated on each run. */
302 lim_data = 0;
305 /* arch-tag: eab04eda-1f69-447a-8d9f-95f0a3983ca5
306 (do not change this comment) */