* ccl.c (CCL_CODE_RANGE): Allow negative numbers. (Bug#8751)
[bpt/emacs.git] / src / vm-limit.c
CommitLineData
94d7c01a 1/* Functions for memory limit warnings.
73b0cd50 2 Copyright (C) 1990, 1992, 2001-2011 Free Software Foundation, Inc.
94d7c01a
JA
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
94d7c01a 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
94d7c01a
JA
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
94d7c01a 18
18160b98 19#include <config.h>
d7306fe6 20#include <setjmp.h>
94d7c01a 21#include "lisp.h"
e231fd42 22#include "mem-limits.h"
94d7c01a
JA
23
24/*
25 Level number of warnings already issued.
26 0 -- no warnings issued.
27 1 -- 75% warning already issued.
28 2 -- 85% warning already issued.
3b672b8f 29 3 -- 95% warning issued; keep warning frequently.
94d7c01a 30*/
395d3972 31enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
395d3972 32static enum warnlevel warnlevel;
94d7c01a 33
8848b728
JD
34typedef POINTER_TYPE *POINTER;
35
94d7c01a
JA
36/* Function to call to issue a warning;
37 0 means don't issue them. */
a8fe7202 38static void (*warn_function) (const char *);
94d7c01a 39
395d3972
RS
40/* Start of data space; can be changed by calling malloc_init. */
41static POINTER data_space_start;
42
43/* Number of bytes of writable memory we can expect to be able to get. */
44static unsigned long lim_data;
45\f
46
51757187
AS
47#if defined (HAVE_GETRLIMIT) && defined (RLIMIT_AS)
48static void
d3da34e0 49get_lim_data (void)
51757187
AS
50{
51 struct rlimit rlimit;
52
53 getrlimit (RLIMIT_AS, &rlimit);
54 if (rlimit.rlim_cur == RLIM_INFINITY)
55 lim_data = -1;
56 else
57 lim_data = rlimit.rlim_cur;
58}
59
60#else /* not HAVE_GETRLIMIT */
61
395d3972
RS
62#ifdef USG
63
64static void
7c3320d8 65get_lim_data (void)
395d3972
RS
66{
67 extern long ulimit ();
68
69 lim_data = -1;
70
71 /* Use the ulimit call, if we seem to have it. */
72#if !defined (ULIMIT_BREAK_VALUE) || defined (GNU_LINUX)
73 lim_data = ulimit (3, 0);
74#endif
75
76 /* If that didn't work, just use the macro's value. */
77#ifdef ULIMIT_BREAK_VALUE
78 if (lim_data == -1)
79 lim_data = ULIMIT_BREAK_VALUE;
80#endif
81
82 lim_data -= (long) data_space_start;
83}
84
85#else /* not USG */
86#ifdef WINDOWSNT
87
88static void
7c3320d8 89get_lim_data (void)
395d3972
RS
90{
91 extern unsigned long reserved_heap_size;
92 lim_data = reserved_heap_size;
93}
94
95#else
0441987e 96#if !defined (BSD4_2) && !defined (CYGWIN)
395d3972
RS
97
98#ifdef MSDOS
99void
7c3320d8 100get_lim_data (void)
395d3972
RS
101{
102 _go32_dpmi_meminfo info;
8a445f76 103 unsigned long lim1, lim2;
395d3972
RS
104
105 _go32_dpmi_get_free_memory_information (&info);
8a445f76
EZ
106 /* DPMI server of Windows NT and its descendants reports in
107 info.available_memory a much lower amount that is really
108 available, which causes bogus "past 95% of memory limit"
109 warnings. Try to overcome that via circumstantial evidence. */
110 lim1 = info.available_memory;
7cf94eac 111 lim2 = info.available_physical_pages;
8a445f76
EZ
112 /* DPMI Spec: "Fields that are unavailable will hold -1." */
113 if ((long)lim1 == -1L)
114 lim1 = 0;
115 if ((long)lim2 == -1L)
116 lim2 = 0;
7cf94eac
EZ
117 else
118 lim2 *= 4096;
8a445f76
EZ
119 /* Surely, the available memory is at least what we have physically
120 available, right? */
7cf94eac 121 if (lim1 >= lim2)
8a445f76
EZ
122 lim_data = lim1;
123 else
124 lim_data = lim2;
125 /* Don't believe they will give us more that 0.5 GB. */
7cf94eac
EZ
126 if (lim_data > 512U * 1024U * 1024U)
127 lim_data = 512U * 1024U * 1024U;
395d3972 128}
ec06ec19
EZ
129
130unsigned long
7c3320d8 131ret_lim_data (void)
ec06ec19
EZ
132{
133 get_lim_data ();
134 return lim_data;
135}
395d3972
RS
136#else /* not MSDOS */
137static void
7c3320d8 138get_lim_data (void)
395d3972
RS
139{
140 lim_data = vlimit (LIM_DATA, -1);
141}
142#endif /* not MSDOS */
143
0441987e 144#else /* BSD4_2 || CYGWIN */
395d3972
RS
145
146static void
7c3320d8 147get_lim_data (void)
395d3972
RS
148{
149 struct rlimit XXrlimit;
150
151 getrlimit (RLIMIT_DATA, &XXrlimit);
152#ifdef RLIM_INFINITY
153 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
154#else
155 lim_data = XXrlimit.rlim_cur; /* soft limit */
156#endif
157}
158#endif /* BSD4_2 */
159#endif /* not WINDOWSNT */
160#endif /* not USG */
51757187 161#endif /* not HAVE_GETRLIMIT */
395d3972
RS
162\f
163/* Verify amount of memory available, complaining if we're near the end. */
94d7c01a 164
fd065466 165static void
d3da34e0 166check_memory_limits (void)
94d7c01a 167{
968e9c04 168#ifdef REL_ALLOC
63f9a672 169 extern POINTER (*real_morecore) (SIZE);
968e9c04 170#endif
63f9a672 171 extern POINTER (*__morecore) (SIZE);
134994ae 172
94d7c01a 173 register POINTER cp;
46b3623d
RS
174 unsigned long five_percent;
175 unsigned long data_size;
395d3972 176 enum warnlevel new_warnlevel;
94d7c01a
JA
177
178 if (lim_data == 0)
179 get_lim_data ();
3b672b8f 180 five_percent = lim_data / 20;
94d7c01a
JA
181
182 /* Find current end of memory and issue warning if getting near max */
968e9c04
AI
183#ifdef REL_ALLOC
184 if (real_morecore)
185 cp = (char *) (*real_morecore) (0);
186 else
187#endif
fd065466 188 cp = (char *) (*__morecore) (0);
da396c5e 189 data_size = (char *) cp - (char *) data_space_start;
94d7c01a 190
395d3972
RS
191 if (!warn_function)
192 return;
193
194 /* What level of warning does current memory usage demand? */
21da04c4
CY
195 new_warnlevel
196 = (data_size > five_percent * 19) ? warned_95
197 : (data_size > five_percent * 17) ? warned_85
198 : (data_size > five_percent * 15) ? warned_75
199 : not_warned;
395d3972
RS
200
201 /* If we have gone up a level, give the appropriate warning. */
202 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
203 {
204 warnlevel = new_warnlevel;
205 switch (warnlevel)
206 {
207 case warned_75:
208 (*warn_function) ("Warning: past 75% of memory limit");
209 break;
210
211 case warned_85:
212 (*warn_function) ("Warning: past 85% of memory limit");
213 break;
214
215 case warned_95:
216 (*warn_function) ("Warning: past 95% of memory limit");
217 }
218 }
219 /* Handle going down in usage levels, with some hysteresis. */
220 else
221 {
222 /* If we go down below 70% full, issue another 75% warning
223 when we go up again. */
224 if (data_size < five_percent * 14)
225 warnlevel = not_warned;
226 /* If we go down below 80% full, issue another 85% warning
227 when we go up again. */
228 else if (warnlevel > warned_75 && data_size < five_percent * 16)
229 warnlevel = warned_75;
230 /* If we go down below 90% full, issue another 95% warning
231 when we go up again. */
232 else if (warnlevel > warned_85 && data_size < five_percent * 18)
233 warnlevel = warned_85;
234 }
3b672b8f
RS
235
236 if (EXCEEDS_LISP_PTR (cp))
da396c5e 237 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
94d7c01a 238}
395d3972 239\f
313d9eb2
DN
240#if !defined(CANNOT_DUMP) || !defined(SYSTEM_MALLOC)
241/* Some systems that cannot dump also cannot implement these. */
242
243/*
244 * Return the address of the start of the data segment prior to
245 * doing an unexec. After unexec the return value is undefined.
246 * See crt0.c for further information and definition of data_start.
247 *
248 * Apparently, on BSD systems this is etext at startup. On
249 * USG systems (swapping) this is highly mmu dependent and
250 * is also dependent on whether or not the program is running
251 * with shared text. Generally there is a (possibly large)
252 * gap between end of text and start of data with shared text.
253 *
254 */
255
8848b728 256char *
313d9eb2
DN
257start_of_data (void)
258{
259#ifdef BSD_SYSTEM
260 extern char etext;
261 return (POINTER)(&etext);
262#elif defined DATA_START
263 return ((POINTER) DATA_START);
264#elif defined ORDINARY_LINK
265 /*
266 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
267 * data_start isn't defined. We take the address of environ, which
268 * is known to live at or near the start of the system crt0.c, and
269 * we don't sweat the handful of bytes that might lose.
270 */
271 extern char **environ;
272 return ((POINTER) &environ);
273#else
274 extern int data_start;
275 return ((POINTER) &data_start);
276#endif
277}
278#endif /* (not CANNOT_DUMP or not SYSTEM_MALLOC) */
279\f
395d3972
RS
280/* Enable memory usage warnings.
281 START says where the end of pure storage is.
282 WARNFUN specifies the function to call to issue a warning. */
94d7c01a
JA
283
284void
a8fe7202 285memory_warnings (POINTER start, void (*warnfun) (const char *))
94d7c01a 286{
d2aa42f8 287 extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
94d7c01a
JA
288
289 if (start)
290 data_space_start = start;
3b672b8f
RS
291 else
292 data_space_start = start_of_data ();
293
da396c5e 294 warn_function = warnfun;
fd065466 295 __after_morecore_hook = check_memory_limits;
b78e8d0a 296
b78e8d0a
AI
297 /* Force data limit to be recalculated on each run. */
298 lim_data = 0;
94d7c01a 299}
ab5796a9 300