Update for font-backend changes.
[bpt/emacs.git] / src / vm-limit.c
CommitLineData
94d7c01a 1/* Functions for memory limit warnings.
429ab54e 2 Copyright (C) 1990, 1992, 2001, 2002, 2003, 2004,
8cabe764 3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
94d7c01a
JA
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
684d6f5b 9the Free Software Foundation; either version 3, or (at your option)
94d7c01a
JA
10any later version.
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
18along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
94d7c01a 21
3b672b8f 22#ifdef emacs
18160b98 23#include <config.h>
94d7c01a 24#include "lisp.h"
3b672b8f
RS
25#endif
26
27#ifndef emacs
28#include <stddef.h>
29typedef size_t SIZE;
30typedef void *POINTER;
31#define EXCEEDS_LISP_PTR(x) 0
32#endif
33
e231fd42 34#include "mem-limits.h"
94d7c01a 35
933f22f4
RS
36#ifdef HAVE_GETRLIMIT
37#include <sys/resource.h>
38#endif
39
94d7c01a
JA
40/*
41 Level number of warnings already issued.
42 0 -- no warnings issued.
43 1 -- 75% warning already issued.
44 2 -- 85% warning already issued.
3b672b8f 45 3 -- 95% warning issued; keep warning frequently.
94d7c01a 46*/
395d3972
RS
47enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
48
49static enum warnlevel warnlevel;
94d7c01a
JA
50
51/* Function to call to issue a warning;
52 0 means don't issue them. */
da396c5e 53static void (*warn_function) ();
94d7c01a 54
395d3972
RS
55/* Start of data space; can be changed by calling malloc_init. */
56static POINTER data_space_start;
57
58/* Number of bytes of writable memory we can expect to be able to get. */
59static unsigned long lim_data;
60\f
61
62#ifdef NO_LIM_DATA
63static void
64get_lim_data ()
65{
66 lim_data = -1;
67}
68#else /* not NO_LIM_DATA */
69
70#ifdef USG
71
72static void
73get_lim_data ()
74{
75 extern long ulimit ();
76
77 lim_data = -1;
78
79 /* Use the ulimit call, if we seem to have it. */
80#if !defined (ULIMIT_BREAK_VALUE) || defined (GNU_LINUX)
81 lim_data = ulimit (3, 0);
82#endif
83
84 /* If that didn't work, just use the macro's value. */
85#ifdef ULIMIT_BREAK_VALUE
86 if (lim_data == -1)
87 lim_data = ULIMIT_BREAK_VALUE;
88#endif
89
90 lim_data -= (long) data_space_start;
91}
92
93#else /* not USG */
94#ifdef WINDOWSNT
95
96static void
97get_lim_data ()
98{
99 extern unsigned long reserved_heap_size;
100 lim_data = reserved_heap_size;
101}
102
103#else
104#if !defined (BSD4_2) && !defined (__osf__)
105
106#ifdef MSDOS
107void
108get_lim_data ()
109{
110 _go32_dpmi_meminfo info;
111
112 _go32_dpmi_get_free_memory_information (&info);
113 lim_data = info.available_memory;
114}
115#else /* not MSDOS */
116static void
117get_lim_data ()
118{
119 lim_data = vlimit (LIM_DATA, -1);
120}
121#endif /* not MSDOS */
122
123#else /* BSD4_2 */
124
125static void
126get_lim_data ()
127{
128 struct rlimit XXrlimit;
129
130 getrlimit (RLIMIT_DATA, &XXrlimit);
131#ifdef RLIM_INFINITY
132 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
133#else
134 lim_data = XXrlimit.rlim_cur; /* soft limit */
135#endif
136}
137#endif /* BSD4_2 */
138#endif /* not WINDOWSNT */
139#endif /* not USG */
140#endif /* not NO_LIM_DATA */
141\f
142/* Verify amount of memory available, complaining if we're near the end. */
94d7c01a 143
fd065466
RM
144static void
145check_memory_limits ()
94d7c01a 146{
968e9c04
AI
147#ifdef REL_ALLOC
148 extern POINTER (*real_morecore) ();
149#endif
134994ae
RM
150 extern POINTER (*__morecore) ();
151
968e9c04 152
94d7c01a 153 register POINTER cp;
46b3623d
RS
154 unsigned long five_percent;
155 unsigned long data_size;
395d3972 156 enum warnlevel new_warnlevel;
94d7c01a 157
933f22f4 158#ifdef HAVE_GETRLIMIT
395d3972
RS
159 struct rlimit rlimit;
160
161 getrlimit (RLIMIT_AS, &rlimit);
162
163 if (RLIM_INFINITY == rlimit.rlim_max)
164 return;
933f22f4 165
395d3972
RS
166 /* This is a nonsensical case, but it happens -- rms. */
167 if (rlimit.rlim_cur > rlimit.rlim_max)
168 return;
933f22f4
RS
169
170 five_percent = rlimit.rlim_max / 20;
171 data_size = rlimit.rlim_cur;
172
173#else /* not HAVE_GETRLIMIT */
174
94d7c01a
JA
175 if (lim_data == 0)
176 get_lim_data ();
3b672b8f 177 five_percent = lim_data / 20;
94d7c01a
JA
178
179 /* Find current end of memory and issue warning if getting near max */
968e9c04
AI
180#ifdef REL_ALLOC
181 if (real_morecore)
182 cp = (char *) (*real_morecore) (0);
183 else
184#endif
fd065466 185 cp = (char *) (*__morecore) (0);
da396c5e 186 data_size = (char *) cp - (char *) data_space_start;
94d7c01a 187
933f22f4
RS
188#endif /* not HAVE_GETRLIMIT */
189
395d3972
RS
190 if (!warn_function)
191 return;
192
193 /* What level of warning does current memory usage demand? */
194 if (data_size > five_percent * 19)
195 new_warnlevel = warned_95;
196 else if (data_size > five_percent * 17)
197 new_warnlevel = warned_85;
198 else if (data_size > five_percent * 15)
199 new_warnlevel = warned_75;
200 else
201 new_warnlevel = not_warned;
202
203 /* If we have gone up a level, give the appropriate warning. */
204 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
205 {
206 warnlevel = new_warnlevel;
207 switch (warnlevel)
208 {
209 case warned_75:
210 (*warn_function) ("Warning: past 75% of memory limit");
211 break;
212
213 case warned_85:
214 (*warn_function) ("Warning: past 85% of memory limit");
215 break;
216
217 case warned_95:
218 (*warn_function) ("Warning: past 95% of memory limit");
219 }
220 }
221 /* Handle going down in usage levels, with some hysteresis. */
222 else
223 {
224 /* If we go down below 70% full, issue another 75% warning
225 when we go up again. */
226 if (data_size < five_percent * 14)
227 warnlevel = not_warned;
228 /* If we go down below 80% full, issue another 85% warning
229 when we go up again. */
230 else if (warnlevel > warned_75 && data_size < five_percent * 16)
231 warnlevel = warned_75;
232 /* If we go down below 90% full, issue another 95% warning
233 when we go up again. */
234 else if (warnlevel > warned_85 && data_size < five_percent * 18)
235 warnlevel = warned_85;
236 }
3b672b8f
RS
237
238 if (EXCEEDS_LISP_PTR (cp))
da396c5e 239 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
94d7c01a 240}
395d3972
RS
241\f
242/* Enable memory usage warnings.
243 START says where the end of pure storage is.
244 WARNFUN specifies the function to call to issue a warning. */
94d7c01a
JA
245
246void
3b672b8f 247memory_warnings (start, warnfun)
94d7c01a
JA
248 POINTER start;
249 void (*warnfun) ();
250{
fd065466 251 extern void (* __after_morecore_hook) (); /* From gmalloc.c */
94d7c01a
JA
252
253 if (start)
254 data_space_start = start;
3b672b8f
RS
255 else
256 data_space_start = start_of_data ();
257
da396c5e 258 warn_function = warnfun;
fd065466 259 __after_morecore_hook = check_memory_limits;
b78e8d0a 260
b78e8d0a
AI
261 /* Force data limit to be recalculated on each run. */
262 lim_data = 0;
94d7c01a 263}
ab5796a9
MB
264
265/* arch-tag: eab04eda-1f69-447a-8d9f-95f0a3983ca5
266 (do not change this comment) */