(with-help-window): Return last value in BODY.
[emacs.git] / src / vm-limit.c
blobf80e5c47604943257b67157e642a4abf38ba4b33
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008 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 "lisp.h"
23 #endif
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
32 #include "mem-limits.h"
34 #ifdef HAVE_GETRLIMIT
35 #include <sys/resource.h>
36 #endif
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.
45 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
47 static enum warnlevel warnlevel;
49 /* Function to call to issue a warning;
50 0 means don't issue them. */
51 static void (*warn_function) ();
53 /* Start of data space; can be changed by calling malloc_init. */
54 static POINTER data_space_start;
56 /* Number of bytes of writable memory we can expect to be able to get. */
57 static unsigned long lim_data;
60 #ifdef NO_LIM_DATA
61 static void
62 get_lim_data ()
64 lim_data = -1;
66 #else /* not NO_LIM_DATA */
68 #if defined (HAVE_GETRLIMIT) && defined (RLIMIT_AS)
69 static void
70 get_lim_data ()
72 struct rlimit rlimit;
74 getrlimit (RLIMIT_AS, &rlimit);
75 if (rlimit.rlim_cur == RLIM_INFINITY)
76 lim_data = -1;
77 else
78 lim_data = rlimit.rlim_cur;
81 #else /* not HAVE_GETRLIMIT */
83 #ifdef USG
85 static void
86 get_lim_data ()
88 extern long ulimit ();
90 lim_data = -1;
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
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
103 lim_data -= (long) data_space_start;
106 #else /* not USG */
107 #ifdef WINDOWSNT
109 static void
110 get_lim_data ()
112 extern unsigned long reserved_heap_size;
113 lim_data = reserved_heap_size;
116 #else
117 #if !defined (BSD4_2) && !defined (__osf__)
119 #ifdef MSDOS
120 void
121 get_lim_data ()
123 _go32_dpmi_meminfo info;
125 _go32_dpmi_get_free_memory_information (&info);
126 lim_data = info.available_memory;
128 #else /* not MSDOS */
129 static void
130 get_lim_data ()
132 lim_data = vlimit (LIM_DATA, -1);
134 #endif /* not MSDOS */
136 #else /* BSD4_2 */
138 static void
139 get_lim_data ()
141 struct rlimit XXrlimit;
143 getrlimit (RLIMIT_DATA, &XXrlimit);
144 #ifdef RLIM_INFINITY
145 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
146 #else
147 lim_data = XXrlimit.rlim_cur; /* soft limit */
148 #endif
150 #endif /* BSD4_2 */
151 #endif /* not WINDOWSNT */
152 #endif /* not USG */
153 #endif /* not HAVE_GETRLIMIT */
154 #endif /* not NO_LIM_DATA */
156 /* Verify amount of memory available, complaining if we're near the end. */
158 static void
159 check_memory_limits ()
161 #ifdef REL_ALLOC
162 extern POINTER (*real_morecore) ();
163 #endif
164 extern POINTER (*__morecore) ();
166 register POINTER cp;
167 unsigned long five_percent;
168 unsigned long data_size;
169 enum warnlevel new_warnlevel;
171 if (lim_data == 0)
172 get_lim_data ();
173 five_percent = lim_data / 20;
175 /* Find current end of memory and issue warning if getting near max */
176 #ifdef REL_ALLOC
177 if (real_morecore)
178 cp = (char *) (*real_morecore) (0);
179 else
180 #endif
181 cp = (char *) (*__morecore) (0);
182 data_size = (char *) cp - (char *) data_space_start;
184 if (!warn_function)
185 return;
187 /* What level of warning does current memory usage demand? */
188 new_warnlevel
189 = (data_size > five_percent * 19) ? warned_95
190 : (data_size > five_percent * 17) ? warned_85
191 : (data_size > five_percent * 15) ? warned_75
192 : not_warned;
194 /* If we have gone up a level, give the appropriate warning. */
195 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
197 warnlevel = new_warnlevel;
198 switch (warnlevel)
200 case warned_75:
201 (*warn_function) ("Warning: past 75% of memory limit");
202 break;
204 case warned_85:
205 (*warn_function) ("Warning: past 85% of memory limit");
206 break;
208 case warned_95:
209 (*warn_function) ("Warning: past 95% of memory limit");
212 /* Handle going down in usage levels, with some hysteresis. */
213 else
215 /* If we go down below 70% full, issue another 75% warning
216 when we go up again. */
217 if (data_size < five_percent * 14)
218 warnlevel = not_warned;
219 /* If we go down below 80% full, issue another 85% warning
220 when we go up again. */
221 else if (warnlevel > warned_75 && data_size < five_percent * 16)
222 warnlevel = warned_75;
223 /* If we go down below 90% full, issue another 95% warning
224 when we go up again. */
225 else if (warnlevel > warned_85 && data_size < five_percent * 18)
226 warnlevel = warned_85;
229 if (EXCEEDS_LISP_PTR (cp))
230 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
233 /* Enable memory usage warnings.
234 START says where the end of pure storage is.
235 WARNFUN specifies the function to call to issue a warning. */
237 void
238 memory_warnings (start, warnfun)
239 POINTER start;
240 void (*warnfun) ();
242 extern void (* __after_morecore_hook) (); /* From gmalloc.c */
244 if (start)
245 data_space_start = start;
246 else
247 data_space_start = start_of_data ();
249 warn_function = warnfun;
250 __after_morecore_hook = check_memory_limits;
252 /* Force data limit to be recalculated on each run. */
253 lim_data = 0;
256 /* arch-tag: eab04eda-1f69-447a-8d9f-95f0a3983ca5
257 (do not change this comment) */