Commit | Line | Data |
---|---|---|
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 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
94d7c01a | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
94d7c01a JA |
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 | |
9ec0b715 | 18 | along 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> | |
27 | typedef size_t SIZE; | |
28 | typedef 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 |
45 | enum warnlevel { not_warned, warned_75, warned_85, warned_95 }; |
46 | ||
47 | static enum warnlevel warnlevel; | |
94d7c01a JA |
48 | |
49 | /* Function to call to issue a warning; | |
50 | 0 means don't issue them. */ | |
da396c5e | 51 | static void (*warn_function) (); |
94d7c01a | 52 | |
395d3972 RS |
53 | /* Start of data space; can be changed by calling malloc_init. */ |
54 | static POINTER data_space_start; | |
55 | ||
56 | /* Number of bytes of writable memory we can expect to be able to get. */ | |
57 | static unsigned long lim_data; | |
58 | \f | |
59 | ||
60 | #ifdef NO_LIM_DATA | |
61 | static void | |
62 | get_lim_data () | |
63 | { | |
64 | lim_data = -1; | |
65 | } | |
66 | #else /* not NO_LIM_DATA */ | |
67 | ||
68 | #ifdef USG | |
69 | ||
70 | static void | |
71 | get_lim_data () | |
72 | { | |
73 | extern long ulimit (); | |
74 | ||
75 | lim_data = -1; | |
76 | ||
77 | /* Use the ulimit call, if we seem to have it. */ | |
78 | #if !defined (ULIMIT_BREAK_VALUE) || defined (GNU_LINUX) | |
79 | lim_data = ulimit (3, 0); | |
80 | #endif | |
81 | ||
82 | /* If that didn't work, just use the macro's value. */ | |
83 | #ifdef ULIMIT_BREAK_VALUE | |
84 | if (lim_data == -1) | |
85 | lim_data = ULIMIT_BREAK_VALUE; | |
86 | #endif | |
87 | ||
88 | lim_data -= (long) data_space_start; | |
89 | } | |
90 | ||
91 | #else /* not USG */ | |
92 | #ifdef WINDOWSNT | |
93 | ||
94 | static void | |
95 | get_lim_data () | |
96 | { | |
97 | extern unsigned long reserved_heap_size; | |
98 | lim_data = reserved_heap_size; | |
99 | } | |
100 | ||
101 | #else | |
102 | #if !defined (BSD4_2) && !defined (__osf__) | |
103 | ||
104 | #ifdef MSDOS | |
105 | void | |
106 | get_lim_data () | |
107 | { | |
108 | _go32_dpmi_meminfo info; | |
109 | ||
110 | _go32_dpmi_get_free_memory_information (&info); | |
111 | lim_data = info.available_memory; | |
112 | } | |
113 | #else /* not MSDOS */ | |
114 | static void | |
115 | get_lim_data () | |
116 | { | |
117 | lim_data = vlimit (LIM_DATA, -1); | |
118 | } | |
119 | #endif /* not MSDOS */ | |
120 | ||
121 | #else /* BSD4_2 */ | |
122 | ||
123 | static void | |
124 | get_lim_data () | |
125 | { | |
126 | struct rlimit XXrlimit; | |
127 | ||
128 | getrlimit (RLIMIT_DATA, &XXrlimit); | |
129 | #ifdef RLIM_INFINITY | |
130 | lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */ | |
131 | #else | |
132 | lim_data = XXrlimit.rlim_cur; /* soft limit */ | |
133 | #endif | |
134 | } | |
135 | #endif /* BSD4_2 */ | |
136 | #endif /* not WINDOWSNT */ | |
137 | #endif /* not USG */ | |
138 | #endif /* not NO_LIM_DATA */ | |
139 | \f | |
140 | /* Verify amount of memory available, complaining if we're near the end. */ | |
94d7c01a | 141 | |
fd065466 RM |
142 | static void |
143 | check_memory_limits () | |
94d7c01a | 144 | { |
968e9c04 AI |
145 | #ifdef REL_ALLOC |
146 | extern POINTER (*real_morecore) (); | |
147 | #endif | |
134994ae RM |
148 | extern POINTER (*__morecore) (); |
149 | ||
968e9c04 | 150 | |
94d7c01a | 151 | register POINTER cp; |
46b3623d RS |
152 | unsigned long five_percent; |
153 | unsigned long data_size; | |
395d3972 | 154 | enum warnlevel new_warnlevel; |
94d7c01a | 155 | |
933f22f4 | 156 | #ifdef HAVE_GETRLIMIT |
395d3972 RS |
157 | struct rlimit rlimit; |
158 | ||
159 | getrlimit (RLIMIT_AS, &rlimit); | |
160 | ||
161 | if (RLIM_INFINITY == rlimit.rlim_max) | |
162 | return; | |
933f22f4 | 163 | |
395d3972 RS |
164 | /* This is a nonsensical case, but it happens -- rms. */ |
165 | if (rlimit.rlim_cur > rlimit.rlim_max) | |
166 | return; | |
933f22f4 RS |
167 | |
168 | five_percent = rlimit.rlim_max / 20; | |
169 | data_size = rlimit.rlim_cur; | |
170 | ||
171 | #else /* not HAVE_GETRLIMIT */ | |
172 | ||
94d7c01a JA |
173 | if (lim_data == 0) |
174 | get_lim_data (); | |
3b672b8f | 175 | five_percent = lim_data / 20; |
94d7c01a JA |
176 | |
177 | /* Find current end of memory and issue warning if getting near max */ | |
968e9c04 AI |
178 | #ifdef REL_ALLOC |
179 | if (real_morecore) | |
180 | cp = (char *) (*real_morecore) (0); | |
181 | else | |
182 | #endif | |
fd065466 | 183 | cp = (char *) (*__morecore) (0); |
da396c5e | 184 | data_size = (char *) cp - (char *) data_space_start; |
94d7c01a | 185 | |
933f22f4 RS |
186 | #endif /* not HAVE_GETRLIMIT */ |
187 | ||
395d3972 RS |
188 | if (!warn_function) |
189 | return; | |
190 | ||
191 | /* What level of warning does current memory usage demand? */ | |
192 | if (data_size > five_percent * 19) | |
193 | new_warnlevel = warned_95; | |
194 | else if (data_size > five_percent * 17) | |
195 | new_warnlevel = warned_85; | |
196 | else if (data_size > five_percent * 15) | |
197 | new_warnlevel = warned_75; | |
198 | else | |
199 | new_warnlevel = not_warned; | |
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 RS |
239 | \f |
240 | /* Enable memory usage warnings. | |
241 | START says where the end of pure storage is. | |
242 | WARNFUN specifies the function to call to issue a warning. */ | |
94d7c01a JA |
243 | |
244 | void | |
3b672b8f | 245 | memory_warnings (start, warnfun) |
94d7c01a JA |
246 | POINTER start; |
247 | void (*warnfun) (); | |
248 | { | |
fd065466 | 249 | extern void (* __after_morecore_hook) (); /* From gmalloc.c */ |
94d7c01a JA |
250 | |
251 | if (start) | |
252 | data_space_start = start; | |
3b672b8f RS |
253 | else |
254 | data_space_start = start_of_data (); | |
255 | ||
da396c5e | 256 | warn_function = warnfun; |
fd065466 | 257 | __after_morecore_hook = check_memory_limits; |
b78e8d0a | 258 | |
b78e8d0a AI |
259 | /* Force data limit to be recalculated on each run. */ |
260 | lim_data = 0; | |
94d7c01a | 261 | } |
ab5796a9 MB |
262 | |
263 | /* arch-tag: eab04eda-1f69-447a-8d9f-95f0a3983ca5 | |
264 | (do not change this comment) */ |