* emacs.c (main): Print and error and exit when no data is read
[bpt/emacs.git] / src / vm-limit.c
CommitLineData
94d7c01a 1/* Functions for memory limit warnings.
9ec0b715
GM
2 Copyright (C) 1990, 1992, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008 Free Software Foundation, Inc.
94d7c01a
JA
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
94d7c01a 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
94d7c01a
JA
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
94d7c01a 19
3b672b8f 20#ifdef emacs
18160b98 21#include <config.h>
94d7c01a 22#include "lisp.h"
3b672b8f
RS
23#endif
24
25#ifndef emacs
26#include <stddef.h>
27typedef size_t SIZE;
28typedef void *POINTER;
29#define EXCEEDS_LISP_PTR(x) 0
30#endif
31
e231fd42 32#include "mem-limits.h"
94d7c01a 33
933f22f4
RS
34#ifdef HAVE_GETRLIMIT
35#include <sys/resource.h>
36#endif
37
94d7c01a
JA
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.
3b672b8f 43 3 -- 95% warning issued; keep warning frequently.
94d7c01a 44*/
395d3972
RS
45enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
46
47static enum warnlevel warnlevel;
94d7c01a
JA
48
49/* Function to call to issue a warning;
50 0 means don't issue them. */
da396c5e 51static void (*warn_function) ();
94d7c01a 52
395d3972
RS
53/* Start of data space; can be changed by calling malloc_init. */
54static POINTER data_space_start;
55
56/* Number of bytes of writable memory we can expect to be able to get. */
57static unsigned long lim_data;
58\f
59
60#ifdef NO_LIM_DATA
61static void
62get_lim_data ()
63{
64 lim_data = -1;
65}
66#else /* not NO_LIM_DATA */
67
51757187
AS
68#if defined (HAVE_GETRLIMIT) && defined (RLIMIT_AS)
69static void
70get_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
395d3972
RS
83#ifdef USG
84
85static void
86get_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
109static void
110get_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
120void
121get_lim_data ()
122{
123 _go32_dpmi_meminfo info;
8a445f76 124 unsigned long lim1, lim2;
395d3972
RS
125
126 _go32_dpmi_get_free_memory_information (&info);
8a445f76
EZ
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;
7cf94eac 132 lim2 = info.available_physical_pages;
8a445f76
EZ
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;
7cf94eac
EZ
138 else
139 lim2 *= 4096;
8a445f76
EZ
140 /* Surely, the available memory is at least what we have physically
141 available, right? */
7cf94eac 142 if (lim1 >= lim2)
8a445f76
EZ
143 lim_data = lim1;
144 else
145 lim_data = lim2;
146 /* Don't believe they will give us more that 0.5 GB. */
7cf94eac
EZ
147 if (lim_data > 512U * 1024U * 1024U)
148 lim_data = 512U * 1024U * 1024U;
395d3972
RS
149}
150#else /* not MSDOS */
151static void
152get_lim_data ()
153{
154 lim_data = vlimit (LIM_DATA, -1);
155}
156#endif /* not MSDOS */
157
158#else /* BSD4_2 */
159
160static void
161get_lim_data ()
162{
163 struct rlimit XXrlimit;
164
165 getrlimit (RLIMIT_DATA, &XXrlimit);
166#ifdef RLIM_INFINITY
167 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
168#else
169 lim_data = XXrlimit.rlim_cur; /* soft limit */
170#endif
171}
172#endif /* BSD4_2 */
173#endif /* not WINDOWSNT */
174#endif /* not USG */
51757187 175#endif /* not HAVE_GETRLIMIT */
395d3972
RS
176#endif /* not NO_LIM_DATA */
177\f
178/* Verify amount of memory available, complaining if we're near the end. */
94d7c01a 179
fd065466
RM
180static void
181check_memory_limits ()
94d7c01a 182{
968e9c04
AI
183#ifdef REL_ALLOC
184 extern POINTER (*real_morecore) ();
185#endif
134994ae
RM
186 extern POINTER (*__morecore) ();
187
94d7c01a 188 register POINTER cp;
46b3623d
RS
189 unsigned long five_percent;
190 unsigned long data_size;
395d3972 191 enum warnlevel new_warnlevel;
94d7c01a
JA
192
193 if (lim_data == 0)
194 get_lim_data ();
3b672b8f 195 five_percent = lim_data / 20;
94d7c01a
JA
196
197 /* Find current end of memory and issue warning if getting near max */
968e9c04
AI
198#ifdef REL_ALLOC
199 if (real_morecore)
200 cp = (char *) (*real_morecore) (0);
201 else
202#endif
fd065466 203 cp = (char *) (*__morecore) (0);
da396c5e 204 data_size = (char *) cp - (char *) data_space_start;
94d7c01a 205
395d3972
RS
206 if (!warn_function)
207 return;
208
209 /* What level of warning does current memory usage demand? */
21da04c4
CY
210 new_warnlevel
211 = (data_size > five_percent * 19) ? warned_95
212 : (data_size > five_percent * 17) ? warned_85
213 : (data_size > five_percent * 15) ? warned_75
214 : not_warned;
395d3972
RS
215
216 /* If we have gone up a level, give the appropriate warning. */
217 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
218 {
219 warnlevel = new_warnlevel;
220 switch (warnlevel)
221 {
222 case warned_75:
223 (*warn_function) ("Warning: past 75% of memory limit");
224 break;
225
226 case warned_85:
227 (*warn_function) ("Warning: past 85% of memory limit");
228 break;
229
230 case warned_95:
231 (*warn_function) ("Warning: past 95% of memory limit");
232 }
233 }
234 /* Handle going down in usage levels, with some hysteresis. */
235 else
236 {
237 /* If we go down below 70% full, issue another 75% warning
238 when we go up again. */
239 if (data_size < five_percent * 14)
240 warnlevel = not_warned;
241 /* If we go down below 80% full, issue another 85% warning
242 when we go up again. */
243 else if (warnlevel > warned_75 && data_size < five_percent * 16)
244 warnlevel = warned_75;
245 /* If we go down below 90% full, issue another 95% warning
246 when we go up again. */
247 else if (warnlevel > warned_85 && data_size < five_percent * 18)
248 warnlevel = warned_85;
249 }
3b672b8f
RS
250
251 if (EXCEEDS_LISP_PTR (cp))
da396c5e 252 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
94d7c01a 253}
395d3972
RS
254\f
255/* Enable memory usage warnings.
256 START says where the end of pure storage is.
257 WARNFUN specifies the function to call to issue a warning. */
94d7c01a
JA
258
259void
3b672b8f 260memory_warnings (start, warnfun)
94d7c01a
JA
261 POINTER start;
262 void (*warnfun) ();
263{
fd065466 264 extern void (* __after_morecore_hook) (); /* From gmalloc.c */
94d7c01a
JA
265
266 if (start)
267 data_space_start = start;
3b672b8f
RS
268 else
269 data_space_start = start_of_data ();
270
da396c5e 271 warn_function = warnfun;
fd065466 272 __after_morecore_hook = check_memory_limits;
b78e8d0a 273
b78e8d0a
AI
274 /* Force data limit to be recalculated on each run. */
275 lim_data = 0;
94d7c01a 276}
ab5796a9
MB
277
278/* arch-tag: eab04eda-1f69-447a-8d9f-95f0a3983ca5
279 (do not change this comment) */