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